use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.
the class ResourcePostTest method onResourceWithInvalidRelationshipNameShouldThrowException.
@Test
public void onResourceWithInvalidRelationshipNameShouldThrowException() throws Exception {
// GIVEN - creating sample project id
Document newProjectBody = new Document();
Resource data = createProject();
newProjectBody.setData(Nullable.of((Object) data));
JsonPath projectPath = pathBuilder.build("/projects");
ResourcePost sut = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
// WHEN
Response projectResponse = sut.handle(projectPath, emptyTaskQuery, 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());
/* ------- */
// GIVEN
Document pojoBody = new Document();
Resource pojoData = new Resource();
pojoBody.setData(Nullable.of((Object) pojoData));
pojoData.setType("pojo");
JsonNode put = objectMapper.createObjectNode().put("value", "hello");
pojoData.setAttribute("other-pojo", objectMapper.readTree("null"));
String invalidRelationshipName = "invalid-relationship";
pojoData.getRelationships().put(invalidRelationshipName, new Relationship(new ResourceIdentifier(Long.toString(projectId), "projects")));
JsonPath pojoPath = pathBuilder.build("/pojo");
// THEN
expectedException.expect(ResourceException.class);
expectedException.expectMessage(String.format("Invalid relationship name: %s", invalidRelationshipName));
// WHEN
sut.handle(pojoPath, new QuerySpecAdapter(new QuerySpec(Pojo.class), resourceRegistry), null, pojoBody);
}
use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.
the class DocumentMapperTest method testMultipleInclusions.
@Test
public void testMultipleInclusions() {
Task task1 = createTask(1, "other task");
Task task2 = createTask(2, "other task");
Task task3 = createTask(3, "sample task");
Project project = new Project();
project.setName("someProject");
project.setId(3L);
project.setTask(task1);
project.setTasks(Arrays.asList(task2, task3));
QuerySpec querySpec = new QuerySpec(Project.class);
querySpec.includeRelation(Arrays.asList("tasks"));
querySpec.includeRelation(Arrays.asList("task"));
QuerySpecAdapter queryAdapter = (QuerySpecAdapter) toAdapter(querySpec);
Document document = mapper.toDocument(toResponse(project), queryAdapter);
Resource resource = document.getSingleData().get();
Assert.assertEquals("3", resource.getId());
Assert.assertEquals("projects", resource.getType());
Relationship taskRel = resource.getRelationships().get("task");
Assert.assertNotNull(taskRel.getLinks());
Assert.assertTrue(taskRel.getData().isPresent());
Assert.assertNotNull(taskRel.getData().get());
Relationship tasksRel = resource.getRelationships().get("tasks");
Assert.assertNotNull(tasksRel.getLinks());
Assert.assertTrue(tasksRel.getData().isPresent());
Assert.assertNotNull(tasksRel.getData().get());
Assert.assertEquals(2, tasksRel.getCollectionData().get().size());
Assert.assertEquals(3, document.getIncluded().size());
}
use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.
the class DocumentMapperTest method testCompactMode.
@Test
public void testCompactMode() {
LinksInformation links = new TaskLinks();
Task task = createTask(2, "sample task");
task.setLinksInformation(links);
QuerySpecAdapter queryAdapter = (QuerySpecAdapter) toAdapter(new QuerySpec(Task.class));
queryAdapter.setCompactMode(true);
Document document = mapper.toDocument(toResponse(task), queryAdapter);
Resource resource = document.getSingleData().get();
Assert.assertEquals("2", resource.getId());
Assert.assertEquals("tasks", resource.getType());
Assert.assertNull(resource.getLinks().get("self"));
Assert.assertNotNull(resource.getLinks().get("someLink"));
Relationship project = resource.getRelationships().get("project");
Assert.assertNull(project.getLinks());
}
use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.
the class IncludeLookupSetterInheritanceTest method setup.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Before
public void setup() {
super.setup();
// get repositories
ResourceRepositoryAdapter taskRepository = resourceRegistry.getEntry(Task.class).getResourceRepository(null);
RelationshipRepositoryAdapter relRepositoryTaskToProject = resourceRegistry.getEntry(Task.class).getRelationshipRepository("projects", null);
ResourceRepositoryAdapter projectRepository = resourceRegistry.getEntry(Project.class).getResourceRepository(null);
// setup test data
ResourceInformation taskInfo = resourceRegistry.getEntry(Task.class).getResourceInformation();
ResourceField includedProjectsField = taskInfo.findRelationshipFieldByName("includedProjects");
QueryAdapter emptyProjectQuery = new QuerySpecAdapter(new QuerySpec(Project.class), resourceRegistry);
QueryAdapter emptyTaskQuery = new QuerySpecAdapter(new QuerySpec(Task.class), resourceRegistry);
Project project1 = new Project();
project1.setId(3L);
projectRepository.create(project1, emptyProjectQuery);
FancyProject project2 = new FancyProject();
project2.setId(ProjectRepository.FANCY_PROJECT_ID);
projectRepository.create(project2, emptyProjectQuery);
Task task = new Task();
task.setId(1L);
taskRepository.create(task, emptyTaskQuery);
relRepositoryTaskToProject.addRelations(task, Collections.singletonList(project1.getId()), includedProjectsField, emptyProjectQuery);
relRepositoryTaskToProject.addRelations(task, Collections.singletonList(project2.getId()), includedProjectsField, emptyProjectQuery);
}
use of io.crnk.core.queryspec.internal.QuerySpecAdapter in project crnk-framework by crnk-project.
the class SleuthUtil method getQuery.
public static String getQuery(RepositoryRequestSpec request, ResourceRegistry resourceRegistry) {
QueryAdapter queryAdapter = request.getQueryAdapter();
StringBuilder builder = new StringBuilder();
builder.append("?");
if (queryAdapter instanceof QuerySpecAdapter) {
QuerySpec querySpec = request.getQuerySpec(queryAdapter.getResourceInformation());
DefaultQuerySpecSerializer serializer = new DefaultQuerySpecSerializer(resourceRegistry);
Map<String, Set<String>> parameters = serializer.serialize(querySpec);
for (Map.Entry<String, Set<String>> entry : parameters.entrySet()) {
if (builder.length() > 1) {
builder.append("&");
}
builder.append(entry.getKey());
builder.append("=");
builder.append(StringUtils.join(",", entry.getValue()));
}
return builder.toString();
}
return null;
}
Aggregations