Search in sources :

Example 46 with JsonArray

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

the class ModifyAceTest method testModifyAceForUser.

@Test
public void testModifyAceForUser() throws IOException, JsonException {
    testUserId = H.createTestUser();
    testFolderUrl = H.createTestFolder();
    String postUrl = testFolderUrl + ".modifyAce.html";
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("principalId", testUserId));
    postParams.add(new NameValuePair("privilege@jcr:read", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:write", "denied"));
    //invalid value should be ignored.
    postParams.add(new NameValuePair("privilege@jcr:modifyAccessControl", "bogus"));
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    //fetch the JSON for the acl to verify the settings.
    String getUrl = testFolderUrl + ".acl.json";
    String json = H.getAuthenticatedContent(creds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json);
    JsonObject jsonObject = JsonUtil.parseObject(json);
    assertEquals(1, jsonObject.size());
    JsonObject aceObject = jsonObject.getJsonObject(testUserId);
    assertNotNull(aceObject);
    String principalString = aceObject.getString("principal");
    assertEquals(testUserId, principalString);
    int order = aceObject.getInt("order");
    assertEquals(0, order);
    JsonArray grantedArray = aceObject.getJsonArray("granted");
    assertNotNull(grantedArray);
    assertEquals(1, grantedArray.size());
    assertEquals("jcr:read", grantedArray.getString(0));
    JsonArray deniedArray = aceObject.getJsonArray("denied");
    assertNotNull(deniedArray);
    assertEquals(1, deniedArray.size());
    assertEquals("jcr:write", deniedArray.getString(0));
}
Also used : JsonArray(javax.json.JsonArray) NameValuePair(org.apache.commons.httpclient.NameValuePair) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) HttpTest(org.apache.sling.commons.testing.integration.HttpTest) Test(org.junit.Test)

Example 47 with JsonArray

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

the class RemoveAcesTest method createFolderWithAces.

