Search in sources :

Example 6 with RepositoryRequestSpec

use of io.crnk.core.engine.dispatcher.RepositoryRequestSpec in project crnk-framework by crnk-project.

the class RepositoryFilterTest method findAllById.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void findAllById() throws Exception {
    resourceAdapter.findAll(Arrays.asList(2L), queryAdapter);
    ArgumentCaptor<Iterable> linksResources = ArgumentCaptor.forClass(Iterable.class);
    ArgumentCaptor<Iterable> metaResources = ArgumentCaptor.forClass(Iterable.class);
    ArgumentCaptor<RepositoryFilterContext> contexts = ArgumentCaptor.forClass(RepositoryFilterContext.class);
    Mockito.verify(filter, Mockito.times(1)).filterRequest(contexts.capture(), Mockito.any(RepositoryRequestFilterChain.class));
    Mockito.verify(filter, Mockito.times(1)).filterResult(Mockito.any(RepositoryFilterContext.class), Mockito.any(RepositoryResultFilterChain.class));
    Mockito.verify(filter, Mockito.times(1)).filterLinks(Mockito.any(RepositoryFilterContext.class), linksResources.capture(), Mockito.any(RepositoryLinksFilterChain.class));
    Mockito.verify(filter, Mockito.times(1)).filterMeta(Mockito.any(RepositoryFilterContext.class), metaResources.capture(), Mockito.any(RepositoryMetaFilterChain.class));
    Assert.assertEquals(1, linksResources.getAllValues().size());
    Assert.assertEquals(1, metaResources.getAllValues().size());
    Assert.assertEquals(1, contexts.getAllValues().size());
    RepositoryFilterContext context = contexts.getAllValues().iterator().next();
    RepositoryRequestSpec requestSpec = context.getRequest();
    Assert.assertEquals(queryAdapter, requestSpec.getQueryAdapter());
    Assert.assertEquals(2L, requestSpec.getId());
    Assert.assertEquals(HttpMethod.GET, requestSpec.getMethod());
    Assert.assertEquals(Arrays.asList(2L), requestSpec.getIds());
    Assert.assertSame(querySpec, requestSpec.getQuerySpec(userInfo));
}
Also used : RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 7 with RepositoryRequestSpec

use of io.crnk.core.engine.dispatcher.RepositoryRequestSpec in project crnk-framework by crnk-project.

the class RepositoryFilterTest method findOne.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void findOne() throws Exception {
    resourceAdapter.findOne(1L, queryAdapter);
    ArgumentCaptor<Iterable> linksResources = ArgumentCaptor.forClass(Iterable.class);
    ArgumentCaptor<Iterable> metaResources = ArgumentCaptor.forClass(Iterable.class);
    ArgumentCaptor<RepositoryFilterContext> contexts = ArgumentCaptor.forClass(RepositoryFilterContext.class);
    Mockito.verify(filter, Mockito.times(1)).filterRequest(contexts.capture(), Mockito.any(RepositoryRequestFilterChain.class));
    Mockito.verify(filter, Mockito.times(1)).filterResult(Mockito.any(RepositoryFilterContext.class), Mockito.any(RepositoryResultFilterChain.class));
    Mockito.verify(filter, Mockito.times(1)).filterLinks(Mockito.any(RepositoryFilterContext.class), linksResources.capture(), Mockito.any(RepositoryLinksFilterChain.class));
    Mockito.verify(filter, Mockito.times(1)).filterMeta(Mockito.any(RepositoryFilterContext.class), metaResources.capture(), Mockito.any(RepositoryMetaFilterChain.class));
    Assert.assertEquals(1, linksResources.getAllValues().size());
    Assert.assertEquals(1, metaResources.getAllValues().size());
    Assert.assertEquals(1, contexts.getAllValues().size());
    RepositoryFilterContext context = contexts.getAllValues().iterator().next();
    RepositoryRequestSpec requestSpec = context.getRequest();
    Assert.assertEquals(queryAdapter, requestSpec.getQueryAdapter());
    Assert.assertEquals(1L, requestSpec.getId());
    Assert.assertEquals(Collections.singleton(1L), requestSpec.getIds());
    Assert.assertSame(querySpec, requestSpec.getQuerySpec(userInfo));
}
Also used : RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 8 with RepositoryRequestSpec

use of io.crnk.core.engine.dispatcher.RepositoryRequestSpec in project crnk-framework by crnk-project.

the class RepositoryFilterTest method findBulkOneTargetsNoBulkImpl.

