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;
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");
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)) {
}
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;
}
*/
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;
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
+}