ServiceServlet extends HttpServlet { @Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { String requestUri = req.getRequestURI(); logger.info("Service call to: " + requestUri + ", method: " + req.getMethod()); int positionOfLeadingText = requestUri.indexOf(LEADING_TEXT); int requestedServicePosition = positionOfLeadingText + LEADING_TEXT.length(); String requestedService = positionOfLeadingText >= 0 ? requestUri.substring(requestedServicePosition) : ""; resp.setContentType("application/json"); resp.setStatus(HttpServletResponse.SC_NOT_FOUND); String response = null; if ("UniqueIdGenerator/uniqueId".equalsIgnoreCase(requestedService) && "get".equalsIgnoreCase(req.getMethod())) { response = getUniqueId(); resp.setStatus(HttpServletResponse.SC_OK); } if (response == null) { response = serviceMap.callExternalService(req, requestedService, resp); } if (response == null) { if (requestedService.length() > 0) { response = "{ \"unknownServiceCall\": \"" + req.getMethod() + " " + requestedService + "\" }"; } else { response = serviceMap.getMapAsResponse(); resp.setStatus(HttpServletResponse.SC_OK); } } PrintWriter out = resp.getWriter(); out.write(response); out.flush(); out.close(); } @Autowired  ServiceServlet(ServiceMap serviceMap);   }
@Test public void testDoGetShouldReturnWithDefaultIfEmptyServiceGet() throws ServletException, IOException { given(resp.getWriter()).willReturn(printWriter); given(req.getRequestURI()).willReturn("/public/service"); given(req.getMethod()).willReturn("get"); given(serviceMap.getMapAsResponse()).willReturn("blah"); underTest.doGet(req, resp); verify(resp).setContentType("application/json"); verify(resp).setStatus(HttpServletResponse.SC_NOT_FOUND); String response = "blah"; verify(printWriter).write(response); verify(printWriter).flush(); verify(printWriter).close(); }
@Test public void testDoGetShouldReturnWithServiceListForEmptyServicePost() throws ServletException, IOException { given(resp.getWriter()).willReturn(printWriter); given(req.getRequestURI()).willReturn("/public/services/"); given(req.getMethod()).willReturn("post"); given(serviceMap.getMapAsResponse()).willReturn("blah"); underTest.doGet(req, resp); verify(resp).setContentType("application/json"); verify(resp).setStatus(HttpServletResponse.SC_NOT_FOUND); String response = "blah"; verify(printWriter).write(response); verify(printWriter).flush(); verify(printWriter).close(); }
@Test public void testDoGetShouldReturnWithServiceListForEmptyServiceGet() throws ServletException, IOException { given(resp.getWriter()).willReturn(printWriter); given(req.getRequestURI()).willReturn("/public/services/"); given(req.getMethod()).willReturn("get"); given(serviceMap.getMapAsResponse()).willReturn("blah"); underTest.doGet(req, resp); verify(resp).setContentType("application/json"); verify(resp).setStatus(HttpServletResponse.SC_NOT_FOUND); String response = "blah"; verify(printWriter).write(response); verify(printWriter).flush(); verify(printWriter).close(); }
@Test public void testDoGetShouldReturnWithDefaultIfUnknownService() throws ServletException, IOException { given(resp.getWriter()).willReturn(printWriter); given(req.getRequestURI()).willReturn("/public/services/unknown"); given(req.getMethod()).willReturn("get"); underTest.doGet(req, resp); verify(resp).setContentType("application/json"); verify(resp).setStatus(HttpServletResponse.SC_NOT_FOUND); String response = "{ \"unknownServiceCall\": \"get unknown\" }"; verify(printWriter).write(response); verify(printWriter).flush(); verify(printWriter).close(); }
@Test public void testDoGetShouldReturnWithWilmaUniqueId() throws ServletException, IOException { given(resp.getWriter()).willReturn(printWriter); given(req.getRequestURI()).willReturn("/public/services/UniqueIdGenerator/uniqueId"); given(req.getMethod()).willReturn("get"); underTest.doGet(req, resp); verify(resp).setContentType("application/json"); verify(resp).setStatus(HttpServletResponse.SC_OK); }