Skip to content

Commit

Permalink
Change Api Provider Test Case Added
Browse files Browse the repository at this point in the history
  • Loading branch information
shnrndk committed Dec 5, 2023
1 parent c7c62fd commit bafe7a7
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package org.wso2.am.integration.tests.admin;

import jdk.internal.joptsimple.internal.Strings;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.junit.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import org.wso2.am.integration.clients.admin.ApiResponse;
import org.wso2.am.integration.clients.store.api.v1.dto.ApplicationDTO;
import org.wso2.am.integration.clients.store.api.v1.dto.ApplicationKeyDTO;
import org.wso2.am.integration.clients.store.api.v1.dto.ApplicationKeyGenerateRequestDTO;
import org.wso2.am.integration.test.impl.RestAPIAdminImpl;
import org.wso2.am.integration.test.impl.RestAPIStoreImpl;
import org.wso2.am.integration.test.utils.base.APIMIntegrationBaseTest;
import org.wso2.am.integration.test.utils.base.APIMIntegrationConstants;
import org.wso2.am.integration.test.utils.bean.APILifeCycleAction;
import org.wso2.am.integration.test.utils.bean.APIRequest;
import org.wso2.carbon.automation.engine.context.TestUserMode;
import org.wso2.carbon.automation.test.utils.http.client.HttpRequestUtil;
import org.wso2.carbon.automation.test.utils.http.client.HttpResponse;
import org.wso2.carbon.integration.common.admin.client.UserManagementClient;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import static org.testng.Assert.assertEquals;
import static org.wso2.am.integration.test.utils.base.APIMIntegrationConstants.SUPER_TENANT_DOMAIN;

