Example usage for org.json JSONArray JSONArray

List of usage examples for org.json JSONArray JSONArray

Introduction

In this page you can find the example usage for org.json JSONArray JSONArray.

Prototype

public JSONArray(Object array) throws JSONException 

Source Link

Document

Construct a JSONArray from an array

Usage

From source file:org.wso2.carbon.connector.integration.test.canvas.CanvasConnectorIntegrationTest.java

/**
 * Test listCalendarEvents method with Mandatory Parameters.
 *//*from  w  ww.  j  av a  2 s.c  o  m*/
@Test(priority = 1, dependsOnMethods = {
        "testCreateCalendarEventWithMandatoryParameters" }, description = "Canvas {listCalendarEvents} integration test with mandatory parameters.")
public void testListCalendarEventsWithMandatoryParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:listCalendarEvents");
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listCalendarEvents_mandatory.json");

    final String apiUrl = connectorProperties.getProperty("apiUrl") + "/api/v1/calendar_events?start_date="
            + connectorProperties.getProperty("calenderEventStartDate") + "T00:00:00Z";

    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiUrl, "GET", apiRequestHeadersMap);

    JSONArray esbResponseArray = new JSONArray(esbRestResponse.getBody().getString("output"));
    JSONArray apiResponseArray = new JSONArray(apiRestResponse.getBody().getString("output"));

    Assert.assertEquals(apiResponseArray.length(), esbResponseArray.length());

    if (apiResponseArray.length() > 0) {
        JSONObject esbFirstResult = esbResponseArray.getJSONObject(0);
        JSONObject apiFirstResult = apiResponseArray.getJSONObject(0);

        Assert.assertEquals(apiFirstResult.getString("id"), esbFirstResult.getString("id"));
        Assert.assertEquals(apiFirstResult.getString("title"), esbFirstResult.getString("title"));
        Assert.assertEquals(apiFirstResult.getString("context_code"), esbFirstResult.getString("context_code"));
    }

}

From source file:org.wso2.carbon.connector.integration.test.canvas.CanvasConnectorIntegrationTest.java

/**
 * Test listCalendarEvents method with Optional Parameters.
 *///w  ww.j  av a2  s .  c  om
@Test(priority = 1, dependsOnMethods = {
        "testCreateCalendarEventWithMandatoryParameters" }, description = "Canvas {listCalendarEvents} integration test with optional parameters.")
public void testListCalendarEventsWithOptionalParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:listCalendarEvents");
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listCalendarEvents_optional.json");

    final String apiUrl = connectorProperties.getProperty("apiUrl")
            + "/api/v1/calendar_events?all_events=true&per_page=4&start_date="
            + connectorProperties.getProperty("calenderEventStartDate") + "T00:00:00Z";

    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiUrl, "GET", apiRequestHeadersMap);

    JSONArray esbResponseArray = new JSONArray(esbRestResponse.getBody().getString("output"));
    JSONArray apiResponseArray = new JSONArray(apiRestResponse.getBody().getString("output"));

    Assert.assertEquals(apiResponseArray.length(), esbResponseArray.length());

    if (apiResponseArray.length() > 0) {
        JSONObject esbFirstResult = esbResponseArray.getJSONObject(0);
        JSONObject apiFirstResult = apiResponseArray.getJSONObject(0);

        Assert.assertEquals(apiFirstResult.getString("id"), esbFirstResult.getString("id"));
        Assert.assertEquals(apiFirstResult.getString("title"), esbFirstResult.getString("title"));
        Assert.assertEquals(apiFirstResult.getString("context_code"), esbFirstResult.getString("context_code"));
    }

}

From source file:org.wso2.carbon.connector.integration.test.canvas.CanvasConnectorIntegrationTest.java

/**
 * Test search method with Optional Parameters.
 *///w w w  . j av a  2  s  .  co  m
