StubResourceHandlerServlet extends HttpServlet { @Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { String type = req.getParameter("type"); if (type != null) { Path path = getResourcePath(type); if (path != null) { writeResponse(req, resp, path); } else { writeError(resp, String.format(ERROR_MESSAGE_TEMPLATE, type, INTERCEPTOR, CONDITION_CHECKER, RESPONSE_FORMATTER, TEMPLATE, JAR, SEQUENCE_HANDLER)); } } else { writeError(resp, NO_TYPE_ERR_MSG); } } @Autowired  StubResourceHandlerServlet(FileListJsonBuilder fileListJsonBuilder, StubResourcePathProvider stubResourcePathProvider, ResourceFileNameHandler resourceFileNameHandler);   }
@Test public void testDoGetWhenThereIsNoTypeParameterShouldWriteError() throws ServletException, IOException { given(request.getParameter(TYPE_PARAMETER)).willReturn(null); underTest.doGet(request, response); verify(writer).write("Please provide a type parameter in the url!"); }
@Test public void testDoGetWhenTypeIsInterceptorShouldWriteResourceToResponse() throws ServletException, IOException { String expectedResult = "{\"files\":[\"Interceptor.class\"]}"; given(request.getParameter(TYPE_PARAMETER)).willReturn(INTERCEPTOR); given(stubResourcePathProvider.getInterceptorPath()).willReturn(path); given(path.toFile()).willReturn(file); given(fileListJsonBuilder.buildFileListJson(file)).willReturn(expectedResult); underTest.doGet(request, response); verify(writer).write(expectedResult); verify(response).setContentType(APPLICATION_JSON); }
@Test public void testDoGetWhenTypeIsConditionCheckerShouldWriteResourceToResponse() throws ServletException, IOException { String expectedResult = "{\"files\":[\"ConditionChecker.class\"]}"; given(request.getParameter(TYPE_PARAMETER)).willReturn(CONDITION_CHECKER); given(stubResourcePathProvider.getConditionCheckerPath()).willReturn(path); given(path.toFile()).willReturn(file); given(fileListJsonBuilder.buildFileListJson(file)).willReturn(expectedResult); underTest.doGet(request, response); verify(writer).write(expectedResult); verify(response).setContentType(APPLICATION_JSON); }
@Test public void testDoGetWhenTypeIsResponseFormatterShouldWriteResourceToResponse() throws ServletException, IOException { String expectedResult = "{\"files\":[\"ResponseFormatter.class\"]}"; given(request.getParameter(TYPE_PARAMETER)).willReturn(RESPONSE_FORMATTER); given(stubResourcePathProvider.getResponseFormatterPath()).willReturn(path); given(path.toFile()).willReturn(file); given(fileListJsonBuilder.buildFileListJson(file)).willReturn(expectedResult); underTest.doGet(request, response); verify(writer).write(expectedResult); }
@Test public void testDoGetWhenTypeIsTemplateShouldWriteResourceToResponse() throws ServletException, IOException { String expectedResult = "{\"files\":[\"template.xml\"]}"; given(request.getParameter(TYPE_PARAMETER)).willReturn(TEMPLATE); given(stubResourcePathProvider.getTemplatesPath()).willReturn(path); given(path.toFile()).willReturn(file); given(fileListJsonBuilder.buildFileListJson(file)).willReturn(expectedResult); underTest.doGet(request, response); verify(writer).write(expectedResult); }
@Test public void testDoGetWhenTypeIsJarShouldWriteResourceToResponse() throws ServletException, IOException { String expectedResult = "{\"files\":[\"something.jar\"]}"; given(request.getParameter(TYPE_PARAMETER)).willReturn(JAR); given(stubResourcePathProvider.getJarPath()).willReturn(path); given(path.toFile()).willReturn(file); given(fileListJsonBuilder.buildFileListJson(file)).willReturn(expectedResult); underTest.doGet(request, response); verify(writer).write(expectedResult); }
@Test public void testDoGetWhenTypeIsSequenceHandlerShouldWriteResourceToResponse() throws ServletException, IOException { String expectedResult = "{\"files\":[\"SequenceHandler.class\"]}"; given(request.getParameter(TYPE_PARAMETER)).willReturn(SEQUENCE_HANDLER); given(stubResourcePathProvider.getSequenceHandlerPath()).willReturn(path); given(path.toFile()).willReturn(file); given(fileListJsonBuilder.buildFileListJson(file)).willReturn(expectedResult); underTest.doGet(request, response); verify(writer).write(expectedResult); }
@Test public void testDoGetWhenTypeIsSomethingElseShouldWriteResourceToResponse() throws ServletException, IOException { String type = "templ"; given(request.getParameter(TYPE_PARAMETER)).willReturn(type); underTest.doGet(request, response); verify(writer).write( String.format(ERROR_MESSAGE_TEMPLATE, type, INTERCEPTOR, CONDITION_CHECKER, RESPONSE_FORMATTER, TEMPLATE, JAR, SEQUENCE_HANDLER)); }
@Test public void testDoGetWhenRequestPathInfoIsNullShouldCallHandleRequest() throws ServletException, IOException { given(request.getParameter(TYPE_PARAMETER)).willReturn(TEMPLATE); given(stubResourcePathProvider.getTemplatesPath()).willReturn(path); given(request.getPathInfo()).willReturn(PATH_INFO); underTest.doGet(request, response); verify(resourceFileNameHandler).writeResourceFile(response, PATH_INFO, path); }