Search in sources :

Example 1 with JSONArray

use of org.codehaus.jettison.json.JSONArray in project hadoop by apache.

the class TestRMWebServicesNodes method testNodesQueryStateLost.

@Test
public void testNodesQueryStateLost() throws JSONException, Exception {
    WebResource r = resource();
    RMNode rmnode1 = getRunningRMNode("h1", 1234, 5120);
    sendLostEvent(rmnode1);
    RMNode rmnode2 = getRunningRMNode("h2", 1235, 5121);
    sendLostEvent(rmnode2);
    ClientResponse response = r.path("ws").path("v1").path("cluster").path("nodes").queryParam("states", NodeState.LOST.toString()).accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
    assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, response.getType().toString());
    JSONObject json = response.getEntity(JSONObject.class);
    JSONObject nodes = json.getJSONObject("nodes");
    assertEquals("incorrect number of elements", 1, nodes.length());
    JSONArray nodeArray = nodes.getJSONArray("node");
    assertEquals("incorrect number of elements", 2, nodeArray.length());
    for (int i = 0; i < nodeArray.length(); ++i) {
        JSONObject info = nodeArray.getJSONObject(i);
        String[] node = info.get("id").toString().split(":");
        NodeId nodeId = NodeId.newInstance(node[0], Integer.parseInt(node[1]));
        RMNode rmNode = rm.getRMContext().getInactiveRMNodes().get(nodeId);
        WebServicesTestUtils.checkStringMatch("nodeHTTPAddress", "", info.getString("nodeHTTPAddress"));
        if (rmNode != null) {
            WebServicesTestUtils.checkStringMatch("state", rmNode.getState().toString(), info.getString("state"));
        }
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) RMNode(org.apache.hadoop.yarn.server.resourcemanager.rmnode.RMNode) JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) NodeId(org.apache.hadoop.yarn.api.records.NodeId) WebResource(com.sun.jersey.api.client.WebResource) Test(org.junit.Test)

Example 2 with JSONArray

use of org.codehaus.jettison.json.JSONArray in project hadoop by apache.

the class TestRMWebServicesNodes method testNodesHelper.

public void testNodesHelper(String path, String media) throws JSONException, Exception {
    WebResource r = resource();
    RMNode rmnode1 = getRunningRMNode("h1", 1234, 5120);
    RMNode rmnode2 = getRunningRMNode("h2", 1235, 5121);
    ClientResponse response = r.path("ws").path("v1").path("cluster").path(path).accept(media).get(ClientResponse.class);
    assertEquals(MediaType.APPLICATION_JSON_TYPE + "; " + JettyUtils.UTF_8, response.getType().toString());
    JSONObject json = response.getEntity(JSONObject.class);
    assertEquals("incorrect number of elements", 1, json.length());
    JSONObject nodes = json.getJSONObject("nodes");
    assertEquals("incorrect number of elements", 1, nodes.length());
    JSONArray nodeArray = nodes.getJSONArray("node");
    assertEquals("incorrect number of elements", 2, nodeArray.length());
    JSONObject info = nodeArray.getJSONObject(0);
    String id = info.get("id").toString();
    if (id.matches("h1:1234")) {
        verifyNodeInfo(info, rmnode1);
        verifyNodeInfo(nodeArray.getJSONObject(1), rmnode2);
    } else {
        verifyNodeInfo(info, rmnode2);
        verifyNodeInfo(nodeArray.getJSONObject(1), rmnode1);
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) RMNode(org.apache.hadoop.yarn.server.resourcemanager.rmnode.RMNode) JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) WebResource(com.sun.jersey.api.client.WebResource)

Example 3 with JSONArray

use of org.codehaus.jettison.json.JSONArray in project hadoop by apache.

the class TestRMWebServicesReservation method testInvalidTimeIntervalRequestListReservation.

