/** * 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 -> { }); }
/** * Initializes the container using the given application. * <p> * May be called once, only. * * @param application * @param parentManager */ public final void init(@Nonnull Application application, @Nullable Object parentManager) { requireNonNull(application); init(new JRestlessHandlerContainer<>(application, createBinder(), parentManager)); }
/** * Creates a container request from the given input and delegates it to the * application. * * @param request * @param responseWriter * the response writer. * @param securityContext * the security context of the request. * @param containerRequestEnhancer * additional container request customizer. */ public void handleRequest(@Nonnull RequestT request, @Nonnull JRestlessResponseWriter responseWriter, @Nonnull SecurityContext securityContext, @Nonnull Consumer<ContainerRequest> containerRequestEnhancer) { requireNonNull(responseWriter, "responseWriter may not be null"); requireNonNull(containerRequestEnhancer, "containerRequestExtender may not be null"); ContainerRequest containerRequest = createContainerRequest(request, new JRestlessContainerResponseWriter(responseWriter), securityContext); containerRequestEnhancer.accept(containerRequest); handleRequest(containerRequest); }
@Override public void reload() { reload(getConfiguration()); }
@Test public void reload_ConfigGiven_ShouldResetAppHandler() { ResourceConfig config = new ApplicationHandler().getConfiguration(); ApplicationHandler newAppHandler = mock(ApplicationHandler.class); doReturn(newAppHandler).when(container).createNewApplicationHandler(any()); container.reload(config); assertSame(newAppHandler, container.getApplicationHandler()); }
@Test public void reload_ConfigGiven_ShouldCreateAppHandlerUsingConfiguration() { ResourceConfig config = new ApplicationHandler().getConfiguration(); container.reload(config); verify(container, times(1)).createNewApplicationHandler(config); }
public void createContainerRequest_NoRequestBaseUriGiven_ShouldPass() { JRestlessContainerRequest request = createAnyRequest(); when(request.getBaseUri()).thenReturn(null); container.createContainerRequest(request, mock(ContainerResponseWriter.class), mock(SecurityContext.class)); }
@BeforeEach public void setup() { testService = mock(ArticleService.class); Binder binder = new InstanceBinder.Builder().addInstance(testService, ArticleService.class).build(); container = new JRestlessHandlerContainer<JRestlessContainerRequest>( new ResourceConfig().register(TestResource.class).register(binder).register(RolesAllowedDynamicFeature.class)); container.onStartup(); }
/** * 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; }
@Override public void reload(ResourceConfig configuration) { LOG.info("reloading container..."); appHandler.onShutdown(this); appHandler = createNewApplicationHandler(configuration); appHandler.onReload(this); appHandler.onStartup(this); LOG.info("reloaded container"); }
@Test public void getConfiguration_ShouldReturnAppHandlerConfiguration() { ResourceConfig config = mock(ResourceConfig.class); when(appHandler.getConfiguration()).thenReturn(config); assertSame(config, container.getConfiguration()); }
@Test public void reload_ConfigGiven_ShouldShutdownOldAppHandler() { ResourceConfig config = new ApplicationHandler().getConfiguration(); container.reload(config); verify(appHandler, times(1)).onShutdown(container); }
@Test public void reload_ConfigGiven_ShouldStartNewAppHandler() { ResourceConfig config = new ApplicationHandler().getConfiguration(); ApplicationHandler newAppHandler = mock(ApplicationHandler.class); doReturn(newAppHandler).when(container).createNewApplicationHandler(any()); container.reload(config); verify(newAppHandler, times(1)).onStartup(container); }
@Test public void createContainerRequest_NoRequestGiven_ShouldThrowNpe() { assertThrows(NullPointerException.class, () -> container.createContainerRequest(null, mock(ContainerResponseWriter.class), mock(SecurityContext.class))); }
@Test public void reload_NoConfigGiven_ShouldCreateAppHandlerUsingCurrentConfig() { doNothing().when(container).reload(any()); ResourceConfig config = container.getConfiguration(); container.reload(); verify(container, times(1)).reload(config); }
@Test public void onShutdown_ShouldShutdownAppHandler() { container.onShutdown(); verify(appHandler, times(1)).onShutdown(container); }
@Test public void onStartup_ShouldStartAppHandler() { container.onStartup(); verify(appHandler, times(1)).onStartup(container); }
@Test public void handleRequest_ContainerRequestGiven_ShouldInvokeAppHandler() { ContainerRequest request = mock(ContainerRequest.class); container.handleRequest(request); verify(container, times(1)).handleRequest(eq(request)); }
@Test public void reload_ConfigGiven_ShouldReloadNewAppHandler() { ResourceConfig config = new ApplicationHandler().getConfiguration(); ApplicationHandler newAppHandler = mock(ApplicationHandler.class); doReturn(newAppHandler).when(container).createNewApplicationHandler(any()); container.reload(config); verify(newAppHandler, times(1)).onReload(container); }