JsonSerializer { public String serialize(Result result, UriInfo ui) { Writer json = new StringWriter(); JsonWriter writer = new JsonWriter(json); writer.setIndent(" "); try { writeValue(writer, result.getPropertyMaps(), ui.getBaseUri().toASCIIString()); } catch (IOException e) { throw new CatalogRuntimeException("Unable to write JSON response.", e); } return json.toString(); }  String serialize(Result result, UriInfo ui);  }
@Test public void testSerialize() throws Exception { UriInfo uriInfo = createStrictMock(UriInfo.class); URI uri = new URI("http: expect(uriInfo.getBaseUri()).andReturn(uri); replay(uriInfo); Collection<Map<String, Object>> resultMaps = new ArrayList<>(); ResourceComparator resourceComparator = new ResourceComparator(); Map<String, Object> resultMap1 = new TreeMap<>(resourceComparator); resultMaps.add(resultMap1); resultMap1.put("prop1", "property 1 value"); resultMap1.put("booleanProp", true); resultMap1.put("numberProp", 100); resultMap1.put("href", "v1/testResources/foo"); ArrayList<String> listProp = new ArrayList<>(); listProp.add("one"); listProp.add("two"); resultMap1.put("listProp", listProp); Map<String, Object> mapProp = new TreeMap<>(resourceComparator); mapProp.put("mapProp1", "mapProp1Value"); ArrayList<String> mapListProp = new ArrayList<>(); mapListProp.add("mapListOne"); mapListProp.add("mapListTwo"); mapProp.put("mapListProp", mapListProp); mapProp.put("href", "v1/testResources/foobar"); resultMap1.put("mapProp", mapProp); Map<String, Object> resultMap2 = new TreeMap<>(resourceComparator); resultMaps.add(resultMap2); resultMap2.put("nullProp", null); resultMap2.put("href", "v1/testResources/bar"); ArrayList<Map<String, Object>> listProp2 = new ArrayList<>(); listProp2.add(Collections.<String, Object>singletonMap("listMapProp", "listMapPropValue")); resultMap2.put("listProp", listProp2); Result result = new Result(resultMaps); JsonSerializer serializer = new JsonSerializer(); String resultJson = serializer.serialize(result, uriInfo); assertEquals(resultJson, EXPECTED_JSON); }