Search in sources :

Example 1 with HttpRequestProcessorImpl

use of io.crnk.core.engine.internal.http.HttpRequestProcessorImpl in project crnk-framework by crnk-project.

the class HomeResourceFilteringTest method checkResponse.

private void checkResponse(boolean filtered) throws IOException {
    ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
    ArgumentCaptor<byte[]> responseCaptor = ArgumentCaptor.forClass(byte[].class);
    HttpRequestContextBase requestContextBase = Mockito.mock(HttpRequestContextBase.class);
    Mockito.when(requestContextBase.getMethod()).thenReturn("GET");
    Mockito.when(requestContextBase.getPath()).thenReturn("/");
    Mockito.when(requestContextBase.getRequestHeader("Accept")).thenReturn("*");
    HttpRequestProcessorImpl requestDispatcher = boot.getRequestDispatcher();
    requestDispatcher.process(requestContextBase);
    Mockito.verify(requestContextBase, Mockito.times(1)).setResponse(statusCaptor.capture(), responseCaptor.capture());
    Assert.assertEquals(200, (int) statusCaptor.getValue());
    String json = new String(responseCaptor.getValue());
    JsonNode response = boot.getObjectMapper().reader().readTree(json);
    JsonNode resourcesNode = response.get("links");
    JsonNode tasksNode = resourcesNode.get("tasks");
    if (filtered) {
        Assert.assertNull(tasksNode);
    } else {
        Assert.assertEquals("http://localhost/tasks", tasksNode.asText());
    }
}
Also used : HttpRequestContextBase(io.crnk.core.engine.http.HttpRequestContextBase) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpRequestProcessorImpl(io.crnk.core.engine.internal.http.HttpRequestProcessorImpl)

Example 2 with HttpRequestProcessorImpl

use of io.crnk.core.engine.internal.http.HttpRequestProcessorImpl in project crnk-framework by crnk-project.

the class JsonHomeFormatTest method testHomeJsonReturned.

private void testHomeJsonReturned(boolean anyRequest) throws IOException {
    ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
    ArgumentCaptor<byte[]> responseCaptor = ArgumentCaptor.forClass(byte[].class);
    HttpRequestContextBase requestContextBase = Mockito.mock(HttpRequestContextBase.class);
    Mockito.when(requestContextBase.getMethod()).thenReturn("GET");
    Mockito.when(requestContextBase.getPath()).thenReturn("/");
    Mockito.when(requestContextBase.getRequestHeader("Accept")).thenReturn(anyRequest ? "*" : HomeModule.JSON_HOME_CONTENT_TYPE);
    HttpRequestProcessorImpl requestDispatcher = boot.getRequestDispatcher();
    requestDispatcher.process(requestContextBase);
    Mockito.verify(requestContextBase, Mockito.times(1)).setResponse(statusCaptor.capture(), responseCaptor.capture());
    String expectedContentType = anyRequest ? HomeModule.JSON_CONTENT_TYPE : HomeModule.JSON_HOME_CONTENT_TYPE;
    Mockito.verify(requestContextBase, Mockito.times(1)).setResponseHeader("Content-Type", expectedContentType);
    Assert.assertEquals(200, (int) statusCaptor.getValue());
    String json = new String(responseCaptor.getValue());
    JsonNode response = boot.getObjectMapper().reader().readTree(json);
    JsonNode resourcesNode = response.get("resources");
    JsonNode usersNode = resourcesNode.get("tag:tasks");
    Assert.assertEquals("tasks", usersNode.get("href").asText());
}
Also used : HttpRequestContextBase(io.crnk.core.engine.http.HttpRequestContextBase) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpRequestProcessorImpl(io.crnk.core.engine.internal.http.HttpRequestProcessorImpl)

Example 3 with HttpRequestProcessorImpl

use of io.crnk.core.engine.internal.http.HttpRequestProcessorImpl in project crnk-framework by crnk-project.

the class HttpRequestProcessorImplTest method onGivenPathAndRequestTypeControllerShouldHandleRequest.

