### Question:
EntityXMLWriter implements MessageBodyWriter<EntityResponse> { @Override public void writeTo(EntityResponse entityResponse, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { try { writeEntity(entityResponse, entityStream); } catch (XMLStreamException e) { LOG.error("Could not write out to XML {}", e); throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR); } }  @Override boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override long getSize(EntityResponse entityResponse, Class<?> type, Type genericType,
                        Annotation[] annotations, MediaType mediaType); @Override void writeTo(EntityResponse entityResponse, Class<?> type, Type genericType, Annotation[] annotations,
                        MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream);  }### Answer:
@Test public void testEntityBody() throws IOException { EntityDefinition def = mock(EntityDefinition.class); when(entityDefinitionStore.lookupByEntityType(anyString())).thenReturn(def); ByteArrayOutputStream out = new ByteArrayOutputStream(); EntityBody body = new EntityBody(); body.put("id", "1234"); body.put("name", "test name"); EntityResponse response = new EntityResponse("TestEntity", body); writer.writeTo(response, null, null, null, null, null, out); assertNotNull("Should not be null", out); String value = new String(out.toByteArray()); assertTrue("Should match", value.indexOf("<TestEntity") > 0); assertTrue("Should match", value.indexOf("<id>") > 0); assertTrue("Should match", value.indexOf("<name>") > 0); }
@Test public void testEntityNullCollection() throws IOException { EntityDefinition def = mock(EntityDefinition.class); when(entityDefinitionStore.lookupByEntityType(anyString())).thenReturn(def); ByteArrayOutputStream out = new ByteArrayOutputStream(); EntityBody body = new EntityBody(); body.put("id", "1234"); body.put("name", "test name"); EntityResponse response = new EntityResponse(null, body); writer.writeTo(response, null, null, null, null, null, out); assertNotNull("Should not be null", out); String value = new String(out.toByteArray()); assertTrue("Should match", value.indexOf("<Entity") > 0); }
@Test public void testHandleLists() throws IOException { final EntityDefinition def = mock(EntityDefinition.class); when(entityDefinitionStore.lookupByEntityType(anyString())).thenReturn(def); final ByteArrayOutputStream out = new ByteArrayOutputStream(); final EntityBody body = new EntityBody(); final Map<String, Object> testMap = new HashMap<String, Object>(); testMap.put("k1", "v1"); testMap.put("k2", "v2"); body.put("mapKey", testMap); final List<String> list = new ArrayList<String>(); list.add("test1"); list.add("test2"); list.add("test3"); body.put("listItem", list); body.put("id", "1234"); body.put("name", "test name"); final EntityResponse response = new EntityResponse("TestEntity", Arrays.asList(body)); writer.writeTo(response, null, null, null, null, null, out); assertNotNull("Should not be null", out); String value = new String(out.toByteArray()); assertTrue("Should match", value.indexOf("<TestEntityList") > 0); assertTrue("Should match", value.indexOf("<id>") > 0); assertTrue("Should match", value.indexOf("<name>") > 0); assertEquals(3, StringUtils.countMatches(value, "<listItem")); assertEquals(1, StringUtils.countMatches(value, "<mapKey>")); }