use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class ImplicitOwnerBasedRelationshipRepository method findTargets.
@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
RegistryEntry sourceEntry = getSourceEntry();
ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
List sources = (List) sourceEntry.getResourceRepository().findAll(sourceIds, getSaveQueryAdapter(fieldName)).getEntity();
ResourceInformation targetInformation = getTargetInformation(field);
List<D> targets;
if (field.hasIdField()) {
Set targetIds = new HashSet();
for (Object source : sources) {
Object targetId = field.getIdAccessor().getValue(source);
if (field.isCollection()) {
targetIds.addAll((Collection) targetId);
} else {
targetIds.add(targetId);
}
}
QuerySpec idQuerySpec = new QuerySpec(targetInformation);
ResourceRepositoryAdapter<D, J> targetAdapter = getTargetEntry(field).getResourceRepository();
JsonApiResponse response = targetAdapter.findAll(targetIds, new QuerySpecAdapter(idQuerySpec, resourceRegistry));
targets = (List<D>) response.getEntity();
return toResult(fieldName, targetInformation, sources, targets);
} else {
MultivaluedMap bulkResult = new MultivaluedMap<I, D>() {
@Override
protected List<D> newList() {
return new DefaultResourceList<>();
}
};
for (Object source : sources) {
Object sourceId = sourceInformation.getId(source);
Object target = field.getAccessor().getValue(source);
if (target != null) {
if (field.isCollection()) {
bulkResult.addAll(sourceId, (Collection) target);
} else {
bulkResult.add(sourceId, target);
}
}
}
return bulkResult;
}
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class GetFromOppositeStrategy method findTargets.
@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
RegistryEntry sourceEntry = context.getSourceEntry();
ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
RegistryEntry targetEntry = context.getTargetEntry(field);
ResourceInformation targetInformation = targetEntry.getResourceInformation();
ResourceField oppositeField = Objects.requireNonNull(targetInformation.findFieldByUnderlyingName(field.getOppositeName()));
QuerySpec idQuerySpec = querySpec.duplicate();
idQuerySpec.addFilter(new FilterSpec(Arrays.asList(oppositeField.getUnderlyingName(), sourceInformation.getIdField().getUnderlyingName()), FilterOperator.EQ, sourceIds));
idQuerySpec.includeRelation(Arrays.asList(oppositeField.getUnderlyingName()));
ResourceRepositoryAdapter<D, J> targetAdapter = targetEntry.getResourceRepository();
JsonApiResponse response = targetAdapter.findAll(context.createQueryAdapter(idQuerySpec));
Collection<D> results = (Collection<D>) response.getEntity();
MultivaluedMap<I, D> bulkResult = new MultivaluedMap<I, D>() {
@Override
protected List<D> newList() {
return new DefaultResourceList<>();
}
};
Set<I> sourceIdSet = new HashSet<>();
for (I sourceId : sourceIds) {
sourceIdSet.add(sourceId);
}
for (D result : results) {
handleTarget(bulkResult, result, sourceIdSet, oppositeField, sourceInformation);
}
return bulkResult;
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class CustomResourceRegistryTest method test.
@Test
public void test() {
CrnkBoot boot = new CrnkBoot();
boot.addModule(new CustomRegistryPartModule());
boot.setServiceDiscovery(new TestServiceDiscovery());
boot.boot();
ResourceRegistry resourceRegistry = boot.getResourceRegistry();
RegistryEntry entry = resourceRegistry.getEntry("somePrefix/custom");
Assert.assertNotNull(entry);
ResourceRepositoryAdapter adapter = entry.getResourceRepository();
QueryAdapter queryAdapter = new QuerySpecAdapter(new QuerySpec("somePrefix/custom"), resourceRegistry);
JsonApiResponse response = adapter.findAll(queryAdapter);
Assert.assertNotNull(response.getEntity());
List<Resource> resources = (List<Resource>) response.getEntity();
Assert.assertEquals(1, resources.size());
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class BraveResponseFilterTest method statusCodeNotOkWhenEmptyErrors.
@Test
public void statusCodeNotOkWhenEmptyErrors() {
ErrorData errorData = new ErrorDataBuilder().setId("test").build();
JsonApiResponse response = new JsonApiResponse();
response.setErrors(Arrays.asList(errorData));
Mockito.when(filterChain.doFilter(Mockito.any(RepositoryFilterContext.class))).thenReturn(response);
filter.filterRequest(filterContext, filterChain);
Mockito.verify(span, Mockito.times(1)).tag(BraveRepositoryFilter.STATUS_CODE_ANNOTATION, BraveRepositoryFilter.STRING_EXCEPTION);
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class BraveResponseFilterTest method statusCodeOkWhenNpErrors.
@Test
public void statusCodeOkWhenNpErrors() {
JsonApiResponse response = new JsonApiResponse();
Mockito.when(filterChain.doFilter(Mockito.any(RepositoryFilterContext.class))).thenReturn(response);
filter.filterRequest(filterContext, filterChain);
Mockito.verify(span, Mockito.times(1)).tag(BraveRepositoryFilter.STATUS_CODE_ANNOTATION, BraveRepositoryFilter.STRING_OK);
}
Aggregations