@Test(priority = 1, dependsOnMethods = {
        "testCreateCalendarEventWithMandatoryParameters" }, description = "Canvas {search} integration test with optional parameters.")
public void testSearchWithOptionalParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:search");
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_search_optional.json");

    final String apiUrl = connectorProperties.getProperty("apiUrl") + "/api/v1/search/recipients?context="
            + connectorProperties.getProperty("contextCode");

    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiUrl, "GET", apiRequestHeadersMap);

    JSONArray esbResponseArray = new JSONArray(esbRestResponse.getBody().getString("output"));
    JSONArray apiResponseArray = new JSONArray(apiRestResponse.getBody().getString("output"));

    Assert.assertEquals(apiResponseArray.length(), esbResponseArray.length());

    if (apiResponseArray.length() > 0) {
        JSONObject esbFirstResult = esbResponseArray.getJSONObject(0);
        JSONObject apiFirstResult = apiResponseArray.getJSONObject(0);

        Assert.assertEquals(apiFirstResult.getString("id"), esbFirstResult.getString("id"));
        Assert.assertEquals(apiFirstResult.getString("name"), esbFirstResult.getString("name"));
        Assert.assertEquals(apiFirstResult.getString("avatar_url"), esbFirstResult.getString("avatar_url"));
    }

}

From source file:org.wso2.carbon.connector.integration.test.canvas.CanvasConnectorIntegrationTest.java

/**
 * Positive test case for listDiscussionTopics method with mandatory parameters.
 *//*from  w w w  .ja va 2 s.c o m*/
@Test(groups = { "wso2.esb" }, dependsOnMethods = {
        "testCreateCourseWithMandatoryParameters" }, description = "Canvas {listDiscussionTopics} integration test with mandatory parameters.")
public void testListDiscussionTopicsWithMandatoryParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:listDiscussionTopics");
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listDiscussionTopics_mandatory.json");

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/api/v1/courses/"
            + connectorProperties.getProperty("courseId") + "/discussion_topics";

    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);

    JSONArray esbResponseArray = new JSONArray(esbRestResponse.getBody().getString("output"));
    JSONArray apiResponseArray = new JSONArray(apiRestResponse.getBody().getString("output"));

    Assert.assertEquals(apiResponseArray.length(), esbResponseArray.length());

    if (apiResponseArray.length() > 0) {
        JSONObject esbFirstResult = esbResponseArray.getJSONObject(0);
        JSONObject apiFirstResult = apiResponseArray.getJSONObject(0);

        Assert.assertEquals(apiFirstResult.getString("id"), esbFirstResult.getString("id"));
        Assert.assertEquals(apiFirstResult.getString("title"), esbFirstResult.getString("title"));
        Assert.assertEquals(apiFirstResult.getString("message"), esbFirstResult.getString("message"));
    }

}

From source file:org.wso2.carbon.connector.integration.test.canvas.CanvasConnectorIntegrationTest.java

/**
 * Positive test case for listDiscussionTopics method with optional parameters.
 */// w ww .j a  v a 2s  . co  m
@Test(groups = { "wso2.esb" }, dependsOnMethods = {
        "testCreateDiscussionTopicWithOptionalParameters" }, description = "Canvas {listDiscussionTopics} integration test with optional parameters.")
public void testListDiscussionTopicsWithOptionalParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:listDiscussionTopics");
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listDiscussionTopics_optional.json");

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/api/v1/courses/"
            + connectorProperties.getProperty("courseId")
            + "/discussion_topics?order_by=position&scope=unlocked";

    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);

    JSONArray esbResponseArray = new JSONArray(esbRestResponse.getBody().getString("output"));
    JSONArray apiResponseArray = new JSONArray(apiRestResponse.getBody().getString("output"));

    Assert.assertEquals(apiResponseArray.length(), esbResponseArray.length());

    if (apiResponseArray.length() > 0) {
        JSONObject esbFirstResult = esbResponseArray.getJSONObject(0);
        JSONObject apiFirstResult = apiResponseArray.getJSONObject(0);

        Assert.assertEquals(apiFirstResult.getString("id"), esbFirstResult.getString("id"));
        Assert.assertEquals(apiFirstResult.getString("title"), esbFirstResult.getString("title"));
        Assert.assertEquals(apiFirstResult.getString("message"), esbFirstResult.getString("message"));
    }

}

