### Question:
TemplatedHttpHandler extends HttpResource { @Override public FullHttpResponse respond(FullHttpRequest req, ChannelHandlerContext ctx) throws Exception { HttpResponseStatus responseStatus = HttpResponseStatus.OK; Optional<String> response = Optional.empty(); try { TemplatedResponse handlerResponse = doHandle(req, ctx); if (handlerResponse.sectionId.isPresent()) { Map<String, String> multiResponse = templateService.renderMultipart(handlerResponse.templateId.get(), handlerResponse.context); response = Optional.of(multiResponse.get(handlerResponse.sectionId.get())); } else if (handlerResponse.templateId.isPresent()){ response = Optional.of(templateService.render(handlerResponse.templateId.get(), handlerResponse.context)); } if(!response.isPresent()){ responseStatus=HttpResponseStatus.NO_CONTENT; } if(handlerResponse.responseStatus.isPresent()){ responseStatus=handlerResponse.responseStatus.get(); } } catch (Throwable th) { LOGGER.error("Error responding to TemplatedRequestHandler", th); responseStatus = HttpResponseStatus.INTERNAL_SERVER_ERROR; } FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, responseStatus, Unpooled.copiedBuffer(response.orElse("").getBytes())); httpResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE, getContentType()); return httpResponse; } TemplatedHttpHandler(AlwaysAllow alwaysAllow, HttpSender httpSender, TemplateService templateService); @Override FullHttpResponse respond(FullHttpRequest req, ChannelHandlerContext ctx); abstract TemplatedResponse doHandle(FullHttpRequest request, ChannelHandlerContext ctx); abstract String getContentType();  }### Answer:
@Test public void shouldRespondSuccessSingleTemplate() throws Exception { EasyMock.expect(templateService.render("test.template","Context")).andReturn(""); replay(); FullHttpResponse response = handler.respond(fullHttpRequest,ctx); verify(); assertEquals(HttpResponseStatus.OK, response.getStatus()); assertEquals("application/xml", response.headers().get(HttpHeaders.Names.CONTENT_TYPE)); }
@Test public void shouldRespondReturnError() throws Exception { EasyMock.expect(templateService.render("test.template","Context")).andThrow(new RuntimeException("trouble")); replay(); FullHttpResponse response = handler.respond(fullHttpRequest,ctx); verify(); assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR, response.getStatus()); }
@Test public void shouldRespondWithResponseCode() throws Exception { handler = new TemplatedHttpHandler(new AlwaysAllow(),httpSender,templateService) { public String getContentType() { return "application/xml"; } public TemplatedResponse doHandle(FullHttpRequest request, ChannelHandlerContext ctx) { return new TemplatedResponse().withResponseStatus(HttpResponseStatus.NOT_FOUND); } }; replay(); FullHttpResponse response = handler.respond(fullHttpRequest,ctx); verify(); assertEquals(HttpResponseStatus.NOT_FOUND, response.getStatus()); }