Example usage for org.json JSONArray length

List of usage examples for org.json JSONArray length

Introduction

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

Prototype

public int length() 

Source Link

Document

Get the number of elements in the JSONArray, included nulls.

Usage

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

/**
 * Test listCourses method with Mandatory Parameters.
 *///from ww w .  j  av a  2  s . c om
@Test(groups = { "wso2.esb" }, dependsOnMethods = { "testCreateCourseWithMandatoryParameters",
        "testCreateCourseWithOptionalParameters" }, description = "canvas {listCourses} integration test with mandatory parameters.")
public void testListCoursesWithMandatoryParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:listCourses");
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listCourses_mandatory.json");
    JSONArray esbResponseArray = new JSONArray(esbRestResponse.getBody().getString("output"));

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/api/v1/courses";
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    JSONArray apiResponseArray = new JSONArray(apiRestResponse.getBody().getString("output"));

    Assert.assertEquals(esbResponseArray.length(), apiResponseArray.length());
    Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("id"),
            apiResponseArray.getJSONObject(0).getString("id"));
    Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("name"),
            apiResponseArray.getJSONObject(0).getString("name"));
    Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("course_code"),
            apiResponseArray.getJSONObject(0).getString("course_code"));
    Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("start_at"),
            apiResponseArray.getJSONObject(0).getString("start_at"));
    Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("end_at"),
            apiResponseArray.getJSONObject(0).getString("end_at"));

}

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

/**
 * Test listCourses method with Optional Parameters.
 */// w w w. j av a 2s .c o m
@Test(groups = { "wso2.esb" }, dependsOnMethods = { "testCreateCourseWithMandatoryParameters",
        "testCreateCourseWithOptionalParameters" }, description = "canvas {listCourses} integration test with optional parameters.")
public void testListCoursesWithOptionalParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:listCourses");
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listCourses_optional.json");
    JSONArray esbResponseArray = new JSONArray(esbRestResponse.getBody().getString("output"));

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/api/v1/courses"
            + "?include[]=term&include[]=sections";
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    JSONArray apiResponseArray = new JSONArray(apiRestResponse.getBody().getString("output"));

    Assert.assertEquals(esbResponseArray.length(), apiResponseArray.length());
    // Term details
    Assert.assertEquals(esbResponseArray.getJSONObject(0).getJSONObject("term").getString("id"),
            apiResponseArray.getJSONObject(0).getJSONObject("term").getString("id"));
    Assert.assertEquals(esbResponseArray.getJSONObject(0).getJSONObject("term").getString("name"),
            apiResponseArray.getJSONObject(0).getJSONObject("term").getString("name"));

    // Section details
    Assert.assertEquals(
            esbResponseArray.getJSONObject(0).getJSONArray("sections").getJSONObject(0).getString("id"),
            apiResponseArray.getJSONObject(0).getJSONArray("sections").getJSONObject(0).getString("id"));
    Assert.assertEquals(
            esbResponseArray.getJSONObject(0).getJSONArray("sections").getJSONObject(0).getString("name"),
            apiResponseArray.getJSONObject(0).getJSONArray("sections").getJSONObject(0).getString("name"));

}

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

/**
 * Test listCourseUsers method with Mandatory Parameters.
 *///ww w  . j a va 2s .  co  m
@Test(groups = { "wso2.esb" }, dependsOnMethods = {
        "testCreateCourseWithMandatoryParameters" }, description = "canvas {listCourseUsers} integration test with mandatory parameters.")
public void testListCourseUsersWithMandatoryParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:listCourseUsers");
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listCourseUsers_mandatory.json");
    JSONArray esbResponseArray = new JSONArray(esbRestResponse.getBody().getString("output"));
    connectorProperties.put("userId", esbResponseArray.getJSONObject(0).getString("id"));

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/api/v1/courses/"
            + connectorProperties.getProperty("courseId") + "/users";
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    JSONArray apiResponseArray = new JSONArray(apiRestResponse.getBody().getString("output"));

    Assert.assertEquals(esbResponseArray.length(), apiResponseArray.length());
    Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("id"),
            apiResponseArray.getJSONObject(0).getString("id"));
    Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("name"),
            apiResponseArray.getJSONObject(0).getString("name"));
    Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("sortable_name"),
            apiResponseArray.getJSONObject(0).getString("sortable_name"));
    Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("login_id"),
            apiResponseArray.getJSONObject(0).getString("login_id"));

}

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

