@Test public void testSerialize() throws Exception { UriInfo uriInfo = createMock(UriInfo.class); Resource resource = createMock(Resource.class); Result result = new ResultImpl(true); result.setResultStatus(new ResultStatus(ResultStatus.STATUS.OK)); TreeNode<Resource> tree = result.getResultTree(); TreeNode<Resource> child = tree.addChild(resource, "resource1"); child.setProperty("href", "this is an href"); Map<String, Object> mapRootProps = new LinkedHashMap<>(); mapRootProps.put("prop2", "value2"); mapRootProps.put("prop1", "value1"); Map<String, Object> mapCategoryProps = new LinkedHashMap<String, Object>(); mapCategoryProps.put("catProp1", "catValue1"); mapCategoryProps.put("catProp2", "catValue2"); Map<String, Map<String, Object>> propertyMap = new LinkedHashMap<String, Map<String, Object>>(); propertyMap.put(null, mapRootProps); propertyMap.put("category", mapCategoryProps); expect(resource.getPropertiesMap()).andReturn(propertyMap).anyTimes(); expect(resource.getType()).andReturn(Resource.Type.Cluster).anyTimes(); replay(uriInfo, resource); Object o = new JsonSerializer().serialize(result).toString().replace("\r", ""); String expected = "{\n" + " \"href\" : \"this is an href\",\n" + " \"prop2\" : \"value2\",\n" + " \"prop1\" : \"value1\",\n" + " \"category\" : {\n" + " \"catProp1\" : \"catValue1\",\n" + " \"catProp2\" : \"catValue2\"\n" + " }\n" + "}"; assertEquals(expected, o); verify(uriInfo, resource); }
@Override public Object serialize(Result result) { try { ByteArrayOutputStream bytesOut = init(); if (result.getStatus().isErrorState()) { return serializeError(result.getStatus()); } TreeNode<Resource> treeNode = result.getResultTree(); processNode(treeNode); processResultMetadata(result.getResultMetadata()); m_generator.close(); return bytesOut.toString("UTF-8"); } catch (IOException e) { throw new RuntimeException("Unable to serialize to json: " + e, e); } }
JsonSerializer implements ResultSerializer { @Override public Object serialize(Result result) { try { ByteArrayOutputStream bytesOut = init(); if (result.getStatus().isErrorState()) { return serializeError(result.getStatus()); } TreeNode<Resource> treeNode = result.getResultTree(); processNode(treeNode); processResultMetadata(result.getResultMetadata()); m_generator.close(); return bytesOut.toString("UTF-8"); } catch (IOException e) { throw new RuntimeException("Unable to serialize to json: " + e, e); } } }
JsonSerializer implements ResultSerializer { @Override public Object serialize(Result result) { try { ByteArrayOutputStream bytesOut = init(); if (result.getStatus().isErrorState()) { return serializeError(result.getStatus()); } TreeNode<Resource> treeNode = result.getResultTree(); processNode(treeNode); processResultMetadata(result.getResultMetadata()); m_generator.close(); return bytesOut.toString("UTF-8"); } catch (IOException e) { throw new RuntimeException("Unable to serialize to json: " + e, e); } }  }
JsonSerializer implements ResultSerializer { @Override public Object serialize(Result result) { try { ByteArrayOutputStream bytesOut = init(); if (result.getStatus().isErrorState()) { return serializeError(result.getStatus()); } TreeNode<Resource> treeNode = result.getResultTree(); processNode(treeNode); processResultMetadata(result.getResultMetadata()); m_generator.close(); return bytesOut.toString("UTF-8"); } catch (IOException e) { throw new RuntimeException("Unable to serialize to json: " + e, e); } }  @Override Object serialize(Result result); @Override Object serializeError(ResultStatus error); }
JsonSerializer implements ResultSerializer { @Override public Object serialize(Result result) { try { ByteArrayOutputStream bytesOut = init(); if (result.getStatus().isErrorState()) { return serializeError(result.getStatus()); } TreeNode<Resource> treeNode = result.getResultTree(); processNode(treeNode); processResultMetadata(result.getResultMetadata()); m_generator.close(); return bytesOut.toString("UTF-8"); } catch (IOException e) { throw new RuntimeException("Unable to serialize to json: " + e, e); } }  @Override Object serialize(Result result); @Override Object serializeError(ResultStatus error);  }