Package org.apache.sling.commons.json

Examples of org.apache.sling.commons.json.JSONObject


    /**
     * To query chunk upload in json
     */
    private JSONObject getChunkJson(String path) throws Exception {
        JSONObject json = null;
        HttpResponse response = httpGet(path + ".3.json");
        String jsonStr = getRequestExecutor().getContent();
        json = new JSONObject(jsonStr);
        if (json.has(JCR_CONTENT)) {
            json = json.getJSONObject(JCR_CONTENT);
        }
        return json;
    }
View Full Code Here


        entity.addPart("field1", new StringBody("HelloWorld"));
        entity.addPart("field2", new StringBody("30.01.1988"));
        entity.addPart(SlingPostConstants.RP_OPERATION, new StringBody("validation"));
        RequestExecutor re = getRequestExecutor().execute(getRequestBuilder().buildPostRequest
                ("/validation/testing/fakeFolder1/resource").withEntity(entity)).assertStatus(200);
        JSONObject jsonResponse = new JSONObject(re.getContent());
        assertTrue(jsonResponse.getBoolean("valid"));
    }
View Full Code Here

        entity.addPart("field1", new StringBody("Hello World"));
        entity.addPart(SlingPostConstants.RP_OPERATION, new StringBody("validation"));
        RequestExecutor re = getRequestExecutor().execute(getRequestBuilder().buildPostRequest
                ("/validation/testing/fakeFolder1/resource").withEntity(entity)).assertStatus(200);
        String content = re.getContent();
        JSONObject jsonResponse = new JSONObject(content);
        assertFalse(jsonResponse.getBoolean("valid"));
    }
View Full Code Here

    private String getProperty(String url, String name) throws Exception {
        return getContent(url + "/" + name + ".txt", CONTENT_TYPE_PLAIN);
    }

    private String[] getPropertyArray(String url, String name) throws Exception {
        JSONObject jo = new JSONObject(getContent(url + "/" + name + ".json", CONTENT_TYPE_JSON));
        JSONArray arr = jo.getJSONArray(name);
        String[] result = new String[arr.length()];
        for (int i = 0; i < arr.length(); i++) {
            result[i] = arr.getString(i);
        }
        return result;
View Full Code Here

        props.put("jcr:primaryType","nt:unstructured");
        final String location = testClient.createNode(postUrl + SlingPostConstants.DEFAULT_CREATE_SUFFIX, props);
       
        // assert no mixins
        String content = getContent(location + ".json", CONTENT_TYPE_JSON);
        JSONObject json = new JSONObject(content);
        assertFalse("jcr:mixinTypes not expected to be set", json.has("jcr:mixinTypes"));
       
        // add mixin
        props.clear();
        props.put("jcr:mixinTypes", "mix:versionable");
        testClient.createNode(location, props);
       
        content = getContent(location + ".json", CONTENT_TYPE_JSON);
        json = new JSONObject(content);
        assertTrue("jcr:mixinTypes expected after setting them", json.has("jcr:mixinTypes"));
       
        Object mixObject = json.get("jcr:mixinTypes");
        assertTrue("jcr:mixinTypes must be an array", mixObject instanceof JSONArray);
       
        JSONArray mix = (JSONArray) mixObject;
        assertTrue("jcr:mixinTypes must have a single entry", mix.length() == 1);
        assertEquals("jcr:mixinTypes must have correct value", "mix:versionable", mix.get(0));

        // remove mixin
        props.clear();
        props.put("jcr:mixinTypes@Delete", "-");
        testClient.createNode(location, props);

        content = getContent(location + ".json", CONTENT_TYPE_JSON);
        json = new JSONObject(content);
        final boolean noMixins = !json.has("jcr:mixinTypes") || json.getJSONArray("jcr:mixinTypes").length() == 0;
        assertTrue("no jcr:mixinTypes expected after clearing it", noMixins);
    }
View Full Code Here

        props.put("jcr:primaryType","nt:unstructured");
        final String location = testClient.createNode(postUrl + SlingPostConstants.DEFAULT_CREATE_SUFFIX, props);
       
        // assert correct nodetype
        String content = getContent(location + ".json", CONTENT_TYPE_JSON);
        JSONObject json = new JSONObject(content);
        assertTrue("jcr:primaryType isn't set correctly", json.getString("jcr:primaryType").equals("nt:unstructured"));
       
        // change nodetype
        props.clear();
        props.put("jcr:primaryType", "sling:Folder");
        testClient.createNode(location, props);
       
        // assert correct nodetype
        content = getContent(location + ".json", CONTENT_TYPE_JSON);
        json = new JSONObject(content);
        assertTrue("jcr:primaryType isn't set correctly", json.getString("jcr:primaryType").equals("sling:Folder"));
    }
