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());
}
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());
}
Aggregations