Search in sources :

Example 41 with JsonObject

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

the class JsonResponseTest method testSetError.

@SuppressWarnings({ "ThrowableInstanceNeverThrown" })
public void testSetError() throws IOException {
    String errMsg = "Dummy error";
    res.setError(new Error(errMsg));
    MockSlingHttpServletResponse resp = new MockSlingHttpServletResponse();
    res.send(resp, true);
    JsonObject json = res.getJson();
    JsonValue error = assertProperty(json, "error");
    assertProperty((JsonObject) error, "class", Error.class.getName());
    assertProperty((JsonObject) error, "message", errMsg);
}
Also used : JsonValue(javax.json.JsonValue) MockSlingHttpServletResponse(org.apache.sling.commons.testing.sling.MockSlingHttpServletResponse) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString)

Example 42 with JsonObject

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

the class JSONGroovyBuilderTest method testRichObject.

public void testRichObject() throws IOException, JsonException {
    final String toDelete = uploadTestScript("builder_rich_object.groovy", "json.groovy");
    try {
        final String content = getContent(displayUrl + ".json", CONTENT_TYPE_JSON);
        log.debug("{} content: {}", displayUrl, content);
        JsonObject jo = Json.createReader(new StringReader(content)).readObject();
        assertEquals("Content contained wrong number of items", 2, jo.size());
        assertEquals("Content contained wrong data", testText, jo.getString("text"));
        assertEquals("Content contained wrong data", "bar", ((JsonObject) jo.getJsonObject("obj")).getString("foo"));
    } finally {
        testClient.delete(toDelete);
    }
}
Also used : StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject)

Example 43 with JsonObject

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

the class JSONGroovyBuilderTest method testObject.

public void testObject() throws IOException, JsonException {
    final String toDelete = uploadTestScript("builder_object.groovy", "json.groovy");
    try {
        final String content = getContent(displayUrl + ".json", CONTENT_TYPE_JSON);
        JsonObject jo = Json.createReader(new StringReader(content)).readObject();
        assertEquals("Content contained wrong number of items", 1, jo.size());
        assertEquals("Content contained wrong key", "text", jo.keySet().iterator().next());
        assertEquals("Content contained wrong data", testText, jo.getString("text"));
    } finally {
        testClient.delete(toDelete);
    }
}
Also used : StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject)

Example 44 with JsonObject

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

the class AccessPrivilegesInfoTest method testSLING_1090.

/**
	 * Test the fix for SLING-1090
	 */