From source file:org.wso2.carbon.connector.integration.test.canvas.CanvasConnectorIntegrationTest.java

/**
 * Test createEntry method with Mandatory Parameters.
 *///from www  .  j  a v  a  2 s.c o m
@Test(groups = { "wso2.esb" }, dependsOnMethods = {
        "testCreateDiscussionTopicWithOptionalParameters" }, description = "Canvas {createEntry} integration test with Mandatory parameters.")
public void testCreateEntryWithMandatoryParameters() throws IOException, JSONException {

    headersMap.put("Action", "urn:createEntry");

    final String requestString = proxyUrl + "?courseId=" + connectorProperties.getProperty("courseId")
            + "&topicId=" + connectorProperties.getProperty("topicId") + "&apiUrl="
            + connectorProperties.getProperty("apiUrl") + "&accessToken="
            + connectorProperties.getProperty("accessToken");

    MultipartFormdataProcessor multipartProcessor = new MultipartFormdataProcessor(requestString, headersMap);
    File file = new File(pathToResourcesDirectory + connectorProperties.getProperty("attachmentFileName"));
    multipartProcessor.addFileToRequest("attachment", file,
            URLConnection.guessContentTypeFromName(file.getName()));
    multipartProcessor.addFormDataToRequest("message", connectorProperties.getProperty("entryMessage"));

    RestResponse<JSONObject> esbRestResponse = multipartProcessor.processForJsonResponse();
    String entryId = esbRestResponse.getBody().getString("id");
    String userId = esbRestResponse.getBody().getString("user_id");

    connectorProperties.put("entryId", entryId);

    final String apiUrl = connectorProperties.getProperty("apiUrl") + "/api/v1/courses/"
            + connectorProperties.getProperty("courseId") + "/discussion_topics/"
            + connectorProperties.getProperty("topicId") + "/entry_list?ids[]=" + entryId;

    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiUrl, "GET", apiRequestHeadersMap);
    JSONArray apiResponseArray = new JSONArray(apiRestResponse.getBody().getString("output"));
    JSONObject apiFirstElement = apiResponseArray.getJSONObject(0);

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    Assert.assertEquals(apiFirstElement.getString("created_at").split("T")[0], sdf.format(new Date()));
    Assert.assertEquals(apiFirstElement.getString("user_id"), userId);
}

From source file:org.wso2.carbon.connector.integration.test.canvas.CanvasConnectorIntegrationTest.java

/**
 * Test createEntry method with Optional Parameters.
 *///from   w  w w.  j  a v  a  2  s  . com
@Test(groups = { "wso2.esb" }, dependsOnMethods = {
        "testCreateDiscussionTopicWithOptionalParameters" }, description = "Canvas {createEntry} integration test with optional parameters.")