@Test
public void testInvalidTimeIntervalRequestListReservation() throws Exception {
    rm.start();
    setupCluster(100);
    long time = clock.getTime() + MINIMUM_RESOURCE_DURATION;
    ReservationId id1 = getReservationIdTestHelper(1);
    ReservationId id2 = getReservationIdTestHelper(2);
    reservationSubmissionTestHelper("reservation/submit", MediaType.APPLICATION_JSON, time, "res_1", id1);
    reservationSubmissionTestHelper("reservation/submit", MediaType.APPLICATION_JSON, time + MINIMUM_RESOURCE_DURATION, "res_2", id2);
    WebResource resource;
    resource = constructWebResource(LIST_RESERVATION_PATH).queryParam("start-time", "-100").queryParam("end-time", "-100").queryParam("include-resource-allocations", "true").queryParam("queue", DEFAULT_QUEUE);
    JSONObject json = testListReservationHelper(resource);
    if (!this.isAuthenticationEnabled() && json == null) {
        return;
    }
    JSONArray reservations = json.getJSONArray("reservations");
    assertEquals(2, reservations.length());
    testRDLHelper(reservations.getJSONObject(0));
    testRDLHelper(reservations.getJSONObject(1));
    rm.stop();
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) ReservationId(org.apache.hadoop.yarn.api.records.ReservationId) JSONArray(org.codehaus.jettison.json.JSONArray) WebResource(com.sun.jersey.api.client.WebResource) Test(org.junit.Test)

Example 4 with JSONArray

use of org.codehaus.jettison.json.JSONArray in project jersey by jersey.

the class MainTest method testGetColoursAsJson.

/**
     * Test checks that a GET on the resource "/form/colours" with mime-type "application/json"
     * shows the appropriate colours based on the query param "match".
     */
@Test
public void testGetColoursAsJson() {
    Response response = target().path("form").path("colours").request(MediaType.APPLICATION_JSON).get();
    assertEquals("GET on path '/form/colours' with mime type 'application/json' doesn't give expected response", Response.Status.OK.getStatusCode(), response.getStatusInfo().getStatusCode());
    JSONArray jsonArray = target().path("form").path("colours").request(MediaType.APPLICATION_JSON).get(JSONArray.class);
    assertEquals("Returned JSONArray doesn't have expected number of entries", 7, jsonArray.length());
    // with the query param "match" value "re"
    jsonArray = target("form/colours").queryParam("match", "re").request(MediaType.APPLICATION_JSON).get(JSONArray.class);
    assertEquals("Returned JSONArray doesn't have expected number of entries with the query param 'match=re'", 2, jsonArray.length());
}
Also used : Response(javax.ws.rs.core.Response) JSONArray(org.codehaus.jettison.json.JSONArray) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 5 with JSONArray

use of org.codehaus.jettison.json.JSONArray in project jersey by jersey.

the class UsersResource method getUsersAsJsonArray.

@GET
@Produces("application/json")
public JSONArray getUsersAsJsonArray() {
    JSONArray uriArray = new JSONArray();
    for (UserEntity userEntity : getUsers()) {
        UriBuilder ub = uriInfo.getAbsolutePathBuilder();
        URI userUri = ub.path(userEntity.getUserid()).build();
        uriArray.put(userUri.toASCIIString());
    }
    return uriArray;
}
Also used : JSONArray(org.codehaus.jettison.json.JSONArray) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) UserEntity(org.glassfish.jersey.examples.bookmark_em.entity.UserEntity) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

JSONArray (org.codehaus.jettison.json.JSONArray)489 JSONObject (org.codehaus.jettison.json.JSONObject)397 Test (org.junit.Test)140 JSONException (org.codehaus.jettison.json.JSONException)98 ClientResponse (com.sun.jersey.api.client.ClientResponse)81 WebResource (com.sun.jersey.api.client.WebResource)69 ArrayList (java.util.ArrayList)61 HashMap (java.util.HashMap)40 IOException (java.io.IOException)31 Test (org.testng.annotations.Test)30 Map (java.util.Map)29 Produces (javax.ws.rs.Produces)26 GET (javax.ws.rs.GET)21 Vertex (com.tinkerpop.blueprints.Vertex)20 UnsupportedEncodingException (java.io.UnsupportedEncodingException)18 Path (javax.ws.rs.Path)18 BatfishException (org.batfish.common.BatfishException)17 Job (org.apache.hadoop.mapreduce.v2.app.job.Job)16 WebTarget (javax.ws.rs.client.WebTarget)15 Response (javax.ws.rs.core.Response)14