Esempio n. 1
0
  @Test(
      groups = {"wso2.am"},
      description = "API Life cycle test case")
  public void testDAOTestCase() throws Exception {
    String APIName = "DAOTestAPI";
    String APIContext = "DAOTestAPI";
    String tags = "youtube, video, media";
    String url = "http://gdata.youtube.com/feeds/api/standardfeeds";
    String description = "This is test API create by API manager integration test";

    String APIVersion = "1.0.0";
    String apiContextAddedValue = APIContext + "/" + APIVersion;

    APIRequest apiRequest = new APIRequest(APIName, APIContext, new URL(url));
    apiRequest.setTags(tags);
    apiRequest.setDescription(description);
    apiRequest.setVersion(APIVersion);
    apiRequest.setProvider(providerName);
    apiPublisher.addAPI(apiRequest);
    apiPublisher.deleteAPI(APIName, APIVersion, providerName);
    apiPublisher.addAPI(apiRequest);
    APIBean apiBean =
        APIMTestCaseUtils.getAPIBeanFromHttpResponse(apiPublisher.getAPI(APIName, providerName));
    APILifeCycleStateRequest updateRequest =
        new APILifeCycleStateRequest(APIName, providerName, APILifeCycleState.PUBLISHED);
    apiPublisher.changeAPILifeCycleStatus(updateRequest);
    // Test API properties
    assertEquals(apiBean.getId().getApiName(), APIName, "API Name mismatch");
    assertTrue(apiBean.getContext().contains(apiContextAddedValue), "API context mismatch");
    assertEquals(apiBean.getId().getVersion(), APIVersion, "API version mismatch");
    assertEquals(apiBean.getId().getProviderName(), providerName, "Provider Name mismatch");
    for (String tag : apiBean.getTags()) {
      assertTrue(tags.contains(tag), "API tag data mismatched");
    }
    assertEquals(apiBean.getDescription(), description, "API description mismatch");

    apiStore.addApplication("DAOTestAPI-Application", "Gold", "", "this-is-test");
    SubscriptionRequest subscriptionRequest =
        new SubscriptionRequest(
            APIName, storeContext.getContextTenant().getContextUser().getUserName());
    subscriptionRequest.setApplicationName("DAOTestAPI-Application");
    apiStore.subscribe(subscriptionRequest);

    APPKeyRequestGenerator generateAppKeyRequest =
        new APPKeyRequestGenerator("DAOTestAPI-Application");
    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);
  }
  @Test(groups = "wso2.am", description = "Check functionality of the default version API")
  public void testDefaultVersionAPI() throws Exception {

    // Login to the API Publisher
    apiPublisher.login(user.getUserName(), user.getPassword());

    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.setDefault_version("default_version");
    apiRequest.setDefault_version_checked("default_version");
    apiRequest.setVersion(apiVersion);
    apiRequest.setTiersCollection("Unlimited");
    apiRequest.setTier("Unlimited");
    apiRequest.setProvider(provider);

    // Add the API using the API publisher.
    HttpResponse response = apiPublisher.addAPI(apiRequest);

    APILifeCycleStateRequest updateRequest =
        new APILifeCycleStateRequest(apiName, user.getUserName(), APILifeCycleState.PUBLISHED);
    // Publish the API
    response = apiPublisher.changeAPILifeCycleStatus(updateRequest);

    // Login to the API Store
    apiStore.login(user.getUserName(), user.getPassword());

    // Add an Application in the Store.
    response = apiStore.addApplication("DefaultVersionAPP", "Unlimited", "", "");
    verifyResponse(response);

    // Subscribe the API to the DefaultApplication
    SubscriptionRequest subscriptionRequest =
        new SubscriptionRequest(apiName, apiVersion, provider, "DefaultVersionAPP", "Unlimited");
    response = apiStore.subscribe(subscriptionRequest);

    // Generate production token and invoke with that
    APPKeyRequestGenerator generateAppKeyRequest = new APPKeyRequestGenerator("DefaultVersionAPP");
    String responseString = apiStore.generateApplicationKey(generateAppKeyRequest).getData();
    JSONObject jsonResponse = new JSONObject(responseString);

    // Get the accessToken which was generated.
    String accessToken =
        jsonResponse.getJSONObject("data").getJSONObject("key").getString("accessToken");

    String apiInvocationUrl = getAPIInvocationURLHttp(apiContext);

    // Going to access the API without the version in the request url.
    HttpResponse directResponse = HttpRequestUtil.doGet(endpointUrl, new HashMap<String, String>());

    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Authorization", "Bearer " + accessToken);
    // Invoke the API
    HttpResponse httpResponse = HttpRequestUtil.doGet(apiInvocationUrl, headers);

    // 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.");
  }