use of io.crnk.core.engine.registry.RegistryEntry 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.registry.RegistryEntry in project crnk-framework by crnk-project.
the class ResourceFieldContributorTest method checkFieldAddedToResourceInformation.
@Test
public void checkFieldAddedToResourceInformation() {
ResourceRegistry resourceRegistry = boot.getResourceRegistry();
RegistryEntry entry = resourceRegistry.getEntry(Task.class);
ResourceInformation resourceInformation = entry.getResourceInformation();
ResourceField contributedField = resourceInformation.findFieldByName("contributedProject");
Assert.assertNotNull(contributedField);
Assert.assertEquals("projects", contributedField.getOppositeResourceType());
Assert.assertEquals(SerializeType.LAZY, contributedField.getSerializeType());
}
use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.
the class RepositoryFilterTest method findAllWithResourceListResult.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void findAllWithResourceListResult() throws Exception {
RegistryEntry scheduleRegistry = resourceRegistry.getEntry(Schedule.class);
ResourceRepositoryAdapter<Schedule, Serializable> scheduleResourceAdapter = scheduleRegistry.getResourceRepository(null);
querySpec = new QuerySpec(Schedule.class);
querySpec.setPagingSpec(new OffsetLimitPagingSpec());
queryAdapter = new QuerySpecAdapter(querySpec, resourceRegistry);
scheduleResourceAdapter.findAll(queryAdapter);
ArgumentCaptor<Iterable> linksResources = ArgumentCaptor.forClass(Iterable.class);
ArgumentCaptor<Iterable> metaResources = ArgumentCaptor.forClass(Iterable.class);
ArgumentCaptor<RepositoryFilterContext> contexts = ArgumentCaptor.forClass(RepositoryFilterContext.class);
Mockito.verify(filter, Mockito.times(1)).filterRequest(contexts.capture(), Mockito.any(RepositoryRequestFilterChain.class));
Mockito.verify(filter, Mockito.times(1)).filterResult(Mockito.any(RepositoryFilterContext.class), Mockito.any(RepositoryResultFilterChain.class));
Mockito.verify(filter, Mockito.times(1)).filterLinks(Mockito.any(RepositoryFilterContext.class), linksResources.capture(), Mockito.any(RepositoryLinksFilterChain.class));
Mockito.verify(filter, Mockito.times(1)).filterMeta(Mockito.any(RepositoryFilterContext.class), metaResources.capture(), Mockito.any(RepositoryMetaFilterChain.class));
Assert.assertEquals(1, linksResources.getAllValues().size());
Assert.assertEquals(1, metaResources.getAllValues().size());
Assert.assertEquals(1, contexts.getAllValues().size());
RepositoryFilterContext context = contexts.getAllValues().iterator().next();
RepositoryRequestSpec requestSpec = context.getRequest();
Assert.assertEquals(queryAdapter, requestSpec.getQueryAdapter());
Assert.assertNull(requestSpec.getId());
Assert.assertNull(requestSpec.getIds());
QuerySpec actualQuerySpec = requestSpec.getQuerySpec(scheduleInfo);
Assert.assertSame(querySpec, actualQuerySpec);
}
use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.
the class PathBuilderTest method prepare.
@Before
public void prepare() {
CrnkBoot boot = new CrnkBoot();
boot.setServiceDiscovery(new ReflectionsServiceDiscovery(MockConstants.TEST_MODELS_PACKAGE));
boot.setServiceUrlProvider(new ConstantServiceUrlProvider(ResourceRegistryTest.TEST_MODELS_URL));
boot.boot();
pathBuilder = new PathBuilder(boot.getResourceRegistry());
RegistryEntry entry = boot.getResourceRegistry().findEntry(Task.class);
ResourceRepositoryInformation repositoryInformation = entry.getRepositoryInformation();
repositoryInformation.getActions().put("someRepositoryAction", Mockito.mock(RepositoryAction.class));
repositoryInformation.getActions().put("someResourceAction", Mockito.mock(RepositoryAction.class));
}
use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.
the class ResourceMetaParitition method computeId.
private String computeId(MetaType element) {
Type implementationType = element.getImplementationType();
Class<?> rawType = ClassUtils.getRawType(implementationType);
Class<?> enclosingClass = rawType.getEnclosingClass();
boolean isLinks = LinksInformation.class.isAssignableFrom(rawType);
boolean isMeta = MetaInformation.class.isAssignableFrom(rawType);
ResourceRegistry resourceRegistry = context.getModuleContext().getResourceRegistry();
if (enclosingClass != null && (isLinks || isMeta)) {
RegistryEntry entry = resourceRegistry.getEntry(enclosingClass);
if (entry != null) {
String id = getId(entry.getResourceInformation().getResourceType());
if (isMeta) {
return id + "$meta";
} else {
return id + "$links";
}
}
}
if (!element.hasId()) {
PreconditionUtil.assertNotNull("must have package", rawType.getPackage());
String packageName = rawType.getPackage().getName();
String closedPackageName = null;
String closedResourceType = null;
for (RegistryEntry entry : resourceRegistry.getResources()) {
ResourceInformation resourceInformation = entry.getResourceInformation();
Class<?> resourceClass = resourceInformation.getResourceClass();
String resourcePackageName = resourceClass.getPackage().getName();
if (packageName.startsWith(resourcePackageName) && (closedPackageName == null || closedPackageName.length() < resourcePackageName.length())) {
closedPackageName = resourcePackageName;
closedResourceType = resourceInformation.getResourceType();
}
Object resourceRepository = entry.getResourceRepository().getResourceRepository();
resourcePackageName = resourceRepository.getClass().getPackage().getName();
if (packageName.startsWith(resourcePackageName) && (closedPackageName == null || closedPackageName.length() < resourcePackageName.length())) {
closedPackageName = resourcePackageName;
closedResourceType = resourceInformation.getResourceType();
}
}
if (closedResourceType != null) {
String resourceId = getId(closedResourceType);
String basePath = resourceId.substring(0, resourceId.lastIndexOf('.'));
String relativePath = packageName.substring(closedPackageName.length());
return basePath + relativePath + "." + element.getName().toLowerCase();
}
}
return idProvider.computeIdPrefixFromPackage(rawType, element) + element.getName().toLowerCase();
}
Aggregations