From bafe7a7ca0b0af6d595c3a5284d7fa6af7b0e657 Mon Sep 17 00:00:00 2001 From: shnrndk Date: Mon, 4 Dec 2023 14:24:32 +0530 Subject: [PATCH] Change Api Provider Test Case Added --- .../admin/ChangeApiProviderTestCase.java | 155 ++++++++++++++++++ .../src/test/resources/testng.xml | 1 + 2 files changed, 156 insertions(+) create mode 100644 modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/admin/ChangeApiProviderTestCase.java diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/admin/ChangeApiProviderTestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/admin/ChangeApiProviderTestCase.java new file mode 100644 index 0000000000..84f7c6f8da --- /dev/null +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/admin/ChangeApiProviderTestCase.java @@ -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 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 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 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(); + } +} diff --git a/modules/integration/tests-integration/tests-backend/src/test/resources/testng.xml b/modules/integration/tests-integration/tests-backend/src/test/resources/testng.xml index b2d080143b..a76cc6a335 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/resources/testng.xml +++ b/modules/integration/tests-integration/tests-backend/src/test/resources/testng.xml @@ -256,6 +256,7 @@ +