Search in sources :

Example 81 with JsonArray

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

the class WebconsoleClient method getBundleData.

private JsonObject getBundleData(String symbolicName) throws Exception {
    // This returns a data structure like
    // {"status":"Bundle information: 173 bundles in total - all 173 bundles active.","s":[173,171,2,0,0],"data":
    //  [
    //      {"id":0,"name":"System Bundle","fragment":false,"stateRaw":32,"state":"Active","version":"3.0.7","symbolicName":"org.apache.felix.framework","category":""},
    //  ]}
    final String path = getBundlePath(symbolicName, ".json");
    final String content = executor.execute(builder.buildGetRequest(path).withCredentials(username, password)).assertStatus(200).getContent();
    final JsonObject root = Json.createReader(new StringReader(content)).readObject();
    if (!root.containsKey(JSON_KEY_DATA)) {
        fail(path + " does not provide '" + JSON_KEY_DATA + "' element, JSON content=" + content);
    }
    final JsonArray data = root.getJsonArray(JSON_KEY_DATA);
    if (data.size() < 1) {
        fail(path + "." + JSON_KEY_DATA + " is empty, JSON content=" + content);
    }
    final JsonObject bundle = data.getJsonObject(0);
    if (!bundle.containsKey(JSON_KEY_STATE)) {
        fail(path + ".data[0].state missing, JSON content=" + content);
    }
    return bundle;
}
Also used : JsonArray(javax.json.JsonArray) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject)

Example 82 with JsonArray

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

the class JSONGroovyBuilderTest method testArray.

public void testArray() throws IOException, JsonException {
    final String toDelete = uploadTestScript("builder_array.groovy", "json.groovy");
    try {
        final String content = getContent(displayUrl + ".json", CONTENT_TYPE_JSON);
        JsonArray jo = Json.createReader(new StringReader(content)).readArray();
        assertEquals("Content contained wrong number of items", 1, jo.size());
        assertEquals("Content contained wrong data", testText, jo.getString(0));
    } finally {
        testClient.delete(toDelete);
    }
}
Also used : JsonArray(javax.json.JsonArray) StringReader(java.io.StringReader)

Example 83 with JsonArray

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

the class ServerSideTestClient method runTests.

public TestResults runTests(String testPackageOrClassName) throws Exception {
    final RemoteTestHttpClient testClient = new RemoteTestHttpClient(serverBaseUrl + "/system/sling/junit", serverUsername, serverPassword, true);
    final TestResults r = new TestResults();
    final Map<String, String> options = new HashMap<String, String>();
    options.put("forceReload", "true");
    final RequestExecutor executor = testClient.runTests(testPackageOrClassName, null, "json", options);
    executor.assertContentType("application/json");
    String content = executor.getContent();
    if (!content.trim().isEmpty()) {
        final JsonArray json = JsonUtil.parseArray(content);
        for (int i = 0; i < json.size(); i++) {
            final JsonObject obj = json.getJsonObject(i);
            if ("test".equals(obj.getString("INFO_TYPE"))) {
                r.testCount++;
                if (obj.containsKey("failure")) {
                    r.failures.add(JsonUtil.toString(obj.get("failure")));
                }
            }
        }
    }
    return r;
}
Also used : JsonArray(javax.json.JsonArray) HashMap(java.util.HashMap) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) JsonObject(javax.json.JsonObject) RemoteTestHttpClient(org.apache.sling.junit.remote.httpclient.RemoteTestHttpClient)

Example 84 with JsonArray

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

the class UpdateGroupTest method getTestUserMemberships.

JsonArray getTestUserMemberships(Credentials creds) throws IOException, JsonException {
    String getUrl = HTTP_BASE_URL + "/system/userManager/user/" + testUserId + ".json";
    //make sure the profile request returns some data
    assertAuthenticatedHttpStatus(creds, getUrl, HttpServletResponse.SC_OK, null);
    String json = getAuthenticatedContent(creds, getUrl, CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json);
    JsonObject jsonObj = JsonUtil.parseObject(json);
    JsonArray memberships = jsonObj.getJsonArray("memberOf");
    return memberships;
}
Also used : JsonArray(javax.json.JsonArray) JsonObject(javax.json.JsonObject)

Example 85 with JsonArray

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

the class UpdateGroupTest method testUpdateGroupMembers.

public void testUpdateGroupMembers() throws IOException, JsonException {
    testGroupId = createTestGroup();
    testUserId = createTestUser();
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    // verify that the members array exists, but is empty
    JsonArray members = getTestGroupMembers(creds);
    assertEquals(0, members.size());
    JsonArray memberships = getTestUserMemberships(creds);
    assertEquals(0, memberships.size());
    String postUrl = HTTP_BASE_URL + "/system/userManager/group/" + testGroupId + ".update.html";
    // add a group member
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair(":member", testUserId));
    assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    members = getTestGroupMembers(creds);
    assertEquals(1, members.size());
    assertEquals("/system/userManager/user/" + testUserId, members.getString(0));
    memberships = getTestUserMemberships(creds);
    assertEquals(1, memberships.size());
    assertEquals("/system/userManager/group/" + testGroupId, memberships.getString(0));
    // delete a group member
    postParams.clear();
    postParams.add(new NameValuePair(":member@Delete", testUserId));
    assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    members = getTestGroupMembers(creds);
    assertEquals(0, members.size());
    memberships = getTestUserMemberships(creds);
    assertEquals(0, memberships.size());
}
Also used : JsonArray(javax.json.JsonArray) NameValuePair(org.apache.commons.httpclient.NameValuePair) ArrayList(java.util.ArrayList) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Aggregations

JsonArray (javax.json.JsonArray)128 JsonObject (javax.json.JsonObject)97 Test (org.junit.Test)42 ArrayList (java.util.ArrayList)32 JsonReader (javax.json.JsonReader)31 HashMap (java.util.HashMap)29 JsonString (javax.json.JsonString)28 HashSet (java.util.HashSet)21 NameValuePair (org.apache.commons.httpclient.NameValuePair)20 Credentials (org.apache.commons.httpclient.Credentials)19 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)19 StringReader (java.io.StringReader)18 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)17 LinkedHashMap (java.util.LinkedHashMap)14 JsonValue (javax.json.JsonValue)11 Map (java.util.Map)10 JsonException (javax.json.JsonException)9 Response (javax.ws.rs.core.Response)9 IOException (java.io.IOException)7 JerseyTest (org.glassfish.jersey.test.JerseyTest)7