/**
 * Test listCourseUsers method with Optional Parameters.
 *//*from  www.  j a v  a 2 s.  c  om*/
@Test(groups = { "wso2.esb" }, dependsOnMethods = {
        "testCreateCourseWithOptionalParameters" }, description = "canvas {listCourseUsers} integration test with optional parameters.")
public void testListCourseUsersWithOptionalParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:listCourseUsers");
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listCourseUsers_optional.json");
    JSONArray esbResponseArray = new JSONArray(esbRestResponse.getBody().getString("output"));

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/api/v1/courses/"
            + connectorProperties.getProperty("courseIdOptional") + "/users"
            + "?include[]=email&include[]=enrollments";
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    JSONArray apiResponseArray = new JSONArray(apiRestResponse.getBody().getString("output"));

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

    // ID and Email details
    Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("id"),
            apiResponseArray.getJSONObject(0).getString("id"));
    Assert.assertEquals(esbResponseArray.getJSONObject(0).getString("email"),
            apiResponseArray.getJSONObject(0).getString("email"));

    // Enrollment details
    Assert.assertEquals(
            esbResponseArray.getJSONObject(0).getJSONArray("enrollments").getJSONObject(0).getString("role"),
            apiResponseArray.getJSONObject(0).getJSONArray("enrollments").getJSONObject(0).getString("role"));
    Assert.assertEquals(
            esbResponseArray.getJSONObject(0).getJSONArray("enrollments").getJSONObject(0)
                    .getString("course_id"),
            apiResponseArray.getJSONObject(0).getJSONArray("enrollments").getJSONObject(0)
                    .getString("course_id"));

}

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

/**
 * Test listCalendarEvents method with Mandatory Parameters.
 *//*from   www  .  j  a v  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.
 *///from  w ww .j  a va2s.  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 a va  2  s.  c o 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.
 *//* w w w .jav a2 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.
 *///  ww w .  ja  v  a2s  .  c o 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:com.mifos.mifosxdroid.dialogfragments.loanchargedialog.LoanChargeDialogFragment.java

@Override
public void showAllChargesV3(ResponseBody result) {

    /* Activity is null - Fragment has been detached; no need to do anything. */
    if (getActivity() == null)
        return;/*from ww  w .  j  av a 2  s .  c  o m*/

    Log.d(LOG_TAG, "");

    final List<Charges> charges = new ArrayList<>();
    // you can use this array to populate your spinner
    final ArrayList<String> chargesNames = new ArrayList<String>();
    //Try to get response body
    BufferedReader reader = null;
    StringBuilder sb = new StringBuilder();
    try {
        reader = new BufferedReader(new InputStreamReader(result.byteStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        JSONObject obj = new JSONObject(sb.toString());
        if (obj.has("chargeOptions")) {
            JSONArray chargesTypes = obj.getJSONArray("chargeOptions");
            for (int i = 0; i < chargesTypes.length(); i++) {
                JSONObject chargesObject = chargesTypes.getJSONObject(i);
                Charges charge = new Charges();
                charge.setId(chargesObject.optInt("id"));
                charge.setName(chargesObject.optString("name"));
                charges.add(charge);
                chargesNames.add(chargesObject.optString("name"));
                chargeNameIdHashMap.put(charge.getName(), charge.getId());
            }

        }
        String stringResult = sb.toString();
    } catch (Exception e) {
        Log.e(LOG_TAG, "", e);
    }
    final ArrayAdapter<String> chargesAdapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_item, chargesNames);
    chargesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sp_charge_name.setAdapter(chargesAdapter);
    sp_charge_name.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            Id = chargeNameIdHashMap.get(chargesNames.get(i));
            Log.d("chargesoptionss" + chargesNames.get(i), String.valueOf(Id));
            if (Id != -1) {

            } else {

                Toast.makeText(getActivity(), getString(R.string.error_select_charge), Toast.LENGTH_SHORT)
                        .show();

            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}