Fix 'Tosca List Entry Schema failed to be recoginized with creating VSP'-bug 27/132727/4
authorvasraz <vasyl.razinkov@est.tech>
Thu, 15 Dec 2022 10:12:06 +0000 (10:12 +0000)
committerVasyl Razinkov <vasyl.razinkov@est.tech>
Thu, 15 Dec 2022 18:44:43 +0000 (18:44 +0000)
Signed-off-by: Vasyl Razinkov <vasyl.razinkov@est.tech>
Change-Id: Id33f7eff96b06a1cfb5e3e5d967b4e17da14a9a8
Issue-ID: SDC-2851

common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/CommonUtil.java
common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/YamlUtil.java
common/onap-tosca-datatype/src/test/java/org/onap/sdc/tosca/services/CommonUtilTest.java

index d6ff22c..66fac93 100644 (file)
@@ -19,7 +19,6 @@
 package org.onap.sdc.tosca.services;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
-import com.google.common.collect.ImmutableSet;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.util.Arrays;
@@ -30,13 +29,15 @@ import java.util.Objects;
 import java.util.Optional;
 import java.util.Set;
 import org.apache.commons.beanutils.BeanUtils;
+import org.onap.sdc.tosca.datatypes.model.EntrySchema;
 
 public class CommonUtil {
 
     public static final String DEFAULT = "default";
     public static final String UNDERSCORE_DEFAULT = "_default";
-    private static ImmutableSet<Class<?>> complexClassType = ImmutableSet
-        .of(Map.class, String.class, Integer.class, Float.class, Double.class, Set.class, Object.class, List.class);
+    private static final String ENTRY_SCHEMA = "entry_schema";
+    private static final Set<Class<?>> complexClassType =
+        Set.of(Map.class, String.class, Integer.class, Float.class, Double.class, Set.class, Object.class, List.class);
 
     private CommonUtil() {
         throw new IllegalStateException("Utility class");
@@ -49,10 +50,22 @@ public class CommonUtil {
         Map<String, Object> objectAsMap = getObjectAsMap(objectCandidate);
         Field[] declaredFields = classToCreate.getDeclaredFields();
         createSubObjectsUsingSetters(objectAsMap, declaredFields);
+        handleEntrySchema(objectAsMap);
         T result = populateBean(objectAsMap, classToCreate);
         return Optional.of(result);
     }
 
+    private static void handleEntrySchema(final Map<String, Object> objectAsMap) {
+        if (objectAsMap.containsKey(ENTRY_SCHEMA)) {
+            final Object entrySchema = objectAsMap.get(ENTRY_SCHEMA);
+            if (entrySchema instanceof Map) {
+                objectAsMap.remove(ENTRY_SCHEMA);
+                final Map<String, String> map = (Map<String, String>) entrySchema;
+                objectAsMap.put(ENTRY_SCHEMA, new EntrySchema(map.get("type")));
+            }
+        }
+    }
+
     public static void createSubObjectsUsingSetters(Map<String, Object> objectAsMap, Field[] declaredFields) throws Exception {
         for (Field field : declaredFields) {
             if (isComplexClass(field)) {
@@ -66,8 +79,8 @@ public class CommonUtil {
     }
 
     public static <T> T populateBean(Map<String, Object> propertiesMap, Class<T> classToCreate)
-        throws IllegalAccessException, InstantiationException, InvocationTargetException {
-        T result = classToCreate.newInstance();
+        throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
+        T result = classToCreate.getDeclaredConstructor().newInstance();
         BeanUtils.populate(result, propertiesMap);
         return result;
     }
index d8e592d..45409c3 100644 (file)
@@ -88,7 +88,7 @@ public class YamlUtil {
         TypeDescription yamlFileDescription = new TypeDescription(typClass);
         constructor.addTypeDescription(yamlFileDescription);
         T yamlObj = new Yaml(constructor, new Representer(), new DumperOptions(), getLoaderOptions()).load(yamlContent);
-        ;
+
         //noinspection ResultOfMethodCallIgnored
         yamlObj.toString();
         return yamlObj;
index d9d7f69..74dd40c 100644 (file)
  */
 package org.onap.sdc.tosca.services;
 
-import java.lang.reflect.InvocationTargetException;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
 import java.util.HashMap;
 import java.util.Map;
-import org.junit.Assert;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 
-public class CommonUtilTest {
+class CommonUtilTest {
 
     private static final String INT_FIELD_KEY = "field1";
     private static final Integer INT_FIELD_VALUE = 1;
@@ -33,13 +33,12 @@ public class CommonUtilTest {
     private static final String STRING_FIELD_VALUE = "abc";
 
     @Test
-    public void testPopulateBeanMethod()
-        throws InstantiationException, IllegalAccessException, InvocationTargetException {
+    void testPopulateBeanMethod() throws Exception {
         Map<String, Object> props = new HashMap<>();
         props.put(INT_FIELD_KEY, INT_FIELD_VALUE);
         props.put(STRING_FIELD_KEY, STRING_FIELD_VALUE);
         TestModel testModel = CommonUtil.populateBean(props, TestModel.class);
-        Assert.assertEquals(testModel.getField1(), INT_FIELD_VALUE);
-        Assert.assertEquals(testModel.getField2(), STRING_FIELD_VALUE);
+        assertEquals(testModel.getField1(), INT_FIELD_VALUE);
+        assertEquals(testModel.getField2(), STRING_FIELD_VALUE);
     }
-}
\ No newline at end of file
+}