Search in sources :

Example 21 with ResourceRepositoryAdapter

use of io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter in project crnk-framework by crnk-project.

the class ResourceFilterTest method checkMutationsOnForbiddenField.

@Test
public void checkMutationsOnForbiddenField() throws IOException {
    ObjectMapper objectMapper = boot.getObjectMapper();
    RegistryEntry entry = resourceRegistry.getEntry(Task.class);
    ResourceInformation resourceInformation = entry.getResourceInformation();
    ResourceField nameField = resourceInformation.findFieldByUnderlyingName("name");
    // prepare test data
    ResourceRepositoryAdapter resourceRepository = entry.getResourceRepository();
    Resource task = new Resource();
    task.setType("tasks");
    task.setId("12");
    task.setAttribute("name", objectMapper.readTree("\"Doe\""));
    String path = "/tasks/";
    String method = HttpMethod.POST.toString();
    Map<String, Set<String>> parameters = Collections.emptyMap();
    Document requestBody = new Document();
    requestBody.setData(Nullable.of((Object) task));
    // try save while forbidden
    Mockito.when(filter.filterField(Mockito.eq(nameField), 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());
    // try save with ok
    Mockito.when(filter.filterField(Mockito.eq(nameField), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.NONE);
    response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
    Assert.assertEquals(HttpStatus.CREATED_201, response.getHttpStatus().intValue());
    // try update while forbidden
    path = "/tasks/" + task.getId();
    method = HttpMethod.PATCH.toString();
    Mockito.when(filter.filterField(Mockito.eq(nameField), Mockito.eq(HttpMethod.PATCH))).thenReturn(FilterBehavior.FORBIDDEN);
    response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
    Assert.assertEquals(HttpStatus.FORBIDDEN_403, response.getHttpStatus().intValue());
    Mockito.when(filter.filterField(Mockito.eq(nameField), Mockito.eq(HttpMethod.PATCH))).thenReturn(FilterBehavior.NONE);
    response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
    Assert.assertEquals(HttpStatus.OK_200, response.getHttpStatus().intValue());
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Set(java.util.Set) Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Response(io.crnk.core.engine.dispatcher.Response) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpMethod(io.crnk.core.engine.http.HttpMethod) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 22 with ResourceRepositoryAdapter

use of io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter in project crnk-framework by crnk-project.

the class CustomResourceRegistryTest method test.

@Test
public void test() {
    CrnkBoot boot = new CrnkBoot();
    boot.addModule(new CustomRegistryPartModule());
    boot.setServiceDiscovery(new TestServiceDiscovery());
    boot.boot();
    ResourceRegistry resourceRegistry = boot.getResourceRegistry();
    RegistryEntry entry = resourceRegistry.getEntry("somePrefix/custom");
    Assert.assertNotNull(entry);
    ResourceRepositoryAdapter adapter = entry.getResourceRepository();
    QueryAdapter queryAdapter = new QuerySpecAdapter(new QuerySpec("somePrefix/custom"), resourceRegistry);
    JsonApiResponse response = adapter.findAll(queryAdapter);
    Assert.assertNotNull(response.getEntity());
    List<Resource> resources = (List<Resource>) response.getEntity();
    Assert.assertEquals(1, resources.size());
}
Also used : QueryAdapter(io.crnk.core.engine.query.QueryAdapter) Resource(io.crnk.core.engine.document.Resource) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) CrnkBoot(io.crnk.core.boot.CrnkBoot) TestServiceDiscovery(io.crnk.core.module.discovery.TestServiceDiscovery) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) List(java.util.List) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test)

Aggregations

ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)22 Response (io.crnk.core.engine.dispatcher.Response)11 Document (io.crnk.core.engine.document.Document)10 Resource (io.crnk.core.engine.document.Resource)10 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)9 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)9 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)9 ResourceField (io.crnk.core.engine.information.resource.ResourceField)7 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)7 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)7 Task (io.crnk.core.mock.models.Task)6 QuerySpec (io.crnk.core.queryspec.QuerySpec)6 Test (org.junit.Test)6 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)5 Serializable (java.io.Serializable)5 Project (io.crnk.core.mock.models.Project)4 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)3 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)3 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)3 PathIds (io.crnk.core.engine.internal.dispatcher.path.PathIds)3