@Test
public void testSLING_1090() throws Exception {
    testUserId = H.createTestUser();
    //grant jcr: removeChildNodes to the root node
    ArrayList<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:removeChildNodes", "granted"));
    Credentials adminCreds = new UsernamePasswordCredentials("admin", "admin");
    H.assertAuthenticatedPostStatus(adminCreds, HttpTest.HTTP_BASE_URL + "/.modifyAce.html", HttpServletResponse.SC_OK, postParams, null);
    //create a node as a child of the root folder
    testFolderUrl = H.getTestClient().createNode(HttpTest.HTTP_BASE_URL + "/testFolder" + random.nextInt() + SlingPostConstants.DEFAULT_CREATE_SUFFIX, null);
    String postUrl = testFolderUrl + ".modifyAce.html";
    //grant jcr:removeNode to the test node
    postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("principalId", testUserId));
    postParams.add(new NameValuePair("privilege@jcr:read", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:removeNode", "granted"));
    H.assertAuthenticatedPostStatus(adminCreds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    //fetch the JSON for the test page to verify the settings.
    String getUrl = testFolderUrl + ".privileges-info.json";
    Credentials testUserCreds = new UsernamePasswordCredentials(testUserId, "testPwd");
    String json = H.getAuthenticatedContent(testUserCreds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json);
    JsonObject jsonObj = JsonUtil.parseObject(json);
    assertEquals(true, jsonObj.getBoolean("canDelete"));
}
Also used : 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 45 with JsonObject

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

the class GetAclTest method testEffectiveAclForUser.

/**
	 * Test for SLING-2600, Effective ACL servlet returns incorrect information
	 */
@Test
public void testEffectiveAclForUser() throws IOException, JsonException {
    testUserId = H.createTestUser();
    testUserId2 = H.createTestUser();
    String testFolderUrl = H.createTestFolder("{ \"jcr:primaryType\": \"nt:unstructured\", \"propOne\" : \"propOneValue\", \"child\" : { \"childPropOne\" : true } }");
    String postUrl = testFolderUrl + ".modifyAce.html";
    //1. create an initial set of privileges
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("principalId", testUserId));
    postParams.add(new NameValuePair("privilege@jcr:write", "granted"));
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("principalId", testUserId2));
    postParams.add(new NameValuePair("privilege@jcr:write", "granted"));
    H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("principalId", testUserId2));
    postParams.add(new NameValuePair("privilege@jcr:lockManagement", "granted"));
    postUrl = testFolderUrl + "/child.modifyAce.html";
    H.assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    //fetch the JSON for the eacl to verify the settings.
    String getUrl = testFolderUrl + "/child.eacl.json";
    String json = H.getAuthenticatedContent(creds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json);
    JsonObject jsonObject = JsonUtil.parseObject(json);
    JsonObject aceObject = jsonObject.getJsonObject(testUserId);
    assertNotNull(aceObject);
    String principalString = aceObject.getString("principal");
    assertEquals(testUserId, principalString);
    JsonArray grantedArray = aceObject.getJsonArray("granted");
    assertNotNull(grantedArray);
    assertEquals(1, grantedArray.size());
    Set<String> grantedPrivilegeNames = new HashSet<String>();
    for (int i = 0; i < grantedArray.size(); i++) {
        grantedPrivilegeNames.add(grantedArray.getString(i));
    }
    H.assertPrivilege(grantedPrivilegeNames, true, "jcr:write");
    Object deniedArray = aceObject.get("denied");
    assertNull(deniedArray);
    JsonObject aceObject2 = jsonObject.getJsonObject(testUserId2);
    assertNotNull(aceObject2);
    String principalString2 = aceObject2.getString("principal");
    assertEquals(testUserId2, principalString2);
    JsonArray grantedArray2 = aceObject2.getJsonArray("granted");
    assertNotNull(grantedArray2);
    assertEquals(2, grantedArray2.size());
    Set<String> grantedPrivilegeNames2 = new HashSet<String>();
    for (int i = 0; i < grantedArray2.size(); i++) {
        grantedPrivilegeNames2.add(grantedArray2.getString(i));
    }
    H.assertPrivilege(grantedPrivilegeNames2, true, "jcr:write");
    H.assertPrivilege(grantedPrivilegeNames2, true, "jcr:lockManagement");
    Object deniedArray2 = aceObject2.get("denied");
    assertNull(deniedArray2);
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) JsonArray(javax.json.JsonArray) JsonObject(javax.json.JsonObject) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) HashSet(java.util.HashSet) HttpTest(org.apache.sling.commons.testing.integration.HttpTest) Test(org.junit.Test)

Aggregations

JsonObject (javax.json.JsonObject)214 Test (org.junit.Test)101 JsonArray (javax.json.JsonArray)58 StringReader (java.io.StringReader)52 Credentials (org.apache.commons.httpclient.Credentials)52 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)52 ArrayList (java.util.ArrayList)51 HashMap (java.util.HashMap)47 NameValuePair (org.apache.commons.httpclient.NameValuePair)43 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)41 JsonString (javax.json.JsonString)31 JsonReader (javax.json.JsonReader)25 HashSet (java.util.HashSet)16 Map (java.util.Map)12 Response (javax.ws.rs.core.Response)12 JsonObjectBuilder (javax.json.JsonObjectBuilder)11 NameValuePairList (org.apache.sling.commons.testing.integration.NameValuePairList)11 StringWriter (java.io.StringWriter)10 JsonException (javax.json.JsonException)10 JsonArrayBuilder (javax.json.JsonArrayBuilder)9