Search in sources :

Example 1 with ActionPath

use of io.crnk.core.engine.internal.dispatcher.path.ActionPath in project crnk-framework by crnk-project.

the class JsonApiActionResponseTest method testInvokeResourceAction.

@Test
public void testInvokeResourceAction() {
    Schedule schedule = new Schedule();
    schedule.setId(1L);
    schedule.setName("scheduleName");
    scheduleRepository.create(schedule);
    String result = scheduleRepository.resourceAction(1, "hello");
    Assert.assertEquals("{\"data\":\"resource action: hello@scheduleName\"}", result);
    // check filters
    ArgumentCaptor<DocumentFilterContext> contexts = ArgumentCaptor.forClass(DocumentFilterContext.class);
    Mockito.verify(filter, Mockito.times(2)).filter(contexts.capture(), Mockito.any(DocumentFilterChain.class));
    DocumentFilterContext actionContext = contexts.getAllValues().get(1);
    Assert.assertEquals("GET", actionContext.getMethod());
    Assert.assertTrue(actionContext.getJsonPath() instanceof ActionPath);
}
Also used : DocumentFilterContext(io.crnk.core.engine.filter.DocumentFilterContext) Schedule(io.crnk.test.mock.models.Schedule) DocumentFilterChain(io.crnk.core.engine.filter.DocumentFilterChain) ActionPath(io.crnk.core.engine.internal.dispatcher.path.ActionPath) Test(org.junit.Test) AbstractClientTest(io.crnk.client.AbstractClientTest)

Example 2 with ActionPath

use of io.crnk.core.engine.internal.dispatcher.path.ActionPath in project crnk-framework by crnk-project.

the class JsonApiRequestProcessor method process.

@Override
public void process(HttpRequestContext requestContext) throws IOException {
    if (isJsonApiRequest(requestContext, isAcceptingPlainJson())) {
        ResourceRegistry resourceRegistry = moduleContext.getResourceRegistry();
        RequestDispatcher requestDispatcher = moduleContext.getRequestDispatcher();
        String path = requestContext.getPath();
        JsonPath jsonPath = new PathBuilder(resourceRegistry).build(path);
        Map<String, Set<String>> parameters = requestContext.getRequestParameters();
        String method = requestContext.getMethod();
        if (jsonPath instanceof ActionPath) {
            // inital implementation, has to improve
            requestDispatcher.dispatchAction(path, method, parameters);
        } else if (jsonPath != null) {
            byte[] requestBody = requestContext.getRequestBody();
            Document document = null;
            if (requestBody != null && requestBody.length > 0) {
                ObjectMapper objectMapper = moduleContext.getObjectMapper();
                try {
                    document = objectMapper.readerFor(Document.class).readValue(requestBody);
                } catch (JsonProcessingException e) {
                    final String message = "Json Parsing failed";
                    setResponse(requestContext, buildBadRequestResponse(message, e.getMessage()));
                    LOGGER.error(message, e);
                    return;
                }
            }
            RepositoryMethodParameterProvider parameterProvider = requestContext.getRequestParameterProvider();
            Response crnkResponse = requestDispatcher.dispatchRequest(path, method, parameters, parameterProvider, document);
            setResponse(requestContext, crnkResponse);
        } else {
        // no repositories invoked, we do nothing
        }
    }
}
Also used : Set(java.util.Set) ActionPath(io.crnk.core.engine.internal.dispatcher.path.ActionPath) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) Document(io.crnk.core.engine.document.Document) RequestDispatcher(io.crnk.core.engine.dispatcher.RequestDispatcher) Response(io.crnk.core.engine.dispatcher.Response) PathBuilder(io.crnk.core.engine.internal.dispatcher.path.PathBuilder) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with ActionPath

use of io.crnk.core.engine.internal.dispatcher.path.ActionPath in project crnk-framework by crnk-project.

the class InteroperabilityTest method testInvokeRepositoryActionWithResourceResult.

@Test
public void testInvokeRepositoryActionWithResourceResult() {
    // resources should be received in json api format
    String url = getBaseUri() + "schedules/repositoryActionWithResourceResult?msg=hello";
    io.restassured.response.Response res = RestAssured.get(url);
    Assert.assertEquals(200, res.getStatusCode());
    res.then().assertThat().body("data.attributes.name", Matchers.equalTo("hello"));
    // check filters
    ArgumentCaptor<DocumentFilterContext> contexts = ArgumentCaptor.forClass(DocumentFilterContext.class);
    Mockito.verify(filter, Mockito.times(1)).filter(contexts.capture(), Mockito.any(DocumentFilterChain.class));
    DocumentFilterContext actionContext = contexts.getAllValues().get(0);
    Assert.assertEquals("GET", actionContext.getMethod());
    Assert.assertTrue(actionContext.getJsonPath() instanceof ActionPath);
}
Also used : DocumentFilterContext(io.crnk.core.engine.filter.DocumentFilterContext) DocumentFilterChain(io.crnk.core.engine.filter.DocumentFilterChain) ActionPath(io.crnk.core.engine.internal.dispatcher.path.ActionPath) Test(org.junit.Test)