@SuppressWarnings({ "unchecked" })
@Test
public void findBulkOneTargetsNoBulkImpl() throws Exception {
    projectRelationAdapter.findBulkOneTargets(Arrays.asList(13L, 14L), assignedProjectsField, queryAdapter);
    ArgumentCaptor<RepositoryFilterContext> contexts = ArgumentCaptor.forClass(RepositoryFilterContext.class);
    Mockito.verify(filter, Mockito.times(2)).filterRequest(contexts.capture(), Mockito.any(RepositoryRequestFilterChain.class));
    Mockito.verify(filter, Mockito.times(0)).filterBulkRequest(contexts.capture(), Mockito.any(RepositoryBulkRequestFilterChain.class));
    Mockito.verify(filter, Mockito.times(2)).filterResult(Mockito.any(RepositoryFilterContext.class), Mockito.any(RepositoryResultFilterChain.class));
    Mockito.verify(filter, Mockito.times(2)).filterLinks(Mockito.any(RepositoryFilterContext.class), Mockito.any(Iterable.class), Mockito.any(RepositoryLinksFilterChain.class));
    Mockito.verify(filter, Mockito.times(2)).filterMeta(Mockito.any(RepositoryFilterContext.class), Mockito.any(Iterable.class), Mockito.any(RepositoryMetaFilterChain.class));
    Assert.assertEquals(2, contexts.getAllValues().size());
    RepositoryFilterContext context1 = contexts.getAllValues().get(0);
    RepositoryFilterContext context2 = contexts.getAllValues().get(1);
    RepositoryRequestSpec requestSpec1 = context1.getRequest();
    RepositoryRequestSpec requestSpec2 = context2.getRequest();
    Assert.assertEquals(queryAdapter, requestSpec1.getQueryAdapter());
    Assert.assertNull(requestSpec1.getEntity());
    Assert.assertEquals("assignedProjects", requestSpec1.getRelationshipField().getUnderlyingName());
    Assert.assertEquals(HttpMethod.GET, requestSpec1.getMethod());
    Assert.assertEquals(13L, requestSpec1.getId());
    Assert.assertEquals(14L, requestSpec2.getId());
    Assert.assertSame(querySpec, requestSpec1.getQuerySpec(userInfo));
}
Also used : RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 9 with RepositoryRequestSpec

use of io.crnk.core.engine.dispatcher.RepositoryRequestSpec in project crnk-framework by crnk-project.

the class RelationshipRepositoryAdapter method setRelation.

@SuppressWarnings("rawtypes")
public JsonApiResponse setRelation(T source, J targetId, ResourceField field, QueryAdapter queryAdapter) {
    RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {

        @Override
        protected JsonApiResponse invoke(RepositoryFilterContext context) {
            RepositoryRequestSpec request = context.getRequest();
            Object source = request.getEntity();
            Serializable targetId = request.getId();
            ResourceField field = request.getRelationshipField();
            QueryAdapter queryAdapter = request.getQueryAdapter();
            if (isAnnotated) {
                ((AnnotatedRelationshipRepositoryAdapter) relationshipRepository).setRelation(source, targetId, field.getUnderlyingName(), queryAdapter);
            } else if (relationshipRepository instanceof RelationshipRepositoryV2) {
                ((RelationshipRepositoryV2) relationshipRepository).setRelation(source, targetId, field.getUnderlyingName());
            } else {
                ((RelationshipRepository) relationshipRepository).setRelation(source, targetId, field.getUnderlyingName());
            }
            return new JsonApiResponse();
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forRelation(moduleRegistry, HttpMethod.PATCH, source, queryAdapter, Arrays.asList(targetId), field);
    return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) Serializable(java.io.Serializable) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) AnnotatedRelationshipRepositoryAdapter(io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) RelationshipRepositoryV2(io.crnk.core.repository.RelationshipRepositoryV2) BulkRelationshipRepositoryV2(io.crnk.core.repository.BulkRelationshipRepositoryV2)

Example 10 with RepositoryRequestSpec

use of io.crnk.core.engine.dispatcher.RepositoryRequestSpec in project crnk-framework by crnk-project.

the class RelationshipRepositoryAdapter method addRelations.

@SuppressWarnings("rawtypes")
public JsonApiResponse addRelations(T source, Iterable<J> targetIds, ResourceField field, QueryAdapter queryAdapter) {
    RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {

        @Override
        protected JsonApiResponse invoke(RepositoryFilterContext context) {
            RepositoryRequestSpec request = context.getRequest();
            Object source = request.getEntity();
            Iterable<?> targetIds = request.getIds();
            ResourceField field = request.getRelationshipField();
            QueryAdapter queryAdapter = request.getQueryAdapter();
            if (isAnnotated) {
                ((AnnotatedRelationshipRepositoryAdapter) relationshipRepository).addRelations(source, targetIds, field.getUnderlyingName(), queryAdapter);
            } else if (relationshipRepository instanceof RelationshipRepositoryV2) {
                ((RelationshipRepositoryV2) relationshipRepository).addRelations(source, targetIds, field.getUnderlyingName());
            } else {
                ((RelationshipRepository) relationshipRepository).addRelations(source, targetIds, field.getUnderlyingName());
            }
            return new JsonApiResponse();
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forRelation(moduleRegistry, HttpMethod.POST, source, queryAdapter, targetIds, field);
    return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) AnnotatedRelationshipRepositoryAdapter(io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) RelationshipRepositoryV2(io.crnk.core.repository.RelationshipRepositoryV2) BulkRelationshipRepositoryV2(io.crnk.core.repository.BulkRelationshipRepositoryV2)

Aggregations

RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)33 ResourceRegistryTest (io.crnk.core.resource.registry.ResourceRegistryTest)17 Test (org.junit.Test)17 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)13 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)12 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)10 ResourceField (io.crnk.core.engine.information.resource.ResourceField)8 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)8 RelationshipRepositoryV2 (io.crnk.core.repository.RelationshipRepositoryV2)6 Serializable (java.io.Serializable)6 ResourceRepositoryV2 (io.crnk.core.repository.ResourceRepositoryV2)5 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)4 AnnotatedRelationshipRepositoryAdapter (io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter)4 ResourceRepository (io.crnk.legacy.repository.ResourceRepository)4 QuerySpec (io.crnk.core.queryspec.QuerySpec)3 HashMap (java.util.HashMap)3 User (io.crnk.core.mock.models.User)2 RelationshipRepository (io.crnk.legacy.repository.RelationshipRepository)2 Span (brave.Span)1 Tracer (brave.Tracer)1