Skip to content

Commit

Permalink
tests: Add tests for wso2#11015
Browse files Browse the repository at this point in the history
  • Loading branch information
praminda authored and Krishanx92 committed Oct 5, 2021
1 parent 1b0ea6b commit 88c9bb4
Showing 1 changed file with 44 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.Response;

import static org.testng.Assert.assertEquals;

/**
Expand All @@ -53,6 +55,14 @@
public class DefaultVersionAPITestCase extends APIManagerLifecycleBaseTest {
private String applicationID;
private String apiId;
private String v2ApiId;
private String provider;
private String apiName;
private String apiVersion;
private String apiContext;
private String endpointUrl;
Map<String, String> headers;

@Factory(dataProvider = "userModeDataProvider")
public DefaultVersionAPITestCase(TestUserMode userMode) {
this.userMode = userMode;
Expand All @@ -66,23 +76,22 @@ public static Object[][] userModeDataProvider() {
};
}


@BeforeClass(alwaysRun = true)
public void setEnvironment() throws Exception {
super.init(userMode);
provider = publisherContext.getContextTenant().getContextUser().getUserName();
apiName = "DefaultVersionAPI";
apiVersion = "1.0.0";
apiContext = "defaultversion";
endpointUrl = getGatewayURLNhttp() + "response";
headers = new HashMap<>();
}

@Test(groups = "wso2.am", description = "Check functionality of the default version API")
public void testDefaultVersionAPI() throws Exception {

String apiName = "DefaultVersionAPI";
String apiVersion = "1.0.0";
String apiContext = "defaultversion";
String endpointUrl = getGatewayURLNhttp() + "response";

//Create the api creation request object
APIRequest apiRequest = new APIRequest(apiName, apiContext, new URL(endpointUrl));
apiRequest.setProvider(publisherContext.getContextTenant().getContextUser().getUserName());
apiRequest.setProvider(provider);
apiRequest.setDefault_version("default_version");
apiRequest.setDefault_version_checked("true");
apiRequest.setVersion(apiVersion);
Expand All @@ -106,6 +115,7 @@ public void testDefaultVersionAPI() throws Exception {
ApplicationKeyDTO applicationKeyDTO = restAPIStore.generateKeys(applicationID, "3600", null,
ApplicationKeyGenerateRequestDTO.KeyTypeEnum.PRODUCTION, null, grantTypes);
String accessToken = applicationKeyDTO.getToken().getAccessToken();
headers.put("Authorization", "Bearer " + accessToken);

String apiInvocationUrl = getAPIInvocationURLHttp(apiContext);
waitForAPIDeploymentSync(apiRequest.getProvider(), apiRequest.getName(), apiRequest.getVersion(),
Expand All @@ -114,9 +124,6 @@ public void testDefaultVersionAPI() throws Exception {
//Going to access the API without the version in the request url.
HttpResponse directResponse = HttpRequestUtil.doGet(endpointUrl, new HashMap<>());

Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + accessToken);

//Invoke the API
waitForAPIDeploymentSync(apiRequest.getProvider(), apiRequest.getName(), apiRequest.getVersion(),
APIMIntegrationConstants.IS_API_EXISTS);
Expand All @@ -125,7 +132,31 @@ public void testDefaultVersionAPI() throws Exception {

//Check if accessing the back-end directly and accessing it via the API yield the same responses.
assertEquals(httpResponse.getData(), directResponse.getData(),
"Default version API test failed while " + "invoking the API.");
"Default version API test failed while invoking the API.");
}

@Test(groups = "wso2.am", dependsOnMethods = "testDefaultVersionAPI", description = "Check if the default" +
" version API is available after creating a new version of the original API")
public void testDefaultVersionAPIAfterNewVersion() throws Exception {
String newVersion = "2.0.0";
String defaultUrl = getAPIInvocationURLHttp(apiContext);

// create new version
HttpResponse v2Response = restAPIPublisher.copyAPI(newVersion, apiId, false);
v2ApiId = v2Response.getData();
assertEquals(v2Response.getResponseCode(), Response.Status.OK.getStatusCode(),
"Response Code Mismatch. Didn't create new API version");

// publish new version
HttpResponse v2PublishResponse = restAPIPublisher.changeAPILifeCycleStatusToPublish(v2ApiId, false);
assertEquals(v2PublishResponse.getResponseCode(), Response.Status.OK.getStatusCode(),
"Response Code Mismatch, Didn't publish new API version");

// invoke the API
waitForAPIDeploymentSync(provider, apiName, newVersion, APIMIntegrationConstants.IS_API_EXISTS);
HttpResponse httpResponse = HttpRequestUtil.doGet(defaultUrl, headers);
assertEquals(httpResponse.getResponseCode(), Response.Status.OK.getStatusCode(),
"Can't invoke default version after creating a new version");
}

@AfterClass(alwaysRun = true)
Expand All @@ -136,6 +167,7 @@ public void destroy() throws Exception {
}
restAPIStore.deleteApplication(applicationID);
restAPIPublisher.deleteAPI(apiId);
restAPIPublisher.deleteAPI(v2ApiId);
super.cleanUp();
}

Expand Down

0 comments on commit 88c9bb4

Please sign in to comment.