Improve test coverage
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / ToscaElementOperation.java
index bc77d20..bb3b3b8 100644 (file)
@@ -27,6 +27,7 @@ import com.google.gson.reflect.TypeToken;
 import fj.data.Either;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.EnumMap;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -54,6 +55,7 @@ import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
 import org.openecomp.sdc.be.dao.jsongraph.utils.JsonParserUtils;
 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
+import org.openecomp.sdc.be.datatypes.category.MetadataKeyDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
@@ -133,15 +135,10 @@ public abstract class ToscaElementOperation extends BaseOperation {
     }
 
     public Either<GraphVertex, StorageOperationStatus> markComponentToDelete(GraphVertex componentToDelete) {
-        Either<GraphVertex, StorageOperationStatus> result = null;
-
         Boolean isDeleted = (Boolean) componentToDelete.getMetadataProperty(GraphPropertyEnum.IS_DELETED);
         if (isDeleted != null && isDeleted && !(Boolean) componentToDelete.getMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION)) {
             // component already marked for delete
-            result = Either.left(componentToDelete);
-            return result;
         } else {
-
             componentToDelete.addMetadataProperty(GraphPropertyEnum.IS_DELETED, Boolean.TRUE);
             componentToDelete.setJsonMetadataField(JsonPresentationFields.LAST_UPDATE_DATE, System.currentTimeMillis());
 
@@ -151,13 +148,10 @@ public abstract class ToscaElementOperation extends BaseOperation {
             if (updateNode.isRight()) {
                 log.debug("Failed to update component {}. status is {}", componentToDelete.getUniqueId(), updateNode.right().value());
                 updateComponent = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(updateNode.right().value());
-                result = Either.right(updateComponent);
-                return result;
+                return Either.right(updateComponent);
             }
-
-            result = Either.left(componentToDelete);
-            return result;
         }
+        return Either.left(componentToDelete);
     }
 
     /**
@@ -330,7 +324,7 @@ public abstract class ToscaElementOperation extends BaseOperation {
         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_ARCHIVED, toscaElement.getMetadataValue(JsonPresentationFields.IS_ARCHIVED));
         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.ARCHIVE_TIME, toscaElement.getMetadataValue(JsonPresentationFields.ARCHIVE_TIME));
         nodeTypeVertex.addMetadataProperty(GraphPropertyEnum.IS_VSP_ARCHIVED, toscaElement.getMetadataValue(JsonPresentationFields.IS_VSP_ARCHIVED));
-        toscaElement.getMetadata().entrySet().stream().filter(e -> e.getValue() != null).forEach(e -> nodeTypeVertex.setJsonMetadataField(JsonPresentationFields.getByPresentation(e.getKey()), e.getValue()));
+        toscaElement.getMetadata().entrySet().stream().filter(e -> e.getValue() != null).forEach(e -> nodeTypeVertex.setJsonMetadataField(e.getKey(), e.getValue()));
 
         nodeTypeVertex.setUniqueId(toscaElement.getUniqueId());
         nodeTypeVertex.setType(toscaElement.getComponentType());
@@ -538,6 +532,10 @@ public abstract class ToscaElementOperation extends BaseOperation {
         return JanusGraphOperationStatus.OK;
     }
 
+    protected StorageOperationStatus assosiateCommonForToscaElement(GraphVertex nodeTypeVertex, ToscaElement toscaElement) {
+        return assosiateCommonForToscaElement(nodeTypeVertex, toscaElement, null);
+    }
+
     protected StorageOperationStatus assosiateCommonForToscaElement(GraphVertex nodeTypeVertex, ToscaElement toscaElement, List<GraphVertex> derivedResources) {
 
         StorageOperationStatus associateUsers = assosiateToUsers(nodeTypeVertex, toscaElement);
@@ -727,8 +725,6 @@ public abstract class ToscaElementOperation extends BaseOperation {
 
     protected <T extends ToscaElement> Either<List<T>, StorageOperationStatus> getFollowedComponent(String userId, Set<LifecycleStateEnum> lifecycleStates, Set<LifecycleStateEnum> lastStateStates, ComponentTypeEnum neededType) {
 
-        Either<List<T>, StorageOperationStatus> result = null;
-
         Map<GraphPropertyEnum, Object> props = null;
 
         if (userId != null) {
@@ -793,10 +789,7 @@ public abstract class ToscaElementOperation extends BaseOperation {
             }
 
         } // whlile users
-        ;
-        result = Either.left(components);
-        return result;
-
+        return Either.left(components);
     }
 
     private <T extends ToscaElement> List<T> fetchComponents(String userId, Set<LifecycleStateEnum> lifecycleStates, List<GraphVertex> vertices, ComponentTypeEnum neededType, EdgeLabelEnum edgelabel) {
@@ -929,7 +922,6 @@ public abstract class ToscaElementOperation extends BaseOperation {
         switch (label) {
             case NODE_TYPE:
                 toscaElement = new NodeType();
-                ((NodeType) toscaElement).setAttributes(getAttributesFromComponentV(componentV));
                 break;
             case TOPOLOGY_TEMPLATE:
                 toscaElement = new TopologyTemplate();
@@ -1001,34 +993,6 @@ public abstract class ToscaElementOperation extends BaseOperation {
         return dataType;
     }
 
-    private Map<String, AttributeDataDefinition> getAttributesFromComponentV(final GraphVertex componentV) {
-        final Map<String, Object> jsonMetada = componentV.getMetadataJson();
-        final Map<String, AttributeDataDefinition> attributeDataDefinitionMap = new HashMap<>();
-        if (MapUtils.isNotEmpty(jsonMetada)) {
-            final Object attributes = jsonMetada.get(ToscaTagNamesEnum.ATTRIBUTES.getElementName());
-            if (attributes instanceof Map) {
-                final Map<String, Object> map = (Map<String, Object>) attributes;
-                attributeDataDefinitionMap.putAll(map.values().stream().map(attributeMap -> {
-                    final AttributeDataDefinition attributeDef = new AttributeDataDefinition();
-                    final String name = (String) ((Map<String, Object>) attributeMap).get("name");
-                    attributeDef.setName(name);
-                    final String type = (String) ((Map<String, Object>) attributeMap).get("type");
-                    attributeDef.setType(type);
-                    final String description = (String) ((Map<String, Object>) attributeMap).get("description");
-                    attributeDef.setDescription(description);
-                    final Object _default = ((Map<String, Object>) attributeMap).get("_default");
-                    attributeDef.set_default(_default);
-                    final String status = (String) ((Map<String, Object>) attributeMap).get("status");
-                    attributeDef.setStatus(status);
-                    final EntrySchema entry_schema = (EntrySchema) ((Map<String, Object>) attributeMap).get("entry_schema");
-                    attributeDef.setEntry_schema(entry_schema);
-                    return attributeDef;
-                }).collect(Collectors.toMap(AttributeDataDefinition::getName, a -> a)));
-            }
-        }
-        return attributeDataDefinitionMap;
-    }
-
     protected JanusGraphOperationStatus setResourceCategoryFromGraphV(Vertex vertex, CatalogComponent catalogComponent) {
         List<CategoryDefinition> categories = new ArrayList<>();
         SubCategoryDefinition subcategory;
@@ -1046,6 +1010,15 @@ public abstract class ToscaElementOperation extends BaseOperation {
         subcategory.setUniqueId((String) subCategoryV.property(GraphPropertyEnum.UNIQUE_ID.getProperty()).value());
         subcategory.setNormalizedName(subCategoryNormalizedName);
         subcategory.setName((String) subCategoryV.property(GraphPropertyEnum.NAME.getProperty()).value());
+        
+        Type listTypeSubcat = new TypeToken<List<MetadataKeyDataDefinition>>() {
+        }.getType();
+        List<MetadataKeyDataDefinition> metadataKeys =
+                subCategoryV.property(GraphPropertyEnum.METADATA_KEYS.getProperty()).isPresent() ? getGson().fromJson(
+                        (String) subCategoryV.property(GraphPropertyEnum.METADATA_KEYS.getProperty()).value(),
+                        listTypeSubcat) : Collections.emptyList();
+        subcategory.setMetadataKeys(metadataKeys);
+        
         Either<Vertex, JanusGraphOperationStatus> parentVertex = janusGraphDao.getParentVertex(subCategoryV, EdgeLabelEnum.SUB_CATEGORY, JsonParseFlagEnum.NoParse);
         Vertex categoryV = parentVertex.left().value();
         String categoryNormalizedName = (String) categoryV.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()).value();
@@ -1077,6 +1050,14 @@ public abstract class ToscaElementOperation extends BaseOperation {
         category.setName((String) categoryV.property(GraphPropertyEnum.NAME.getProperty()).value());
         category.setUseServiceSubstitutionForNestedServices((Boolean) categoryV.property(GraphPropertyEnum.USE_SUBSTITUTION_FOR_NESTED_SERVICES.getProperty()).orElse(false));
 
+        Type listTypeCat = new TypeToken<List<MetadataKeyDataDefinition>>() {
+        }.getType();
+        List<MetadataKeyDataDefinition> metadataKeys =
+                categoryV.property(GraphPropertyEnum.METADATA_KEYS.getProperty()).isPresent() ? getGson().fromJson(
+                        (String) categoryV.property(GraphPropertyEnum.METADATA_KEYS.getProperty()).value(), listTypeCat)
+                        : Collections.emptyList();
+        category.setMetadataKeys(metadataKeys);
+        
         categories.add(category);
         catalogComponent.setCategories(categories);
         return JanusGraphOperationStatus.OK;
@@ -1104,6 +1085,10 @@ public abstract class ToscaElementOperation extends BaseOperation {
         List<String> iconsfromJsonSubcat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.ICONS), listTypeSubcat);
         subcategory.setIcons(iconsfromJsonSubcat);
 
+        final Type metadataKeysTypeCat = new TypeToken<List<MetadataKeyDataDefinition>>() {}.getType();
+        final List<MetadataKeyDataDefinition> metadataKeysfromJsonCat = getGson().fromJson((String) metadataProperties.get(GraphPropertyEnum.METADATA_KEYS), metadataKeysTypeCat);
+        subcategory.setMetadataKeys(metadataKeysfromJsonCat);
+  
         Either<GraphVertex, JanusGraphOperationStatus> parentVertex = janusGraphDao
             .getParentVertex(subCategoryV, EdgeLabelEnum.SUB_CATEGORY, JsonParseFlagEnum.NoParse);
         if (parentVertex.isRight()) {