Search in sources :

Example 86 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField 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 87 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField 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 88 with ResourceField

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

the class ResourceMetaParitition method discoverResource.

private MetaResource discoverResource(ResourceInformation information) {
    String id = getId(information.getResourceType());
    // check if already done (as super types get setup recursively)
    Optional<MetaElement> existingElement = context.getMetaElement(id);
    if (existingElement.isPresent()) {
        return (MetaResource) existingElement.get();
    }
    String superResourceType = information.getSuperResourceType();
    MetaResource superMeta = null;
    ResourceInformation superInformation = null;
    if (superResourceType != null) {
        superInformation = context.getModuleContext().getResourceRegistry().getEntry(superResourceType).getResourceInformation();
        superMeta = discoverResource(superInformation);
    }
    String resourceType = information.getResourceType();
    MetaResource resource = new MetaResource();
    resource.setId(id);
    resource.setElementType(resource);
    resource.setImplementationType(information.getResourceClass());
    resource.setName(getName(information));
    resource.setResourceType(resourceType);
    if (superMeta != null) {
        resource.setSuperType(superMeta);
        if (superMeta != null) {
            superMeta.addSubType(resource);
        }
    }
    ResourceRegistry resourceRegistry = context.getModuleContext().getResourceRegistry();
    RegistryEntry entry = resourceRegistry.getEntry(information.getResourceType());
    if (entry != null) {
        boolean readOnlyImpl = entry.getResourceRepository().getResourceRepository() instanceof ReadOnlyResourceRepositoryBase;
        resource.setUpdatable(resource.isUpdatable() && !readOnlyImpl);
        resource.setInsertable(resource.isInsertable() && !readOnlyImpl);
        resource.setDeletable(resource.isDeletable() && !readOnlyImpl);
    }
    List<ResourceField> fields = information.getFields();
    for (ResourceField field : fields) {
        if (superInformation == null || superInformation.findFieldByUnderlyingName(field.getUnderlyingName()) == null) {
            // TODO check whether overriden and changed
            addAttribute(resource, field);
        }
    }
    Class<?> resourceClass = information.getResourceClass();
    addElement(resourceClass, resource);
    return resource;
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaElement(io.crnk.meta.model.MetaElement) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ReadOnlyResourceRepositoryBase(io.crnk.core.repository.ReadOnlyResourceRepositoryBase)

Example 89 with ResourceField

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

the class ResourceMetaFilter method onInitialized.

@Override
public void onInitialized(MetaElement element) {
    if (element instanceof MetaResourceBase) {
        MetaResourceBase metaResource = (MetaResourceBase) element;
        ResourceInformation information = getResourceInformation(metaResource, true);
        PreconditionUtil.assertNotNull(information.getResourceType(), metaResource);
        for (ResourceField field : information.getRelationshipFields()) {
            if (field.getOppositeName() != null) {
                String oppositeType = field.getOppositeResourceType();
                MetaResource oppositeMeta = (MetaResource) context.getMetaElement(partition.getId(oppositeType)).get();
                MetaAttribute attr = metaResource.getAttribute(field.getUnderlyingName());
                MetaAttribute oppositeAttr = oppositeMeta.getAttribute(field.getOppositeName());
                PreconditionUtil.assertNotNull(attr.getId() + " opposite not found", oppositeAttr);
                attr.setOppositeAttribute(oppositeAttr);
            }
        }
        ResourceField idField = information.getIdField();
        if (idField != null) {
            MetaAttribute idAttr = metaResource.getAttribute(idField.getUnderlyingName());
            idAttr.setPrimaryKeyAttribute(true);
            if (metaResource.getSuperType() == null || metaResource.getSuperType().getPrimaryKey() == null) {
                MetaPrimaryKey primaryKey = new MetaPrimaryKey();
                primaryKey.setName(metaResource.getName() + "$primaryKey");
                primaryKey.setName(metaResource.getId() + "$primaryKey");
                primaryKey.setElements(Arrays.asList(idAttr));
                primaryKey.setUnique(true);
                primaryKey.setParent(metaResource, true);
                metaResource.setPrimaryKey(primaryKey);
                partition.addElement(null, primaryKey);
            }
        }
    }
    if (element instanceof MetaAttribute && element.getParent() instanceof MetaResourceBase) {
        MetaAttribute attr = (MetaAttribute) element;
        MetaResourceBase parent = (MetaResourceBase) attr.getParent();
        ResourceInformation information = getResourceInformation(parent, true);
        ResourceField field = information.findFieldByUnderlyingName(attr.getName());
        PreconditionUtil.assertNotNull(attr.getName(), field);
        if (field.getResourceFieldType() == ResourceFieldType.RELATIONSHIP) {
            String oppositeType = field.getOppositeResourceType();
            String oppositeId = partition.getId(oppositeType);
            Optional<MetaElement> optOppositeMeta = context.getMetaElement(oppositeId);
            if (!optOppositeMeta.isPresent()) {
                throw new IllegalStateException("opposite meta element '" + oppositeId + "' for element '" + element.getId() + "' not found");
            }
            MetaResource oppositeMeta = (MetaResource) optOppositeMeta.get();
            if (field.isCollection()) {
                boolean isSet = Set.class.isAssignableFrom(field.getType());
                String suffix = (isSet ? "$set" : "$list");
                Optional<MetaElement> optMetaCollection = context.getMetaElement(oppositeId + suffix);
                MetaCollectionType metaCollection;
                if (optMetaCollection.isPresent()) {
                    metaCollection = (MetaCollectionType) optMetaCollection.get();
                } else {
                    metaCollection = isSet ? new MetaSetType() : new MetaListType();
                    metaCollection.setId(oppositeMeta.getId() + suffix);
                    metaCollection.setName(oppositeMeta.getName() + suffix);
                    metaCollection.setImplementationType(field.getGenericType());
                    metaCollection.setElementType(oppositeMeta);
                    partition.addElement(null, metaCollection);
                }
                attr.setType(metaCollection);
            } else {
                attr.setType(oppositeMeta);
            }
        } else {
            Type implementationType = field.getGenericType();
            MetaElement metaType = partition.allocateMetaElement(implementationType).get();
            attr.setType(metaType.asType());
        }
    } else if (element instanceof MetaAttribute && element.getParent() instanceof MetaJsonObject) {
        MetaAttribute attr = (MetaAttribute) element;
        MetaDataObject parent = attr.getParent();
        Type implementationType = PropertyUtils.getPropertyType(parent.getImplementationClass(), attr.getName());
        MetaElement metaType = partition.allocateMetaElement(implementationType).get();
        attr.setType(metaType.asType());
    }
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaPrimaryKey(io.crnk.meta.model.MetaPrimaryKey) MetaElement(io.crnk.meta.model.MetaElement) MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaResourceBase(io.crnk.meta.model.resource.MetaResourceBase) MetaCollectionType(io.crnk.meta.model.MetaCollectionType) ResourceField(io.crnk.core.engine.information.resource.ResourceField) MetaResourceField(io.crnk.meta.model.resource.MetaResourceField) MetaListType(io.crnk.meta.model.MetaListType) ResourceFieldType(io.crnk.core.engine.information.resource.ResourceFieldType) MetaSetType(io.crnk.meta.model.MetaSetType) MetaCollectionType(io.crnk.meta.model.MetaCollectionType) Type(java.lang.reflect.Type) MetaListType(io.crnk.meta.model.MetaListType) MetaSetType(io.crnk.meta.model.MetaSetType) MetaResource(io.crnk.meta.model.resource.MetaResource) MetaJsonObject(io.crnk.meta.model.resource.MetaJsonObject) MetaAttribute(io.crnk.meta.model.MetaAttribute)

Example 90 with ResourceField

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

the class JpaRelationshipRepositoryTestBase method setup.

@Override
@Before
public void setup() {
    super.setup();
    ResourceField resourceField = this.resourceRegistry.getEntry(TestEntity.class).getResourceInformation().findFieldByUnderlyingName("oneRelatedValue");
    ResourceField relatedField = this.resourceRegistry.getEntry(RelatedEntity.class).getResourceInformation().findFieldByUnderlyingName("testEntity");
    repo = new JpaRelationshipRepository<TestEntity, Long, RelatedEntity, Long>(module, resourceField, JpaRepositoryConfig.create(RelatedEntity.class));
    relatedRepo = new JpaRelationshipRepository<RelatedEntity, Long, TestEntity, Long>(module, relatedField, JpaRepositoryConfig.create(TestEntity.class));
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) ResourceField(io.crnk.core.engine.information.resource.ResourceField) RelatedEntity(io.crnk.jpa.model.RelatedEntity) Before(org.junit.Before)

Aggregations

ResourceField (io.crnk.core.engine.information.resource.ResourceField)109 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)75 Test (org.junit.Test)41 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)36 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)14 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)13 QuerySpec (io.crnk.core.queryspec.QuerySpec)11 ArrayList (java.util.ArrayList)9 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)8 Response (io.crnk.core.engine.dispatcher.Response)8 Resource (io.crnk.core.engine.document.Resource)8 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)8 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)8 HashSet (java.util.HashSet)8 Document (io.crnk.core.engine.document.Document)7 Task (io.crnk.core.mock.models.Task)7 Serializable (java.io.Serializable)7 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)6 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)6 MultivaluedMap (io.crnk.core.engine.internal.utils.MultivaluedMap)6