public class ChangeApiProviderTestCase extends APIMIntegrationBaseTest {

private String publisherURLHttp;
private RestAPIAdminImpl restAPIAdminClient;
private String BEARER = "Bearer ";
private String APIName = "NewApiForProviderChange";
private String APIContext = "NewApiForProviderChange";
private String tags = "youtube, token, media";
private String apiEndPointUrl;
private String description = "This is test API create by API manager integration test";
private String APIVersion = "1.0.0";
private String apiID;
private String newUser = "peter123";
private String newUserPass = "test123";
private String[] subscriberRole = {APIMIntegrationConstants.APIM_INTERNAL_ROLE.CREATOR};
private String APPLICATION_NAME = "testApplicationForProviderChange";
private String applicationId;
private String TIER_GOLD = "Gold";
private String API_ENDPOINT_POSTFIX_URL = "jaxrs_basic/services/customers/customerservice/";
private String API_ENDPOINT_METHOD = "customers/123";
private int HTTP_RESPONSE_CODE_OK = Response.Status.OK.getStatusCode();
private String RESPONSE_CODE_MISMATCH_ERROR_MESSAGE = "Response code mismatch";
private String TENANT_ADMIN = "admin";
private String TENANT_ADMIN_PWD = "admin123";
private String TENANT_DOMAIN = "tenant.com";

@Factory(dataProvider = "userModeDataProvider")
public ChangeApiProviderTestCase(TestUserMode userMode) {

this.userMode = userMode;
}

@DataProvider
public static Object[][] userModeDataProvider() {

return new Object[][]{new Object[]{TestUserMode.SUPER_TENANT_ADMIN},
new Object[]{TestUserMode.TENANT_ADMIN},};
}

@BeforeClass(alwaysRun = true)
public void setEnvironment() throws Exception {

super.init(userMode);
publisherURLHttp = getPublisherURLHttp();
userManagementClient = new UserManagementClient(keyManagerContext.getContextUrls().getBackEndUrl(),
createSession(keyManagerContext));
userManagementClient.addUser(newUser, newUserPass, subscriberRole, newUser);
restAPIStore =
new RestAPIStoreImpl(storeContext.getContextTenant().getContextUser().getUserNameWithoutDomain(),
storeContext.getContextTenant().getContextUser().getPassword(),
storeContext.getContextTenant().getDomain(), storeURLHttps);
apiEndPointUrl = backEndServerUrl.getWebAppURLHttp() + API_ENDPOINT_POSTFIX_URL;
restAPIAdminClient = new RestAPIAdminImpl(TENANT_ADMIN, TENANT_ADMIN_PWD, TENANT_DOMAIN, publisherURLHttps);
}

@Test(groups = {"wso2.am"}, description = "Calling API with invalid token")
public void ChangeApiProvider() throws Exception {
String providerName = user.getUserName();
APIRequest apiRequest = new APIRequest(APIName, APIContext, new URL(apiEndPointUrl));
apiRequest.setTags(tags);
apiRequest.setProvider(providerName);
apiRequest.setDescription(description);
apiRequest.setVersion(APIVersion);
apiRequest.setResourceMethod("GET");

//add test api
HttpResponse serviceResponse = restAPIPublisher.addAPI(apiRequest);
assertEquals(serviceResponse.getResponseCode(), Response.Status.CREATED.getStatusCode(),
"Response Code miss matched when creating the API");
apiID = serviceResponse.getData();

// Create Revision and Deploy to Gateway
createAPIRevisionAndDeployUsingRest(apiID, restAPIPublisher);

//publish the api
restAPIPublisher.changeAPILifeCycleStatus(apiID, APILifeCycleAction.PUBLISH.getAction(), null);

HttpResponse applicationResponse = restAPIStore.createApplication(APPLICATION_NAME, Strings.EMPTY,
APIMIntegrationConstants.APPLICATION_TIER.UNLIMITED,
ApplicationDTO.TokenTypeEnum.JWT);

applicationId = applicationResponse.getData();

restAPIStore.subscribeToAPI(apiID, applicationId, TIER_GOLD);
ArrayList<String> grantTypes = new ArrayList<>();
grantTypes.add(APIMIntegrationConstants.GRANT_TYPE.CLIENT_CREDENTIAL);
ApplicationKeyDTO applicationKeyDTO = restAPIStore.generateKeys(applicationId,
APIMIntegrationConstants.DEFAULT_TOKEN_VALIDITY_TIME,
null,
ApplicationKeyGenerateRequestDTO.KeyTypeEnum.PRODUCTION,
null, grantTypes);
Assert.assertNotNull(applicationKeyDTO.getToken());
String accessToken = applicationKeyDTO.getToken().getAccessToken();

HashMap<String, String> requestHeaders = new HashMap<>();
requestHeaders.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
requestHeaders.put(HttpHeaders.AUTHORIZATION, BEARER + accessToken);
HttpResponse apiInvokeResponse = HttpRequestUtil.doGet(
getAPIInvocationURLHttps(APIContext.replace(File.separator, Strings.EMPTY), APIVersion)
+ File.separator + API_ENDPOINT_METHOD, requestHeaders);
assertEquals(apiInvokeResponse.getResponseCode(), HTTP_RESPONSE_CODE_OK, RESPONSE_CODE_MISMATCH_ERROR_MESSAGE);

//Update provider of the api
ApiResponse<Void> changeProviderResponse = restAPIAdminClient.changeApiProvider(newUser, apiID);
Assert.assertEquals(changeProviderResponse.getStatusCode(), HttpStatus.SC_OK);

apiInvokeResponse = HttpRequestUtil.doGet(
getAPIInvocationURLHttps(APIContext.replace(File.separator, Strings.EMPTY), APIVersion)
+ File.separator + API_ENDPOINT_METHOD, requestHeaders);
assertEquals(apiInvokeResponse.getResponseCode(), HTTP_RESPONSE_CODE_OK, RESPONSE_CODE_MISMATCH_ERROR_MESSAGE);
}

@AfterClass(alwaysRun = true)
public void destroy() throws Exception {
undeployAndDeleteAPIRevisionsUsingRest(apiID, restAPIPublisher);
restAPIPublisher.deleteAPI(apiID);
super.cleanUp();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@
<class name="org.wso2.am.integration.tests.other.APIMANAGER5326CustomStatusMsgTestCase"/>
<!--<class name="org.wso2.am.integration.tests.application.ApplicationCallbackURLTestCase"/-->
<class name="org.wso2.am.integration.tests.admin.OAuthApplicationOwnerUpdateTestCase"/>
<class name="org.wso2.am.integration.tests.admin.ChangeApiProviderTestCase"/>
<class name="org.wso2.am.integration.tests.jwt.JWTGrantTestCase"/>
<class name="org.wso2.am.integration.tests.api.sdk.SDKGenerationTestCase"/>
<class name="org.wso2.am.integration.tests.application.CAPIMGT12CallBackURLOverwriteTestCase"/>
Expand Down

0 comments on commit bafe7a7

Please sign in to comment.