Skip to content

Commit

Permalink
- issue #469
Browse files Browse the repository at this point in the history
  • Loading branch information
rathnapandi committed Mar 12, 2024
1 parent 64f4a01 commit 776816b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -591,6 +600,7 @@ public enum APIType {
String apiPath;
String queryStringVersion;
String state;
String version;
String backendBasepath;
String inboundSecurity;
String outboundAuthentication;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -862,6 +879,7 @@ public Builder failOnError(boolean failOnError) {
this.failOnError = failOnError;
return this;
}

}

private static boolean isPolicyUsed(API api, String policyName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ URI getAPIRequestUri(APIFilter filter) throws URISyntaxException, AppException {
.build();
}

API getUniqueAPI(List<API> foundAPIs, APIFilter filter) throws AppException {
public API getUniqueAPI(List<API> 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)
Expand All @@ -184,7 +184,7 @@ API getUniqueAPI(List<API> foundAPIs, APIFilter filter) throws AppException {
Map<String, List<API>> 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 {
Expand All @@ -193,7 +193,7 @@ API getUniqueAPI(List<API> 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);
}
Expand All @@ -202,6 +202,14 @@ API getUniqueAPI(List<API> 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<API> filterAPIs(APIFilter filter) throws IOException {
List<API> apis = mapper.readValue(this.apiManagerResponse.get(filter), new TypeReference<List<API>>() {
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<API> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 776816b

Please sign in to comment.