Search in sources :

Example 81 with JsonObject

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

the class CreateGroupTest method testCreateGroupResponseAsJSON.

/**
	 * Test for SLING-1677
	 */
public void testCreateGroupResponseAsJSON() throws IOException, JsonException {
    String postUrl = HTTP_BASE_URL + "/system/userManager/group.create.json";
    testGroupId = "testGroup" + random.nextInt();
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair(":name", testGroupId));
    postParams.add(new NameValuePair("marker", testGroupId));
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    String json = getAuthenticatedPostContent(creds, postUrl, CONTENT_TYPE_JSON, postParams, HttpServletResponse.SC_OK);
    //make sure the json response can be parsed as a JSON object
    JsonObject jsonObj = JsonUtil.parseObject(json);
    assertNotNull(jsonObj);
}
Also used : 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 82 with JsonObject

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

the class PostServletUpdateTest method testMixinTypes.

public void testMixinTypes() throws IOException, JsonException {
    // create a node without mixin node types
    final Map<String, String> props = new HashMap<String, String>();
    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 = JsonUtil.parseObject(content);
    assertFalse("jcr:mixinTypes not expected to be set", json.containsKey("jcr:mixinTypes"));
    // add mixin
    props.clear();
    props.put("jcr:mixinTypes", "mix:versionable");
    testClient.createNode(location, props);
    content = getContent(location + ".json", CONTENT_TYPE_JSON);
    json = JsonUtil.parseObject(content);
    assertTrue("jcr:mixinTypes expected after setting them", json.containsKey("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.size() == 1);
    assertEquals("jcr:mixinTypes must have correct value", "mix:versionable", mix.getString(0));
    // remove mixin
    props.clear();
    props.put("jcr:mixinTypes@Delete", "-");
    testClient.createNode(location, props);
    content = getContent(location + ".json", CONTENT_TYPE_JSON);
    json = JsonUtil.parseObject(content);
    final boolean noMixins = !json.containsKey("jcr:mixinTypes") || json.getJsonArray("jcr:mixinTypes").size() == 0;
    assertTrue("no jcr:mixinTypes expected after clearing it", noMixins);
}
Also used : JsonArray(javax.json.JsonArray) HashMap(java.util.HashMap) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject)

Example 83 with JsonObject

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

the class RemoveAuthorizablesTest method testRemoveAuthorizablesResponseAsJSON.

/**
	 * Test for SLING-1677
	 */
public void testRemoveAuthorizablesResponseAsJSON() throws IOException, JsonException {
    String userId = createTestUser();
    String groupId = createTestGroup();
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    String postUrl = HTTP_BASE_URL + "/system/userManager.delete.json";
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair(":applyTo", "group/" + groupId));
    postParams.add(new NameValuePair(":applyTo", "user/" + userId));
    String json = getAuthenticatedPostContent(creds, postUrl, CONTENT_TYPE_JSON, postParams, HttpServletResponse.SC_OK);
    //make sure the json response can be parsed as a JSON object
    JsonObject jsonObj = JsonUtil.parseObject(json);
    assertNotNull(jsonObj);
}
Also used : 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 84 with JsonObject

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

the class InstallManyBundlesTest method assertActiveBundle.

private void assertActiveBundle(String bsn, String expectedState, String expectedVersion) throws IOException, JsonException {
    final String consoleUrl = HttpTest.HTTP_BASE_URL + "/system/console/bundles/" + bsn + ".json";
    String state = null;
    String version = null;
    final long timeoutMsec = TimeoutsProvider.getInstance().getTimeout(WAIT_ACTIVE_TIMEOUT_MSEC);
    final long endTime = System.currentTimeMillis() + timeoutMsec;
    while (System.currentTimeMillis() < endTime) {
        try {
            final String jsonStr = H.getContent(consoleUrl, HttpTest.CONTENT_TYPE_JSON);
            final JsonObject json = JsonUtil.parseObject(jsonStr);
            state = json.getJsonArray("data").getJsonObject(0).getString("state");
            version = json.getJsonArray("data").getJsonObject(0).getString("version");
            if (expectedState.equalsIgnoreCase(state) && expectedVersion.equalsIgnoreCase(version)) {
                return;
            }
            Thread.sleep(100);
        } catch (AssertionFailedError dontCare) {
        // Thrown by getContent - might happen before the
        // bundle is installed
        } catch (InterruptedException ignore) {
        }
    }
    fail("Did not get state=" + expectedState + " and version=" + expectedVersion + " within " + timeoutMsec + " msec" + ", got " + state + " / " + version);
}
Also used : JsonObject(javax.json.JsonObject) AssertionFailedError(junit.framework.AssertionFailedError)

Example 85 with JsonObject

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

the class PostServletImportTest method testImportJSONWithUTF8Content.

/**
     * SLING-2143: test import where json is in a UTF-8 charset
     */
public void testImportJSONWithUTF8Content() 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);
    final String jsonContent = getStreamAsString(getClass().getResourceAsStream("/integration-test/servlets/post/testimport_utf8.json"), "UTF-8");
    props.put(SlingPostConstants.RP_CONTENT, jsonContent);
    props.put(SlingPostConstants.RP_CONTENT_TYPE, "json");
    props.put(SlingPostConstants.RP_REDIRECT_TO, SERVLET_CONTEXT + testPath + "/*");
    String importedNodeUrl = testClient.createNode(HTTP_BASE_URL + testPath, props);
    // assert content at new location
    String content = getContent(importedNodeUrl + ".json", CONTENT_TYPE_JSON);
    JsonObject jsonObj = JsonUtil.parseObject(content);
    assertNotNull(jsonObj);
    //assert the imported content is there.
    assertExpectedJSON(JsonUtil.parseObject(jsonContent), jsonObj);
}
Also used : HashMap(java.util.HashMap) 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