Search in sources :

Example 1 with ProjectPolymorphicToObjectRepository

use of io.crnk.core.mock.repository.ProjectPolymorphicToObjectRepository in project crnk-framework by crnk-project.

the class RelationshipsResourceGetTest method supportPolymorphicRelationshipTypes.

@Test
public void supportPolymorphicRelationshipTypes() throws JsonProcessingException {
    // GIVEN
    Long projectId = 1L;
    String type = ClassUtils.getAnnotation(ProjectPolymorphic.class, JsonApiResource.class).get().type();
    ProjectPolymorphic projectPolymorphic = new ProjectPolymorphic();
    projectPolymorphic.setId(projectId);
    ProjectPolymorphicToObjectRepository projectPolymorphicToObjectRepository = new ProjectPolymorphicToObjectRepository();
    projectPolymorphicToObjectRepository.setRelation(projectPolymorphic, 42L, "task");
    JsonPath jsonPath = pathBuilder.build("/" + type + "/" + projectId + "/relationships/task");
    RelationshipsResourceGet resourceGet = new RelationshipsResourceGet(resourceRegistry, typeParser, documentMapper);
    // WHEN
    Response baseResponseContext = resourceGet.handle(jsonPath, emptyTaskQuery, null, null);
    // THEN
    Assert.assertNotNull(baseResponseContext);
    String resultJson = objectMapper.writeValueAsString(baseResponseContext.getDocument());
    assertThatJson(resultJson).node("data.id").isStringEqualTo("42");
    assertThatJson(resultJson).node("data.type").isEqualTo("tasks");
    // GIVEN
    projectPolymorphicToObjectRepository.setRelations(projectPolymorphic, Arrays.asList(44L, 45L), "tasks");
    jsonPath = pathBuilder.build("/" + type + "/" + projectId + "/relationships/tasks");
    resourceGet = new RelationshipsResourceGet(resourceRegistry, typeParser, documentMapper);
    // WHEN
    baseResponseContext = resourceGet.handle(jsonPath, emptyTaskQuery, null, null);
    Assert.assertNotNull(baseResponseContext);
    resultJson = objectMapper.writeValueAsString(baseResponseContext.getDocument());
    ReadContext resultCtx = com.jayway.jsonpath.JsonPath.parse(resultJson);
    assertIncludeDoNotCareAboutOrder(new ArrayList<>(Arrays.asList("44", "45")), Arrays.asList(0, 1), resultCtx);
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) ProjectPolymorphic(io.crnk.core.mock.models.ProjectPolymorphic) ReadContext(com.jayway.jsonpath.ReadContext) ProjectPolymorphicToObjectRepository(io.crnk.core.mock.repository.ProjectPolymorphicToObjectRepository) RelationshipsResourceGet(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourceGet) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Aggregations

ReadContext (com.jayway.jsonpath.ReadContext)1 Response (io.crnk.core.engine.dispatcher.Response)1 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)1 RelationshipsResourceGet (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourceGet)1 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)1 ProjectPolymorphic (io.crnk.core.mock.models.ProjectPolymorphic)1 ProjectPolymorphicToObjectRepository (io.crnk.core.mock.repository.ProjectPolymorphicToObjectRepository)1 Test (org.junit.Test)1