Skip to content

Commit

Permalink
- Fix Integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
rathnapandi committed Sep 17, 2023
1 parent 32fd024 commit 535fbbd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,63 +315,47 @@ public void createApplication(ClientApplication desiredApp) throws AppException
}

public void createOrUpdateApplication(ClientApplication desiredApp, ClientApplication actualApp) throws AppException {
createOrUpdateApplication(desiredApp, actualApp, false);
}

private void createOrUpdateApplication(ClientApplication desiredApp, ClientApplication actualApp, boolean baseAppOnly) throws AppException {
LOG.debug("Actual Application : {} vs Desired Application : {}", actualApp, desiredApp);
ClientApplication createdApp;
try {
URI uri;
if (actualApp == null) {
uri = new URIBuilder(cmd.getAPIManagerURL()).setPath(cmd.getApiBasepath() + APPLICATIONS).build();
} else {
if (desiredApp.getApiAccess() != null && desiredApp.getApiAccess().isEmpty())
desiredApp.setApiAccess(null);
desiredApp.setId(actualApp.getId());
uri = new URIBuilder(cmd.getAPIManagerURL()).setPath(cmd.getApiBasepath() + APPLICATIONS + "/" + actualApp.getId()).build();
}
mapper.setSerializationInclusion(Include.NON_NULL);
try {
RestAPICall request = null;
if (actualApp == null) {
LOG.info("Creating new application");
URI uri = new URIBuilder(cmd.getAPIManagerURL()).setPath(cmd.getApiBasepath() + APPLICATIONS).build();
FilterProvider filter = new SimpleFilterProvider().setDefaultFilter(
SimpleBeanPropertyFilter.serializeAllExcept("credentials", "appQuota", "organization", "image", "appScopes", "permissions"));
mapper.setFilterProvider(filter);
String json = mapper.writeValueAsString(desiredApp);
HttpEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
request = new POSTRequest(entity, uri);
} else {
if (!actualApp.equals(desiredApp)) {
FilterProvider filter = new SimpleFilterProvider().setDefaultFilter(
SimpleBeanPropertyFilter.serializeAllExcept("credentials", "appQuota", "organization", "image", "apis", "appScopes", "permissions"));
mapper.setFilterProvider(filter);
String json = mapper.writeValueAsString(desiredApp);
HttpEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
request = new PUTRequest(entity, uri);
}
}
if (!(actualApp != null && actualApp.equals(desiredApp))) {
RestAPICall request = new POSTRequest(entity, uri);
try (CloseableHttpResponse httpResponse = (CloseableHttpResponse) request.execute()) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode < 200 || statusCode > 299) {
LOG.error("Error creating/updating application. Response-Code: {} Got response: {}", statusCode, EntityUtils.toString(httpResponse.getEntity()));
throw new AppException("Error creating/updating application. Response-Code: " + statusCode, ErrorCode.API_MANAGER_COMMUNICATION);
LOG.error("Error creating application. Response-Code: {} Got response: {}", statusCode, EntityUtils.toString(httpResponse.getEntity()));
throw new AppException("Error creating application. Response-Code: " + statusCode, ErrorCode.API_MANAGER_COMMUNICATION);
}
createdApp = mapper.readValue(httpResponse.getEntity().getContent(), ClientApplication.class);
// enabled=false for a new application is ignored during initial creation, hence another update of the just created app is required
if (actualApp == null && !desiredApp.isEnabled()) {
createOrUpdateApplication(desiredApp, createdApp, true);
if (!desiredApp.isEnabled()) {
uri = new URIBuilder(cmd.getAPIManagerURL()).setPath(cmd.getApiBasepath() + APPLICATIONS + "/" + createdApp.getId()).build();
desiredApp.setId(createdApp.getId());
createdApp = updateApplication(uri, desiredApp);
}
}
} else {
createdApp = desiredApp;
} else if (!actualApp.equals(desiredApp)) {
LOG.info("Creating new application");
URI uri = new URIBuilder(cmd.getAPIManagerURL()).setPath(cmd.getApiBasepath() + APPLICATIONS + "/" + actualApp.getId()).build();
desiredApp.setId(actualApp.getId());
createdApp = updateApplication(uri, desiredApp);
}else {
createdApp = actualApp;
}
} catch (Exception e) {
throw new AppException("Error creating/updating application. Error: " + e.getMessage(), ErrorCode.CANT_CREATE_API_PROXY, e);
}
// Remove application from cache to force reload next time
applicationsCache.remove(createdApp.getId());
if (baseAppOnly) return;
desiredApp.setId(createdApp.getId());
saveImage(desiredApp, actualApp);
saveAPIAccess(desiredApp, actualApp);
Expand All @@ -384,6 +368,23 @@ private void createOrUpdateApplication(ClientApplication desiredApp, ClientAppli
}
}

