From c1f694a9e263b18285dda60bb955b0889a3209a4 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 14 Jan 2021 18:26:26 +0100 Subject: [PATCH] Fixed type update --- .../types/SchemaManagement.java | 45 ++++++++++--- .../types/SchemaManagementImplTest.java | 63 +++++++++++++++---- .../types/TestFacet1_0_1.java | 1 + .../types/TestFacet1_0_2.java | 2 + 4 files changed, 90 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagement.java index c4c33b1..d57bfc8 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagement.java @@ -462,7 +462,7 @@ public class SchemaManagement { private boolean superClassesMatch(Type actualTypeDefinition, Type newTypeDefinition) { // Checking superclasses. Must be the same. If differs the operation will be aborted. Set actualSuperClasses = new HashSet<>(actualTypeDefinition.getSuperClasses()); - Set newSuperClasses = newTypeDefinition.getSuperClasses(); + Set newSuperClasses = new HashSet<>(newTypeDefinition.getSuperClasses()); if(actualSuperClasses.size()!=newSuperClasses.size()) { return false; @@ -475,7 +475,7 @@ public class SchemaManagement { actualSuperClasses = new HashSet<>(actualTypeDefinition.getSuperClasses()); newSuperClasses.removeAll(actualSuperClasses); - if(actualSuperClasses.size()>0) { + if(newSuperClasses.size()>0) { return false; } @@ -545,16 +545,25 @@ public class SchemaManagement { } Set newPropertyDefinitions = newTypeDefinition.getProperties(); + Map newPropertyDefinitionMap = new HashMap<>(actualPropertyDefinitions.size()); + for(PropertyDefinition newPropertyDefinition : newPropertyDefinitions) { + newPropertyDefinitionMap.put(newPropertyDefinition.getName(), newPropertyDefinition); + } + if(newPropertyDefinitions!=null) { for(PropertyDefinition newPropertyDefinition : newPropertyDefinitions) { - if(newTypeDefinition.getName().compareTo(IdentifiableElement.HEADER_PROPERTY)==0 || newTypeDefinition.getName().compareTo(Relation.PROPAGATION_CONSTRAINT_PROPERTY)==0) { + String propertyName = newPropertyDefinition.getName(); + + if(propertyName.compareTo(IdentifiableElement.HEADER_PROPERTY)==0 || propertyName.compareTo(Relation.PROPAGATION_CONSTRAINT_PROPERTY)==0) { continue; } - if(newPropertyDefinition.equals(actualPropertyDefinitionMap.get(newPropertyDefinition.getName()))) { - // This property was not changed. Checking the next one. + PropertyDefinition actualPropertyDefinition = actualPropertyDefinitionMap.get(propertyName); + + if(newPropertyDefinition.equals(actualPropertyDefinition)) { + // This property was not changed. Going to managing the next one. continue; } @@ -578,7 +587,14 @@ public class SchemaManagement { } } - OProperty op = oClass.createProperty(newPropertyDefinition.getName(), oType); + OProperty op; + if(actualPropertyDefinition!=null) { + // The property already exists and has changed (the check has been performed few lines above). + op = oClass.getProperty(propertyName); + }else { + op = oClass.createProperty(propertyName, oType); + } + op.setDescription(newPropertyDefinition.getDescription()); /* @@ -613,11 +629,13 @@ public class SchemaManagement { } } - actualPropertyDefinitions.removeAll(newPropertyDefinitions); + for(String propertyName : newPropertyDefinitionMap.keySet()) { + actualPropertyDefinitionMap.remove(propertyName); + } // Removing old properties which are no more present in the new type definition - for(PropertyDefinition propertyDefinitionToRemove : actualPropertyDefinitions) { - oClass.dropProperty(propertyDefinitionToRemove.getName()); + for(String propertyNameToRemove : actualPropertyDefinitionMap.keySet()) { + oClass.dropProperty(propertyNameToRemove); } } @@ -807,6 +825,9 @@ public class SchemaManagement { oSchema.dropClass(typeName); + ElementManagement erManagement = getTypeManagement(accessType, typeName); + erManagement.delete(); + oDatabaseDocument.commit(); return true; @@ -814,6 +835,12 @@ public class SchemaManagement { throw e; } catch(SchemaNotFoundException e) { throw e; + } catch (OSchemaException e) { + if(e.getMessage().contains("was not found in current database")) { + throw new SchemaNotFoundException(e); + }else { + throw new SchemaException(e); + } } catch(Exception e) { throw new SchemaException(e); } finally { diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagementImplTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagementImplTest.java index 1b5ea9a..8669c48 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagementImplTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/types/SchemaManagementImplTest.java @@ -18,8 +18,10 @@ import org.gcube.informationsystem.model.reference.properties.Property; import org.gcube.informationsystem.model.reference.relations.ConsistsOf; import org.gcube.informationsystem.model.reference.relations.IsRelatedTo; import org.gcube.informationsystem.model.reference.relations.Relation; +import org.gcube.informationsystem.resourceregistry.ContextTest; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; import org.gcube.informationsystem.types.TypeMapper; import org.gcube.informationsystem.types.impl.entities.EntityTypeImpl; import org.gcube.informationsystem.types.impl.properties.PropertyTypeImpl; @@ -44,7 +46,7 @@ import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) */ -public class SchemaManagementImplTest { +public class SchemaManagementImplTest extends ContextTest { private static Logger logger = LoggerFactory .getLogger(SchemaManagementImplTest.class); @@ -324,19 +326,56 @@ public class SchemaManagementImplTest { @Test public void createUpdateDeleteFacetType() throws Exception { - @SuppressWarnings("unchecked") - Class[] classes = new Class[]{TestFacet.class, TestFacet1_0_1.class, TestFacet1_0_2.class}; - for(Class c : classes) { - SchemaManagement schemaManagement = new SchemaManagement(); - Type type = TypeMapper.createTypeDefinition(c); - schemaManagement.setTypeName(type.getName()); - schemaManagement.update(TypeMapper.serializeTypeDefinition(type), AccessType.FACET); - } - SchemaManagement schemaManagement = new SchemaManagement(); - schemaManagement.setTypeName(TestFacet.NAME); - schemaManagement.delete(AccessType.FACET); + try { + @SuppressWarnings("unchecked") + Class[] classes = new Class[]{TestFacet.class, TestFacet1_0_1.class, TestFacet1_0_2.class}; + for(Class c : classes) { + SchemaManagement schemaManagement = new SchemaManagement(); + Type type = TypeMapper.createTypeDefinition(c); + schemaManagement.setTypeName(type.getName()); + if(c == TestFacet.class) { + logger.info("Going to create {} : {}", type.getName(), TypeMapper.serializeTypeDefinition(type)); + String ret = schemaManagement.create(TypeMapper.serializeTypeDefinition(type), AccessType.FACET); + logger.info("Created {} : {}", type.getName(), ret); + } else { + logger.info("Going to update {} : {}", type.getName(), TypeMapper.serializeTypeDefinition(type)); + String ret = schemaManagement.update(TypeMapper.serializeTypeDefinition(type), AccessType.FACET); + logger.info("Updated {} : {}", type.getName(), ret); + } + } + + SchemaManagement schemaManagement = new SchemaManagement(); + Type type = TypeMapper.createTypeDefinition(TestFacet.class); + schemaManagement.setTypeName(type.getName()); + schemaManagement.setSkipVersionCheckOnUpdate(true); + logger.info("Going to update {} : {}", type.getName(), TypeMapper.serializeTypeDefinition(type)); + String ret = schemaManagement.update(TypeMapper.serializeTypeDefinition(type), AccessType.FACET); + logger.info("Updated {} : {}", type.getName(), ret); + + } catch (Exception e) { + throw e; + } finally { + SchemaManagement schemaManagement = new SchemaManagement(); + String typeName = TestFacet.NAME; + schemaManagement.setTypeName(typeName); + try { + logger.info("Going to delete {}", typeName); + schemaManagement.delete(AccessType.FACET); + logger.info("Deleted {}", typeName); + } catch (SchemaNotFoundException e) { + // The test has been failed before creating the type + } catch (Exception e) { + throw e; + } + } } + @Test(expected=SchemaNotFoundException.class) + public void deleteFacetType() throws Exception { + SchemaManagement schemaManagement = new SchemaManagement(); + schemaManagement.setTypeName(TestFacet.NAME); + schemaManagement.delete(AccessType.FACET); + } } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/types/TestFacet1_0_1.java b/src/test/java/org/gcube/informationsystem/resourceregistry/types/TestFacet1_0_1.java index 1e4b654..25ec8b1 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/types/TestFacet1_0_1.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/types/TestFacet1_0_1.java @@ -30,6 +30,7 @@ import org.gcube.resourcemanagement.model.impl.entities.facets.SimpleFacetImpl; version = "1.0.1" ) @Change(version = TypeVersion.MINIMAL_VERSION_STRING, description = TypeVersion.MINIMAL_VERSION_DESCRIPTION) +@Change(version = "1.0.1", description = "Removed 'name' property, added 'title' property.") public interface TestFacet1_0_1 extends Facet { public static final String NAME = TestFacet.NAME; // SimpleFacet.class.getSimpleName(); diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/types/TestFacet1_0_2.java b/src/test/java/org/gcube/informationsystem/resourceregistry/types/TestFacet1_0_2.java index d93a5c1..35d14c5 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/types/TestFacet1_0_2.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/types/TestFacet1_0_2.java @@ -30,6 +30,8 @@ import org.gcube.resourcemanagement.model.impl.entities.facets.SimpleFacetImpl; version = "1.0.2" ) @Change(version = TypeVersion.MINIMAL_VERSION_STRING, description = TypeVersion.MINIMAL_VERSION_DESCRIPTION) +@Change(version = "1.0.1", description = "Removed 'name' property, added 'title' property.") +@Change(version = "1.0.2", description = "Restored 'name' property. Set 'title' property as not mandatory and nullable.") public interface TestFacet1_0_2 extends Facet { public static final String NAME = TestFacet.NAME; // SimpleFacet.class.getSimpleName();