Skip to content

Commit

Permalink
Agent integration tests for ratelimit and Interceptor services
Browse files Browse the repository at this point in the history
  • Loading branch information
O-sura committed Apr 15, 2024
1 parent d9749db commit 9e25255
Show file tree
Hide file tree
Showing 16 changed files with 1,443 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -576,4 +576,20 @@ public void create_new_version_of_the_api(String newVersion, String isDefaultVer
sharedContext.setApiUUID(Utils.extractID(sharedContext.getResponseBody()));
Thread.sleep(3000);
}

@Then("I set new API throttling policy allowing {string} requests per every {string} minute")
public void add_new_custom_throttling_policy(String requestCount, String unitTime) throws Exception {
// {"policyName":"TestRatelimit","description":"Test descroption","conditionalGroups":[],"defaultLimit":{"requestCount":{"timeUnit":"min","unitTime":"1","requestCount":"3"},"type":"REQUESTCOUNTLIMIT","bandwidth":null}}
String payload = "{\"policyName\":\"TestRatelimit\",\"description\":\"Test descroption\",\"conditionalGroups\":[],\"defaultLimit\":{\"requestCount\":{\"timeUnit\":\"min\",\"unitTime\":"+ unitTime + ",\"requestCount\":" + requestCount + "},\"type\":\"REQUESTCOUNTLIMIT\",\"bandwidth\":null}}";

Map<String, String> headers = new HashMap<>();
headers.put(Constants.REQUEST_HEADERS.AUTHORIZATION, "Bearer " + sharedContext.getAdminAccessToken());
headers.put(Constants.REQUEST_HEADERS.HOST, Constants.DEFAULT_API_HOST);
//https://am.wso2.com/api/am/admin/v4/throttling/policies/advanced
// https:// + DEFAULT_API_HOST + "/" + DEFAULT_ADMINPORTAL + "throttling/policies/advanced"
HttpResponse httpResponse = sharedContext.getHttpClient().doPost(Utils.getAPIThrottlingConfigEndpoint(), headers, payload, Constants.CONTENT_TYPES.APPLICATION_JSON);
sharedContext.setResponse(httpResponse);
sharedContext.setResponseBody(SimpleHTTPClient.responseEntityBodyToString(sharedContext.getResponse()));
Thread.sleep(3000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,22 @@ public void iHaveValidDevportalAccessToken() throws Exception {
logger.info("Devportal Access Token: " + sharedContext.getDevportalAccessToken());
}

@Given("I have a valid Adminportal access token")
public void iHaveValidAdminportalAccessToken() throws Exception {
logger.info("Basic Auth Header: " + sharedContext.getBasicAuthToken());

Map<String, String> headers = new HashMap<>();
String basicAuthHeader = "Basic " + sharedContext.getBasicAuthToken();
headers.put(Constants.REQUEST_HEADERS.HOST, Constants.DEFAULT_IDP_HOST);
headers.put(Constants.REQUEST_HEADERS.AUTHORIZATION, basicAuthHeader);

HttpResponse httpResponse = httpClient.doPost(Utils.getTokenEndpointURL(), headers, "grant_type=password&username=admin&password=admin&scope=apim:app_manage apim:admin_tier_view apim:admin_tier_manage",
Constants.CONTENT_TYPES.APPLICATION_X_WWW_FORM_URLENCODED);
sharedContext.setAdminAccessToken(Utils.extractToken(httpResponse));
sharedContext.addStoreValue("adminportalAccessToken", sharedContext.getAdminAccessToken());
logger.info("Admin Access Token: " + sharedContext.getAdminAccessToken());
}

@Then("the response should be given as valid")
public void theResponseShouldBeGivenAs() throws IOException {
Boolean status = sharedContext.getDefinitionValidStatus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class SharedContext {
private SimpleHTTPClient httpClient;
private String publisherAccessToken;
private String devportalAccessToken;
private String adminportalAccessToken;
private String basicAuthToken;
private HttpResponse response;
private String responseBody;
Expand Down Expand Up @@ -79,6 +80,16 @@ public void setDevportalAccessToken(String accessToken) {
this.devportalAccessToken = accessToken;
}

public String getAdminAccessToken() {

return adminportalAccessToken;
}

public void setAdminAccessToken(String accessToken) {

this.adminportalAccessToken = accessToken;
}

public String getBasicAuthToken() {

return basicAuthToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class Constants {
public static final String DEFAULT_API_CONFIGURATOR = "api/configurator/1.1.0/";
public static final String DEFAULT_API_DEPLOYER = "api/am/publisher/v4/";
public static final String DEFAULT_DEVPORTAL = "api/am/devportal/v3/";
public static final String DEFAULT_ADMINPORTAL = "api/am/admin/v4/";
public static final String ACCESS_TOKEN = "accessToken";
public static final String EMPTY_STRING = "";
public static final String API_CREATE_SCOPE = "apk:api_create";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ public static String getAPINewVersionCreationURL() {
+ Constants.DEFAULT_API_DEPLOYER + "apis/copy-api";
}

public static String getAPIThrottlingConfigEndpoint() {
return "https://" + Constants.DEFAULT_API_HOST + ":" + Constants.DEFAULT_GW_PORT + "/"
+ Constants.DEFAULT_ADMINPORTAL+ "throttling/policies/advanced";
}

public static String extractID(String payload) throws IOException {

JSONParser parser = new JSONParser();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{
"name": "EmployeeAPI",
"context": "/api-policy",
"version": "3.14",
"type": "HTTP",
"audience": null,
"transport": ["http", "https"],
"policies": ["Unlimited, Gold"],
"authorizationHeader": "Authorization",
"apiKeyHeader": "ApiKey",
"securityScheme": ["oauth_basic_auth_api_key_mandatory", "oauth2"],
"apiPolicies": {
"request": [
{
"policyName": "addLogMessage",
"policyId": "e64c0cf0-8df0-4659-a299-099d390de6d0",
"policyVersion": "v1",
"parameters": {}
},
{
"policyName": "jsonValidator",
"policyId": "f11983f9-6429-415f-880a-b407192f137f",
"policyVersion": "v1",
"parameters": {
"maxPropertyCount": "100",
"maxStringLength": "100",
"maxArrayElementCount": "100",
"maxKeyLength": "100",
"maxJsonDepth": "100",
"requestMessageBufferSize": "1024",
"jsonSchemaValidator": "org.wso2.carbon.apimgt.gateway.mediators.JsonSchemaValidator"
}
}
],
"response": [
{
"policyName": "addLogMessage",
"policyId": "e64c0cf0-8df0-4659-a299-099d390de6d0",
"policyVersion": "v1",
"parameters": {}
}
],
"fault": [
{
"policyName": "jsonFault",
"policyId": "cba00e78-34a3-4380-8a76-766b99147f18",
"policyVersion": "v1",
"parameters": {}
}
]
},
"endpointConfig": {
"endpoint_type": "http",
"sandbox_endpoints": {"url": "http://backend:80/anything"},
"production_endpoints": {"url": "http://backend:80/anything"}
},
"endpointImplementationType": "ENDPOINT",
"operations": [
{
"id": "",
"target": "/employee",
"verb": "GET",
"authType": "Application & Application User",
"throttlingPolicy": "10KPerMin",
"scopes": [],
"usedProductIds": [],
"amznResourceName": null,
"amznResourceTimeout": null,
"amznResourceContentEncode": null,
"payloadSchema": null,
"uriMapping": null,
"operationPolicies": {"request": [], "response": [], "fault": []}
},
{
"id": "",
"target": "/employee",
"verb": "POST",
"authType": "Application & Application User",
"throttlingPolicy": "20KPerMin",
"scopes": [],
"usedProductIds": [],
"amznResourceName": null,
"amznResourceTimeout": null,
"amznResourceContentEncode": null,
"payloadSchema": null,
"uriMapping": null,
"operationPolicies": {"request": [], "response": [], "fault": []}
},
{
"id": "",
"target": "/employee/{employeeId}",
"verb": "PUT",
"authType": "Application & Application User",
"throttlingPolicy": "Unlimited",
"scopes": [],
"usedProductIds": [],
"amznResourceName": null,
"amznResourceTimeout": null,
"amznResourceContentEncode": null,
"payloadSchema": null,
"uriMapping": null,
"operationPolicies": {"request": [], "response": [], "fault": []}
},
{
"id": "",
"target": "/employee/{employeeId}",
"verb": "DELETE",
"authType": "Application & Application User",
"throttlingPolicy": "Unlimited",
"scopes": [],
"usedProductIds": [],
"amznResourceName": null,
"amznResourceTimeout": null,
"amznResourceContentEncode": null,
"payloadSchema": null,
"uriMapping": null,
"operationPolicies": {"request": [], "response": [], "fault": []}
}
],
"gatewayVendor": "wso2",
"gatewayType":"wso2/apk"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{
"name": "EmployeeAPI",
"context": "/api-policy",
"version": "3.14",
"type": "HTTP",
"audience": null,
"transport": ["http", "https"],
"policies": ["Unlimited, Gold"],
"authorizationHeader": "Authorization",
"apiKeyHeader": "ApiKey",
"securityScheme": ["oauth_basic_auth_api_key_mandatory", "oauth2"],
"apiPolicies": {
"request": [
{
"policyName": "addLogMessage",
"policyId": "e64c0cf0-8df0-4659-a299-099d390de6d0",
"policyVersion": "v1",
"parameters": {}
},
{
"policyName": "jsonValidator",
"policyId": "f11983f9-6429-415f-880a-b407192f137f",
"policyVersion": "v1",
"parameters": {
"maxPropertyCount": "100",
"maxStringLength": "100",
"maxArrayElementCount": "100",
"maxKeyLength": "100",
"maxJsonDepth": "100",
"requestMessageBufferSize": "1024",
"jsonSchemaValidator": "org.wso2.carbon.apimgt.gateway.mediators.JsonSchemaValidator"
}
}
],
"response": [
{
"policyName": "addLogMessage",
"policyId": "e64c0cf0-8df0-4659-a299-099d390de6d0",
"policyVersion": "v1",
"parameters": {}
}
],
"fault": [
{
"policyName": "jsonFault",
"policyId": "cba00e78-34a3-4380-8a76-766b99147f18",
"policyVersion": "v1",
"parameters": {}
}
]
},
"endpointConfig": {
"endpoint_type": "http",
"sandbox_endpoints": {"url": "http://backend:80/anything"},
"production_endpoints": {"url": "http://backend:80/anything"}
},
"endpointImplementationType": "ENDPOINT",
"operations": [
{
"id": "",
"target": "/employee",
"verb": "GET",
"authType": "Application & Application User",
"throttlingPolicy": "10KPerMin",
"scopes": [],
"usedProductIds": [],
"amznResourceName": null,
"amznResourceTimeout": null,
"amznResourceContentEncode": null,
"payloadSchema": null,
"uriMapping": null,
"operationPolicies": {"request": [], "response": [], "fault": []}
},
{
"id": "",
"target": "/employee",
"verb": "POST",
"authType": "Application & Application User",
"throttlingPolicy": "20KPerMin",
"scopes": [],
"usedProductIds": [],
"amznResourceName": null,
"amznResourceTimeout": null,
"amznResourceContentEncode": null,
"payloadSchema": null,
"uriMapping": null,
"operationPolicies": {"request": [], "response": [], "fault": []}
},
{
"id": "",
"target": "/employee/{employeeId}",
"verb": "PUT",
"authType": "Application & Application User",
"throttlingPolicy": "Unlimited",
"scopes": [],
"usedProductIds": [],
"amznResourceName": null,
"amznResourceTimeout": null,
"amznResourceContentEncode": null,
"payloadSchema": null,
"uriMapping": null,
"operationPolicies": {"request": [], "response": [], "fault": []}
},
{
"id": "",
"target": "/employee/{employeeId}",
"verb": "DELETE",
"authType": "Application & Application User",
"throttlingPolicy": "Unlimited",
"scopes": [],
"usedProductIds": [],
"amznResourceName": null,
"amznResourceTimeout": null,
"amznResourceContentEncode": null,
"payloadSchema": null,
"uriMapping": null,
"operationPolicies": {"request": [], "response": [], "fault": []}
}
],
"gatewayVendor": "wso2",
"gatewayType":"wso2/apk"
}
Loading

0 comments on commit 9e25255

Please sign in to comment.