Search in sources :

Example 91 with RegistryEntry

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());
}
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 92 with RegistryEntry

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());
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Test(org.junit.Test)

Example 93 with RegistryEntry

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);
}
Also used : Serializable(java.io.Serializable) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) OffsetLimitPagingSpec(io.crnk.core.queryspec.pagingspec.OffsetLimitPagingSpec) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Schedule(io.crnk.core.mock.models.Schedule) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 94 with RegistryEntry

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));
}
Also used : CrnkBoot(io.crnk.core.boot.CrnkBoot) ResourceRepositoryInformation(io.crnk.core.engine.information.repository.ResourceRepositoryInformation) ReflectionsServiceDiscovery(io.crnk.core.module.discovery.ReflectionsServiceDiscovery) ConstantServiceUrlProvider(io.crnk.core.engine.url.ConstantServiceUrlProvider) RepositoryAction(io.crnk.core.engine.information.repository.RepositoryAction) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Before(org.junit.Before)

Example 95 with RegistryEntry

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();
}
Also used : MetaType(io.crnk.meta.model.MetaType) ResourceFieldType(io.crnk.core.engine.information.resource.ResourceFieldType) SerializeType(io.crnk.core.resource.annotations.SerializeType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaDataObject(io.crnk.meta.model.MetaDataObject) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Aggregations

RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)119 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)60 Test (org.junit.Test)38 ResourceField (io.crnk.core.engine.information.resource.ResourceField)36 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)19 QuerySpec (io.crnk.core.queryspec.QuerySpec)18 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)14 Serializable (java.io.Serializable)14 Task (io.crnk.core.mock.models.Task)13 Response (io.crnk.core.engine.dispatcher.Response)12 Document (io.crnk.core.engine.document.Document)11 Resource (io.crnk.core.engine.document.Resource)11 ResourceRegistryTest (io.crnk.core.resource.registry.ResourceRegistryTest)10 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)9 FilterSpec (io.crnk.core.queryspec.FilterSpec)9 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)8 Before (org.junit.Before)8 Collection (java.util.Collection)7 HashSet (java.util.HashSet)7 HttpMethod (io.crnk.core.engine.http.HttpMethod)6