@Test
public void onGivenPathAndRequestTypeControllerShouldHandleRequest() throws Exception {
    // GIVEN
    String path = "/tasks/";
    String requestType = "GET";
    ControllerRegistry controllerRegistry = new ControllerRegistry(null);
    CollectionGet controller = mock(CollectionGet.class);
    controllerRegistry.addController(controller);
    QuerySpecAdapterBuilder queryAdapterBuilder = new QuerySpecAdapterBuilder(new DefaultQuerySpecDeserializer(), moduleRegistry);
    RequestDispatcher sut = new HttpRequestProcessorImpl(moduleRegistry, controllerRegistry, null, queryAdapterBuilder);
    // WHEN
    when(controller.isAcceptable(any(JsonPath.class), eq(requestType))).thenCallRealMethod();
    Map<String, Set<String>> parameters = new HashMap<>();
    sut.dispatchRequest(path, requestType, parameters, null, null);
    // THEN
    verify(controller, times(1)).handle(any(JsonPath.class), any(QueryAdapter.class), any(RepositoryMethodParameterProvider.class), any(Document.class));
}
Also used : ControllerRegistry(io.crnk.core.engine.internal.dispatcher.ControllerRegistry) QuerySpecAdapterBuilder(io.crnk.core.queryspec.internal.QuerySpecAdapterBuilder) Set(java.util.Set) HashMap(java.util.HashMap) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) DefaultQuerySpecDeserializer(io.crnk.core.queryspec.DefaultQuerySpecDeserializer) Matchers.anyString(org.mockito.Matchers.anyString) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) Document(io.crnk.core.engine.document.Document) RequestDispatcher(io.crnk.core.engine.dispatcher.RequestDispatcher) HttpRequestProcessorImpl(io.crnk.core.engine.internal.http.HttpRequestProcessorImpl) CollectionGet(io.crnk.core.engine.internal.dispatcher.controller.CollectionGet) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider) ExceptionMapperRegistryTest(io.crnk.core.engine.internal.exception.ExceptionMapperRegistryTest) Test(org.junit.Test)

Example 4 with HttpRequestProcessorImpl

use of io.crnk.core.engine.internal.http.HttpRequestProcessorImpl in project crnk-framework by crnk-project.

the class HttpRequestProcessorImplTest method checkProcess.

@Test
public void checkProcess() throws IOException {
    HttpRequestContextBase requestContextBase = Mockito.mock(HttpRequestContextBase.class);
    HttpRequestContextBaseAdapter requestContext = new HttpRequestContextBaseAdapter(requestContextBase);
    Mockito.when(requestContextBase.getMethod()).thenReturn("GET");
    Mockito.when(requestContextBase.getPath()).thenReturn("/tasks/");
    Mockito.when(requestContextBase.getRequestHeader("Accept")).thenReturn("*");
    ControllerRegistry controllerRegistry = new ControllerRegistry(null);
    CollectionGet controller = mock(CollectionGet.class);
    when(controller.isAcceptable(any(JsonPath.class), eq("GET"))).thenCallRealMethod();
    controllerRegistry.addController(controller);
    QuerySpecAdapterBuilder queryAdapterBuilder = new QuerySpecAdapterBuilder(new DefaultQuerySpecDeserializer(), moduleRegistry);
    RequestDispatcher sut = new HttpRequestProcessorImpl(moduleRegistry, controllerRegistry, null, queryAdapterBuilder);
    sut.process(requestContext);
    verify(controller, times(1)).handle(any(JsonPath.class), any(QueryAdapter.class), any(RepositoryMethodParameterProvider.class), any(Document.class));
}
Also used : HttpRequestContextBaseAdapter(io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter) ControllerRegistry(io.crnk.core.engine.internal.dispatcher.ControllerRegistry) CollectionGet(io.crnk.core.engine.internal.dispatcher.controller.CollectionGet) QuerySpecAdapterBuilder(io.crnk.core.queryspec.internal.QuerySpecAdapterBuilder) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) DefaultQuerySpecDeserializer(io.crnk.core.queryspec.DefaultQuerySpecDeserializer) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) Document(io.crnk.core.engine.document.Document) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider) RequestDispatcher(io.crnk.core.engine.dispatcher.RequestDispatcher) HttpRequestProcessorImpl(io.crnk.core.engine.internal.http.HttpRequestProcessorImpl) ExceptionMapperRegistryTest(io.crnk.core.engine.internal.exception.ExceptionMapperRegistryTest) Test(org.junit.Test)

