Search in sources :

Example 6 with DocumentFilterContext

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

Example 7 with DocumentFilterContext

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

the class FilterTest method test.

@Test
public void test() throws Exception {
    // WHEN
    ArgumentCaptor<DocumentFilterContext> captor = ArgumentCaptor.forClass(DocumentFilterContext.class);
    when(collectionGet.isAcceptable(any(JsonPath.class), eq(requestType))).thenCallRealMethod();
    when(filter.filter(any(DocumentFilterContext.class), any(DocumentFilterChain.class))).thenCallRealMethod();
    Map<String, Set<String>> queryParams = new HashMap<>();
    RepositoryMethodParameterProvider parameterProvider = new NewInstanceRepositoryMethodParameterProvider();
    Document requestBody = new Document();
    dispatcher.dispatchRequest(path, requestType, queryParams, parameterProvider, requestBody);
    // THEN
    verify(filter).filter(captor.capture(), any(DocumentFilterChain.class));
    verify(collectionGet, times(1)).handle(any(JsonPath.class), any(QueryAdapter.class), any(RepositoryMethodParameterProvider.class), any(Document.class));
    verify(filter, times(1)).filter(any(DocumentFilterContext.class), any(DocumentFilterChain.class));
    DocumentFilterContext value = captor.getValue();
    Assert.assertEquals("tasks", value.getJsonPath().getElementName());
    Assert.assertEquals(parameterProvider, value.getParameterProvider());
    Assert.assertEquals(requestBody, value.getRequestBody());
    Assert.assertEquals("GET", value.getMethod());
}
Also used : DocumentFilterContext(io.crnk.core.engine.filter.DocumentFilterContext) NewInstanceRepositoryMethodParameterProvider(io.crnk.core.engine.repository.mock.NewInstanceRepositoryMethodParameterProvider) Set(java.util.Set) HashMap(java.util.HashMap) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) DocumentFilterChain(io.crnk.core.engine.filter.DocumentFilterChain) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) Document(io.crnk.core.engine.document.Document) NewInstanceRepositoryMethodParameterProvider(io.crnk.core.engine.repository.mock.NewInstanceRepositoryMethodParameterProvider) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 8 with DocumentFilterContext

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

the class BasicActionTest 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 9 with DocumentFilterContext

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

the class BasicActionTest method testInvokeResourceAction.

@Test
public void testInvokeResourceAction() {
    Schedule schedule = new Schedule();
    schedule.setId(1L);
    schedule.setName("scheduleName");
    scheduleRepo.create(schedule);
    String result = scheduleRepo.resourceAction(1, "hello");
    Assert.assertEquals("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 10 with DocumentFilterContext

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

the class BasicActionTest method testInvokeRepositoryAction.

@Test
@Ignore
public // The DocumentFilterContext is not invoked with this request any more
void testInvokeRepositoryAction() {
    String result = scheduleRepo.repositoryAction("hello");
    Assert.assertEquals("repository action: hello", result);
    // 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) Ignore(org.junit.Ignore) Test(org.junit.Test) AbstractClientTest(io.crnk.client.AbstractClientTest)

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