Search in sources :

Example 36 with JsonApiResponse

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

the class DocumentMapperTest method testDocumentInformation.

@Test
public void testDocumentInformation() {
    Task task = createTask(2, "sample task");
    TestLinksInformation links = new TestLinksInformation();
    links.value = "linksValue";
    TestMetaInformation meta = new TestMetaInformation();
    meta.value = "metaValue";
    JsonApiResponse response = toResponse(task);
    response.setMetaInformation(meta);
    response.setLinksInformation(links);
    Document document = mapper.toDocument(response, createAdapter(Task.class));
    Assert.assertEquals("linksValue", getLinkText(document.getLinks().get("value")));
    Assert.assertEquals("metaValue", document.getMeta().get("value").asText());
}
Also used : LazyTask(io.crnk.core.mock.models.LazyTask) Task(io.crnk.core.mock.models.Task) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Example 37 with JsonApiResponse

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

the class DocumentMapperTest method testAttributesSelection.

@Test
public void testAttributesSelection() {
    Task task = createTask(2, "sample task");
    task.setCategory("sample category");
    task.setProject(new Project());
    JsonApiResponse response = new JsonApiResponse();
    response.setEntity(task);
    QuerySpec querySpec = new QuerySpec(Task.class);
    querySpec.includeField(Arrays.asList("category"));
    Document document = mapper.toDocument(response, toAdapter(querySpec));
    Resource resource = document.getSingleData().get();
    Assert.assertEquals("2", resource.getId());
    Assert.assertEquals("tasks", resource.getType());
    Assert.assertNull(resource.getAttributes().get("name"));
    Assert.assertNull(resource.getRelationships().get("project"));
    Assert.assertEquals("sample category", resource.getAttributes().get("category").asText());
}
Also used : Project(io.crnk.core.mock.models.Project) LazyTask(io.crnk.core.mock.models.LazyTask) Task(io.crnk.core.mock.models.Task) Resource(io.crnk.core.engine.document.Resource) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpec(io.crnk.core.queryspec.QuerySpec) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Example 38 with JsonApiResponse

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

the class DocumentMapperTest method testErrors.

@Test
public void testErrors() {
    JsonApiResponse response = new JsonApiResponse();
    ErrorData error = Mockito.mock(ErrorData.class);
    response.setErrors(Arrays.asList(error));
    Document document = mapper.toDocument(response, createAdapter(Task.class));
    List<ErrorData> errors = document.getErrors();
    Assert.assertEquals(1, errors.size());
    Assert.assertSame(error, errors.get(0));
}
Also used : LazyTask(io.crnk.core.mock.models.LazyTask) Task(io.crnk.core.mock.models.Task) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Document(io.crnk.core.engine.document.Document) ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

Example 39 with JsonApiResponse

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

the class SleuthRepositoryFilter method filterRequest.

@Override
public JsonApiResponse filterRequest(RepositoryFilterContext context, RepositoryRequestFilterChain chain) {
    RepositoryRequestSpec request = context.getRequest();
    String query = SleuthUtil.getQuery(request, moduleContext.getResourceRegistry());
    Span span = tracer.createSpan(SleuthUtil.getSpanName(request));
    JsonApiResponse result = null;
    Exception exception = null;
    try {
        span.tag("lc", COMPONENT_NAME);
        result = chain.doFilter(context);
        return result;
    } catch (RuntimeException e) {
        exception = e;
        throw e;
    } finally {
        boolean resultError = result != null && result.getErrors() != null && result.getErrors().iterator().hasNext();
        boolean failed = exception != null || resultError;
        String status = failed ? STRING_EXCEPTION : STRING_OK;
        span.tag(STATUS_CODE_ANNOTATION, status);
        writeQuery(span, query);
        writeResults(span, result);
        tracer.close(span);
    }
}
Also used : RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Span(org.springframework.cloud.sleuth.Span)

Example 40 with JsonApiResponse

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

the class ResourceRepositoryStubImpl method executeUpdate.

private Object executeUpdate(String requestUrl, T resource, boolean create) {
    JsonApiResponse response = new JsonApiResponse();
    response.setEntity(resource);
    ClientDocumentMapper documentMapper = client.getDocumentMapper();
    final Document requestDocument = documentMapper.toDocument(response, null);
    final ObjectMapper objectMapper = client.getObjectMapper();
    String requestBodyValue = ExceptionUtil.wrapCatchedExceptions(new Callable<String>() {

        @Override
        public String call() throws Exception {
            return objectMapper.writeValueAsString(requestDocument);
        }
    });
    HttpMethod method = create || client.getPushAlways() ? HttpMethod.POST : HttpMethod.PATCH;
    return execute(requestUrl, ResponseType.RESOURCE, method, requestBodyValue);
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Document(io.crnk.core.engine.document.Document) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpMethod(io.crnk.core.engine.http.HttpMethod)

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