WebDispatch { public WebResponse dispatch(WebRequest request) { try { for (Entry<WebPath, Handler> handlerEntry : pathToHandler.entrySet()) { WebPath webPath = handlerEntry.getKey(); MatchResult match = webPath.match(request.getHttpMethod(), request.getPath()); if (match.matches()) { return invoker.invoke(request, match, handlerEntry.getValue()); } } return notFound(request); } catch (Exception e) { logger.log(Level.SEVERE, format("Unexpected exception: {0}", e.getMessage()), e); return invoker.toWebResponse(e); } } @Inject  WebDispatch(Injector injector, Invoker invoker, Logger logger); WebResponse dispatch(WebRequest request);  }
@Test public void dispatch() { WebResponse response = dispatch.dispatch(new WebRequest("GET", "/abouts/current", null)); assertNotNull(response); assertEquals(200, response.getResponseCode()); int currentYear = LocalDate.now().get(ChronoField.YEAR); assertEquals("{\"applicationName\":\"" + GreenBeans.APP_NAME + "\",\"copyrightNotice\":\"Copyright (c) 2015, " + currentYear + " David Green. All rights reserved.\"}", response.getEntity()); }
@Test public void dispatchNotFound() { WebResponse response = dispatch.dispatch(new WebRequest("GET", "/not-present", null)); assertNotNull(response); assertEquals(404, response.getResponseCode()); assertEquals("{\"errorCode\":\"NotFoundException\",\"message\":\"Not found: GET /not-present\"}", response.getEntity()); }
@Test public void dispatchRootPath() { WebResponse response = dispatch .dispatch(new WebRequest("POST", "/imports", "{files:['one','two'], deleteAfterImport:true}")); assertNotNull(response); assertEquals(204, response.getResponseCode()); }
@Test public void dispatchToEncryptionSettingsWebService() { WebResponse response = dispatch.dispatch(new WebRequest("PUT", "/encryption-settings", "{}")); assertNotNull(response); assertEquals(Status.NO_CONTENT.getStatusCode(), response.getResponseCode()); }