use of io.crnk.core.engine.dispatcher.RepositoryRequestSpec in project crnk-framework by crnk-project.
the class ResourceRepositoryAdapter method save.
private <S extends T> JsonApiResponse save(S entity, QueryAdapter queryAdapter, final HttpMethod method) {
RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {
@SuppressWarnings("rawtypes")
@Override
protected JsonApiResponse invoke(RepositoryFilterContext context) {
RepositoryRequestSpec request = context.getRequest();
Object entity = request.getEntity();
Object resource;
if (isAnnotated) {
resource = ((AnnotatedResourceRepositoryAdapter) resourceRepository).save(entity);
} else if (resourceRepository instanceof ResourceRepositoryV2) {
if (method == HttpMethod.POST) {
resource = ((ResourceRepositoryV2) resourceRepository).create(entity);
} else {
resource = ((ResourceRepositoryV2) resourceRepository).save(entity);
}
} else {
resource = ((ResourceRepository) resourceRepository).save(entity);
}
return getResponse(resourceRepository, resource, request);
}
};
RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forSave(moduleRegistry, method, resourceInformation, queryAdapter, entity);
return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
use of io.crnk.core.engine.dispatcher.RepositoryRequestSpec in project crnk-framework by crnk-project.
the class ResourceRepositoryAdapter method findAll.
public JsonApiResponse findAll(QueryAdapter queryAdapter) {
RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {
@SuppressWarnings("rawtypes")
@Override
protected JsonApiResponse invoke(RepositoryFilterContext context) {
RepositoryRequestSpec request = context.getRequest();
QueryAdapter queryAdapter = request.getQueryAdapter();
Object resources;
if (isAnnotated) {
resources = ((AnnotatedResourceRepositoryAdapter) resourceRepository).findAll(queryAdapter);
} else if (resourceRepository instanceof ResourceRepositoryV2) {
QuerySpec querySpec = request.getQuerySpec(resourceInformation);
resources = ((ResourceRepositoryV2) resourceRepository).findAll(querySpec);
} else {
resources = ((ResourceRepository) resourceRepository).findAll(request.getQueryParams());
}
return getResponse(resourceRepository, resources, request);
}
};
RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forFindAll(moduleRegistry, resourceInformation, queryAdapter);
return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
use of io.crnk.core.engine.dispatcher.RepositoryRequestSpec in project crnk-framework by crnk-project.
the class RepositoryFilterTest method create.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void create() throws Exception {
User user = new User();
user.setId(3L);
resourceAdapter.create(user, 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(3L, requestSpec.getId());
Assert.assertEquals(HttpMethod.POST, requestSpec.getMethod());
Assert.assertEquals(Collections.singleton(3L), requestSpec.getIds());
Assert.assertSame(querySpec, requestSpec.getQuerySpec(userInfo));
}
use of io.crnk.core.engine.dispatcher.RepositoryRequestSpec in project crnk-framework by crnk-project.
the class RepositoryFilterTest method findAllWithResourceList.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void findAllWithResourceList() throws Exception {
resourceAdapter.findAll(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.assertNull(requestSpec.getId());
Assert.assertNull(requestSpec.getIds());
Assert.assertSame(querySpec, requestSpec.getQuerySpec(userInfo));
}
use of io.crnk.core.engine.dispatcher.RepositoryRequestSpec in project crnk-framework by crnk-project.
the class RepositoryFilterTest method save.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void save() throws Exception {
User user = new User();
user.setId(3L);
resourceAdapter.update(user, 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(3L, requestSpec.getId());
Assert.assertEquals(HttpMethod.PATCH, requestSpec.getMethod());
Assert.assertEquals(Collections.singleton(3L), requestSpec.getIds());
Assert.assertSame(querySpec, requestSpec.getQuerySpec(userInfo));
}
Aggregations