EverrestProcessor implements Lifecycle { public void process(GenericContainerRequest request, GenericContainerResponse response, EnvironmentContext environmentContext) throws IOException { EnvironmentContext.setCurrent(environmentContext); ApplicationContext context = anApplicationContext() .withRequest(request) .withResponse(response) .withProviders(requestHandler.getProviders()) .withProperties(properties) .withApplication(everrestApplication) .withConfiguration(new EverrestConfiguration(configuration)) .withDependencySupplier(dependencySupplier) .withMethodInvokerDecoratorFactory(methodInvokerDecoratorFactory) .build(); try { context.start(); ApplicationContext.setCurrent(context); if (configuration.isNormalizeUri()) { normalizeRequestUri(request); } if (configuration.isHttpMethodOverride()) { overrideHttpMethod(request); } requestHandler.handleRequest(request, response); } finally { try { context.stop(); } finally { ApplicationContext.setCurrent(null); } EnvironmentContext.setCurrent(null); } } EverrestProcessor(DependencySupplier dependencySupplier, RequestHandler requestHandler);  EverrestProcessor(EverrestConfiguration configuration, DependencySupplier dependencySupplier, RequestHandler requestHandler, Application application); String getProperty(String name); void setProperty(String name, String value); void process(GenericContainerRequest request, GenericContainerResponse response, EnvironmentContext environmentContext); void addApplication(Application application); @Override void start(); @Override void stop(); ProviderBinder getProviders(); ResourceBinder getResources(); EverrestApplication getApplication(); DependencySupplier getDependencySupplier();  }
@Test public void processesRequest() throws Exception { ContainerRequest containerRequest = mockContainerRequest(); ContainerResponse containerResponse = mockContainerResponse(); EnvironmentContext environmentContext = mock(EnvironmentContext.class); doAnswer(invocation -> { Response response = mock(Response.class); when(response.getStatus()).thenReturn(200); when(response.getEntity()).thenReturn("foo"); containerResponse.setResponse(response); return null; }).when(requestHandler).handleRequest(containerRequest, containerResponse); everrestProcessor.process(containerRequest, containerResponse, environmentContext); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(containerResponse).setResponse(argumentCaptor.capture()); assertEquals(200, argumentCaptor.getValue().getStatus()); assertEquals("foo", argumentCaptor.getValue().getEntity()); }
@Test public void resetsApplicationContextAlterCallToRequestHandler() throws Exception { ContainerRequest containerRequest = mockContainerRequest(); ContainerResponse containerResponse = mockContainerResponse(); EnvironmentContext environmentContext = mock(EnvironmentContext.class); everrestProcessor.process(containerRequest, containerResponse, environmentContext); ApplicationContext applicationContextAfterCall = ApplicationContext.getCurrent(); assertNull(applicationContextAfterCall); }
@Test public void setsUpEnvironmentContextBeforeCallToRequestHandler() throws Exception { ContainerRequest containerRequest = mockContainerRequest(); ContainerResponse containerResponse = mockContainerResponse(); EnvironmentContext environmentContext = mock(EnvironmentContext.class); EnvironmentContext[] environmentContextThatRequestHandlerCalledWith = new EnvironmentContext[1]; doAnswer(invocation -> { environmentContextThatRequestHandlerCalledWith[0] = EnvironmentContext.getCurrent(); return null; }).when(requestHandler).handleRequest(containerRequest, containerResponse); everrestProcessor.process(containerRequest, containerResponse, environmentContext); assertNotNull(environmentContextThatRequestHandlerCalledWith[0]); assertSame(environmentContext, environmentContextThatRequestHandlerCalledWith[0]); }
@Test public void resetsEnvironmentContextAlterCallToRequestHandler() throws Exception { ContainerRequest containerRequest = mockContainerRequest(); ContainerResponse containerResponse = mockContainerResponse(); EnvironmentContext environmentContext = mock(EnvironmentContext.class); everrestProcessor.process(containerRequest, containerResponse, environmentContext); EnvironmentContext environmentContextAfterCall = EnvironmentContext.getCurrent(); assertNull(environmentContextAfterCall); }
@Test public void overridesHttpMethodWhenXHTTPMethodOverrideHeaderIsSet() throws Exception { when(configuration.isHttpMethodOverride()).thenReturn(true); requestHeaders.putSingle(X_HTTP_METHOD_OVERRIDE, "PUT"); ContainerRequest containerRequest = mockContainerRequest(); ContainerResponse containerResponse = mockContainerResponse(); EnvironmentContext environmentContext = mock(EnvironmentContext.class); everrestProcessor.process(containerRequest, containerResponse, environmentContext); verify(containerRequest).setMethod("PUT"); }