StringReaderWriter extends AbstractReaderWriter { @Override public void writeTo(InternalResponse<?> response, InternalRequest<?> request) { try { if (!response.getEntity().isPresent()) { logger.warn(() -> "No entity in the response to write to an output stream."); return; } Charset charset = request.getHeader(HeaderName.ACCEPT_CHARSET) .map(Charset::forName) .orElse(DEFAULT_CHARSET); java.io.Writer writer = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), charset)); writer.append((CharSequence) response.getEntity().get()); writer.flush(); } catch (UnsupportedEncodingException e) { throw internalServerErrorSupplier("Unsupported Encoding Exception").get(); } catch (IOException e) { throw internalServerErrorSupplier("IO Exception occurred during writing from String").get(); } }  @SuppressWarnings("unchecked") @Override T readFrom(InternalRequest<Object> request, Type<T> clazz); @Override void writeTo(InternalResponse<?> response, InternalRequest<?> request); @Override boolean isClassCompatible(Class<?> clazz); @Override MediaType getMediaType();  }
@Test public void write_to_blank() { RequestStub request = new RequestStub(); ResponseStub response = new ResponseStub(); response.setOutputStream(new ByteArrayOutputStream()); testedClass.writeTo(response, request); ByteArrayOutputStream outputStream = (ByteArrayOutputStream) response.getOutputStream(); Assert.assertEquals("", new String(outputStream.toByteArray())); }
@Test public void write_to_success() { String entity = "Well Done!"; RequestStub request = new RequestStub(); ResponseStub response = new ResponseStub(); response.entity(entity); response.setOutputStream(new ByteArrayOutputStream()); testedClass.writeTo(response, request); ByteArrayOutputStream outputStream = (ByteArrayOutputStream) response.getOutputStream(); Assert.assertEquals(entity, new String(outputStream.toByteArray())); }