View Full Code Here

        clientNodeProperties.add("propOne", "propOneValue2");
        clientNodeProperties.add("propTwo", "propTwoValue");
      String testNodeUrl = H.getTestClient().createNode(createTestNodeUrl, clientNodeProperties, null, false);

        String content = H.getContent(testNodeUrl + ".json", HttpTest.CONTENT_TYPE_JSON);
        JSONObject json = new JSONObject(content);
        Object propOneObj = json.opt("propOne");
        assertTrue(propOneObj instanceof JSONArray);
        assertEquals(2, ((JSONArray)propOneObj).length());
        assertEquals("propOneValue1", ((JSONArray)propOneObj).get(0));
        assertEquals("propOneValue2", ((JSONArray)propOneObj).get(1));
     
        Object propTwoObj = json.opt("propTwo");
        assertTrue(propTwoObj instanceof String);
        assertEquals("propTwoValue", propTwoObj);
     
     
        //3. Attempt to update property of node as testUser (500: javax.jcr.AccessDeniedException: /test/node/propOne: not allowed to add or modify item)
        // curl -FpropOne=propOneValueChanged -FpropTwo=propTwoValueChanged1 -FpropTwo=propTwoValueChanged2 http://myuser:password@localhost:8080/test/node
      List<NameValuePair> postParams = new ArrayList<NameValuePair>();
      postParams.add(new NameValuePair("propOne", "propOneValueChanged"));
      postParams.add(new NameValuePair("propTwo", "propTwoValueChanged1"));
      postParams.add(new NameValuePair("propTwo", "propTwoValueChanged2"));
    Credentials testUserCreds = new UsernamePasswordCredentials(testUserId, "testPwd");
    String expectedMessage = "Expected javax.jcr.AccessDeniedException";
      H.assertAuthenticatedPostStatus(testUserCreds, testNodeUrl, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, postParams, expectedMessage);
     
        //4. Grant jcr:modifyProperties rights to testUser as admin (OK)
        // curl -FprincipalId=myuser -Fprivilege@jcr:modifyProperties=granted http://admin:admin@localhost:8080/test/node.modifyAce.html
        Map<String, String> nodeAceProperties = new HashMap<String, String>();
        nodeAceProperties.put("principalId", testUserId);
        nodeAceProperties.put("privilege@jcr:modifyProperties", "granted");
      H.getTestClient().createNode(testNodeUrl + ".modifyAce.html", nodeAceProperties);
     
        //use a davex session to verify the correct JCR events are delivered
        Repository repository = JcrUtils.getRepository(HttpTest.HTTP_BASE_URL + "/server/");
        Session jcrSession = null;
        TestEventListener listener = new TestEventListener();
        ObservationManager observationManager = null;
        try {
            jcrSession = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
            observationManager = jcrSession.getWorkspace().getObservationManager();
          String testNodePath = testNodeUrl.substring(HttpTest.HTTP_BASE_URL.length());
            observationManager.addEventListener(listener,
          Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED, //event types
          testNodePath, //absPath
          true, //isDeep
          null, //uuid
          null, //nodeTypeName
          false); //noLocal
       
            //5. Attempt to update properties of node (OK)
            // curl -FpropOne=propOneValueChanged -FpropTwo=propTwoValueChanged1 -FpropTwo=propTwoValueChanged2 http://myuser:password@localhost:8080/test/node
          H.assertAuthenticatedPostStatus(testUserCreds, testNodeUrl, HttpServletResponse.SC_OK, postParams, expectedMessage);
         
          //verify the change happened
            String afterUpdateContent = H.getContent(testNodeUrl + ".json", HttpTest.CONTENT_TYPE_JSON);
            JSONObject afterUpdateJson = new JSONObject(afterUpdateContent);
            Object afterUpdatePropOneObj = afterUpdateJson.opt("propOne");
            assertTrue(afterUpdatePropOneObj instanceof JSONArray);
            assertEquals(1, ((JSONArray)afterUpdatePropOneObj).length());
            assertEquals("propOneValueChanged", ((JSONArray)afterUpdatePropOneObj).get(0));
         
            Object afterUpdatePropTwoObj = afterUpdateJson.opt("propTwo");
            assertTrue(afterUpdatePropTwoObj instanceof JSONArray);
            assertEquals(2, ((JSONArray)afterUpdatePropTwoObj).length());
            assertEquals("propTwoValueChanged1", ((JSONArray)afterUpdatePropTwoObj).get(0));
            assertEquals("propTwoValueChanged2", ((JSONArray)afterUpdatePropTwoObj).get(1));
           
View Full Code Here

            HttpServletResponse.SC_NOT_FOUND);

        // assert content test
        String content = getContent(HTTP_BASE_URL + testPath
            + "/dest/src1.json", CONTENT_TYPE_JSON);
        JSONObject json = new JSONObject(content);
        assertEquals("Hello", json.get("text"));

        // modify src1 content
        nvPairs.clear();
        nvPairs.add(new NameValuePair("text", "Modified Hello"));
        assertPostStatus(HTTP_BASE_URL + testPath + "/src1",
            HttpServletResponse.SC_OK, nvPairs, "Expect Content Update Success");

        // copy the src? nodes
        nvPairs.clear();
        nvPairs.add(new NameValuePair(SlingPostConstants.RP_OPERATION,
            SlingPostConstants.OPERATION_COPY));
        nvPairs.add(new NameValuePair(SlingPostConstants.RP_DEST, testPath
            + "/dest/"));
        nvPairs.add(new NameValuePair(SlingPostConstants.RP_APPLY_TO, testPath
            + "/src1"));
        nvPairs.add(new NameValuePair(SlingPostConstants.RP_APPLY_TO, testPath
            + "/src2"));
        assertPostStatus(testRoot, HttpServletResponse.SC_OK, nvPairs,
            "Expecting Copy Success");

        // assert content test
        String content2 = getContent(HTTP_BASE_URL + testPath
            + "/dest/src1.json", CONTENT_TYPE_JSON);
        JSONObject json2 = new JSONObject(content2);
        assertEquals("Modified Hello", json2.get("text"));

        // clean up
        testClient.delete(testRoot);
    }
