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