StubConfigHandlerServlet extends HttpServlet { @Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { String sourceParameter = req.getParameter("groupname"); if (sourceParameter != null) { byte[] xml = getActualUsedJson(sourceParameter); if (xml != null) { setHeader(req, resp, sourceParameter); writeStubConfigToResponse(req, resp, xml); } else { writeErrorToResponse(resp); } } } @Autowired  StubConfigHandlerServlet(JsonBasedObjectTransformer jsonBasedObjectTransformer, StubResourceHolder stubResourceHolder, ByteArrayConverter byteArrayConverter);   }
@Test public void testDoGetShouldWriteActualJsonToResponse() throws ServletException, IOException, JsonTransformationException { given(request.getHeader("User-Agent")).willReturn("windows"); given(stubResourceHolder.getActualStubConfigJsonObject(DEFAULT_GROUPNAME)).willReturn(jsonObject); byte[] jsonAsByteArray = "DOCUMENT".getBytes(); given(jsonBasedObjectTransformer.transform(jsonObject)).willReturn(jsonAsByteArray); given(byteArrayConverter.toString(jsonAsByteArray)).willReturn(FILE_CONTENT); underTest.doGet(request, response); verify(writer).write("content \r\n newline"); verify(writer).flush(); verify(writer).close(); }
@Test public void testDoGetShouldWriteErrorResponseWhenActualJsonIsNull() throws ServletException, IOException { given(stubResourceHolder.getActualStubConfigJsonObject(DEFAULT_GROUPNAME)).willReturn(null); underTest.doGet(request, response); verify(writer).write(anyString()); }
@Test public void testDoGetShouldNotReplaceNewLineWhenNotOnWindows() throws ServletException, IOException, JsonTransformationException { given(request.getHeader("User-Agent")).willReturn("mac"); given(stubResourceHolder.getActualStubConfigJsonObject(DEFAULT_GROUPNAME)).willReturn(jsonObject); byte[] xmlAsByteArray = "DOCUMENT".getBytes(); given(jsonBasedObjectTransformer.transform(jsonObject)).willReturn(xmlAsByteArray); given(byteArrayConverter.toString(xmlAsByteArray)).willReturn(FILE_CONTENT); underTest.doGet(request, response); verify(writer).write(FILE_CONTENT); }
@Test public void testDoGetShouldNotReplaceNewLineWhenUserAgentIsNull() throws ServletException, IOException, JsonTransformationException { given(request.getHeader("User-Agent")).willReturn(null); given(stubResourceHolder.getActualStubConfigJsonObject(DEFAULT_GROUPNAME)).willReturn(jsonObject); byte[] xmlAsByteArray = "DOCUMENT".getBytes(); given(jsonBasedObjectTransformer.transform(jsonObject)).willReturn(xmlAsByteArray); given(byteArrayConverter.toString(xmlAsByteArray)).willReturn(FILE_CONTENT); underTest.doGet(request, response); verify(writer).write(FILE_CONTENT); }
@Test public void testDoGetShouldSetContentTypeToJsonWhenReqParamSourceIsTrue() throws ServletException, IOException, JsonTransformationException { given(request.getParameter("source")).willReturn("true"); given(request.getHeader("User-Agent")).willReturn(null); given(stubResourceHolder.getActualStubConfigJsonObject(DEFAULT_GROUPNAME)).willReturn(jsonObject); byte[] xmlAsByteArray = "DOCUMENT".getBytes(); given(jsonBasedObjectTransformer.transform(jsonObject)).willReturn(xmlAsByteArray); given(byteArrayConverter.toString(xmlAsByteArray)).willReturn(FILE_CONTENT); underTest.doGet(request, response); verify(response).setContentType("application/json"); }
@Test public void testDoGetShouldSetContentTypeToTextWhenReqParamSourceIsNotTrue() throws ServletException, IOException, JsonTransformationException { given(request.getParameter("source")).willReturn(""); given(request.getHeader("User-Agent")).willReturn(null); given(stubResourceHolder.getActualStubConfigJsonObject(DEFAULT_GROUPNAME)).willReturn(jsonObject); byte[] xmlAsByteArray = "DOCUMENT".getBytes(); given(jsonBasedObjectTransformer.transform(jsonObject)).willReturn(xmlAsByteArray); given(byteArrayConverter.toString(xmlAsByteArray)).willReturn(FILE_CONTENT); underTest.doGet(request, response); verify(response).setContentType("text/plain"); }
@Test public void testDoGetShouldSetContentTypeToTextWhenReqParamSourceIsNull() throws ServletException, IOException, JsonTransformationException { given(request.getParameter("source")).willReturn(null); given(request.getHeader("User-Agent")).willReturn(null); given(stubResourceHolder.getActualStubConfigJsonObject(DEFAULT_GROUPNAME)).willReturn(jsonObject); byte[] xmlAsByteArray = "DOCUMENT".getBytes(); given(jsonBasedObjectTransformer.transform(jsonObject)).willReturn(xmlAsByteArray); given(byteArrayConverter.toString(xmlAsByteArray)).willReturn(FILE_CONTENT); underTest.doGet(request, response); verify(response).setContentType("text/plain"); }