Monday, March 17, 2014

ApiMgr 2.0.0 
Api creation , publish test scenario in Test Automation Framework 4.3.0

package org.wso2.carbon.am.tests.rest;

import org.json.JSONObject;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.am.integration.services.jaxrs.customersample.CustomerConfig;
import org.wso2.carbon.am.tests.util.APIPublisherRestClient;
import org.wso2.carbon.am.tests.util.APIStoreRestClient;
import org.wso2.carbon.am.tests.util.bean.*;
import org.wso2.carbon.automation.engine.context.AutomationContext;
import org.wso2.carbon.automation.engine.context.TestUserMode;
import org.wso2.carbon.automation.extensions.servers.tomcatserver.TomcatServerManager;
import org.wso2.carbon.automation.extensions.servers.tomcatserver.TomcatServerType;
import org.wso2.carbon.automation.test.utils.http.client.HttpRequestUtil;

import javax.xml.xpath.XPathExpressionException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import static org.testng.AssertJUnit.assertTrue;
import static org.wso2.carbon.automation.engine.configurations.AutomationConfiguration.getConfigurationValue;

public class RestPeopleTestCase {

    private APIPublisherRestClient apiPublisher;
    private APIStoreRestClient apiStore;
    private AutomationContext automationContext;
    private String userName;
    private String password;
    private int tomcatServerPort = 8080;
    private String url;

    @BeforeClass(alwaysRun = true)
    protected void init() throws Exception {
        getContext();
        url = automationContext.getContextUrls().getServiceUrl().split("/services")[0];
        userName = automationContext.getConfigurationNode("//userManagement/" +
                "superTenant/tenant/admin/user").getChildNodes().item(0).
                getFirstChild().getNodeValue();
        password = automationContext.getConfigurationNode
                ("//userManagement/" + "superTenant/tenant/admin/user").getChildNodes()
                .item(1).getChildNodes().item(0).getNodeValue();
        TomcatServerManager tomcatServerManager = new TomcatServerManager(
                CustomerConfig.class.getName(), TomcatServerType.jaxrs.name(), tomcatServerPort);
        tomcatServerManager.startServer();
        apiStore = new APIStoreRestClient(url);
        apiPublisher = new APIPublisherRestClient(url);
    }

    @Test(groups = {"wso2.am"}, description = "Add RestBackendTest API and sending request to " +
            "rest backend service")
    public void addAPITestCaseAndSendRequest() throws Exception {

        // adding api
        String APIContext = "restBackendTestAPI";
        String tags = "rest, tomcat, jaxrs";
        String restBackendUrl = automationContext.getContextUrls().getWebAppURL() + ":" + tomcatServerPort + "/rest/api/customerservice";
        String description = "This RestBackendTest API was created by API manager integration test";
        String APIVersion = "1.0.0";
        String providerName = "admin";

        apiPublisher.login(userName, password);
        String APIName = "RestBackendTestAPI";
        APIRequest apiRequest = new APIRequest(APIName, APIContext, new URL(restBackendUrl));
        apiRequest.setTags(tags);
        apiRequest.setDescription(description);
        apiRequest.setVersion(APIVersion);
        apiRequest.setSandbox(restBackendUrl);
        apiPublisher.addAPI(apiRequest);

        // publishing
        APILifeCycleStateRequest updateRequest = new APILifeCycleStateRequest(APIName,
                providerName, APILifeCycleState.PUBLISHED);
        apiPublisher.changeAPILifeCycleStatusTo(updateRequest);

        // subscribing
        apiStore.login(userName, password);
        SubscriptionRequest subscriptionRequest = new SubscriptionRequest(APIName, providerName);
        apiStore.subscribe(subscriptionRequest);
        GenerateAppKeyRequest generateAppKeyRequest = new GenerateAppKeyRequest("DefaultApplication");
        String responseString = apiStore.generateApplicationKey(generateAppKeyRequest).getData();
        JSONObject response = new JSONObject(responseString);
        String accessToken = response.getJSONObject("data").getJSONObject("key").get("accessToken").toString();
        Map<String, String> requestHeaders = new HashMap<String, String>();
        requestHeaders.put("Authorization", "Bearer " + accessToken);

        Thread.sleep(2000);

        // invoke backend-service through api mgr
        assertTrue(HttpRequestUtil.doGet("http://10.100.0.42:8280/restBackendTestAPI/1.0.0/customerservice/customers/123/"
                , requestHeaders).toString().contains("John"));
    }

    private void getContext() throws XPathExpressionException {
        automationContext = new AutomationContext("AM", TestUserMode.SUPER_TENANT_USER);
    }

    private void setupDistributedSetup() throws XPathExpressionException {
        getConfigurationValue("//productGroup/");
    }
}

No comments:

Post a Comment