Example 5 with HttpRequestProcessorImpl

use of io.crnk.core.engine.internal.http.HttpRequestProcessorImpl in project crnk-framework by crnk-project.

the class HttpRequestProcessorImplTest method shouldMapExceptionToErrorResponseIfMapperIsAvailable.

@Test
public void shouldMapExceptionToErrorResponseIfMapperIsAvailable() throws Exception {
    ControllerRegistry controllerRegistry = mock(ControllerRegistry.class);
    // noinspection unchecked
    when(controllerRegistry.getController(any(JsonPath.class), anyString())).thenThrow(IllegalStateException.class);
    QuerySpecAdapterBuilder queryAdapterBuilder = new QuerySpecAdapterBuilder(new DefaultQuerySpecDeserializer(), moduleRegistry);
    RequestDispatcher requestDispatcher = new HttpRequestProcessorImpl(moduleRegistry, controllerRegistry, ExceptionMapperRegistryTest.exceptionMapperRegistry, queryAdapterBuilder);
    Response response = requestDispatcher.dispatchRequest("tasks", null, null, null, null);
    assertThat(response).isNotNull();
    assertThat(response.getHttpStatus()).isEqualTo(HttpStatus.BAD_REQUEST_400);
}
Also used : ControllerRegistry(io.crnk.core.engine.internal.dispatcher.ControllerRegistry) Response(io.crnk.core.engine.dispatcher.Response) QuerySpecAdapterBuilder(io.crnk.core.queryspec.internal.QuerySpecAdapterBuilder) DefaultQuerySpecDeserializer(io.crnk.core.queryspec.DefaultQuerySpecDeserializer) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) RequestDispatcher(io.crnk.core.engine.dispatcher.RequestDispatcher) HttpRequestProcessorImpl(io.crnk.core.engine.internal.http.HttpRequestProcessorImpl) ExceptionMapperRegistryTest(io.crnk.core.engine.internal.exception.ExceptionMapperRegistryTest) Test(org.junit.Test)

Aggregations

HttpRequestProcessorImpl (io.crnk.core.engine.internal.http.HttpRequestProcessorImpl)17 Test (org.junit.Test)12 QuerySpecAdapterBuilder (io.crnk.core.queryspec.internal.QuerySpecAdapterBuilder)9 ControllerRegistry (io.crnk.core.engine.internal.dispatcher.ControllerRegistry)8 DefaultQuerySpecDeserializer (io.crnk.core.queryspec.DefaultQuerySpecDeserializer)8 RequestDispatcher (io.crnk.core.engine.dispatcher.RequestDispatcher)6 HttpRequestContextBase (io.crnk.core.engine.http.HttpRequestContextBase)6 ExceptionMapperRegistryTest (io.crnk.core.engine.internal.exception.ExceptionMapperRegistryTest)6 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 Document (io.crnk.core.engine.document.Document)4 HashMap (java.util.HashMap)4 Set (java.util.Set)4 Response (io.crnk.core.engine.dispatcher.Response)3 CollectionGet (io.crnk.core.engine.internal.dispatcher.controller.CollectionGet)3 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)3 QueryAdapterBuilder (io.crnk.core.engine.query.QueryAdapterBuilder)3 ConstantServiceUrlProvider (io.crnk.core.engine.url.ConstantServiceUrlProvider)3 RepositoryMethodParameterProvider (io.crnk.legacy.internal.RepositoryMethodParameterProvider)3 Matchers.anyString (org.mockito.Matchers.anyString)3