Search in sources :

Example 76 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class ResourceFilterDirectoryImplTest method checkForbiddenRelationshipsField.

@Test
public void checkForbiddenRelationshipsField() {
    ResourceInformation projectsInformation = Mockito.mock(ResourceInformation.class);
    Mockito.when(filter.filterField(Mockito.eq(resourceField), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.NONE);
    Mockito.when(resourceField.getResourceFieldType()).thenReturn(ResourceFieldType.RELATIONSHIP);
    Mockito.when(resourceField.getOppositeResourceType()).thenReturn("projects");
    RegistryEntry projectsEntry = Mockito.mock(RegistryEntry.class);
    Mockito.when(projectsEntry.getResourceInformation()).thenReturn(projectsInformation);
    Mockito.when(resourceRegistry.getEntry(Mockito.eq("projects"))).thenReturn(projectsEntry);
    // forbid related resource
    Mockito.when(filter.filterResource(Mockito.eq(projectsInformation), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.FORBIDDEN);
    Assert.assertEquals(FilterBehavior.FORBIDDEN, directory.get(resourceField, HttpMethod.GET));
    // allow related resource
    Mockito.when(filter.filterResource(Mockito.eq(projectsInformation), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.NONE);
    Assert.assertEquals(FilterBehavior.NONE, directory.get(resourceField, HttpMethod.GET));
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) HttpMethod(io.crnk.core.engine.http.HttpMethod) Test(org.junit.Test)

Example 77 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class ResourceFieldImplTest method checkToString.

@Test
public void checkToString() {
    ResourceFieldImpl impl = new ResourceFieldImpl("test-name", "testName", ResourceFieldType.ID, String.class, String.class, null);
    ResourceInformation resourceInformation = Mockito.mock(ResourceInformation.class);
    Mockito.when(resourceInformation.getResourceType()).thenReturn("someResource");
    Mockito.when(resourceInformation.getResourceClass()).thenReturn((Class) Resource.class);
    impl.setResourceInformation(resourceInformation);
    Assert.assertEquals("ResourceFieldImpl[jsonName=test-name,resourceType=someResource]", impl.toString());
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Resource(io.crnk.core.engine.document.Resource) Test(org.junit.Test)

Example 78 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class ConstraintViolationExceptionMapper method getResourceType.

protected String getResourceType(Object resource) {
    ResourceRegistry resourceRegistry = context.getResourceRegistry();
    RegistryEntry entry = resourceRegistry.findEntry(resource.getClass());
    ResourceInformation resourceInformation = entry.getResourceInformation();
    return resourceInformation.getResourceType();
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 79 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class DefaultRegistryEntryBuilder method contributeFields.

private void contributeFields(ResourceInformation resourceInformation) {
    // TODO make service discovery the primary target to resolve all objects => wrapped it with module
    List<ResourceFieldContributor> contributors = new ArrayList<>();
    contributors.addAll(moduleRegistry.getServiceDiscovery().getInstancesByType(ResourceFieldContributor.class));
    for (Object repo : moduleRegistry.getRepositories()) {
        if (repo instanceof ResourceFieldContributor && !contributors.contains(repo)) {
            contributors.add((ResourceFieldContributor) repo);
        }
    }
    for (ResourceFieldContributor contributor : contributors) {
        List<ResourceField> contributedFields = contributor.getResourceFields(new ResourceFieldContributorContext() {

            @Override
            public ResourceInformation getResourceInformation() {
                return resourceInformation;
            }

            @Override
            public InformationBuilder getInformationBuilder() {
                return new DefaultInformationBuilder(moduleRegistry.getTypeParser());
            }
        });
        List<ResourceField> fields = new ArrayList<>();
        fields.addAll(resourceInformation.getFields());
        fields.addAll(contributedFields);
        resourceInformation.setFields(fields);
    }
}
Also used : InformationBuilder(io.crnk.core.engine.information.InformationBuilder) DefaultInformationBuilder(io.crnk.core.engine.internal.information.DefaultInformationBuilder) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) DefaultInformationBuilder(io.crnk.core.engine.internal.information.DefaultInformationBuilder) ResourceFieldContributor(io.crnk.core.engine.information.contributor.ResourceFieldContributor) ArrayList(java.util.ArrayList) ResourceFieldContributorContext(io.crnk.core.engine.information.contributor.ResourceFieldContributorContext)

Example 80 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class DefaultRegistryEntryBuilder method buildResource.

private ResourceInformation buildResource() {
    ResourceInformation resourceInformation = resource.build();
    contributeFields(resourceInformation);
    return resourceInformation;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation)

Aggregations

ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)167 Test (org.junit.Test)79 ResourceField (io.crnk.core.engine.information.resource.ResourceField)76 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)60 QuerySpec (io.crnk.core.queryspec.QuerySpec)16 Resource (io.crnk.core.engine.document.Resource)15 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)15 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)9 Task (io.crnk.core.mock.models.Task)9 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)9 HashSet (java.util.HashSet)9 Collection (java.util.Collection)8 Relationship (io.crnk.core.engine.document.Relationship)7 HttpMethod (io.crnk.core.engine.http.HttpMethod)7 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)7 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)7 OffsetLimitPagingBehavior (io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior)7 DefaultResourceList (io.crnk.core.resource.list.DefaultResourceList)7 Set (java.util.Set)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6