@Override public void reload() { reload(getConfiguration()); }
@Override public void commit() { response.close(); }
@Override public void failure(Throwable error) { LOG.error("container failure", error); response.setStatusType(Status.INTERNAL_SERVER_ERROR); try { commit(); } catch (RuntimeException e) { LOG.error("failed to commit failure", e); throw e; } }
@Test public void getArticle_OneSupportedAcceptGiven_ShouldReturnArticle() throws IOException { when(testService.getArticle(1)).thenReturn("some article"); JRestlessResponseWriter responseWriter = createResponseWriterMock(); TestRequest req = new TestRequest("/articles/1", "GET"); req.getHeadersAsMultimap().add("Accept", MediaType.APPLICATION_XML + "," + MediaType.TEXT_PLAIN); container.handleRequest(req, responseWriter, mock(SecurityContext.class)); verify(responseWriter, times(1)).writeResponse(eq(Status.OK), any(), eqBaos("some article")); }
public void createContainerRequest_NoRequestBaseUriGiven_ShouldPass() { JRestlessContainerRequest request = createAnyRequest(); when(request.getBaseUri()).thenReturn(null); container.createContainerRequest(request, mock(ContainerResponseWriter.class), mock(SecurityContext.class)); }
/** * Shortcut for * {@link #handleRequest(JRestlessContainerRequest, JRestlessResponseWriter, SecurityContext, Consumer)} * with noop containerRequestEnhancer. * * @param request * @param responseWriter * @param securityContext */ public void handleRequest(@Nonnull RequestT request, @Nonnull JRestlessResponseWriter responseWriter, @Nonnull SecurityContext securityContext) { handleRequest(request, responseWriter, securityContext, req -> { }); }
@Test public void createContainerRequest_NoSecurityContext_ShouldThrowNpe() { assertThrows(NullPointerException.class, () -> container.createContainerRequest(createAnyRequest(), mock(ContainerResponseWriter.class), null)); }
/** * Stops the container. * <p> * May be called once, only. * <p> * {@link #start()} must be called, first. */ public final void stop() { checkState(started, "container has already been stopped"); container.onShutdown(); started = false; }
/** * Starts the container. * <p> * May be called once, only. * <p> * One of the init methods must be called, first. */ public final void start() { checkState(initialized, "handler has not been initialized"); checkState(!started, "container has already been started"); container.onStartup(); started = true; }
public MultivaluedMap<String, String> getHeadersAsMultimap() { return (MultivaluedMap<String, String>) getHeaders(); } }
@Test public void getArticle_ExistingIdGiven_ShouldReturnArticle() throws IOException { when(testService.getArticle(1)).thenReturn("some article"); JRestlessResponseWriter responseWriter = createResponseWriterMock(); container.handleRequest(new TestRequest("/articles/1", "GET"), responseWriter, mock(SecurityContext.class)); verify(responseWriter, times(1)).writeResponse(eq(Status.OK), any(), eqBaos("some article")); }
@Test public void createContainerRequest_NoContainerResponseWriterGiven_ShouldThrowNpe() { assertThrows(NullPointerException.class, () -> container.createContainerRequest(createAnyRequest(), null, mock(SecurityContext.class))); }
@Test public void getArticle_NonSupportedAccept_ShouldReturnNotAcceptable() throws IOException { when(testService.getArticle(1)).thenReturn("some article"); JRestlessResponseWriter responseWriter = createResponseWriterMock(); TestRequest req = new TestRequest("/articles/1", "GET"); req.getHeadersAsMultimap().add("Accept", MediaType.APPLICATION_XML); container.handleRequest(req, responseWriter, mock(SecurityContext.class)); verify(responseWriter, times(1)).writeResponse(eq(Status.NOT_ACCEPTABLE), any(), emptyBaos()); }
@Test public void getArticle_NonSupportedMethodGiven_ShouldReturnInternalServerError() throws IOException { JRestlessResponseWriter responseWriter = createResponseWriterMock(); container.handleRequest(new TestRequest("/articles/1", "PATCH"), responseWriter, mock(SecurityContext.class)); verify(responseWriter, times(1)).writeResponse(eq(Status.METHOD_NOT_ALLOWED), any(), emptyBaos()); }
@Test public void getArticle_ServiceExceptionGiven_ShouldReturnInternalServerError() throws IOException { when(testService.getArticle(1)).thenThrow(new RuntimeException()); JRestlessResponseWriter responseWriter = createResponseWriterMock(); container.handleRequest(new TestRequest("/articles/1", "GET"), responseWriter, mock(SecurityContext.class)); verify(responseWriter, times(1)).writeResponse(eq(Status.INTERNAL_SERVER_ERROR), any(), emptyBaos()); }
@Test public void getArticles_ContainsGiven_ShouldReturnArticles() throws IOException { when(testService.getArticles("test")).thenReturn(Arrays.asList("test article 1", "test article 2")); JRestlessResponseWriter responseWriter = createResponseWriterMock(); TestRequest req = new TestRequest("/articles?contains=test", "GET"); container.handleRequest(req, responseWriter, mock(SecurityContext.class)); verify(responseWriter, times(1)).writeResponse(eq(Status.OK), any(), eqBaos("test article 1, test article 2")); }
@Test public void getHeaderParam_HeaderParamNameGiven_ShouldReturnHeaderParam() throws IOException { JRestlessResponseWriter responseWriter = createResponseWriterMock(); TestRequest req = new TestRequest("/header", "GET"); req.getHeadersAsMultimap().add("key", "value"); container.handleRequest(req, responseWriter, mock(SecurityContext.class)); verify(responseWriter, times(1)).writeResponse(eq(Status.OK), any(), eqBaos("value")); }
@Test public void getArticle_NonExistingIdGiven_ShouldReturnNotFound() throws IOException { JRestlessResponseWriter responseWriter = createResponseWriterMock(); container.handleRequest(new TestRequest("/articles/1", "GET"), responseWriter, mock(SecurityContext.class)); verify(responseWriter, times(1)).writeResponse(eq(Status.NOT_FOUND), any(), emptyBaos()); }
@Test public void updateArticle_ArticleBodyGiven_ShouldReturnUpdatedArticle() throws IOException { when(testService.updateArticle(1, "some updated article text")).thenReturn("some updated persisted article text"); JRestlessResponseWriter responseWriter = createResponseWriterMock(); container.handleRequest(new TestRequest("/articles/1", "PUT", new ByteArrayInputStream("some updated article text".getBytes())), responseWriter, mock(SecurityContext.class)); verify(responseWriter, times(1)).writeResponse(eq(Status.OK), any(), eqBaos("some updated persisted article text")); }