information-system-model/src/test/java/org/gcube/informationsystem/types/SerializationTest.java

210 lines
12 KiB
Java
Raw Normal View History

package org.gcube.informationsystem.types;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.entities.EntityElement;
import org.gcube.informationsystem.base.reference.properties.PropertyElement;
2020-01-23 17:14:44 +01:00
import org.gcube.informationsystem.context.reference.entities.Context;
2019-11-04 18:06:46 +01:00
import org.gcube.informationsystem.context.reference.relations.IsParentOf;
2020-01-23 17:14:44 +01:00
import org.gcube.informationsystem.model.reference.entities.Entity;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource;
2020-01-29 16:33:51 +01:00
import org.gcube.informationsystem.model.reference.properties.Encrypted;
2020-01-20 10:24:55 +01:00
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint;
2020-01-29 16:33:51 +01:00
import org.gcube.informationsystem.model.reference.properties.Property;
2020-01-23 17:14:44 +01:00
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
2019-11-04 18:06:46 +01:00
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
2020-01-23 17:14:44 +01:00
import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.types.reference.entities.EntityType;
import org.gcube.informationsystem.types.reference.entities.FacetType;
import org.gcube.informationsystem.types.reference.entities.ResourceType;
2021-02-12 12:08:30 +01:00
import org.gcube.informationsystem.types.reference.properties.Changelog;
2020-05-08 17:33:40 +02:00
import org.gcube.informationsystem.types.reference.properties.LinkedEntity;
2020-01-29 16:33:51 +01:00
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;
import org.gcube.informationsystem.types.reference.properties.PropertyType;
import org.gcube.informationsystem.types.reference.relations.ConsistsOfType;
import org.gcube.informationsystem.types.reference.relations.IsRelatedToType;
import org.gcube.informationsystem.types.reference.relations.RelationType;
2020-02-03 10:51:29 +01:00
import org.gcube.informationsystem.utils.ElementMapper;
2021-10-21 10:11:45 +02:00
import org.gcube.informationsystem.utils.Version;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class SerializationTest {
private static Logger logger = LoggerFactory.getLogger(SerializationTest.class);
@Test
public void serialize() throws Exception{
2020-02-04 09:30:19 +01:00
TypeMapper.serializeType(EntityTest.class);
}
2020-01-23 17:14:44 +01:00
@Test
2020-02-03 10:51:29 +01:00
public void makeFacetTypeDefinition() throws Exception{
2020-02-04 09:30:19 +01:00
EntityType facetTypeDefinitionSelf = (EntityType) TypeMapper.createTypeDefinition(FacetType.class);
2020-02-03 10:51:29 +01:00
logger.info(ElementMapper.marshal(facetTypeDefinitionSelf));
}
@Test
public void makeResourceTypeDefinition() throws Exception{
2020-02-04 09:30:19 +01:00
EntityType resourceTypeDefinitionSelf = (EntityType) TypeMapper.createTypeDefinition(ResourceType.class);
2020-02-03 10:51:29 +01:00
logger.info(ElementMapper.marshal(resourceTypeDefinitionSelf));
2020-01-23 17:14:44 +01:00
}
2020-01-29 16:33:51 +01:00
@Test
public void testPropertyTypeDefinition() throws Exception{
@SuppressWarnings("unchecked")
PropertyType<PropertyElement> propertyElement = (PropertyType<PropertyElement>) TypeMapper.createTypeDefinition(PropertyElement.class);
Assert.assertTrue(propertyElement.getAccessType()==AccessType.PROPERTY_ELEMENT);
logger.info(ElementMapper.marshal(propertyElement));
2020-01-29 16:33:51 +01:00
@SuppressWarnings({ "unchecked", "rawtypes" })
PropertyType<PropertyType> propertyType = (PropertyType<PropertyType>) TypeMapper.createTypeDefinition(PropertyType.class);
Assert.assertTrue(propertyType.getAccessType()==AccessType.PROPERTY_TYPE);
logger.info(ElementMapper.marshal(propertyType));
2020-01-29 16:33:51 +01:00
@SuppressWarnings("unchecked")
PropertyType<PropertyDefinition> propertyDefinition = (PropertyType<PropertyDefinition>) TypeMapper.createTypeDefinition(PropertyDefinition.class);
Assert.assertTrue(propertyDefinition.getAccessType()==AccessType.PROPERTY_DEFINITION);
logger.info(ElementMapper.marshal(propertyDefinition));
2020-01-29 16:33:51 +01:00
@SuppressWarnings("unchecked")
PropertyType<LinkedEntity> linkedEntity = (PropertyType<LinkedEntity>) TypeMapper.createTypeDefinition(LinkedEntity.class);
Assert.assertTrue(linkedEntity.getAccessType()==AccessType.PROPERTY_ELEMENT);
logger.info(ElementMapper.marshal(linkedEntity));
2020-01-29 16:33:51 +01:00
@SuppressWarnings("unchecked")
PropertyType<Property> property = (PropertyType<Property>) TypeMapper.createTypeDefinition(Property.class);
Assert.assertTrue(property.getAccessType()==AccessType.PROPERTY);
logger.info(ElementMapper.marshal(property));
2020-01-29 16:33:51 +01:00
@SuppressWarnings("unchecked")
PropertyType<Header> header = (PropertyType<Header>) TypeMapper.createTypeDefinition(Header.class);
Assert.assertTrue(header.getAccessType()==AccessType.PROPERTY);
logger.info(ElementMapper.marshal(header));
2020-01-29 16:33:51 +01:00
@SuppressWarnings("unchecked")
PropertyType<PropagationConstraint> propagationConstraint = (PropertyType<PropagationConstraint>) TypeMapper.createTypeDefinition(PropagationConstraint.class);
Assert.assertTrue(propagationConstraint.getAccessType()==AccessType.PROPERTY);
logger.info(ElementMapper.marshal(propagationConstraint));
2020-01-29 16:33:51 +01:00
@SuppressWarnings("unchecked")
PropertyType<Encrypted> encrypted = (PropertyType<Encrypted>) TypeMapper.createTypeDefinition(Encrypted.class);
Assert.assertTrue(encrypted.getAccessType()==AccessType.PROPERTY);
logger.info(ElementMapper.marshal(encrypted));
2021-02-12 12:08:30 +01:00
@SuppressWarnings("unchecked")
PropertyType<Changelog> changelog = (PropertyType<Changelog>) TypeMapper.createTypeDefinition(Changelog.class);
Assert.assertTrue(changelog.getAccessType()==AccessType.PROPERTY_ELEMENT);
logger.info(ElementMapper.marshal(changelog));
2020-01-29 16:33:51 +01:00
}
2020-01-23 17:14:44 +01:00
@Test
public void testEntityTypeDefinition() throws Exception{
EntityType entityElement = (EntityType) TypeMapper.createTypeDefinition(EntityElement.class);
Assert.assertTrue(entityElement.getAccessType()==AccessType.ENTITY_ELEMENT);
logger.info(ElementMapper.marshal(entityElement));
2020-01-23 17:14:44 +01:00
EntityType entity = (EntityType) TypeMapper.createTypeDefinition(Entity.class);
Assert.assertTrue(entity.getAccessType()==AccessType.ENTITY);
logger.info(ElementMapper.marshal(entity));
2020-01-23 17:14:44 +01:00
ResourceType resource = (ResourceType) TypeMapper.createTypeDefinition(Resource.class);
Assert.assertTrue(resource.getAccessType()==AccessType.RESOURCE);
logger.info(ElementMapper.marshal(resource));
2020-01-23 17:14:44 +01:00
FacetType facet = (FacetType) TypeMapper.createTypeDefinition(Facet.class);
Assert.assertTrue(facet.getAccessType()==AccessType.FACET);
logger.info(ElementMapper.marshal(facet));
2020-01-23 17:14:44 +01:00
EntityType context = (EntityType) TypeMapper.createTypeDefinition(Context.class);
Assert.assertTrue(context.getAccessType()==AccessType.CONTEXT);
logger.info(ElementMapper.marshal(context));
2020-01-23 17:14:44 +01:00
EntityType entityType = (EntityType) TypeMapper.createTypeDefinition(EntityType.class);
Assert.assertTrue(entityType.getAccessType()==AccessType.ENTITY_TYPE);
logger.info(ElementMapper.marshal(entityType));
2020-01-23 17:14:44 +01:00
EntityType resourceType = (EntityType) TypeMapper.createTypeDefinition(ResourceType.class);
Assert.assertTrue(resourceType.getAccessType()==AccessType.ENTITY_TYPE);
logger.info(ElementMapper.marshal(resourceType));
2020-01-23 17:14:44 +01:00
EntityType facetType = (EntityType) TypeMapper.createTypeDefinition(FacetType.class);
Assert.assertTrue(facetType.getAccessType()==AccessType.ENTITY_TYPE);
logger.info(ElementMapper.marshal(facetType));
2020-01-23 17:14:44 +01:00
}
2019-11-04 18:06:46 +01:00
@Test
2020-01-23 17:14:44 +01:00
public void testRelationTypeDefinition() throws Exception{
@SuppressWarnings("unchecked")
RelationType<EntityType,EntityType> relation = (RelationType<EntityType,EntityType>) TypeMapper.createTypeDefinition(Relation.class);
Assert.assertTrue(relation.getAccessType()==AccessType.RELATION);
logger.info(ElementMapper.marshal(relation));
2019-11-04 18:06:46 +01:00
IsRelatedToType isRelatedTo = (IsRelatedToType) TypeMapper.createTypeDefinition(IsRelatedTo.class);
Assert.assertTrue(isRelatedTo.getAccessType()==AccessType.IS_RELATED_TO);
logger.info(ElementMapper.marshal(isRelatedTo));
2020-01-23 17:14:44 +01:00
ConsistsOfType consistsOf = (ConsistsOfType) TypeMapper.createTypeDefinition(ConsistsOf.class);
Assert.assertTrue(consistsOf.getAccessType()==AccessType.CONSISTS_OF);
logger.info(ElementMapper.marshal(consistsOf));
2019-11-04 18:06:46 +01:00
2020-01-23 17:14:44 +01:00
@SuppressWarnings("unchecked")
RelationType<EntityType,EntityType> isParentOf = (RelationType<EntityType,EntityType>) TypeMapper.createTypeDefinition(IsParentOf.class);
Assert.assertTrue(isParentOf.getAccessType()==AccessType.IS_PARENT_OF);
logger.info(ElementMapper.marshal(isParentOf));
2020-01-23 17:14:44 +01:00
@SuppressWarnings("unchecked")
RelationType<EntityType,EntityType> relationType = (RelationType<EntityType,EntityType>) TypeMapper.createTypeDefinition(RelationType.class);
Assert.assertTrue(relationType.getAccessType()==AccessType.RELATION_TYPE);
logger.info(ElementMapper.marshal(relationType));
2020-01-23 17:14:44 +01:00
@SuppressWarnings("unchecked")
RelationType<EntityType,EntityType> isRelatedToType = (RelationType<EntityType,EntityType>) TypeMapper.createTypeDefinition(IsRelatedToType.class);
Assert.assertTrue(isRelatedToType.getAccessType()==AccessType.RELATION_TYPE);
logger.info(ElementMapper.marshal(isRelatedToType));
2019-11-05 18:42:25 +01:00
2020-01-23 17:14:44 +01:00
@SuppressWarnings("unchecked")
RelationType<EntityType,EntityType> consistsOfType = (RelationType<EntityType,EntityType>) TypeMapper.createTypeDefinition(ConsistsOfType.class);
Assert.assertTrue(consistsOfType.getAccessType()==AccessType.RELATION_TYPE);
logger.info(ElementMapper.marshal(consistsOfType));
2019-11-04 18:06:46 +01:00
}
@Test
public void testGetEnumcostants(){
Class<?> clz = PropagationConstraint.RemoveConstraint.class;
Object[] constants = clz.getEnumConstants();
for(Object constant : constants){
logger.trace("{}", constant.toString());
}
}
2019-10-24 15:03:14 +02:00
@Test
public void testTypeSerialization() throws Exception {
2020-02-04 09:30:19 +01:00
String serialized = TypeMapper.serializeType(Header.class);
2019-10-24 15:03:14 +02:00
logger.info(serialized);
@SuppressWarnings("unchecked")
PropertyType<Header> propertyType = (PropertyType<Header>) TypeMapper.deserializeTypeDefinition(serialized);
2021-10-21 10:11:45 +02:00
Version typeVersion = propertyType.getVersion();
logger.debug("Version {}", typeVersion.toString());
logger.info(ElementMapper.marshal(propertyType));
2021-06-23 10:50:12 +02:00
String json = "{\"@class\":\"PropertyType\",\"header\":null,\"name\":\"Header\",\"description\":\"This class provides metadata per every IdentifiableElement\",\"superClasses\":[\"Property\"],\"properties\":[{\"@class\":\"PropertyDefinition\",\"name\":\"creationTime\",\"description\":\"Creation time. It represents the difference, measured in milliseconds, between the creation time and midnight, January 1, 1970, UTC.\",\"mandatory\":true,\"readonly\":true,\"notnull\":true,\"max\":null,\"min\":null,\"regexp\":null,\"type\":\"Date\"},{\"@class\":\"PropertyDefinition\",\"name\":\"lastUpdateTime\",\"description\":\"Last Update time. At creation time it assumes the same value of creationTime. It represents the difference, measured in milliseconds, between the creation time and midnight, January 1, 1970, UTC.\",\"mandatory\":true,\"readonly\":false,\"notnull\":true,\"max\":null,\"min\":null,\"regexp\":null,\"type\":\"Date\"},{\"@class\":\"PropertyDefinition\",\"name\":\"createdBy\",\"description\":\"The user that created the Entity or the Relation. It is initialized at creation time. \",\"mandatory\":true,\"readonly\":true,\"notnull\":true,\"max\":null,\"min\":null,\"regexp\":null,\"type\":\"String\"},{\"@class\":\"PropertyDefinition\",\"name\":\"uuid\",\"description\":\"This UUID is be used to identify the Entity or the Relation univocally.\",\"mandatory\":true,\"readonly\":true,\"notnull\":true,\"max\":null,\"min\":null,\"regexp\":\"^([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}){1}$\",\"type\":\"String\"},{\"@class\":\"PropertyDefinition\",\"name\":\"lastUpdateBy\",\"description\":\"The user that made the last update to the Entity or the Relation. At creation time, it assumes the same value of creator.\",\"mandatory\":true,\"readonly\":false,\"notnull\":true,\"max\":null,\"min\":null,\"regexp\":null,\"type\":\"String\"}],\"accessType\":\"PROPERTY\",\"abstract\":false,\"version\":\"1.0.0\",\"changelog\":{\"1.0.0\":\"First Version\"}}";
logger.info(json);
@SuppressWarnings("unchecked")
PropertyType<Header> headerType = (PropertyType<Header>) TypeMapper.deserializeTypeDefinition(json);
logger.info(ElementMapper.marshal(headerType));
2021-06-23 10:50:12 +02:00
2019-10-24 15:03:14 +02:00
}
2019-10-24 19:50:43 +02:00
}