Search in sources :

Example 71 with Response

use of io.crnk.core.engine.dispatcher.Response in project crnk-framework by crnk-project.

the class ResourceFilterTest method testFilterResource.

@Test
public void testFilterResource() {
    ResourceInformation resourceInformation = resourceRegistry.getEntry(Task.class).getResourceInformation();
    String path = "/tasks/";
    String method = HttpMethod.GET.toString();
    Map<String, Set<String>> parameters = Collections.emptyMap();
    Document requestBody = null;
    // forbid resource
    Mockito.when(filter.filterResource(Mockito.eq(resourceInformation), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.FORBIDDEN);
    Response response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
    Assert.assertEquals(HttpStatus.FORBIDDEN_403, response.getHttpStatus().intValue());
    // allow resource
    Mockito.when(filter.filterResource(Mockito.eq(resourceInformation), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.NONE);
    response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
    Assert.assertEquals(HttpStatus.OK_200, response.getHttpStatus().intValue());
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Task(io.crnk.core.mock.models.Task) Set(java.util.Set) Document(io.crnk.core.engine.document.Document) HttpMethod(io.crnk.core.engine.http.HttpMethod) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 72 with Response

use of io.crnk.core.engine.dispatcher.Response in project crnk-framework by crnk-project.

the class ResourceFieldContributorTest method checkInclusionUponRequest.

@Test
public void checkInclusionUponRequest() {
    TaskRepository repo = new TaskRepository();
    Task task = new Task();
    task.setId(1L);
    task.setName("someTask");
    repo.save(task);
    HttpRequestProcessorImpl requestDispatcher = boot.getRequestDispatcher();
    HttpRequestContextProvider httpRequestContextProvider = boot.getModuleRegistry().getHttpRequestContextProvider();
    HttpRequestContext request = Mockito.mock(HttpRequestContext.class);
    httpRequestContextProvider.onRequestStarted(request);
    Map<String, Set<String>> parameters = new HashMap<>();
    parameters.put("include", Sets.newHashSet("contributedProject"));
    Response response = requestDispatcher.dispatchRequest("tasks", "GET", parameters, null, null);
    Document document = response.getDocument();
    Resource resource = document.getCollectionData().get().get(0);
    Assert.assertEquals("1", resource.getId());
    Assert.assertEquals("tasks", resource.getType());
    Assert.assertEquals("someTask", resource.getAttributes().get("name").asText());
    Relationship relationship = resource.getRelationships().get("contributedProject");
    Assert.assertNotNull(relationship);
    ResourceIdentifier relationshipId = relationship.getSingleData().get();
    Assert.assertEquals("projects", relationshipId.getType());
    Assert.assertEquals("11", relationshipId.getId());
    List<Resource> included = document.getIncluded();
    Assert.assertEquals(1, included.size());
    Resource includedResource = included.get(0);
    Assert.assertEquals("projects", includedResource.getType());
    Assert.assertEquals("11", includedResource.getId());
    Assert.assertEquals("someProject", includedResource.getAttributes().get("name").asText());
}
Also used : Task(io.crnk.core.mock.models.Task) Set(java.util.Set) HashMap(java.util.HashMap) ProjectToTaskRepository(io.crnk.core.mock.repository.ProjectToTaskRepository) TaskRepository(io.crnk.core.mock.repository.TaskRepository) Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) HttpRequestProcessorImpl(io.crnk.core.engine.internal.http.HttpRequestProcessorImpl) Response(io.crnk.core.engine.dispatcher.Response) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Relationship(io.crnk.core.engine.document.Relationship) HttpRequestContext(io.crnk.core.engine.http.HttpRequestContext) HttpRequestContextProvider(io.crnk.core.engine.http.HttpRequestContextProvider) Test(org.junit.Test)

Aggregations

Response (io.crnk.core.engine.dispatcher.Response)72 Document (io.crnk.core.engine.document.Document)56 Test (org.junit.Test)54 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)52 Resource (io.crnk.core.engine.document.Resource)43 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)41 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)30 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)18 Project (io.crnk.core.mock.models.Project)15 Relationship (io.crnk.core.engine.document.Relationship)14 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)14 ResourceField (io.crnk.core.engine.information.resource.ResourceField)12 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)12 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)12 Task (io.crnk.core.mock.models.Task)12 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)11 QuerySpec (io.crnk.core.queryspec.QuerySpec)11 TaskToProjectRepository (io.crnk.core.mock.repository.TaskToProjectRepository)9 QueryParams (io.crnk.legacy.queryParams.QueryParams)9 RelationshipsResourcePost (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost)8