use of org.apache.sling.commons.json.JSONObject in project sling by apache.
the class NodeTypesJSONServlet method doGet.
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json; charset=UTF-8");
PrintWriter writer = response.getWriter();
Resource resource = request.getResource();
Node currentNode = resource.adaptTo(Node.class);
try {
NodeTypeIterator nodeTypeIterator = currentNode.getSession().getWorkspace().getNodeTypeManager().getAllNodeTypes();
JSONObject nodeTypes = new JSONObject();
while (nodeTypeIterator.hasNext()) {
NodeType nodeType = nodeTypeIterator.nextNodeType();
if (nodeType.getName() != null) {
JSONNodeType jsonNodeType = new JSONNodeType(nodeType);
nodeTypes.put(nodeType.getName(), jsonNodeType.getJson());
}
}
writer.println(nodeTypes.toString(2));
writer.flush();
writer.close();
} catch (RepositoryException e) {
log.error("Could not generate the node types.", e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} catch (JSONException e) {
log.error("Could not generate the node types.", e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
use of org.apache.sling.commons.json.JSONObject in project sling by apache.
the class MockNodeTypeGenerator method assertEqualsWithServletResult.
public void assertEqualsWithServletResult(String filename) throws JSONException, ServletException, IOException {
NodeTypesJSONServlet generationServlet = new NodeTypesJSONServlet();
generationServlet.service(request, response);
verify(response, never()).sendError(anyInt());
String resultingJSON = new String(outStream.toByteArray(), "utf-8");
String expectedNTJSON = getExpectedNTJSON(filename);
JSONObject actualJsonObjectFromServlet = new JSONObject(new JSONTokener(resultingJSON));
JSONAssert.assertEquals("Actual JSON: " + resultingJSON + "\nExpected JSON: " + expectedNTJSON, new JSONObject(new JSONTokener(expectedNTJSON)), actualJsonObjectFromServlet);
}
use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.
the class PackageHelperImplTest method testGetPathFilterSetPreviewJSON.
@Test
public void testGetPathFilterSetPreviewJSON() throws Exception {
final List<PathFilterSet> pathFilterSets = new ArrayList<PathFilterSet>();
pathFilterSets.add(new PathFilterSet("/a/b/c"));
pathFilterSets.add(new PathFilterSet("/d/e/f"));
pathFilterSets.add(new PathFilterSet("/g/h/i"));
final String actual = packageHelper.getPathFilterSetPreviewJSON(pathFilterSets);
final JSONObject json = new JSONObject(actual);
assertEquals("preview", json.getString("status"));
assertEquals("Not applicable (Preview)", json.getString("path"));
final String[] expectedFilterSets = new String[] { "/a/b/c", "/d/e/f", "/g/h/i" };
JSONArray actualArray = json.getJSONArray("filterSets");
for (int i = 0; i < actualArray.length(); i++) {
JSONObject tmp = actualArray.getJSONObject(i);
assertTrue(ArrayUtils.contains(expectedFilterSets, tmp.get("rootPath")));
}
assertEquals(expectedFilterSets.length, actualArray.length());
}
use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.
the class PackageHelperImplTest method testGetErrorJSON.
@Test
public void testGetErrorJSON() throws Exception {
final String expectedMessage = "This is a test error message!";
final String actual = packageHelper.getErrorJSON(expectedMessage);
final JSONObject json = new JSONObject(actual);
assertEquals("error", json.getString("status"));
assertEquals(expectedMessage, json.getString("msg"));
}
use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.
the class PackageHelperImplTest method testGetSuccessJSON.
@Test
public void testGetSuccessJSON() throws Exception {
final String actual = packageHelper.getSuccessJSON(packageOne);
final JSONObject json = new JSONObject(actual);
assertEquals("success", json.getString("status"));
assertEquals("/etc/packages/testGroup/testPackageName-1.0.0.zip", json.getString("path"));
final String[] expectedFilterSets = new String[] { "/a/b/c", "/d/e/f", "/g/h/i" };
JSONArray actualArray = json.getJSONArray("filterSets");
for (int i = 0; i < actualArray.length(); i++) {
JSONObject tmp = actualArray.getJSONObject(i);
assertTrue(ArrayUtils.contains(expectedFilterSets, tmp.get("rootPath")));
}
assertEquals(expectedFilterSets.length, actualArray.length());
}
Aggregations