use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.
the class CollectionGetTest method onGivenRequestResourceShouldNotLoadAutoIncludeFields.
@Test
public void onGivenRequestResourceShouldNotLoadAutoIncludeFields() throws Exception {
// GIVEN
Document newTaskBody = new Document();
Resource data = createTask();
newTaskBody.setData(Nullable.of((Object) data));
data.setType("tasks");
JsonPath taskPath = pathBuilder.build("/tasks");
ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, new ArrayList<ResourceModificationFilter>());
// WHEN -- adding a task
Response taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
// THEN
assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
Long taskId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
assertThat(taskId).isNotNull();
/* ------- */
// GIVEN
Document newProjectBody = new Document();
data = createProject();
newProjectBody.setData(Nullable.of((Object) data));
JsonPath projectPath = pathBuilder.build("/projects");
// WHEN -- adding a project
Response projectResponse = resourcePost.handle(projectPath, emptyProjectQuery, null, newProjectBody);
// THEN
assertThat(projectResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects");
assertThat(projectResponse.getDocument().getSingleData().get().getId()).isNotNull();
assertThat(projectResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample project");
Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
assertThat(projectId).isNotNull();
/* ------- */
// GIVEN
Document newTaskToProjectBody = new Document();
data = new Resource();
newTaskToProjectBody.setData(Nullable.of((Object) Collections.singletonList(data)));
data.setType("projects");
data.setId(projectId.toString());
JsonPath savedTaskPath = pathBuilder.build("/tasks/" + taskId + "/relationships/projects");
RelationshipsResourcePost sut = new RelationshipsResourcePost(resourceRegistry, typeParser, new ArrayList<ResourceModificationFilter>());
// WHEN -- adding a relation between task and project
Response projectRelationshipResponse = sut.handle(savedTaskPath, emptyProjectQuery, null, newTaskToProjectBody);
assertThat(projectRelationshipResponse).isNotNull();
// THEN
TaskToProjectRepository taskToProjectRepository = new TaskToProjectRepository();
Project project = taskToProjectRepository.findOneTarget(taskId, "projects", new QueryParams());
assertThat(project.getId()).isNotNull();
// Given
JsonPath jsonPath = pathBuilder.build("/tasks/" + taskId);
ResourceGet responseGetResp = new ResourceGet(resourceRegistry, typeParser, documentMapper);
Map<String, Set<String>> queryParams = new HashMap<>();
queryParams.put(RestrictedQueryParamsMembers.include.name() + "[tasks]", Collections.singleton("[\"projects\"]"));
QueryParams requestParams = new QueryParamsBuilder(new DefaultQueryParamsParser()).buildQueryParams(queryParams);
// WHEN
Response response = responseGetResp.handle(jsonPath, new QueryParamsAdapter(resourceRegistry.getEntry(Task.class).getResourceInformation(), requestParams, moduleRegistry), null, null);
// THEN
Assert.assertNotNull(response);
assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
// eager loading but no inclusion
RegistryEntry entry = resourceRegistry.getEntry(Task.class);
ResourceField projectsField = entry.getResourceInformation().findFieldByUnderlyingName("projects");
Assert.assertEquals(SerializeType.ONLY_ID, projectsField.getSerializeType());
assertThat(response.getDocument().getSingleData().get().getRelationships().get("projects").getData().isPresent()).isTrue();
}
use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.
the class ResourceFilterTest method checkFilterGetOnResourceField.
@Test
public void checkFilterGetOnResourceField() {
// setup test data
RegistryEntry entry = resourceRegistry.getEntry(Task.class);
ResourceRepositoryAdapter resourceRepository = entry.getResourceRepository();
Project project = new Project();
project.setId(13L);
project.setName("myProject");
Task task = new Task();
task.setId(12L);
task.setName("myTask");
task.setProject(project);
resourceRepository.create(task, new QuerySpecAdapter(new QuerySpec(Task.class), resourceRegistry));
// get information
ResourceInformation resourceInformation = entry.getResourceInformation();
ResourceField projectField = resourceInformation.findFieldByUnderlyingName("project");
ResourceField nameField = resourceInformation.findFieldByUnderlyingName("name");
String path = "/tasks/";
String method = HttpMethod.GET.toString();
Map<String, Set<String>> parameters = Collections.emptyMap();
Document requestBody = null;
// forbid field
Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.FORBIDDEN);
Response response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
Assert.assertEquals(HttpStatus.OK_200, response.getHttpStatus().intValue());
Resource taskResource = response.getDocument().getCollectionData().get().get(0);
Assert.assertTrue(taskResource.getRelationships().containsKey("projects"));
Assert.assertFalse(taskResource.getRelationships().containsKey("project"));
Assert.assertTrue(taskResource.getAttributes().containsKey("name"));
// allow resource
Mockito.when(filter.filterField(Mockito.eq(nameField), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.FORBIDDEN);
response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
Assert.assertEquals(HttpStatus.OK_200, response.getHttpStatus().intValue());
taskResource = response.getDocument().getCollectionData().get().get(0);
Assert.assertTrue(taskResource.getRelationships().containsKey("projects"));
Assert.assertFalse(taskResource.getRelationships().containsKey("project"));
Assert.assertFalse(taskResource.getAttributes().containsKey("name"));
}
use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.
the class CrnkBootTest method boot.
@Test
public void boot() {
CrnkBoot boot = new CrnkBoot();
boot.setDefaultServiceUrlProvider(new ServiceUrlProvider() {
@Override
public String getUrl() {
return "http://127.0.0.1";
}
});
boot.setServiceDiscovery(new ReflectionsServiceDiscovery(MockConstants.TEST_MODELS_PACKAGE));
boot.addModule(new SimpleModule("test"));
boot.boot();
RequestDispatcher requestDispatcher = boot.getRequestDispatcher();
ResourceRegistry resourceRegistry = boot.getResourceRegistry();
RegistryEntry taskEntry = resourceRegistry.getEntry(Task.class);
Assert.assertNotEquals(0, taskEntry.getRelationshipEntries().size());
ResourceRepositoryAdapter<?, ?> repositoryAdapter = taskEntry.getResourceRepository(null);
Assert.assertNotNull(repositoryAdapter.getResourceRepository());
JsonApiResponse response = repositoryAdapter.findAll(new QueryParamsAdapter(taskEntry.getResourceInformation(), new QueryParams(), boot.getModuleRegistry()));
Assert.assertNotNull(response);
Assert.assertNotNull(requestDispatcher);
ServiceDiscovery serviceDiscovery = boot.getServiceDiscovery();
Assert.assertNotNull(serviceDiscovery);
Assert.assertNotNull(boot.getModuleRegistry());
Assert.assertNotNull(boot.getExceptionMapperRegistry());
List<Module> modules = boot.getModuleRegistry().getModules();
Assert.assertEquals(4, modules.size());
boot.setDefaultPageLimit(20L);
boot.setMaxPageLimit(100L);
Assert.assertEquals(1, boot.getPagingBehaviors().size());
Assert.assertTrue(boot.getPagingBehaviors().get(0) instanceof OffsetLimitPagingBehavior);
}
use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.
the class RepositoryFilterTest method prepare.
@Before
public void prepare() {
boot = new CrnkBoot();
boot.setServiceDiscovery(new ReflectionsServiceDiscovery(MockConstants.TEST_MODELS_PACKAGE));
boot.setServiceUrlProvider(new ConstantServiceUrlProvider(ResourceRegistryTest.TEST_MODELS_URL));
SimpleModule filterModule = new SimpleModule("filter");
filterModule.addRepositoryFilter(filter);
boot.addModule(filterModule);
boot.boot();
resourceRegistry = boot.getResourceRegistry();
querySpec = new QuerySpec(User.class);
querySpec.setPagingSpec(new OffsetLimitPagingSpec());
queryAdapter = new QuerySpecAdapter(querySpec, resourceRegistry);
scheduleInfo = resourceRegistry.getEntry(Schedule.class).getResourceInformation();
RegistryEntry userEntry = resourceRegistry.getEntry(User.class);
resourceAdapter = userEntry.getResourceRepository(null);
projectRelationAdapter = userEntry.getRelationshipRepository("assignedProjects", null);
taskRelationAdapter = userEntry.getRelationshipRepository("assignedTasks", null);
userInfo = userEntry.getResourceInformation();
UserRepository resourceRepository = (UserRepository) resourceAdapter.getResourceRepository();
user1 = new User();
user1.setId(1L);
resourceRepository.save(user1);
user2 = new User();
user2.setId(2L);
resourceRepository.save(user2);
UserToProjectRepository userProjectRepository = (UserToProjectRepository) ((AnnotatedRelationshipRepositoryAdapter<?, ?, ?, ?>) projectRelationAdapter.getRelationshipRepository()).getImplementationObject();
userProjectRepository.setRelation(user1, 11L, "assignedProjects");
UserToTaskRepository userTaskRepository = new UserToTaskRepository();
userTaskRepository.addRelations(user1, Arrays.asList(21L), "assignedTasks");
userTaskRepository.addRelations(user2, Arrays.asList(22L), "assignedTasks");
assignedTasksField = resourceRegistry.getEntry(User.class).getResourceInformation().findRelationshipFieldByName("assignedTasks");
assignedProjectsField = resourceRegistry.getEntry(User.class).getResourceInformation().findRelationshipFieldByName("assignedProjects");
}
use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.
the class GetFromOwnerStrategy 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);
List sources = (List) sourceEntry.getResourceRepository().findAll(sourceIds, context.createSaveQueryAdapter(fieldName)).getEntity();
ResourceInformation targetInformation = targetEntry.getResourceInformation();
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 = targetEntry.getResourceRepository();
JsonApiResponse response = targetAdapter.findAll(targetIds, context.createQueryAdapter(idQuerySpec));
targets = (List<D>) response.getEntity();
return toResult(field, 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;
}
}
Aggregations