View Full Code Here

            HttpServletResponse.SC_NOT_FOUND);

        // assert content test
        String content = getContent(HTTP_BASE_URL + testPath
            + "/dest/src1.json", CONTENT_TYPE_JSON);
        JSONObject json = new JSONObject(content);
        assertEquals("Hello", json.get("text"));

        // modify src1 content
        nvPairs.clear();
        nvPairs.add(new NameValuePair("text", "Modified Hello"));
        assertPostStatus(HTTP_BASE_URL + testPath + "/src1",
            HttpServletResponse.SC_CREATED, nvPairs,
            "Expect Content Create Success");

        // move the src? nodes
        nvPairs.clear();
        nvPairs.add(new NameValuePair(SlingPostConstants.RP_OPERATION,
            SlingPostConstants.OPERATION_MOVE));
        nvPairs.add(new NameValuePair(SlingPostConstants.RP_DEST, testPath
            + "/dest/"));
        nvPairs.add(new NameValuePair(SlingPostConstants.RP_APPLY_TO, testPath
            + "/src1"));
        nvPairs.add(new NameValuePair(SlingPostConstants.RP_APPLY_TO, testPath
            + "/src2"));
        assertPostStatus(testRoot, HttpServletResponse.SC_OK, nvPairs,
            "Expecting Move Success");

        // assert content test
        String content2 = getContent(HTTP_BASE_URL + testPath
            + "/dest/src1.json", CONTENT_TYPE_JSON);
        JSONObject json2 = new JSONObject(content2);
        assertEquals("Modified Hello", json2.get("text"));

        // clean up
        testClient.delete(testRoot);
    }
View Full Code Here

        assertEquals(importedNodeUrl, importedNodeUrl2);

        // assert content at new location
        String content = getContent(importedNodeUrl2 + ".3.json", CONTENT_TYPE_JSON);

    JSONObject jsonObj = new JSONObject(content);
    assertNotNull(jsonObj);
    assertNull(jsonObj.optString("propTest", null)); //test property should be gone.

    //assert the imported content is there.
        String expectedJsonContent = getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/importresults.json"));
    assertExpectedJSON(new JSONObject(expectedJsonContent), jsonObj);
    }
View Full Code Here

TOP

Related Classes of org.apache.sling.commons.json.JSONObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.