Search in sources :

Example 1 with JsonApiResponse

use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.

the class RelationshipRepositoryBase method findTargets.

@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
    RegistryEntry sourceEntry = resourceRegistry.findEntry(sourceResourceClass);
    ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
    ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
    RegistryEntry targetEntry = getTargetEntry(field);
    String oppositeName = getOppositeName(fieldName);
    QuerySpec idQuerySpec = querySpec.duplicate();
    idQuerySpec.addFilter(new FilterSpec(Arrays.asList(oppositeName, sourceInformation.getIdField().getUnderlyingName()), FilterOperator.EQ, sourceIds));
    idQuerySpec.includeRelation(Arrays.asList(oppositeName));
    ResourceRepositoryAdapter<D, J> targetAdapter = targetEntry.getResourceRepository();
    JsonApiResponse response = targetAdapter.findAll(new QuerySpecAdapter(idQuerySpec, resourceRegistry));
    List<D> results = (List<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, oppositeName, sourceInformation);
    }
    return bulkResult;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) FilterSpec(io.crnk.core.queryspec.FilterSpec) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceField(io.crnk.core.engine.information.resource.ResourceField) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) ResourceList(io.crnk.core.resource.list.ResourceList) ArrayList(java.util.ArrayList) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) List(java.util.List) QuerySpec(io.crnk.core.queryspec.QuerySpec) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) HashSet(java.util.HashSet)

Example 2 with JsonApiResponse

use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.

the class JsonApiRequestProcessorTest method createRequestBody.

private String createRequestBody(String name) throws JsonProcessingException {
    Task task = new Task();
    task.setId(1L);
    task.setName(name);
    task.setCategory("testCategory");
    JsonApiResponse request = new JsonApiResponse();
    request.setEntity(task);
    Document requestDocument = boot.getDocumentMapper().toDocument(request, new QuerySpecAdapter(new QuerySpec(Task.class), boot.getResourceRegistry()));
    return boot.getObjectMapper().writeValueAsString(requestDocument);
}
Also used : Task(io.crnk.core.mock.models.Task) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Document(io.crnk.core.engine.document.Document) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec)

Example 3 with JsonApiResponse

use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.

the class ResourcePatchTest method omittedFieldsShouldBeIgnored.

@Test
public void omittedFieldsShouldBeIgnored() throws Exception {
    // GIVEN
    ResourceRepositoryAdapter taskRepo = resourceRegistry.getEntry(Task.class).getResourceRepository(null);
    Task task = new Task();
    task.setName("Mary Joe");
    JsonApiResponse jsonApiResponse = taskRepo.create(task, emptyTaskQuery);
    task = (Task) (jsonApiResponse.getEntity());
    // GIVEN
    Resource data = new Resource();
    data.setType("tasks");
    data.setAttribute("category", objectMapper.readTree("\"TestCategory\""));
    Document taskPatch = new Document();
    taskPatch.setData(Nullable.of((Object) data));
    JsonPath jsonPath = pathBuilder.build("/tasks/" + task.getId());
    ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response response = sut.handle(jsonPath, emptyTaskQuery, null, taskPatch);
    // THEN
    Assert.assertNotNull(response);
    assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    Resource updatedTask = response.getDocument().getSingleData().get();
    assertThat(updatedTask.getAttributes().get("name").asText()).isEqualTo("Mary Joe");
    assertThat(updatedTask.getAttributes().get("category").asText()).isEqualTo("TestCategory");
    assertThat(updatedTask.getId()).isEqualTo(task.getId().toString());
    Assert.assertEquals("Mary Joe", task.getName());
    Assert.assertEquals("TestCategory", task.getCategory());
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) Task(io.crnk.core.mock.models.Task) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) Resource(io.crnk.core.engine.document.Resource) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 4 with JsonApiResponse

use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.

the class ResourcePatchTest method omittedFieldsSettersAreNotCalled.

/*
	 * see github #122
	 */
@Test
public void omittedFieldsSettersAreNotCalled() throws Exception {
    // GIVEN
    ResourceRepositoryAdapter taskRepo = resourceRegistry.getEntry(Task.class).getResourceRepository(null);
    Task task = new Task();
    task.setName("Mary Joe");
    JsonApiResponse jsonApiResponse = taskRepo.create(task, emptyTaskQuery);
    task = (Task) (jsonApiResponse.getEntity());
    // GIVEN
    Document taskPatch = new Document();
    Resource data = new Resource();
    taskPatch.setData(Nullable.of((Object) data));
    data.setType("tasks");
    data.setAttribute("name", objectMapper.readTree("\"Mary Jane\""));
    JsonPath jsonPath = pathBuilder.build("/tasks/" + task.getId());
    ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response response = sut.handle(jsonPath, emptyTaskQuery, null, taskPatch);
    // THEN
    Assert.assertNotNull(response);
    assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    Resource updatedTask = response.getDocument().getSingleData().get();
    assertThat(updatedTask.getAttributes().get("name").asText()).isEqualTo("Mary Jane");
    assertThat(updatedTask.getId()).isEqualTo(task.getId().toString());
    assertThat(updatedTask.getAttributes().get("category")).isNull();
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) Task(io.crnk.core.mock.models.Task) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) Resource(io.crnk.core.engine.document.Resource) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 5 with JsonApiResponse

use of io.crnk.core.repository.response.JsonApiResponse 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);
}
Also used : OffsetLimitPagingBehavior(io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior) ConstantServiceUrlProvider(io.crnk.core.engine.url.ConstantServiceUrlProvider) ServiceUrlProvider(io.crnk.core.engine.url.ServiceUrlProvider) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) RequestDispatcher(io.crnk.core.engine.dispatcher.RequestDispatcher) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QueryParams(io.crnk.legacy.queryParams.QueryParams) ReflectionsServiceDiscovery(io.crnk.core.module.discovery.ReflectionsServiceDiscovery) QueryParamsAdapter(io.crnk.legacy.internal.QueryParamsAdapter) SimpleModule(io.crnk.core.module.SimpleModule) Module(io.crnk.core.module.Module) SimpleModule(io.crnk.core.module.SimpleModule) ServiceDiscovery(io.crnk.core.module.discovery.ServiceDiscovery) ReflectionsServiceDiscovery(io.crnk.core.module.discovery.ReflectionsServiceDiscovery) Test(org.junit.Test)

Aggregations

JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)49 Document (io.crnk.core.engine.document.Document)16 Test (org.junit.Test)16 ResourceField (io.crnk.core.engine.information.resource.ResourceField)15 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)14 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)12 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)11 QuerySpec (io.crnk.core.queryspec.QuerySpec)11 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)10 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)10 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)10 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)10 Serializable (java.io.Serializable)10 Response (io.crnk.core.engine.dispatcher.Response)9 Resource (io.crnk.core.engine.document.Resource)9 Task (io.crnk.core.mock.models.Task)7 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)6 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)6 PagedLinksInformation (io.crnk.core.resource.links.PagedLinksInformation)6 HashSet (java.util.HashSet)6