Search in sources :

Example 86 with JsonObject

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

the class PostServletImportTest method testImportAutoCheckoutNodes.

/**
     * SLING-2108 Test import operation which auto checks out versionable nodes.
     */
public void testImportAutoCheckoutNodes() throws IOException, JsonException {
    final String testPath = TEST_BASE_PATH;
    Map<String, String> props = new HashMap<String, String>();
    String testNode = testClient.createNode(HTTP_BASE_URL + testPath, props);
    urlsToDelete.add(testNode);
    //1. first create some content to update.
    props.clear();
    props.put(SlingPostConstants.RP_OPERATION, SlingPostConstants.OPERATION_IMPORT);
    String testNodeName = "testNode_" + String.valueOf(random.nextInt());
    props.put(SlingPostConstants.RP_NODE_NAME_HINT, testNodeName);
    testFile = getTestFile(getClass().getResourceAsStream("/integration-test/servlets/post/testimport3.json"));
    props.put(SlingPostConstants.RP_CONTENT_TYPE, "json");
    props.put(SlingPostConstants.RP_REDIRECT_TO, SERVLET_CONTEXT + testPath + "/*");
    props.put(SlingPostConstants.RP_CHECKIN, "true");
    String importedNodeUrl = testClient.createNode(HTTP_BASE_URL + testPath, new NameValuePairList(props), null, true, testFile, SlingPostConstants.RP_CONTENT_FILE, null);
    // assert content at new location
    String content = getContent(importedNodeUrl + ".json", CONTENT_TYPE_JSON);
    JsonObject jsonObj = JsonUtil.parseObject(content);
    assertNotNull(jsonObj);
    //assert that the versionable node is checked in.
    assertFalse(jsonObj.getBoolean("jcr:isCheckedOut"));
    //2. try an update with the :autoCheckout value set to false
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair(SlingPostConstants.RP_OPERATION, SlingPostConstants.OPERATION_IMPORT));
    postParams.add(new NameValuePair(SlingPostConstants.RP_CONTENT_TYPE, "json"));
    postParams.add(new NameValuePair(SlingPostConstants.RP_CHECKIN, "true"));
    postParams.add(new NameValuePair(SlingPostConstants.RP_REPLACE_PROPERTIES, "true"));
    postParams.add(new NameValuePair(SlingPostConstants.RP_AUTO_CHECKOUT, "false"));
    postParams.add(new NameValuePair(SlingPostConstants.RP_CONTENT, "{ \"abc\": \"def2\" }"));
    assertPostStatus(importedNodeUrl, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, postParams, "Expected error from VersionException");
    //3. now try an update with the :autoCheckout value set to true
    postParams.clear();
    postParams.add(new NameValuePair(SlingPostConstants.RP_OPERATION, SlingPostConstants.OPERATION_IMPORT));
    postParams.add(new NameValuePair(SlingPostConstants.RP_CONTENT_TYPE, "json"));
    postParams.add(new NameValuePair(SlingPostConstants.RP_CHECKIN, "true"));
    postParams.add(new NameValuePair(SlingPostConstants.RP_REPLACE_PROPERTIES, "true"));
    postParams.add(new NameValuePair(SlingPostConstants.RP_AUTO_CHECKOUT, "true"));
    postParams.add(new NameValuePair(SlingPostConstants.RP_CONTENT, "{ \"abc\": \"def2\" }"));
    postParams.add(new NameValuePair(":http-equiv-accept", "application/json,*/*;q=0.9"));
    HttpMethod post = assertPostStatus(importedNodeUrl, HttpServletResponse.SC_CREATED, postParams, "Expected 201 status");
    String responseBodyAsString = post.getResponseBodyAsString();
    JsonObject responseJSON = JsonUtil.parseObject(responseBodyAsString);
    JsonArray changes = responseJSON.getJsonArray("changes");
    JsonObject checkoutChange = changes.getJsonObject(0);
    assertEquals("checkout", checkoutChange.getString("type"));
    // assert content at new location
    String content2 = getContent(importedNodeUrl + ".json", CONTENT_TYPE_JSON);
    JsonObject jsonObj2 = JsonUtil.parseObject(content2);
    assertNotNull(jsonObj2);
    //make sure it was really updated
    assertEquals("def2", jsonObj2.getString("abc"));
    //assert that the versionable node is checked back in.
    assertFalse(jsonObj.getBoolean("jcr:isCheckedOut"));
}
Also used : JsonArray(javax.json.JsonArray) NameValuePair(org.apache.commons.httpclient.NameValuePair) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) NameValuePairList(org.apache.sling.commons.testing.integration.NameValuePairList) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 87 with JsonObject

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

