Search in sources :

Example 66 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class JsonSupportTest method testToJson.

@Test
public void testToJson() {
    Map<String, Object> map = ImmutableMap.<String, Object>builder().put("prop1", "value1").put("prop2", ImmutableList.of("value2", "value3")).put("prop3", ImmutableMap.of("prop4", "value4")).build();
    JsonObject obj = toJson(map);
    assertEquals("value1", obj.getString("prop1"));
    JsonArray array = obj.getJsonArray("prop2");
    assertEquals(2, array.size());
    assertEquals("value2", array.getString(0));
    assertEquals("value3", array.getString(1));
    JsonObject prop3 = obj.getJsonObject("prop3");
    assertEquals("value4", prop3.getString("prop4"));
}
Also used : JsonArray(javax.json.JsonArray) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) JsonSupport.parseObject(org.apache.sling.maven.bundlesupport.JsonSupport.parseObject) Test(org.junit.Test)

Example 67 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class BasicLauncherIT method testSpecificStartLevel.

@Test
@Retry(timeoutMsec = U.LONG_TIMEOUT_MSEC, intervalMsec = U.STD_INTERVAL)
public void testSpecificStartLevel() throws Exception {
    // This bundle should only be installed, as it's set to start level 99
    final String symbolicName = "org.apache.commons.collections";
    assertEquals("Expecting bundle " + symbolicName + " to be installed", "Installed", osgiConsole.getBundleState(symbolicName));
    // Start level is in the props array, with key="Start Level"
    final JsonObject status = U.getBundleData(C, client, symbolicName);
    final JsonArray props = status.getJsonArray("data").getJsonObject(0).getJsonArray("props");
    final String KEY = "key";
    final String SL = "Start Level";
    boolean found = false;
    for (int i = 0; i < props.size(); i++) {
        final JsonObject o = props.getJsonObject(i);
        if (o.containsKey(KEY) && SL.equals(o.getString(KEY))) {
            found = true;
            assertEquals("Expecting the start level that's set in provisioning model", 99, o.getInt("value"));
        }
    }
    assertTrue("Expecting start level to be found in JSON output", found);
}
Also used : JsonArray(javax.json.JsonArray) JsonObject(javax.json.JsonObject) Test(org.junit.Test) Retry(org.apache.sling.commons.testing.junit.Retry)

Example 68 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class PlumberServletTest method testAdditionalBindingsAndWriter.

@Test
public void testAdditionalBindingsAndWriter() throws Exception {
    String testBinding = "testBinding";
    String testBindingLength = testBinding + "Length";
    String bindingValue = "testBindingValue";
    String pathLengthParam = "pathLength";
    String bindings = "{\"" + testBinding + "\":\"" + bindingValue + "\"}";
    String respObject = "{\"" + pathLengthParam + "\":\"${path.get(\\\"dummyGrandChild\\\").length}\",\"" + testBindingLength + "\":\"${" + testBinding + ".length}\"}";
    SlingHttpServletRequest request = mockPlumberServletRequest(context.resourceResolver(), dummyTreePath, null, bindings.toString(), respObject.toString(), null, null);
    servlet.execute(request, response, false);
    assertDummyTree();
    JsonObject response = Json.createReader(new StringReader(stringResponse.toString())).readObject();
    JsonArray array = response.getJsonArray(OutputWriter.KEY_ITEMS);
    for (int i = 0; i < array.size(); i++) {
        JsonObject object = array.getJsonObject(i);
        assertNotNull("there should be an object returned at each time", object);
        String path = object.getString(CustomWriter.PATH_KEY);
        assertNotNull("the string path should be returned for each item, containing the path of the resource");
        String pathLength = object.getString(pathLengthParam);
        assertNotNull("there should be a pathLength param, as specified in the writer", pathLength);
        assertEquals("Pathlength should be the string representation of the path length", path.length() + "", pathLength);
        String testBindingLengthValue = object.getString(testBindingLength);
        assertNotNull("testBindingLength should be there", testBindingLengthValue);
        assertEquals("testBindingLength should be the string representation of the additional binding length", bindingValue.length() + "", testBindingLengthValue);
    }
}
Also used : JsonArray(javax.json.JsonArray) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) AbstractPipeTest(org.apache.sling.pipes.AbstractPipeTest) ContainerPipeTest(org.apache.sling.pipes.ContainerPipeTest) Test(org.junit.Test)

Example 69 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class PlumberServletTest method assertDummyTree.

private void assertDummyTree(int size) {
    String finalResponse = stringResponse.toString();
    assertFalse("There should be a response", StringUtils.isBlank(finalResponse));
    JsonObject object = Json.createReader(new StringReader(finalResponse)).readObject();
    assertEquals("response should be an obj with size value equals to " + DUMMYTREE_TEST_SIZE, object.getInt(OutputWriter.KEY_SIZE), DUMMYTREE_TEST_SIZE);
    assertEquals("response should be an obj with items value equals to a " + size + " valued array", object.getJsonArray(OutputWriter.KEY_ITEMS).size(), size);
}
Also used : StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject)

Example 70 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class PostServletOrderTest method verifyOrder.

/**
     * Verify node order
     */
private void verifyOrder(String parentUrl, String[] names) throws IOException {
    // check that nodes appear in creation order in their parent's list of children
    final String content = getContent(parentUrl + ".1.json", CONTENT_TYPE_JSON);
    String expected = "";
    for (String n : names) {
        expected += n + ",";
    }
    //assertJavascript(expected, content, TEST_SCRIPT);
    try {
        String actual = "";
        JsonObject obj = JsonUtil.parseObject(content);
        for (String name : obj.keySet()) {
            Object o = obj.get(name);
            if (o instanceof JsonObject) {
                actual += name + ",";
            }
        }
        assertEquals(expected, actual);
    } catch (JsonException e) {
        throw new IOException(e.toString());
    }
}
Also used : JsonException(javax.json.JsonException) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) IOException(java.io.IOException)

Aggregations

JsonObject (javax.json.JsonObject)302 Test (org.junit.Test)110 JsonArray (javax.json.JsonArray)97 HashMap (java.util.HashMap)68 StringReader (java.io.StringReader)66 ArrayList (java.util.ArrayList)58 Credentials (org.apache.commons.httpclient.Credentials)52 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)52 JsonString (javax.json.JsonString)50 JsonReader (javax.json.JsonReader)49 NameValuePair (org.apache.commons.httpclient.NameValuePair)43 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)41 HashSet (java.util.HashSet)20 Map (java.util.Map)20 JsonException (javax.json.JsonException)20 JsonObjectBuilder (javax.json.JsonObjectBuilder)20 Response (javax.ws.rs.core.Response)19 LinkedHashMap (java.util.LinkedHashMap)17 JsonValue (javax.json.JsonValue)14 StringWriter (java.io.StringWriter)13