private String createFolderWithAces(boolean addGroupAce) throws IOException, JsonException {
    testUserId = createTestUser();
    testFolderUrl = createTestFolder();
    String postUrl = testFolderUrl + ".modifyAce.html";
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("principalId", testUserId));
    postParams.add(new NameValuePair("privilege@jcr:read", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:write", "denied"));
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    if (addGroupAce) {
        testGroupId = createTestGroup();
        postParams = new ArrayList<NameValuePair>();
        postParams.add(new NameValuePair("principalId", testGroupId));
        postParams.add(new NameValuePair("privilege@jcr:read", "granted"));
        assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    }
    //fetch the JSON for the acl to verify the settings.
    String getUrl = testFolderUrl + ".acl.json";
    String json = getAuthenticatedContent(creds, getUrl, CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json);
    JsonObject jsonObject = JsonUtil.parseObject(json);
    if (addGroupAce) {
        assertEquals(2, jsonObject.size());
    } else {
        assertEquals(1, jsonObject.size());
    }
    JsonObject aceObject = jsonObject.getJsonObject(testUserId);
    assertNotNull(aceObject);
    assertEquals(0, aceObject.getInt("order"));
    String principalString = aceObject.getString("principal");
    assertEquals(testUserId, principalString);
    JsonArray grantedArray = aceObject.getJsonArray("granted");
    assertNotNull(grantedArray);
    assertEquals("jcr:read", grantedArray.getString(0));
    JsonArray deniedArray = aceObject.getJsonArray("denied");
    assertNotNull(deniedArray);
    assertEquals("jcr:write", deniedArray.getString(0));
    if (addGroupAce) {
        aceObject = jsonObject.getJsonObject(testGroupId);
        assertNotNull(aceObject);
        principalString = aceObject.getString("principal");
        assertEquals(testGroupId, principalString);
        assertEquals(1, aceObject.getInt("order"));
        grantedArray = aceObject.getJsonArray("granted");
        assertNotNull(grantedArray);
        assertEquals("jcr:read", grantedArray.getString(0));
    }
    return testFolderUrl;
}
Also used : JsonArray(javax.json.JsonArray) NameValuePair(org.apache.commons.httpclient.NameValuePair) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 48 with JsonArray

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

the class JsonObjectCreatorTest method testCreateArray.

@Test
public void testCreateArray() throws Exception {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("emptyArray", new Object[] {});
    properties.put("stringArray", new String[] { "10", "100" });
    properties.put("intArray", new int[] { 10, 100 });
    properties.put("doubleArray", new double[] { 10d, 100d });
    properties.put("byteArray", new byte[] { 0x0A, 0x64 });
    properties.put("floatArray", new float[] { 10.0f, 100.0f });
    properties.put("shortArray", new short[] { 10, 100 });
    properties.put("longArray", new long[] { 10, 100 });
    properties.put("booleanArray", new boolean[] { true, false });
    properties.put("charArray", new char[] { 'a', 'b' });
    Resource resource = new MockResource("/some/path", properties, resolver);
    JsonObject json = JsonObjectCreator.create(resource).build();
    assertEquals(0, json.getJsonArray("emptyArray").size());
    JsonArray array;
    array = json.getJsonArray("stringArray");
    assertEquals("10", array.getString(0));
    array = json.getJsonArray("intArray");
    assertEquals(10, array.getInt(0));
    array = json.getJsonArray("doubleArray");
    assertEquals("10.0", array.getString(0));
    array = json.getJsonArray("byteArray");
    assertEquals("10", array.getString(0));
    array = json.getJsonArray("floatArray");
    assertEquals("10.0", array.getString(0));
    array = json.getJsonArray("shortArray");
    assertEquals("10", array.getString(0));
    array = json.getJsonArray("longArray");
    assertEquals(10L, array.getJsonNumber(0).longValue());
    array = json.getJsonArray("booleanArray");
    assertEquals(true, array.getBoolean(0));
    array = json.getJsonArray("charArray");
    assertEquals("a", array.getString(0));
}
Also used : JsonArray(javax.json.JsonArray) HashMap(java.util.HashMap) MockResource(org.apache.sling.testing.resourceresolver.MockResource) Resource(org.apache.sling.api.resource.Resource) MockResource(org.apache.sling.testing.resourceresolver.MockResource) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 49 with JsonArray

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

the class SlingRemoteTestRunner method maybeExecuteTests.

private void maybeExecuteTests() throws Exception {
    if (testHttpClient != null) {
        // Tests already ran
        return;
    }
    testHttpClient = new RemoteTestHttpClient(testParameters.getJunitServletUrl(), this.username, this.password, true);
    // Let the parameters class customize the request if desired 
    if (testParameters instanceof RequestCustomizer) {
        testHttpClient.setRequestCustomizer((RequestCustomizer) testParameters);
    }
    // Run tests remotely and get response
    final RequestExecutor executor = testHttpClient.runTests(testParameters.getTestClassesSelector(), testParameters.getTestMethodSelector(), "json");
    executor.assertContentType("application/json");
    final JsonArray json = Json.createReader(new StringReader(JsonTicksConverter.tickToDoubleQuote(executor.getContent()))).readArray();
    // based on this vlaue
    for (int i = 0; i < json.size(); i++) {
        final JsonObject obj = json.getJsonObject(i);
        if (obj.containsKey("INFO_TYPE") && "test".equals(obj.getString("INFO_TYPE"))) {
            children.add(new SlingRemoteTest(testClass, obj));
        }
    }
    log.info("Server-side tests executed as {} at {} with path {}", new Object[] { this.username, testParameters.getJunitServletUrl(), testHttpClient.getTestExecutionPath() });
    // Optionally check that number of tests is as expected
    if (testParameters instanceof SlingTestsCountChecker) {
        ((SlingTestsCountChecker) testParameters).checkNumberOfTests(children.size());
    }
}
Also used : JsonArray(javax.json.JsonArray) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) RemoteTestHttpClient(org.apache.sling.junit.remote.httpclient.RemoteTestHttpClient) RequestCustomizer(org.apache.sling.testing.tools.http.RequestCustomizer)

Example 50 with JsonArray

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

the class ProfileParser method parseFragProfiles.

private void parseFragProfiles() {
    JsonArray frags = getFragmentProfile();
    for (JsonObject fragProfile : frags.getValuesAs(JsonObject.class)) {
        int mId = fragProfile.getInt("majorFragmentId");
        FragInfo major = fragments.get(mId);
        major.parse(fragProfile);
    }
}
Also used : JsonArray(javax.json.JsonArray) JsonObject(javax.json.JsonObject)

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