Example 4 with ActionPath

use of io.crnk.core.engine.internal.dispatcher.path.ActionPath in project crnk-framework by crnk-project.

the class InteroperabilityTest method testInvokeRepositoryActionWithException.

@Test
public void testInvokeRepositoryActionWithException() {
    // resources should be received in json api format
    String url = getBaseUri() + "schedules/repositoryActionWithException?msg=hello";
    io.restassured.response.Response res = RestAssured.get(url);
    Assert.assertEquals(403, res.getStatusCode());
    res.then().assertThat().body("errors[0].status", Matchers.equalTo("403"));
    // check filters
    ArgumentCaptor<DocumentFilterContext> contexts = ArgumentCaptor.forClass(DocumentFilterContext.class);
    Mockito.verify(filter, Mockito.times(1)).filter(contexts.capture(), Mockito.any(DocumentFilterChain.class));
    DocumentFilterContext actionContext = contexts.getAllValues().get(0);
    Assert.assertEquals("GET", actionContext.getMethod());
    Assert.assertTrue(actionContext.getJsonPath() instanceof ActionPath);
}
Also used : DocumentFilterContext(io.crnk.core.engine.filter.DocumentFilterContext) DocumentFilterChain(io.crnk.core.engine.filter.DocumentFilterChain) ActionPath(io.crnk.core.engine.internal.dispatcher.path.ActionPath) Test(org.junit.Test)

Example 5 with ActionPath

use of io.crnk.core.engine.internal.dispatcher.path.ActionPath in project crnk-framework by crnk-project.

the class HttpRequestProcessorImplTest method shouldNotifyWhenActionIsExeecuted.

@Test
public void shouldNotifyWhenActionIsExeecuted() throws Exception {
    // GIVEN
    String path = "/actionResource/1/someAction";
    String requestType = "GET";
    ControllerRegistry controllerRegistry = new ControllerRegistry(null);
    QuerySpecAdapterBuilder queryAdapterBuilder = new QuerySpecAdapterBuilder(new DefaultQuerySpecDeserializer(), moduleRegistry);
    RequestDispatcher sut = new HttpRequestProcessorImpl(moduleRegistry, controllerRegistry, null, queryAdapterBuilder);
    // WHEN
    Map<String, Set<String>> parameters = new HashMap<>();
    sut.dispatchAction(path, "GET", parameters);
    // THEN
    ArgumentCaptor<DocumentFilterContext> filterContextCaptor = ArgumentCaptor.forClass(DocumentFilterContext.class);
    Mockito.verify(documentFilter, Mockito.times(1)).filter(filterContextCaptor.capture(), Mockito.any(DocumentFilterChain.class));
    DocumentFilterContext filterContext = filterContextCaptor.getValue();
    Assert.assertEquals("GET", filterContext.getMethod());
    Assert.assertTrue(filterContext.getJsonPath() instanceof ActionPath);
}
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) DefaultQuerySpecDeserializer(io.crnk.core.queryspec.DefaultQuerySpecDeserializer) DocumentFilterChain(io.crnk.core.engine.filter.DocumentFilterChain) ActionPath(io.crnk.core.engine.internal.dispatcher.path.ActionPath) Matchers.anyString(org.mockito.Matchers.anyString) RequestDispatcher(io.crnk.core.engine.dispatcher.RequestDispatcher) HttpRequestProcessorImpl(io.crnk.core.engine.internal.http.HttpRequestProcessorImpl) DocumentFilterContext(io.crnk.core.engine.filter.DocumentFilterContext) ExceptionMapperRegistryTest(io.crnk.core.engine.internal.exception.ExceptionMapperRegistryTest) Test(org.junit.Test)

Aggregations

ActionPath (io.crnk.core.engine.internal.dispatcher.path.ActionPath)10 DocumentFilterChain (io.crnk.core.engine.filter.DocumentFilterChain)9 DocumentFilterContext (io.crnk.core.engine.filter.DocumentFilterContext)9 Test (org.junit.Test)9 AbstractClientTest (io.crnk.client.AbstractClientTest)6 RequestDispatcher (io.crnk.core.engine.dispatcher.RequestDispatcher)2 Schedule (io.crnk.test.mock.models.Schedule)2 Set (java.util.Set)2 Ignore (org.junit.Ignore)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Response (io.crnk.core.engine.dispatcher.Response)1 Document (io.crnk.core.engine.document.Document)1 ControllerRegistry (io.crnk.core.engine.internal.dispatcher.ControllerRegistry)1 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)1 PathBuilder (io.crnk.core.engine.internal.dispatcher.path.PathBuilder)1 ExceptionMapperRegistryTest (io.crnk.core.engine.internal.exception.ExceptionMapperRegistryTest)1 HttpRequestProcessorImpl (io.crnk.core.engine.internal.http.HttpRequestProcessorImpl)1 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)1 DefaultQuerySpecDeserializer (io.crnk.core.queryspec.DefaultQuerySpecDeserializer)1