Search in sources :

Example 71 with Document

use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.

the class ClientStubBaseTest method checkBodyWithNoErrorsAnd500Status.

@Test
public void checkBodyWithNoErrorsAnd500Status() throws IOException {
    Document document = new Document();
    document.setErrors(new ArrayList<ErrorData>());
    String body = client.getObjectMapper().writeValueAsString(document);
    HttpAdapterResponse response = Mockito.mock(HttpAdapterResponse.class);
    Mockito.when(response.body()).thenReturn(body);
    Mockito.when(response.code()).thenReturn(500);
    RuntimeException exception = stub.handleError(response);
    Assert.assertTrue(exception instanceof InternalServerErrorException);
}
Also used : HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) InternalServerErrorException(io.crnk.core.exception.InternalServerErrorException) Document(io.crnk.core.engine.document.Document) ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

Example 72 with Document

use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.

the class ErrorResponse method toResponse.

public Response toResponse() {
    Document responseDocument = new Document();
    List<ErrorData> errors = new ArrayList<>();
    for (ErrorData error : getErrors()) {
        errors.add(error);
    }
    responseDocument.setErrors(errors);
    return new Response(responseDocument, getHttpStatus());
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) Document(io.crnk.core.engine.document.Document) ErrorData(io.crnk.core.engine.document.ErrorData)

Example 73 with Document

use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.

the class JsonApiRequestProcessorTest method testHttpRequestProcessorRegistration.

@Test
public void testHttpRequestProcessorRegistration() throws IOException {
    Mockito.when(requestContextBase.getMethod()).thenReturn("GET");
    Mockito.when(requestContextBase.getPath()).thenReturn("/tasks/");
    Mockito.when(requestContextBase.getRequestHeader("Accept")).thenReturn("*");
    boot.getRequestDispatcher().process(requestContext);
    ArgumentCaptor<byte[]> contentCaptor = ArgumentCaptor.forClass(byte[].class);
    Mockito.verify(requestContextBase, Mockito.times(1)).setResponse(Mockito.eq(200), contentCaptor.capture());
    String json = new String(contentCaptor.getValue());
    Document document = boot.getObjectMapper().readerFor(Document.class).readValue(json);
    Assert.assertTrue(document.getData().isPresent());
}
Also used : Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Example 74 with Document

use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.

the class JsonApiRequestProcessorTest method getTasksWithCompactHeader.

@Test
public void getTasksWithCompactHeader() throws IOException {
    Mockito.when(requestContextBase.getMethod()).thenReturn("GET");
    Mockito.when(requestContextBase.getPath()).thenReturn("/tasks/");
    Mockito.when(requestContextBase.getRequestHeader("Accept")).thenReturn("*");
    Mockito.when(requestContextBase.getRequestHeader(HttpHeaders.HTTP_HEADER_CRNK_COMPACT)).thenReturn("true");
    processor.process(requestContext);
    ArgumentCaptor<byte[]> contentCaptor = ArgumentCaptor.forClass(byte[].class);
    Mockito.verify(requestContextBase, Mockito.times(1)).setResponse(Mockito.eq(200), contentCaptor.capture());
    String json = new String(contentCaptor.getValue());
    Document document = boot.getObjectMapper().readerFor(Document.class).readValue(json);
    Assert.assertTrue(document.getData().isPresent());
    List<Resource> resources = document.getCollectionData().get();
    Assert.assertEquals(1, resources.size());
    Resource resource = resources.get(0);
    Assert.assertNull(resource.getLinks().get("self"));
    Assert.assertNotNull(resource.getLinks().get("value"));
}
Also used : Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Example 75 with Document

use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.

the class JsonApiRequestProcessorTest method postTasks.

@Test
public void postTasks() throws IOException {
    String requestBody = createRequestBody("test");
    Mockito.when(requestContextBase.getMethod()).thenReturn("POST");
    Mockito.when(requestContextBase.getPath()).thenReturn("/tasks/");
    Mockito.when(requestContextBase.getRequestBody()).thenReturn(requestBody.getBytes());
    Mockito.when(requestContextBase.getRequestHeader(HttpHeaders.HTTP_CONTENT_TYPE)).thenReturn(HttpHeaders.JSONAPI_CONTENT_TYPE);
    Mockito.when(requestContextBase.getRequestHeader("Accept")).thenReturn(HttpHeaders.JSONAPI_CONTENT_TYPE);
    processor.process(requestContext);
    ArgumentCaptor<byte[]> contentCaptor = ArgumentCaptor.forClass(byte[].class);
    Mockito.verify(requestContextBase, Mockito.times(1)).setResponse(Mockito.eq(HttpStatus.CREATED_201), contentCaptor.capture());
    String json = new String(contentCaptor.getValue());
    Document document = boot.getObjectMapper().readerFor(Document.class).readValue(json);
    Assert.assertTrue(document.getData().isPresent());
    Resource updatedTask = (Resource) document.getData().get();
    Assert.assertEquals("1", updatedTask.getId());
    Assert.assertEquals("tasks", updatedTask.getType());
}
Also used : Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Aggregations

Document (io.crnk.core.engine.document.Document)131 Test (org.junit.Test)95 Resource (io.crnk.core.engine.document.Resource)87 Response (io.crnk.core.engine.dispatcher.Response)56 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)47 QuerySpec (io.crnk.core.queryspec.QuerySpec)45 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)40 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)40 Relationship (io.crnk.core.engine.document.Relationship)39 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)35 Task (io.crnk.core.mock.models.Task)34 Project (io.crnk.core.mock.models.Project)27 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)25 LazyTask (io.crnk.core.mock.models.LazyTask)17 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)14 RelationIdTestResource (io.crnk.core.mock.models.RelationIdTestResource)12 ResourceField (io.crnk.core.engine.information.resource.ResourceField)11 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)11 RelationshipsResourcePost (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost)10 AbstractDocumentMapperTest (io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest)10