public void testCreateEntryWithOptionalParameters() throws IOException, JSONException {

    headersMap.put("Action", "urn:createEntry");

    final String requestString = proxyUrl + "?courseId=" + connectorProperties.getProperty("courseId")
            + "&topicId=" + connectorProperties.getProperty("topicId") + "&apiUrl="
            + connectorProperties.getProperty("apiUrl") + "&accessToken="
            + connectorProperties.getProperty("accessToken");

    MultipartFormdataProcessor multipartProcessor = new MultipartFormdataProcessor(requestString, headersMap);
    File file = new File(pathToResourcesDirectory + connectorProperties.getProperty("attachmentFileName"));
    multipartProcessor.addFileToRequest("attachment", file,
            URLConnection.guessContentTypeFromName(file.getName()));
    multipartProcessor.addFormDataToRequest("message", connectorProperties.getProperty("entryMessage"));

    RestResponse<JSONObject> esbRestResponse = multipartProcessor.processForJsonResponse();

    String entryId = esbRestResponse.getBody().getString("id");

    final String apiUrl = connectorProperties.getProperty("apiUrl") + "/api/v1/courses/"
            + connectorProperties.getProperty("courseId") + "/discussion_topics/"
            + connectorProperties.getProperty("topicId") + "/entry_list?ids[]=" + entryId;

    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiUrl, "GET", apiRequestHeadersMap);
    JSONArray apiResponseArray = new JSONArray(apiRestResponse.getBody().getString("output"));
    JSONObject apiFirstElement = apiResponseArray.getJSONObject(0);

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    Assert.assertEquals(connectorProperties.getProperty("entryMessage"), apiFirstElement.getString("message"));
    Assert.assertEquals(connectorProperties.getProperty("attachmentFileName"),
            apiFirstElement.getJSONObject("attachment").getString("filename"));
    Assert.assertEquals(apiFirstElement.getString("created_at").split("T")[0], sdf.format(new Date()));
}

From source file:org.wso2.carbon.connector.integration.test.canvas.CanvasConnectorIntegrationTest.java

/**
 * Test deleteEntry method with Mandatory Parameters.
 */// www  .  jav  a 2s .c  o  m
@Test(groups = { "wso2.esb" }, dependsOnMethods = { "testListEntriesWithNegativeCase",
        "testUpdateEntryWithOptionalParameters" }, description = "canvas {deleteEntry} integration test with mandatory parameters.", priority = 2)
public void testDeleteEntryWithMandatoryParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:deleteEntry");

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/api/v1/courses/"
            + connectorProperties.getProperty("courseId") + "/discussion_topics/"
            + connectorProperties.getProperty("topicId") + "/entry_list?ids[]="
            + connectorProperties.getProperty("entryId");

    RestResponse<JSONObject> apiRestResponseBefore = sendJsonRestRequest(apiEndPoint, "GET",
            apiRequestHeadersMap);
    JSONArray apiResponseArrayBefore = new JSONArray(apiRestResponseBefore.getBody().getString("output"));

    sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap, "esb_deleteEntry_mandatory.json");

    RestResponse<JSONObject> apiRestResponseAfter = sendJsonRestRequest(apiEndPoint, "GET",
            apiRequestHeadersMap);
    JSONArray apiResponseArrayAfter = new JSONArray(apiRestResponseAfter.getBody().getString("output"));

    // Deleted key is not there in the response before the entry was deleted.
    Assert.assertEquals(apiResponseArrayBefore.getJSONObject(0).has("deleted"), false);
    // Deleted key is there with value 'true' in the response after the entry was deleted.
    Assert.assertEquals(apiResponseArrayAfter.getJSONObject(0).getBoolean("deleted"), true);

}

From source file:at.diamonddogs.service.processor.JSONArrayProcessor.java

@Override
protected JSONArray createParsedObjectFromByteArray(byte[] data) {

    try {//  w w w. j a v a 2 s. c o  m
        String jsonString = new String(data, 0, data.length);

        JSONArray jsonData = new JSONArray(jsonString);

        return jsonData;
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}

From source file:nl.hnogames.domoticzapi.Parsers.StatusInfoParser.java

@Override
public void parseResult(String result) {
    // Change the result data here so the view class gets the ready data

    try {//from  w  ww. ja v a 2  s.  c o  m

        JSONArray jsonArray = new JSONArray(result);
        JSONObject jsonObject = jsonArray.getJSONObject(0);

        statusReceiver.onReceiveStatus(new ExtendedStatusInfo(jsonObject));

    } catch (JSONException error) {
        Log.d(TAG, "StatusInfoParser onError");
        statusReceiver.onError(error);
    }
}