diff --git a/modules/apim-adapter/src/main/java/com/axway/apim/adapter/apis/APIFilter.java b/modules/apim-adapter/src/main/java/com/axway/apim/adapter/apis/APIFilter.java index 8ae6a7102..d46e7e12d 100644 --- a/modules/apim-adapter/src/main/java/com/axway/apim/adapter/apis/APIFilter.java +++ b/modules/apim-adapter/src/main/java/com/axway/apim/adapter/apis/APIFilter.java @@ -55,6 +55,7 @@ public enum FILTER_OP { private String name; private String vhost; private String apiPath; + private String version; private String queryStringVersion; private String state; private String backendBasepath; @@ -328,6 +329,14 @@ public String getState() { return state; } + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + public boolean isRetired() { return retired; } @@ -591,6 +600,7 @@ public enum APIType { String apiPath; String queryStringVersion; String state; + String version; String backendBasepath; String inboundSecurity; String outboundAuthentication; @@ -665,6 +675,7 @@ public APIFilter build() { apiFilter.setIncludeRemoteHost(this.includeRemoteHost); apiFilter.setLoadBackendAPI(this.loadBackendAPI); apiFilter.setState(this.state); + apiFilter.setVersion(this.version); apiFilter.setRetired(this.retired); apiFilter.setDeprecated(this.deprecated); apiFilter.setCustomProperties(this.customProperties); @@ -727,6 +738,12 @@ public Builder hasState(String state) { return this; } + public Builder hasVersion(String version) { + this.version = version; + return this; + } + + public Builder hasPolicyName(String policyName) { this.policyName = policyName; return this; @@ -862,6 +879,7 @@ public Builder failOnError(boolean failOnError) { this.failOnError = failOnError; return this; } + } private static boolean isPolicyUsed(API api, String policyName) { diff --git a/modules/apim-adapter/src/main/java/com/axway/apim/adapter/apis/APIManagerAPIAdapter.java b/modules/apim-adapter/src/main/java/com/axway/apim/adapter/apis/APIManagerAPIAdapter.java index a6afab024..15e4ef2c4 100644 --- a/modules/apim-adapter/src/main/java/com/axway/apim/adapter/apis/APIManagerAPIAdapter.java +++ b/modules/apim-adapter/src/main/java/com/axway/apim/adapter/apis/APIManagerAPIAdapter.java @@ -172,7 +172,7 @@ URI getAPIRequestUri(APIFilter filter) throws URISyntaxException, AppException { .build(); } - API getUniqueAPI(List foundAPIs, APIFilter filter) throws AppException { + public API getUniqueAPI(List foundAPIs, APIFilter filter) throws AppException { if (foundAPIs.isEmpty()) return null; // If filtered resultSet contains more than one API, here we try to find a unique API based on the logical // criteria (apiPath, VHost and QueryVersion) @@ -184,7 +184,7 @@ API getUniqueAPI(List foundAPIs, APIFilter filter) throws AppException { Map> apisPerKey = new HashMap<>(); // Create a List of APIs based on the logical keys for (API api : foundAPIs) { - String key = api.getPath() + "###" + api.getVhost() + "###" + api.getApiRoutingKey(); + String key = api.getPath() + "###" + api.getVhost() + "###" + getVersion(api); if (apisPerKey.containsKey(key)) { apisPerKey.get(key).add(api); } else { @@ -193,7 +193,7 @@ API getUniqueAPI(List foundAPIs, APIFilter filter) throws AppException { apisPerKey.put(key, apiWithKey); } } - String filterKey = filter.getApiPath() + "###" + filter.getVhost() + "###" + filter.getQueryStringVersion(); + String filterKey = filter.getApiPath() + "###" + filter.getVhost() + "###" + getVersion(filter); if (apisPerKey.get(filterKey) != null && apisPerKey.get(filterKey).size() == 1) { return apisPerKey.get(filterKey).get(0); } @@ -202,6 +202,14 @@ API getUniqueAPI(List foundAPIs, APIFilter filter) throws AppException { return foundAPIs.get(0); } + public String getVersion(API api) { + return api.getApiRoutingKey() != null ? api.getApiRoutingKey() : api.getVersion(); + } + + public String getVersion(APIFilter apiFilter) { + return apiFilter.getQueryStringVersion() != null ? apiFilter.getQueryStringVersion() : apiFilter.getVersion(); + } + private List filterAPIs(APIFilter filter) throws IOException { List apis = mapper.readValue(this.apiManagerResponse.get(filter), new TypeReference>() { }); diff --git a/modules/apim-adapter/src/test/java/com/axway/apim/adapter/apis/APIManagerAPIAdapterTest.java b/modules/apim-adapter/src/test/java/com/axway/apim/adapter/apis/APIManagerAPIAdapterTest.java index b848aa168..0a9435d88 100644 --- a/modules/apim-adapter/src/test/java/com/axway/apim/adapter/apis/APIManagerAPIAdapterTest.java +++ b/modules/apim-adapter/src/test/java/com/axway/apim/adapter/apis/APIManagerAPIAdapterTest.java @@ -150,6 +150,33 @@ public void testNoUniqueFoundMixedVHost() throws AppException { Assert.assertEquals(uniqueAPI, testAPI2); } + @Test + public void testUniqueApiWithoutVhostAndQueryStringRouting() throws AppException { + API testAPI1 = createTestAPIWithStateAndVersion("/api/v1/resource", "deprecated", "1.0"); + API testAPI2 = createTestAPIWithStateAndVersion("/api/v1/resource", "deprecated", "2.0"); + API testAPI3 = createTestAPIWithStateAndVersion("/api/v1/resource", "published", "3.0"); + + List testAPIs = new ArrayList<>(); + testAPIs.add(testAPI1); + testAPIs.add(testAPI2); + testAPIs.add(testAPI3); + + APIFilter filter = new APIFilter.Builder().hasApiPath("/api/v1/resource").hasVersion("3.0").build(); + + // Must fail (throw an Exception) as the API is really not unique, if we filter with the QueryVersion only + API uniqueAPI = apiManagerAPIAdapter.getUniqueAPI(testAPIs, filter); + Assert.assertEquals(uniqueAPI, testAPI3); + } + + private static API createTestAPIWithStateAndVersion(String apiPath, String state, String version) { + API testAPI = new API(); + testAPI.setPath(apiPath); + testAPI.setState(state); + testAPI.setVersion(version); + return testAPI; + } + + private static API createTestAPI(String apiPath, String vhost, String queryVersion) { API testAPI = new API(); testAPI.setPath(apiPath); diff --git a/modules/apis/src/main/java/com/axway/apim/APIImportApp.java b/modules/apis/src/main/java/com/axway/apim/APIImportApp.java index 43f204a1c..bf90f3c31 100644 --- a/modules/apis/src/main/java/com/axway/apim/APIImportApp.java +++ b/modules/apis/src/main/java/com/axway/apim/APIImportApp.java @@ -79,6 +79,7 @@ public int importAPI(APIImportParams params) { APIFilter filter = new APIFilter.Builder(Builder.APIType.ACTUAL_API) .hasApiPath(desiredAPI.getPath()) .hasVHost(desiredAPI.getVhost()) + .hasVersion(desiredAPI.getVersion()) .includeCustomProperties(desiredAPI.getCustomProperties()) .hasQueryStringVersion(desiredAPI.getApiRoutingKey()) .includeClientOrganizations(true) // We have to load clientOrganization, in case they have to be taken over