Skip to content

Commit

Permalink
- Handle api method quota update in case of spec is updated as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
rathnapandi committed Oct 21, 2024
1 parent a88561b commit 30756f6
Show file tree
Hide file tree
Showing 9 changed files with 1,145 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public List<APIMethod> getAllMethodsForAPI(String apiId) throws AppException {
readMethodsFromAPIManager(apiId);
List<APIMethod> apiMethods;
try {
apiMethods = mapper.readValue(this.apiManagerResponse.get(apiId), new TypeReference<List<APIMethod>>() {
apiMethods = mapper.readValue(this.apiManagerResponse.get(apiId), new TypeReference<>() {
});
} catch (IOException e) {
throw new AppException(ERROR_CANT_LOAD_API_METHODS_FOR_API + apiId + "' from API-Manager.", ErrorCode.API_MANAGER_COMMUNICATION, e);
Expand Down
4 changes: 4 additions & 0 deletions modules/apim-cli/assembly/scripts/apim.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ done

cd "$currentDir"

### Enable Debugging of http headers and payload
### "$_java" -Xms64m -Xmx256m -Dlog4j.configurationFile=../lib/log4j2.xml -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog -Dorg.apache.commons.logging.simplelog.showdatetime=true -Dorg.apache.commons.logging.simplelog.log.org.apache.http=DEBUG -classpath "$CP" com.axway.apim.cli.APIManagerCLI "${@}"


"$_java" -Xms64m -Xmx256m -Dlog4j.configurationFile=../lib/log4j2.xml -classpath "$CP" com.axway.apim.cli.APIManagerCLI "${@}"
rc=$?
exit $rc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public List<QuotaRestriction> mergeRestriction(List<QuotaRestriction> existingRe
desiredRestriction.setApiId(null);
// And compare each desired restriction, if it is already included in the existing restrictions
for (QuotaRestriction existingRestriction : existingRestrictions) {
// It's considered as the same restriction when type, method, period & per are equal
// It's considered as the same restriction when quota type, method, period & per are equal
if (desiredRestriction.isSameRestriction(existingRestriction, true)) {
// If it is the same restriction, we need to update the restriction configuration
if (existingRestriction.getType() == QuotaRestrictionType.throttle) {
Expand Down Expand Up @@ -144,15 +144,21 @@ public void populateMethodId(API createdAPI, List<QuotaRestriction> mergedRestri
// Additionally, we have to change the methodId
// Load the method for actualAPI to get the name of the method to which the existing quota is applied to
if (actualState != null) {
APIMethod actualMethod = methodAdapter.getMethodForName(actualState.getId(), restriction.getMethod());
if (actualMethod != null) {
// Now load the new method based on the name for the createdAPI
APIMethod newMethod = methodAdapter.getMethodForName(createdAPI.getId(), actualMethod.getName());
// Finally modify the restriction
try {
APIMethod actualMethod = methodAdapter.getMethodForName(actualState.getId(), restriction.getMethod());
if (actualMethod != null) {
// Now load the new method based on the name for the createdAPI
APIMethod newMethod = methodAdapter.getMethodForName(createdAPI.getId(), actualMethod.getName());
// Finally modify the restriction
restriction.setMethod(newMethod.getId());
}
} catch (AppException e) {
LOG.warn("{}", e.getMessage());
// Now load the new method based on the name for the createdAPI as existing api does not have the method.
APIMethod newMethod = methodAdapter.getMethodForName(createdAPI.getId(), restriction.getMethod());
restriction.setMethod(newMethod.getId());
} else {
LOG.warn("API Method Name : {} not found in specification", restriction.getMethod());
}

} else {
// For new api creation
APIMethod newMethod = methodAdapter.getMethodForName(createdAPI.getId(), restriction.getMethod());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
package com.axway.apim.apiimport.actions;

import com.axway.apim.WiremockWrapper;
import com.axway.apim.api.API;
import com.axway.apim.api.model.APIQuota;
import com.axway.apim.api.model.QuotaRestriction;
import com.axway.apim.lib.CoreParameters;
import com.axway.apim.lib.utils.Utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;

public class APIQuotaManagerTest {
public class APIQuotaManagerTest extends WiremockWrapper {

@BeforeClass
public void initWiremock() {
super.initWiremock();
}

@AfterClass
public void close() {
super.close();
}

ObjectMapper objectMapper = new ObjectMapper();


@Test
public void mergeRestriction() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String actualQuota = "[\n" +
" {\n" +
" \"api\": \"*\",\n" +
Expand Down Expand Up @@ -69,7 +89,7 @@ public void mergeRestriction() throws IOException {
" }\n" +
" }\n" +
" ]";
TypeReference<List<QuotaRestriction>> quotaRestrictionTypeRef = new TypeReference<List<QuotaRestriction>>() {
TypeReference<List<QuotaRestriction>> quotaRestrictionTypeRef = new TypeReference<>() {
};
List<QuotaRestriction> actualQuotaRestriction = objectMapper.readValue(actualQuota, quotaRestrictionTypeRef);
List<QuotaRestriction> desiredQuotaRestriction = objectMapper.readValue(desiredQuota, quotaRestrictionTypeRef);
Expand All @@ -81,7 +101,6 @@ public void mergeRestriction() throws IOException {

@Test
public void mergeRestrictionActual() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String actualQuota = "[\n" +
" {\n" +
" \"api\": \"4f52e8a2-cba1-4645-8088-2ed98f5ea57e\",\n" +
Expand Down Expand Up @@ -137,7 +156,7 @@ public void mergeRestrictionActual() throws IOException {
" }\n" +
" }\n" +
" ]";
TypeReference<List<QuotaRestriction>> quotaRestrictionTypeRef = new TypeReference<List<QuotaRestriction>>() {
TypeReference<List<QuotaRestriction>> quotaRestrictionTypeRef = new TypeReference<>() {
};
List<QuotaRestriction> actualQuotaRestriction = objectMapper.readValue(actualQuota, quotaRestrictionTypeRef);
List<QuotaRestriction> desiredQuotaRestriction = objectMapper.readValue(desiredQuota, quotaRestrictionTypeRef);
Expand All @@ -148,7 +167,6 @@ public void mergeRestrictionActual() throws IOException {

@Test
public void mergeRestrictionDesired() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String actualQuota = "[\n" +
" {\n" +
" \"method\": \"PATCH /{txid}\",\n" +
Expand Down Expand Up @@ -199,7 +217,7 @@ public void mergeRestrictionDesired() throws IOException {
" }\n" +
" }\n" +
" ]";
TypeReference<List<QuotaRestriction>> quotaRestrictionTypeRef = new TypeReference<List<QuotaRestriction>>() {
TypeReference<List<QuotaRestriction>> quotaRestrictionTypeRef = new TypeReference<>() {
};
List<QuotaRestriction> actualQuotaRestriction = objectMapper.readValue(actualQuota, quotaRestrictionTypeRef);
List<QuotaRestriction> desiredQuotaRestriction = objectMapper.readValue(desiredQuota, quotaRestrictionTypeRef);
Expand All @@ -211,7 +229,6 @@ public void mergeRestrictionDesired() throws IOException {

@Test
public void mergeRestrictionDesiredWithNoActual() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String actualQuota = "[]";

String desiredQuota = "[\n" +
Expand Down Expand Up @@ -243,12 +260,242 @@ public void mergeRestrictionDesiredWithNoActual() throws IOException {
" }\n" +
" }\n" +
" ]";
TypeReference<List<QuotaRestriction>> quotaRestrictionTypeRef = new TypeReference<List<QuotaRestriction>>() {
TypeReference<List<QuotaRestriction>> quotaRestrictionTypeRef = new TypeReference<>() {
};
List<QuotaRestriction> actualQuotaRestriction = objectMapper.readValue(actualQuota, quotaRestrictionTypeRef);
List<QuotaRestriction> desiredQuotaRestriction = objectMapper.readValue(desiredQuota, quotaRestrictionTypeRef);
APIQuotaManager apiQuotaManager = new APIQuotaManager(null, null);
List<QuotaRestriction> result = apiQuotaManager.mergeRestriction(actualQuotaRestriction, desiredQuotaRestriction);
Assert.assertEquals(3, result.size());
}


@Test
public void populateMethodIdForNewApi() throws JsonProcessingException {

CoreParameters coreParameters = new CoreParameters();
coreParameters.setHostname("localhost");
coreParameters.setUsername("test");
coreParameters.setPassword(Utils.getEncryptedPassword());

String desiredQuota = "[\n" +
" {\n" +
" \"method\": \"addPet\",\n" +
" \"type\": \"throttle\",\n" +
" \"config\": {\n" +
" \"period\": \"second\",\n" +
" \"per\": \"1\",\n" +
" \"messages\": \"656\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"method\": \"deleteUser\",\n" +
" \"type\": \"throttle\",\n" +
" \"config\": {\n" +
" \"period\": \"second\",\n" +
" \"per\": \"1\",\n" +
" \"messages\": \"750\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"method\": \"getUserByName\",\n" +
" \"type\": \"throttle\",\n" +
" \"config\": {\n" +
" \"period\": \"second\",\n" +
" \"per\": \"1\",\n" +
" \"messages\": \"887\"\n" +
" }\n" +
" }\n" +
" ]";
TypeReference<List<QuotaRestriction>> quotaRestrictionTypeRef = new TypeReference<>() {
};
List<QuotaRestriction> desiredQuotaRestriction = objectMapper.readValue(desiredQuota, quotaRestrictionTypeRef);
API createdAPI = new API();
APIQuota apiQuota = new APIQuota();
apiQuota.setRestrictions(desiredQuotaRestriction);
createdAPI.setSystemQuota(apiQuota);
createdAPI.setId("e4ded8c8-0a40-4b50-bc13-552fb7209150");
APIQuotaManager apiQuotaManager = new APIQuotaManager(null, null);
apiQuotaManager.populateMethodId(createdAPI, desiredQuotaRestriction);

Assert.assertEquals(createdAPI.getSystemQuota().getRestrictions().get(0).getMethod(), "dfd61eee-cf2d-4d97-bbb6-f602fd2063bd");


}

@Test
public void populateMethodIdForExistingApi() throws JsonProcessingException {

CoreParameters coreParameters = new CoreParameters();
coreParameters.setHostname("localhost");
coreParameters.setUsername("test");
coreParameters.setPassword(Utils.getEncryptedPassword());

String actualQuota = "[\n" +
" {\n" +
" \"method\": \"addPet\",\n" +
" \"type\": \"throttle\",\n" +
" \"config\": {\n" +
" \"period\": \"second\",\n" +
" \"per\": \"1\",\n" +
" \"messages\": \"656\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"method\": \"deleteUser\",\n" +
" \"type\": \"throttle\",\n" +
" \"config\": {\n" +
" \"period\": \"second\",\n" +
" \"per\": \"1\",\n" +
" \"messages\": \"750\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"method\": \"getUserByName\",\n" +
" \"type\": \"throttle\",\n" +
" \"config\": {\n" +
" \"period\": \"second\",\n" +
" \"per\": \"1\",\n" +
" \"messages\": \"887\"\n" +
" }\n" +
" }\n" +
" ]";

String desiredQuota = "[\n" +
" {\n" +
" \"method\": \"addPet\",\n" +
" \"type\": \"throttle\",\n" +
" \"config\": {\n" +
" \"period\": \"second\",\n" +
" \"per\": \"1\",\n" +
" \"messages\": \"656\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"method\": \"deleteUser\",\n" +
" \"type\": \"throttle\",\n" +
" \"config\": {\n" +
" \"period\": \"second\",\n" +
" \"per\": \"1\",\n" +
" \"messages\": \"750\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"method\": \"getUserByName\",\n" +
" \"type\": \"throttle\",\n" +
" \"config\": {\n" +
" \"period\": \"second\",\n" +
" \"per\": \"1\",\n" +
" \"messages\": \"887\"\n" +
" }\n" +
" }\n" +
" ]";
TypeReference<List<QuotaRestriction>> quotaRestrictionTypeRef = new TypeReference<>() {
};
List<QuotaRestriction> desiredQuotaRestriction = objectMapper.readValue(desiredQuota, quotaRestrictionTypeRef);
API createdAPI = new API();
APIQuota apiQuota = new APIQuota();
apiQuota.setRestrictions(desiredQuotaRestriction);
createdAPI.setSystemQuota(apiQuota);
createdAPI.setId("e4ded8c8-0a40-4b50-bc13-552fb7209150");


List<QuotaRestriction> actualQuotaRestriction = objectMapper.readValue(actualQuota, quotaRestrictionTypeRef);
API actualAPI = new API();
APIQuota apiQuotaActualApi = new APIQuota();

apiQuotaActualApi.setRestrictions(actualQuotaRestriction);
actualAPI.setSystemQuota(apiQuotaActualApi);
actualAPI.setId("e4ded8c8-0a40-4b50-bc13-552fb7209150");

APIQuotaManager apiQuotaManager = new APIQuotaManager(createdAPI, actualAPI);
apiQuotaManager.populateMethodId(createdAPI, desiredQuotaRestriction);

Assert.assertEquals(createdAPI.getSystemQuota().getRestrictions().get(0).getMethod(), "dfd61eee-cf2d-4d97-bbb6-f602fd2063bd");
Assert.assertEquals(actualAPI.getSystemQuota().getRestrictions().get(0).getMethod(), "addPet");
}


@Test
public void populateMethodIdForExistingApiWithQuotaChange() throws JsonProcessingException {

CoreParameters coreParameters = new CoreParameters();
coreParameters.setHostname("localhost");
coreParameters.setUsername("test");
coreParameters.setPassword(Utils.getEncryptedPassword());

String actualQuota = "[\n" +
" {\n" +
" \"method\": \"deleteUser\",\n" +
" \"type\": \"throttle\",\n" +
" \"config\": {\n" +
" \"period\": \"second\",\n" +
" \"per\": \"1\",\n" +
" \"messages\": \"750\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"method\": \"getUserByName\",\n" +
" \"type\": \"throttle\",\n" +
" \"config\": {\n" +
" \"period\": \"second\",\n" +
" \"per\": \"1\",\n" +
" \"messages\": \"887\"\n" +
" }\n" +
" }\n" +
" ]";

String desiredQuota = "[\n" +
" {\n" +
" \"method\": \"addPet\",\n" +
" \"type\": \"throttle\",\n" +
" \"config\": {\n" +
" \"period\": \"second\",\n" +
" \"per\": \"1\",\n" +
" \"messages\": \"656\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"method\": \"deleteUser\",\n" +
" \"type\": \"throttle\",\n" +
" \"config\": {\n" +
" \"period\": \"second\",\n" +
" \"per\": \"1\",\n" +
" \"messages\": \"750\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"method\": \"getUserByName\",\n" +
" \"type\": \"throttle\",\n" +
" \"config\": {\n" +
" \"period\": \"second\",\n" +
" \"per\": \"1\",\n" +
" \"messages\": \"887\"\n" +
" }\n" +
" }\n" +
" ]";
TypeReference<List<QuotaRestriction>> quotaRestrictionTypeRef = new TypeReference<>() {
};
List<QuotaRestriction> desiredQuotaRestriction = objectMapper.readValue(desiredQuota, quotaRestrictionTypeRef);
API createdAPI = new API();
APIQuota apiQuota = new APIQuota();
apiQuota.setRestrictions(desiredQuotaRestriction);
createdAPI.setSystemQuota(apiQuota);
createdAPI.setId("e4ded8c8-0a40-4b50-bc13-552fb7209150");


List<QuotaRestriction> actualQuotaRestriction = objectMapper.readValue(actualQuota, quotaRestrictionTypeRef);
API actualAPI = new API();
APIQuota apiQuotaActualApi = new APIQuota();

apiQuotaActualApi.setRestrictions(actualQuotaRestriction);
actualAPI.setSystemQuota(apiQuotaActualApi);
actualAPI.setId("e4ded8c8-0a40-4b50-bc13-552fb7209150");

APIQuotaManager apiQuotaManager = new APIQuotaManager(createdAPI, actualAPI);
apiQuotaManager.populateMethodId(createdAPI, desiredQuotaRestriction);

Assert.assertEquals(createdAPI.getSystemQuota().getRestrictions().get(0).getMethod(), "dfd61eee-cf2d-4d97-bbb6-f602fd2063bd");
Assert.assertEquals(actualAPI.getSystemQuota().getRestrictions().get(0).getMethod(), "deleteUser");
}
}
Loading

0 comments on commit 30756f6

Please sign in to comment.