Search in sources :

Example 21 with QuerySpecAdapter

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);
}
Also used : Resource(io.crnk.core.engine.document.Resource) JsonNode(com.fasterxml.jackson.databind.JsonNode) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) Response(io.crnk.core.engine.dispatcher.Response) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Relationship(io.crnk.core.engine.document.Relationship) QuerySpec(io.crnk.core.queryspec.QuerySpec) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 22 with QuerySpecAdapter

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());
}
Also used : Project(io.crnk.core.mock.models.Project) LazyTask(io.crnk.core.mock.models.LazyTask) Task(io.crnk.core.mock.models.Task) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) QuerySpec(io.crnk.core.queryspec.QuerySpec) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Example 23 with QuerySpecAdapter

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());
}
Also used : LazyTask(io.crnk.core.mock.models.LazyTask) Task(io.crnk.core.mock.models.Task) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) LinksInformation(io.crnk.core.resource.links.LinksInformation) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Example 24 with QuerySpecAdapter

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);
}
Also used : Project(io.crnk.core.mock.models.Project) FancyProject(io.crnk.core.mock.models.FancyProject) ResourceField(io.crnk.core.engine.information.resource.ResourceField) Task(io.crnk.core.mock.models.Task) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) FancyProject(io.crnk.core.mock.models.FancyProject) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) Before(org.junit.Before)

Example 25 with QuerySpecAdapter

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;
}
Also used : Set(java.util.Set) DefaultQuerySpecSerializer(io.crnk.core.queryspec.DefaultQuerySpecSerializer) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec) Map(java.util.Map)

Aggregations

QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)48 QuerySpec (io.crnk.core.queryspec.QuerySpec)32 Test (org.junit.Test)25 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)15 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)13 Task (io.crnk.core.mock.models.Task)11 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)11 PagedLinksInformation (io.crnk.core.resource.links.PagedLinksInformation)11 Document (io.crnk.core.engine.document.Document)10 Resource (io.crnk.core.engine.document.Resource)8 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)8 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)7 Before (org.junit.Before)7 Relationship (io.crnk.core.engine.document.Relationship)6 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)6 Project (io.crnk.core.mock.models.Project)6 HasMoreResourcesMetaInformation (io.crnk.core.resource.meta.HasMoreResourcesMetaInformation)5 CrnkBoot (io.crnk.core.boot.CrnkBoot)4 Response (io.crnk.core.engine.dispatcher.Response)4 ResourceField (io.crnk.core.engine.information.resource.ResourceField)4