Refactor ToscaTypeTest 02/109402/6
authorxuegao <xg353y@intl.att.com>
Mon, 22 Jun 2020 10:15:06 +0000 (12:15 +0200)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Tue, 23 Jun 2020 07:53:45 +0000 (07:53 +0000)
Adding more tests and adding assert for old tests.

Issue-ID: SDC-3143
Signed-off-by: xuegao <xg353y@intl.att.com>
Change-Id: Id7cf5ec763b0d43dece9493613e336a4b3df9b72
Signed-off-by: xuegao <xg353y@intl.att.com>
catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaType.java
catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/ToscaTypeTest.java

index 87ad621..71abc54 100644 (file)
@@ -178,13 +178,13 @@ public enum ToscaType {
                                try {
                                        return ConstraintUtil.parseToCollection(value, new TypeReference<List<Object>>() {});
                                } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
-                                       throw new IllegalArgumentException("Value must be a valid timestamp", e);
+                                       throw new IllegalArgumentException("Value must be a valid List", e);
                                }
                        case MAP:
                                try {
                                        return ConstraintUtil.parseToCollection(value, new TypeReference<Map<String, Object>>() {});
                                } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
-                                       throw new IllegalArgumentException("Value must be a valid timestamp", e);
+                                       throw new IllegalArgumentException("Value must be a valid Map", e);
                                }
                        default:
                                return null;
index 625cfb7..233c44c 100644 (file)
 
 package org.openecomp.sdc.be.model.tosca;
 
-import org.junit.Assert;
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+import java.text.DateFormat;
+import java.util.Date;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import org.junit.jupiter.api.Test;
+import org.openecomp.sdc.be.model.tosca.version.Version;
 
 
 public class ToscaTypeTest {
 
-       private ToscaType createTestSubject() {
-               return  ToscaType.BOOLEAN;
+       @Test
+       public void testIsValidValueBoolean() throws Exception {
+               ToscaType toscaType = ToscaType.BOOLEAN;;
+
+               assertTrue(!toscaType.isValidValue(""));
+               assertTrue(toscaType.isValidValue("false"));
+               assertTrue(toscaType.isValidValue("FalSe"));
+               assertTrue(toscaType.isValidValue("true"));
+               assertTrue(toscaType.isValidValue("TrUe"));
        }
 
+       @Test
+       public void testIsValidValueFloat() throws Exception {
+               ToscaType toscaType = ToscaType.FLOAT;;
+
+               assertTrue(!toscaType.isValidValue("float"));
+               assertTrue(toscaType.isValidValue("1.2534"));
+               assertTrue(toscaType.isValidValue("1.2534f"));
+       }
 
-       
        @Test
-       public void testIsValidValue() throws Exception {
-               ToscaType testSubject;
-               String value = "";
-               boolean result;
-
-               // default test
-               testSubject = createTestSubject();
-               result = testSubject.isValidValue(value);
+       public void testIsValidValueInteger() throws Exception {
+               ToscaType toscaType = ToscaType.INTEGER;;
+
+               assertTrue(!toscaType.isValidValue("integer"));
+               assertTrue(toscaType.isValidValue("1235"));
        }
 
+       @Test
+       public void testIsValidValueTimestamp() throws Exception {
+               ToscaType toscaType = ToscaType.TIMESTAMP;;
+
+               assertTrue(!toscaType.isValidValue("timestamp"));
+               assertTrue(toscaType.isValidValue("Jun 30, 2009 7:03:47 AM"));
+               assertTrue(!toscaType.isValidValue("30 juin 2009 07:03:47"));
+       }
+
+       @Test
+       public void testIsValidValueVersion() throws Exception {
+               ToscaType toscaType = ToscaType.VERSION;;
+
+               assertTrue(!toscaType.isValidValue("version"));
+               assertTrue(toscaType.isValidValue("1.2"));
+               assertTrue(toscaType.isValidValue("1.2.3"));
+               assertTrue(toscaType.isValidValue("1.2-3"));
+       }
+
+       @Test
+       public void testIsValidValueList() throws Exception {
+               ToscaType toscaType = ToscaType.LIST;;
+
+               assertTrue(!toscaType.isValidValue("list"));
+               assertTrue(toscaType.isValidValue("[\"color\",\"type\"]"));
+       }
+
+       @Test
+       public void testIsValidValueMap() throws Exception {
+               ToscaType toscaType = ToscaType.MAP;;
+
+               assertTrue(!toscaType.isValidValue("map"));
+               assertTrue(toscaType.isValidValue("{\"color\":\"yellow\",\"type\":\"renault\"}"));
+       }
+
+       @Test
+       public void testGetToscaType() throws Exception {
+               ToscaType toscaType = ToscaType.MAP;;
+
+               assertEquals(ToscaType.getToscaType("map"), toscaType);
+               assertNull(ToscaType.getToscaType(null));
+               assertNull(ToscaType.getToscaType("InvalidType"));
+       }
+
+       @Test
+       public void testIsPrimitiveType() throws Exception {
+               assertTrue(!ToscaType.isPrimitiveType("map"));
+               assertTrue(!ToscaType.isPrimitiveType("list"));
+               assertTrue(!ToscaType.isPrimitiveType("String"));
+               assertTrue(ToscaType.isPrimitiveType("string"));
+               assertTrue(ToscaType.isPrimitiveType("integer"));
+       }
+
+       @Test
+       public void testIsCollectionType() throws Exception {
+               assertTrue(ToscaType.isCollectionType("map"));
+               assertTrue(ToscaType.isCollectionType("list"));
+               assertTrue(!ToscaType.isCollectionType("Map"));
+               assertTrue(!ToscaType.isCollectionType("string"));
+               assertTrue(!ToscaType.isCollectionType("integer"));
+       }
 
-       
        @Test
        public void testConvert() throws Exception {
-               ToscaType testSubject;
-               String value = "";
-               Object result;
+               ToscaType typeStr = ToscaType.STRING;
+               assertEquals(typeStr.convert("str"), "str");
+
+               ToscaType typeFloat = ToscaType.FLOAT;
+               assertTrue(typeFloat.convert("1.2357f") instanceof Float);
 
-               // default test
-               testSubject = createTestSubject();
-               result = testSubject.convert(value);
+               ToscaType typeTimestamp = ToscaType.TIMESTAMP;
+               assertTrue(typeTimestamp.convert("Jun 30, 2009 7:03:47 AM") instanceof Date);
+
+               ToscaType typeVersion = ToscaType.VERSION;
+               assertTrue(typeVersion.convert("1.2.3.5.6") instanceof Version);
+
+               ToscaType typeList = ToscaType.LIST;
+               assertTrue(typeList.convert("[\"str1\",\"str2\"]") instanceof List);
+
+               ToscaType typeMap = ToscaType.MAP;
+               assertTrue(typeMap.convert("{\"color\":\"yellow\",\"type\":\"renault\"}") instanceof Map);
        }
 
-       
        @Test
        public void testToString() throws Exception {
-               ToscaType testSubject;
-               String result;
-
-               // default test
-               testSubject = createTestSubject();
-               result = testSubject.toString();
+               ToscaType testToscaType = ToscaType.SCALAR_UNIT;
+               assertEquals(testToscaType.toString(), "scalar_unit");
        }
 }