Search in sources :

Example 1 with DocumentFilterContext

use of io.crnk.core.engine.filter.DocumentFilterContext in project crnk-framework by crnk-project.

the class MetaModule method setupModule.

@Override
public void setupModule(ModuleContext context) {
    this.context = context;
    informationBuilder = registerInformationBuilder(context.getPropertiesProvider());
    if (context.isServer()) {
        context.addFilter(new DocumentFilter() {

            @Override
            public Response filter(DocumentFilterContext filterRequestContext, DocumentFilterChain chain) {
                try {
                    return chain.doFilter(filterRequestContext);
                } finally {
                    lookupRequestLocal.remove();
                }
            }
        });
    } else {
        context.addResourceLookup(new ResourceLookup() {

            @SuppressWarnings("unchecked")
            @Override
            public Set<Class<?>> getResourceClasses() {
                return (Set) collectMetaClasses();
            }
        });
    }
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) DocumentFilterContext(io.crnk.core.engine.filter.DocumentFilterContext) HashSet(java.util.HashSet) Set(java.util.Set) DocumentFilterChain(io.crnk.core.engine.filter.DocumentFilterChain) DocumentFilter(io.crnk.core.engine.filter.DocumentFilter) ResourceLookup(io.crnk.core.module.discovery.ResourceLookup)

Example 2 with DocumentFilterContext

use of io.crnk.core.engine.filter.DocumentFilterContext in project crnk-framework by crnk-project.

the class JsonApiActionResponseTest method setupFeature.

@Override
protected void setupFeature(CrnkTestFeature feature) {
    filter = Mockito.spy(new DocumentFilter() {

        @Override
        public Response filter(DocumentFilterContext filterRequestContext, DocumentFilterChain chain) {
            return chain.doFilter(filterRequestContext);
        }
    });
    SimpleModule testModule = new SimpleModule("testFilter");
    testModule.addFilter(filter);
    feature.addModule(testModule);
}
Also used : DocumentFilterContext(io.crnk.core.engine.filter.DocumentFilterContext) DocumentFilterChain(io.crnk.core.engine.filter.DocumentFilterChain) DocumentFilter(io.crnk.core.engine.filter.DocumentFilter) SimpleModule(io.crnk.core.module.SimpleModule)

Example 3 with DocumentFilterContext

use of io.crnk.core.engine.filter.DocumentFilterContext 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 4 with DocumentFilterContext

use of io.crnk.core.engine.filter.DocumentFilterContext 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 5 with DocumentFilterContext

use of io.crnk.core.engine.filter.DocumentFilterContext 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)

Aggregations

DocumentFilterChain (io.crnk.core.engine.filter.DocumentFilterChain)13 DocumentFilterContext (io.crnk.core.engine.filter.DocumentFilterContext)13 Test (org.junit.Test)10 ActionPath (io.crnk.core.engine.internal.dispatcher.path.ActionPath)9 AbstractClientTest (io.crnk.client.AbstractClientTest)6 DocumentFilter (io.crnk.core.engine.filter.DocumentFilter)3 Set (java.util.Set)3 SimpleModule (io.crnk.core.module.SimpleModule)2 Schedule (io.crnk.test.mock.models.Schedule)2 HashMap (java.util.HashMap)2 Ignore (org.junit.Ignore)2 RequestDispatcher (io.crnk.core.engine.dispatcher.RequestDispatcher)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 ExceptionMapperRegistryTest (io.crnk.core.engine.internal.exception.ExceptionMapperRegistryTest)1 HttpRequestProcessorImpl (io.crnk.core.engine.internal.http.HttpRequestProcessorImpl)1 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)1 NewInstanceRepositoryMethodParameterProvider (io.crnk.core.engine.repository.mock.NewInstanceRepositoryMethodParameterProvider)1