the class PostServletImportTest method testImportJCRXMLFromFile.

public void testImportJCRXMLFromFile() throws IOException, JsonException {
    final String testPath = TEST_BASE_PATH;
    Map<String, String> props = new HashMap<String, String>();
    String testNode = testClient.createNode(HTTP_BASE_URL + testPath, props);
    urlsToDelete.add(testNode);
    props.clear();
    props.put(SlingPostConstants.RP_OPERATION, SlingPostConstants.OPERATION_IMPORT);
    String testNodeName = "testnode_1287021810";
    props.put(SlingPostConstants.RP_NODE_NAME, testNodeName);
    testFile = getTestFile(getClass().getResourceAsStream("/integration-test/servlets/post/testimport.jcr.xml"));
    props.put(SlingPostConstants.RP_CONTENT_TYPE, "jcr.xml");
    props.put(SlingPostConstants.RP_REDIRECT_TO, SERVLET_CONTEXT + testPath + "/*");
    String importedNodeUrl = testClient.createNode(HTTP_BASE_URL + testPath, new NameValuePairList(props), null, true, testFile, SlingPostConstants.RP_CONTENT_FILE, null);
    // assert content at new location
    String content = getContent(importedNodeUrl + ".3.json", CONTENT_TYPE_JSON);
    JsonObject jsonObj = JsonUtil.parseObject(content);
    assertNotNull(jsonObj);
    //assert the imported content is there.
    String expectedJsonContent = getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/importresults.json"));
    assertExpectedJSON(JsonUtil.parseObject(expectedJsonContent), jsonObj);
}
Also used : HashMap(java.util.HashMap) JsonObject(javax.json.JsonObject) NameValuePairList(org.apache.sling.commons.testing.integration.NameValuePairList)

Example 88 with JsonObject

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

the class PostServletImportTest method testImportXMLFromRequestParamWithoutOptionalName.

public void testImportXMLFromRequestParamWithoutOptionalName() throws IOException, JsonException {
    final String testPath = TEST_BASE_PATH;
    Map<String, String> props = new HashMap<String, String>();
    String testNode = testClient.createNode(HTTP_BASE_URL + testPath, props);
    urlsToDelete.add(testNode);
    props.clear();
    props.put(SlingPostConstants.RP_OPERATION, SlingPostConstants.OPERATION_IMPORT);
    String xmlContent = getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/testimport2.xml"));
    props.put(SlingPostConstants.RP_CONTENT, xmlContent);
    props.put(SlingPostConstants.RP_CONTENT_TYPE, "xml");
    props.put(SlingPostConstants.RP_REDIRECT_TO, SERVLET_CONTEXT + testPath + "/*");
    String importedNodeUrl = testClient.createNode(HTTP_BASE_URL + testPath, props);
    //make sure the name is what was inside the file.
    assertTrue(importedNodeUrl.endsWith("/nodeName"));
    // assert content at new location
    String content = getContent(importedNodeUrl + ".3.json", CONTENT_TYPE_JSON);
    JsonObject jsonObj = JsonUtil.parseObject(content);
    assertNotNull(jsonObj);
    //assert the imported content is there.
    String expectedJsonContent = getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/importresults.json"));
    assertExpectedJSON(JsonUtil.parseObject(expectedJsonContent), jsonObj);
}
Also used : HashMap(java.util.HashMap) JsonObject(javax.json.JsonObject)

Example 89 with JsonObject

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

the class PostServletImportTest method testImportCheckinNodes.

/**
     * Test import operation which checks in versionable nodes.
     */
