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();
}
}
}
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();
}
}
}
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();
}
}
}
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"));
}
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"));
}
Aggregations