Esempio n. 1
0
  private APIResponse HTTPGet(APIRequest request) {
    APIResponse response = new APIResponse();
    HttpURLConnection urlConnection;
    URL url;
    String path = basePath;
    if (request.getCow() != null && request.getCow().getId() > 0) {
      path = path + "/" + request.getCow().getId();
    }
    try {
      url = new URL(path);

      urlConnection = (HttpURLConnection) url.openConnection();
      urlConnection.setRequestMethod(request.getRequestMethod().toString());
      urlConnection.connect();

      response.setResponseCode(urlConnection.getResponseCode());
      response.setResponseMessage(urlConnection.getResponseMessage());

      InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
      response.setResultJSON(readStream(inputStream));
    } catch (IOException e) {
      e.printStackTrace();
    }
    return response;
  }
Esempio n. 2
0
  private APIResponse HTTPPost(APIRequest request) {
    APIResponse response = new APIResponse();
    HttpURLConnection urlConnection;
    URL url;
    String content = request.getCow().toJSON();
    try {
      url = new URL(basePath);

      urlConnection = (HttpURLConnection) url.openConnection();
      urlConnection.setDoInput(true);
      urlConnection.setDoOutput(true);
      urlConnection.setRequestProperty("Content-Type", "application/json");
      urlConnection.setRequestMethod(request.getRequestMethod().toString());
      OutputStreamWriter outputStream = new OutputStreamWriter(urlConnection.getOutputStream());
      outputStream.write(content);
      outputStream.flush();
      response.setResponseCode(urlConnection.getResponseCode());
      response.setResponseMessage(urlConnection.getResponseMessage());
      outputStream.close();
      InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
      response.setResultJSON(readStream(inputStream));
    } catch (IOException e) {
      e.printStackTrace();
    }
    return response;
  }
Esempio n. 3
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);
  }
Esempio n. 4
0
  @Override
  protected APIResponse doInBackground(APIRequest... requests) {
    APIResponse response = new APIResponse();
    APIRequest request = requests[0];

    switch (request.getRequestMethod()) {
      case GET:
        response = HTTPGet(request);
        break;
      case POST:
        response = HTTPPost(request);
        break;
      case DELETE:
        response = HTTPDelete(request);
        break;
    }
    return response;
  }
Esempio n. 5
0
  private APIResponse HTTPDelete(APIRequest request) {
    APIResponse response = new APIResponse();
    HttpURLConnection urlConnection;
    URL url;
    String path = basePath;
    try {
      path = path + "/" + request.getCow().getId();
      url = new URL(path);

      urlConnection = (HttpURLConnection) url.openConnection();
      urlConnection.setRequestMethod(request.getRequestMethod().toString());
      urlConnection.connect();

      response.setResponseCode(urlConnection.getResponseCode());
      response.setResponseMessage(urlConnection.getResponseMessage());
    } catch (IOException e) {
      e.printStackTrace();
    }
    return response;
  }