public void testImportCheckinNodes() throws IOException, JsonException {
    final String testPath = TEST_BASE_PATH;
    Map<String, String> props = new HashMap<String, String>();
    String testNode = testClient.createNode(HTTP_BASE_URL + testPath, props);
    urlsToDelete.add(testNode);
    props.clear();
    props.put(SlingPostConstants.RP_OPERATION, SlingPostConstants.OPERATION_IMPORT);
    String testNodeName = "testNode_" + String.valueOf(random.nextInt());
    props.put(SlingPostConstants.RP_NODE_NAME_HINT, testNodeName);
    testFile = getTestFile(getClass().getResourceAsStream("/integration-test/servlets/post/testimport3.json"));
    props.put(SlingPostConstants.RP_CONTENT_TYPE, "json");
    props.put(SlingPostConstants.RP_REDIRECT_TO, SERVLET_CONTEXT + testPath + "/*");
    props.put(SlingPostConstants.RP_CHECKIN, "true");
    String importedNodeUrl = testClient.createNode(HTTP_BASE_URL + testPath, new NameValuePairList(props), null, true, testFile, SlingPostConstants.RP_CONTENT_FILE, null);
    // assert content at new location
    String content = getContent(importedNodeUrl + ".json", CONTENT_TYPE_JSON);
    JsonObject jsonObj = JsonUtil.parseObject(content);
    assertNotNull(jsonObj);
    //assert that the versionable node is checked in.
    assertFalse(jsonObj.getBoolean("jcr:isCheckedOut"));
    //now try with the checkin value set to false
    testFile.delete();
    props.clear();
    props.put(SlingPostConstants.RP_OPERATION, SlingPostConstants.OPERATION_IMPORT);
    String testNodeName2 = "testNode_" + String.valueOf(random.nextInt());
    props.put(SlingPostConstants.RP_NODE_NAME_HINT, testNodeName2);
    testFile = getTestFile(getClass().getResourceAsStream("/integration-test/servlets/post/testimport3.json"));
    props.put(SlingPostConstants.RP_CONTENT_TYPE, "json");
    props.put(SlingPostConstants.RP_REDIRECT_TO, SERVLET_CONTEXT + testPath + "/*");
    props.put(SlingPostConstants.RP_CHECKIN, "false");
    String importedNodeUrl2 = testClient.createNode(HTTP_BASE_URL + testPath, new NameValuePairList(props), null, true, testFile, SlingPostConstants.RP_CONTENT_FILE, null);
    // assert content at new location
    String content2 = getContent(importedNodeUrl2 + ".json", CONTENT_TYPE_JSON);
    JsonObject jsonObj2 = JsonUtil.parseObject(content2);
    assertNotNull(jsonObj2);
    //assert that the versionable node is checked in.
    assertTrue(jsonObj2.getBoolean("jcr:isCheckedOut"));
}
Also used : HashMap(java.util.HashMap) JsonObject(javax.json.JsonObject) NameValuePairList(org.apache.sling.commons.testing.integration.NameValuePairList)

Example 90 with JsonObject

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

the class PostServletMoveTest method testMoveNodeMultipleSourceReplace.

public void testMoveNodeMultipleSourceReplace() throws Exception {
    final String testPath = TEST_BASE_PATH + "/mvmult/" + System.currentTimeMillis();
    final String testRoot = testClient.createNode(HTTP_BASE_URL + testPath, null);
    // create multiple source nodes
    Map<String, String> props = new HashMap<String, String>();
    props.put("text", "Hello");
    testClient.createNode(HTTP_BASE_URL + testPath + "/src1", props);
    testClient.createNode(HTTP_BASE_URL + testPath + "/src2", props);
    // move the src? nodes
    List<NameValuePair> nvPairs = new ArrayList<NameValuePair>();
    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_PRECONDITION_FAILED, nvPairs, "Expecting Move Failure: dest parent does not exist");
    // create destination parent
    testClient.createNode(HTTP_BASE_URL + testPath + "/dest", null);
    // now dest exists, so we expect success
    assertPostStatus(testRoot, HttpServletResponse.SC_OK, nvPairs, "Expecting Move Success");
    // assert partial existence of the src?/text properties
    assertHttpStatus(HTTP_BASE_URL + testPath + "/dest/src1/text", HttpServletResponse.SC_OK);
    assertHttpStatus(HTTP_BASE_URL + testPath + "/dest/src2/text", HttpServletResponse.SC_OK);
    assertHttpStatus(HTTP_BASE_URL + testPath + "/dest/src3/text", HttpServletResponse.SC_NOT_FOUND);
    assertHttpStatus(HTTP_BASE_URL + testPath + "/dest/src4/text", HttpServletResponse.SC_NOT_FOUND);
    // assert content test
    String content = getContent(HTTP_BASE_URL + testPath + "/dest/src1.json", CONTENT_TYPE_JSON);
    JsonObject json = JsonUtil.parseObject(content);
    assertEquals("Hello", json.getString("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 = JsonUtil.parseObject(content2);
    assertEquals("Modified Hello", json2.getString("text"));
    // clean up
    testClient.delete(testRoot);
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject)

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