public ClientApplication updateApplication(URI uri, ClientApplication clientApplication) throws IOException {
FilterProvider filter = new SimpleFilterProvider().setDefaultFilter(
SimpleBeanPropertyFilter.serializeAllExcept("credentials", "appQuota", "organization", "image", "apis", "appScopes", "permissions"));
mapper.setFilterProvider(filter);
String json = mapper.writeValueAsString(clientApplication);
HttpEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
RestAPICall request = new PUTRequest(entity, uri);
try (CloseableHttpResponse httpResponse = (CloseableHttpResponse) request.execute()) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode < 200 || statusCode > 299) {
LOG.error("Error updating application. Response-Code: {} Got response: {}", statusCode, EntityUtils.toString(httpResponse.getEntity()));
throw new AppException("Error updating application. Response-Code: " + statusCode, ErrorCode.API_MANAGER_COMMUNICATION);
}
return mapper.readValue(httpResponse.getEntity().getContent(), ClientApplication.class);
}
}

private void saveImage(ClientApplication app, ClientApplication actualApp) throws URISyntaxException, AppException {
if (app.getImage() == null) return;
if (actualApp != null && app.getImage().equals(actualApp.getImage())) return;
Expand Down Expand Up @@ -520,12 +521,12 @@ public void saveQuota(ClientApplication app, ClientApplication actualApp) throws
if (app != null & actualApp != null && app.getAppQuota() != null && actualApp.getAppQuota() != null && app.getAppQuota().equals(actualApp.getAppQuota()))
return;
try {
if(app != null) {
if (app != null) {
URI uri = new URIBuilder(cmd.getAPIManagerURL()).setPath(cmd.getApiBasepath() + APPLICATIONS + "/" + app.getId() + "/quota").build();
if (app.getAppQuota() != null && app.getAppQuota().getRestrictions().isEmpty()) {
// If source is empty and target has values, remove target to match source
deleteApplicationQuota(uri);
} else {
} else if (app.getAppQuota() != null) {
// source and target has different values delete target and add it.
FilterProvider filter = new SimpleFilterProvider().setDefaultFilter(SimpleBeanPropertyFilter.serializeAllExcept("apiId", "apiName", "apiVersion", "apiPath", "vhost", "queryVersion"));
mapper.setFilterProvider(filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public static boolean appsAreEqual(ClientApplication desiredApp, ClientApplicati
boolean apiAccess = (desiredApp.getApiAccess() == null || desiredApp.getApiAccess().equals(actualApp.getApiAccess()));
boolean permission = (desiredApp.getPermissions() == null || desiredApp.getPermissions().containsAll(actualApp.getPermissions()));
boolean quota = (desiredApp.getAppQuota() == null || desiredApp.getAppQuota().equals(actualApp.getAppQuota()));
LOG.debug("apps changed: {}", desiredApp.equals(actualApp));
LOG.debug("api access changed: {}", apiAccess);
LOG.debug("Permission changed: {}", permission);
LOG.debug("Quota changed: {}", quota);
LOG.debug("apps Not changed: {}", application);
LOG.debug("api access Not changed: {}", apiAccess);
LOG.debug("Permission Not changed: {}", permission);
LOG.debug("Quota Not changed: {}", quota);
return application && apiAccess && permission && quota;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public ImportResult importApp(AppImportParams params) {
return result;
} catch (AppException ap) {
ap.logException(LOG);
LOG.error("Error importing application", ap);
result.setError(ap.getError());
return result;
} catch (Exception e) {
Expand Down

0 comments on commit 535fbbd

Please sign in to comment.