private void createIntegrationResponses(Integration integration, JSONObject responses) { if (responses == null) { return; } final Iterator<String> keysIterator = responses.keys(); while (keysIterator.hasNext()) { String key = keysIterator.next(); try { String pattern = key.equals("default") ? null : key; JSONObject response = responses.getJSONObject(key); String status = (String) response.get("statusCode"); PutIntegrationResponseInput input = new PutIntegrationResponseInput() .withResponseParameters(jsonObjectToHashMapString(response.optJSONObject("responseParameters"))) .withResponseTemplates(jsonObjectToHashMapString(response.optJSONObject("responseTemplates"))) .withSelectionPattern(pattern); integration.putIntegrationResponse(input, status); } catch (JSONException e) { } } }
private void createIntegration(Resource resource, Method method, JSONObject config) { if (config == null) { return; } try { final JSONObject integ = config.getJSONObject(resource.getPath()) .getJSONObject(method.getHttpMethod().toLowerCase()) .getJSONObject("integration"); IntegrationType type = IntegrationType.valueOf(integ.getString("type").toUpperCase()); LOG.info("Creating integration with type " + type); PutIntegrationInput input = new PutIntegrationInput() .withType(type) .withUri(integ.getString("uri")) .withCredentials(integ.optString("credentials")) .withHttpMethod(integ.optString("httpMethod")) .withRequestParameters(jsonObjectToHashMapString(integ.optJSONObject("requestParameters"))) .withRequestTemplates(jsonObjectToHashMapString(integ.optJSONObject("requestTemplates"))) .withCacheNamespace(integ.optString("cacheNamespace")) .withCacheKeyParameters(jsonObjectToListString(integ.optJSONArray("cacheKeyParameters"))); Integration integration = method.putIntegration(input); createIntegrationResponses(integration, integ.optJSONObject("responses")); } catch (JSONException e) { LOG.info(format("Skipping integration for method %s of %s: %s", method.getHttpMethod(), resource.getPath(), e)); } }
private static Map<String, Object> getIntegration(Integration integration, String defaultCacheNamespace) { Map<String, Object> integrationMap = new HashMap<String, Object>(); putIfNotNullOrEmpty(integrationMap, "type", integration.getType()); putIfNotNullOrEmpty(integrationMap, "uri", integration.getUri()); putIfNotNullOrEmpty(integrationMap, "httpMethod", integration.getHttpMethod()); putIfNotNullOrEmpty(integrationMap, "credentials", integration.getCredentials()); String cacheNamespace = integration.getCacheNamespace(); if (cacheNamespace != null && !cacheNamespace.equals(defaultCacheNamespace)) { putIfNotNullOrEmpty(integrationMap, "cacheNamespace", cacheNamespace); } putIfNotNullOrEmpty(integrationMap, "cacheKeyParameters", integration.getCacheKeyParameters()); putIfNotNullOrEmpty(integrationMap, "requestTemplates", integration.getRequestTemplates()); putIfNotNullOrEmpty(integrationMap, "requestParameters", integration.getRequestParameters()); Map<String, IntegrationResponse> integrationResponses = integration.getIntegrationResponses(); if (integrationResponses != null && !integrationResponses.isEmpty()) { Map<String, Object> responsesMap = new HashMap<String, Object>(); for (IntegrationResponse integrationResponse : integrationResponses.values()) { String pattern = integrationResponse.getSelectionPattern(); if (pattern == null) { pattern = "default"; } Map<String, Object> map = new HashMap<String, Object>(); putIfNotNullOrEmpty(map, "statusCode", integrationResponse.getStatusCode()); putIfNotNullOrEmpty(map, "responseParameters", integrationResponse.getResponseParameters()); putIfNotNullOrEmpty(map, "responseTemplates", integrationResponse.getResponseTemplates()); putIfNotNullOrEmpty(responsesMap, pattern, map); } putIfNotNullOrEmpty(integrationMap, "responses", responsesMap); } return integrationMap; }
@Before public void setUp() throws Exception { BasicConfigurator.configure(); Injector injector = Guice.createInjector(new SwaggerApiImporterTestModule()); client = injector.getInstance(ApiGateway.class); importer = injector.getInstance(SwaggerApiFileImporter.class); RestApis mockRestApis = mock(RestApis.class); Integration mockIntegration = Mockito.mock(Integration.class); Method mockMethod = Mockito.mock(Method.class); when(mockMethod.getHttpMethod()).thenReturn("GET"); when(mockMethod.putIntegration(any())).thenReturn(mockIntegration); mockChildResource = Mockito.mock(Resource.class); when(mockChildResource.getPath()).thenReturn("/child"); when(mockChildResource.putMethod(any(), any())).thenReturn(mockMethod); mockResource = Mockito.mock(Resource.class); when(mockResource.getPath()).thenReturn("/"); when(mockResource.createResource(any())).thenReturn(mockChildResource); when(mockResource.putMethod(any(), any())).thenReturn(mockMethod); Resources mockResources = mock(Resources.class); when(mockResources.getItem()).thenReturn(Arrays.asList(mockResource)); Model mockModel = Mockito.mock(Model.class); when(mockModel.getName()).thenReturn("test model"); Models mockModels = mock(Models.class); when(mockModels.getItem()).thenReturn(Arrays.asList(mockModel)); mockRestApi = mock(RestApi.class); when(mockRestApi.getResources()).thenReturn(mockResources); when(mockRestApi.getModels()).thenReturn(mockModels); when(mockRestApi.getResourceById(any())).thenReturn(mockResource); when(client.getRestApis()).thenReturn(mockRestApis); when(client.createRestApi(any())).thenReturn(mockRestApi); importer.importApi(getResourcePath(API_GATEWAY)); }