Search in sources :

Example 31 with JsonObject

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

the class ExporterTest method testDoubledServlets.

@Test
public void testDoubledServlets() throws Exception {
    ResourceResolver resolver = null;
    try {
        resolver = rrFactory.getAdministrativeResourceResolver(null);
        FakeResponse response = new FakeResponse();
        slingRequestProcessor.processRequest(new FakeRequest(doubledComponentPath + ".firstmodel.json"), response, resolver);
        JsonObject obj = Json.createReader(new StringReader((response.getStringWriter().toString()))).readObject();
        Assert.assertEquals("application/json", response.getContentType());
        Assert.assertEquals("first", obj.getString("value"));
        response = new FakeResponse();
        slingRequestProcessor.processRequest(new FakeRequest(doubledComponentPath + ".secondmodel.json"), response, resolver);
        obj = Json.createReader(new StringReader((response.getStringWriter().toString()))).readObject();
        Assert.assertEquals("application/json", response.getContentType());
        Assert.assertEquals("second", obj.getString("value"));
    } finally {
        if (resolver != null && resolver.isLive()) {
            resolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 32 with JsonObject

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

the class ExporterTest method testExportToJSON.

@Test
public void testExportToJSON() throws Exception {
    ResourceResolver resolver = null;
    try {
        resolver = rrFactory.getAdministrativeResourceResolver(null);
        final Resource baseComponentResource = resolver.getResource(baseComponentPath);
        Assert.assertNotNull(baseComponentResource);
        String jsonData = modelFactory.exportModelForResource(baseComponentResource, "jackson", String.class, Collections.<String, String>emptyMap());
        Assert.assertTrue("JSON Data should contain the property value", StringUtils.contains(jsonData, "baseTESTValue"));
        JsonObject parsed = Json.createReader(new StringReader(jsonData)).readObject();
        JsonObject resource = parsed.getJsonObject("resource");
        Assert.assertEquals(3, resource.getJsonArray("sampleArray").size());
        Assert.assertEquals(1.0d, resource.getJsonNumber("sampleDoubleValue").doubleValue(), .1);
        Assert.assertEquals(2, resource.getJsonArray(":sampleBinaryArray").size());
        Assert.assertTrue(resource.getBoolean("sampleBooleanValue"));
        Assert.assertEquals(1, resource.getInt("sampleLongValue"));
        Assert.assertEquals(3, resource.getInt(":sampleBinary"));
        Assert.assertEquals(0, resource.getJsonArray("sampleEmptyArray").size());
        final Resource extendedComponentResource = resolver.getResource(extendedComponentPath);
        Assert.assertNotNull(extendedComponentResource);
        jsonData = modelFactory.exportModelForResource(extendedComponentResource, "jackson", String.class, Collections.<String, String>emptyMap());
        Assert.assertTrue("JSON Data should contain the property value", StringUtils.contains(jsonData, "extendedTESTValue"));
        final Resource interfaceComponentResource = resolver.getResource(interfaceComponentPath);
        Assert.assertNotNull(baseComponentResource);
        jsonData = modelFactory.exportModelForResource(interfaceComponentResource, "jackson", String.class, Collections.<String, String>emptyMap());
        Assert.assertTrue("JSON Data should contain the property value", StringUtils.contains(jsonData, "interfaceTESTValue"));
    } finally {
        if (resolver != null && resolver.isLive()) {
            resolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 33 with JsonObject

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

the class ExporterTest method testResourceServlets.

@Test
public void testResourceServlets() throws Exception {
    ResourceResolver resolver = null;
    try {
        resolver = rrFactory.getAdministrativeResourceResolver(null);
        FakeResponse response = new FakeResponse();
        slingRequestProcessor.processRequest(new FakeRequest(baseComponentPath + ".model.json"), response, resolver);
        JsonObject obj = Json.createReader(new StringReader((response.getStringWriter().toString()))).readObject();
        Assert.assertEquals("application/json", response.getContentType());
        Assert.assertEquals("BASETESTVALUE", obj.getString("UPPER"));
        Assert.assertEquals(baseComponentPath, obj.getString("id"));
        response = new FakeResponse();
        slingRequestProcessor.processRequest(new FakeRequest(extendedComponentPath + ".model.json"), response, resolver);
        obj = Json.createReader(new StringReader((response.getStringWriter().toString()))).readObject();
        Assert.assertEquals("application/json", response.getContentType());
        Assert.assertEquals(extendedComponentPath, obj.getString("id"));
        Assert.assertEquals(testDate.getTimeInMillis(), obj.getJsonNumber("date").longValue());
        response = new FakeResponse();
        slingRequestProcessor.processRequest(new FakeRequest(interfaceComponentPath + ".model.json"), response, resolver);
        obj = Json.createReader(new StringReader((response.getStringWriter().toString()))).readObject();
        Assert.assertEquals("application/json", response.getContentType());
        Assert.assertEquals(interfaceComponentPath, obj.getString("id"));
        Assert.assertEquals("interfaceTESTValue", obj.getString("sampleValue"));
    } finally {
        if (resolver != null && resolver.isLive()) {
            resolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 34 with JsonObject

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

the class ValidationServiceIT method testValidRequestModel1.

@Test
public void testValidRequestModel1() throws IOException, JsonException {
    final String url = String.format("http://localhost:%s", httpPort());
    final RequestBuilder requestBuilder = new RequestBuilder(url);
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("sling:resourceType", new StringBody("validation/test/resourceType1"));
    entity.addPart("field1", new StringBody("HELLOWORLD"));
    entity.addPart("field2", new StringBody("30.01.1988"));
    entity.addPart(SlingPostConstants.RP_OPERATION, new StringBody("validation"));
    RequestExecutor re = requestExecutor.execute(requestBuilder.buildPostRequest("/validation/testing/fakeFolder1/resource").withEntity(entity)).assertStatus(200);
    String content = re.getContent();
    JsonObject jsonResponse = Json.createReader(new StringReader(content)).readObject();
    assertTrue(jsonResponse.getBoolean("valid"));
}
Also used : RequestBuilder(org.apache.sling.testing.tools.http.RequestBuilder) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 35 with JsonObject

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

the class ValidationServiceIT method testInvalidRequestModel1.

@Test
public void testInvalidRequestModel1() throws IOException, JsonException {
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("sling:resourceType", new StringBody("validation/test/resourceType1"));
    entity.addPart("field1", new StringBody("Hello World"));
    entity.addPart(SlingPostConstants.RP_OPERATION, new StringBody("validation"));
    final String url = String.format("http://localhost:%s", httpPort());
    RequestBuilder requestBuilder = new RequestBuilder(url);
    RequestExecutor re = requestExecutor.execute(requestBuilder.buildPostRequest("/validation/testing/fakeFolder1/resource").withEntity(entity)).assertStatus(200);
    String content = re.getContent();
    JsonObject jsonResponse = Json.createReader(new StringReader(content)).readObject();
    assertFalse(jsonResponse.getBoolean("valid"));
    JsonObject failure = jsonResponse.getJsonArray("failures").getJsonObject(0);
    assertEquals("Property does not match the pattern \"^\\p{Upper}+$\".", failure.getString("message"));
    assertEquals("field1", failure.getString("location"));
    assertEquals(10, failure.getInt("severity"));
    failure = jsonResponse.getJsonArray("failures").getJsonObject(1);
    assertEquals("Missing required property with name \"field2\".", failure.getString("message"));
    // location is empty as the property is not found (property name is part of the message rather)
    assertEquals("", failure.getString("location"));
    assertEquals(0, failure.getInt("severity"));
}
Also used : RequestBuilder(org.apache.sling.testing.tools.http.RequestBuilder) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) 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