From df1736f39bd653d2baba3874e93940427e30bac7 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 19 Jan 2021 09:20:52 +0100 Subject: [PATCH] Changing the way to serialise the type of a property --- .../properties/PropertyDefinitionImpl.java | 3 + .../informationsystem/types/TypeNameTest.java | 135 ++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 src/test/java/org/gcube/informationsystem/types/TypeNameTest.java diff --git a/src/main/java/org/gcube/informationsystem/types/impl/properties/PropertyDefinitionImpl.java b/src/main/java/org/gcube/informationsystem/types/impl/properties/PropertyDefinitionImpl.java index 0dc8d4c..448ffef 100644 --- a/src/main/java/org/gcube/informationsystem/types/impl/properties/PropertyDefinitionImpl.java +++ b/src/main/java/org/gcube/informationsystem/types/impl/properties/PropertyDefinitionImpl.java @@ -44,6 +44,9 @@ public final class PropertyDefinitionImpl implements PropertyDefinition { private Integer max= null; private Integer min= null; private String regexp= null; + + + private Integer linkedType = null; private String linkedClass = null; private Integer type=null; diff --git a/src/test/java/org/gcube/informationsystem/types/TypeNameTest.java b/src/test/java/org/gcube/informationsystem/types/TypeNameTest.java new file mode 100644 index 0000000..e17cf30 --- /dev/null +++ b/src/test/java/org/gcube/informationsystem/types/TypeNameTest.java @@ -0,0 +1,135 @@ +package org.gcube.informationsystem.types; + +import org.gcube.informationsystem.model.reference.properties.Encrypted; +import org.gcube.informationsystem.types.PropertyTypeName.BaseType; +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TypeNameTest { + + private static Logger logger = LoggerFactory.getLogger(TypeNameTest.class); + + @Test + public void testValidBaseType() throws Exception { + for(BaseType baseType : BaseType.values()) { + BaseType b = BaseType.getBaseTypeFromString(baseType.toString()); + logger.trace("{} == {}", baseType, b); + Assert.assertTrue(baseType == b); + } + } + + @Test(expected = IllegalArgumentException.class) + public void testInvaliBaseType() throws Exception { + try { + BaseType.getBaseTypeFromString("Map"); + }catch (IllegalArgumentException e) { + logger.debug("As expect got {} trying to convert invalid string value to {} Enum. {}", IllegalArgumentException.class.getSimpleName(), BaseType.class.getSimpleName(), e.getMessage()); + throw e; + } + } + + private void checkPropertyTypeName(String value, BaseType expectedBaseType) { + // logger.debug("Going to convert {} string as {}", value, BasePropertyType.class.getSimpleName()); + PropertyTypeName basePropertyType = new PropertyTypeName(value); + BaseType b = basePropertyType.getBaseType(); + Assert.assertTrue(expectedBaseType == b); + Assert.assertTrue(value.compareTo(basePropertyType.toString())==0); + if(basePropertyType.isGenericType()) { + logger.debug("{} : BaseType {} - Generic {}", value, basePropertyType.getBaseType(), basePropertyType.getGenericBaseType()!=null ? basePropertyType.getGenericBaseType().toString() : basePropertyType.getGenericClassName()); + }else { + logger.debug("{} == {}", value, basePropertyType.toString()); + } + + } + + @Test + public void testBasePropertType() { + for(BaseType baseType : BaseType.values()) { + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append(baseType.toString()); + if(baseType.ordinal()>=BaseType.PROPERTY.ordinal()) { + if(baseType == BaseType.PROPERTY) { + stringBuffer = new StringBuffer(); + stringBuffer.append(Encrypted.NAME); + checkPropertyTypeName(stringBuffer.toString(), baseType); + } else { + for(BaseType b : BaseType.values()) { + if(b.ordinal()>BaseType.PROPERTY.ordinal()) { + continue; + } + StringBuffer sb = new StringBuffer(); + sb.append(stringBuffer); + sb.append("<"); + if(baseType == BaseType.MAP) { + sb.append(BaseType.STRING.toString()); + sb.append(","); + } + + if(b == BaseType.PROPERTY) { + sb.append(Encrypted.NAME); + } else { + sb.append(b.toString()); + } + sb.append(">"); + checkPropertyTypeName(sb.toString(), baseType); + } + } + } else { + checkPropertyTypeName(stringBuffer.toString(), baseType); + } + } + } + + protected void assertInvalidBasePropertyType(String value, BaseType expectedBaseType) throws Exception { + try { + checkPropertyTypeName(value, expectedBaseType); + throw new Exception(value + " should not be a valid value"); + }catch (IllegalArgumentException e) { + logger.debug("{} is not a valid value as expected", value); + // This is what we want + } + } + + @Test + public void testInvalidBasePropertType() throws Exception { + for(BaseType baseType : BaseType.values()) { + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append(baseType.toString()); + if(baseType.ordinal()>BaseType.PROPERTY.ordinal()) { + stringBuffer.append("<"); + + switch (baseType) { + case MAP: + // TODO + continue; + + case SET: case LIST: + stringBuffer.append(BaseType.SET.toString()); + stringBuffer.append("<"); + // TODO + stringBuffer.append(">"); + break; + + default: + break; + } + + stringBuffer.append(">"); + assertInvalidBasePropertyType(stringBuffer.toString(), baseType); + + + } else { + for(BaseType b : BaseType.values()) { + StringBuffer sb = new StringBuffer(); + sb.append(stringBuffer); + sb.append("<"); + sb.append(b.toString()); + sb.append(">"); + assertInvalidBasePropertyType(sb.toString(), baseType); + } + } + } + } +}