Refactor CsarUtils::collectComponentTypeArtifacts 11/107011/3
authorFrancis Toth <francis.toth@yoppworks.com>
Sun, 3 May 2020 14:01:47 +0000 (10:01 -0400)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Tue, 5 May 2020 08:14:59 +0000 (08:14 +0000)
This commit is a minor refactoring required to refactor CsarUtils::collectComponentInstanceArtifacts. In order to keep this commit small, we only focused CsarUtils::collectComponentTypeArtifacts, and removed any output that is not reflected by the function's result type. Modifying an argument is a actually a bad practice as explained in Clean Code by Robert Martin (cf. "Output arguments should be avoided"). This commit aims to enforce this aspect.

Change-Id: Ice4d6c9a78e7706c639dd60bc272253e298bc7be
Signed-off-by: Francis Toth <francis.toth@yoppworks.com>
Issue-ID: SDC-2812

catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java

index 85d0c78..47a8478 100644 (file)
@@ -1124,6 +1124,10 @@ public class CsarUtils {
             return artifactsInfoField.isEmpty();
         }
 
+        public boolean isNotEmpty() {
+            return !isEmpty();
+        }
+
     }
 
     /**
@@ -1290,19 +1294,13 @@ public class CsarUtils {
         return result.toString();
     }
 
-    private ComponentTypeArtifacts collectComponentTypeArtifacts(Map<String, ComponentTypeArtifacts> resourcesArtifacts, ComponentInstance componentInstance,
-            Component fetchedComponent) {
-        String toscaComponentName = componentInstance.getToscaComponentName() + "_v" + componentInstance.getComponentVersion();
-
-        ComponentTypeArtifacts componentArtifactsInfo = resourcesArtifacts.get(toscaComponentName);
-        //if there are no artifacts for this component type we need to fetch and build them
-        if (componentArtifactsInfo==null){
-            ArtifactsInfo componentArtifacts = collectComponentArtifacts(fetchedComponent);
-            componentArtifactsInfo = new ComponentTypeArtifacts();
-            if (!componentArtifacts.isEmpty()){
-                componentArtifactsInfo.setComponentArtifacts(componentArtifacts);
-                resourcesArtifacts.put(toscaComponentName, componentArtifactsInfo);
-            }
+    private ComponentTypeArtifacts collectComponentTypeArtifacts(
+        Component fetchedComponent
+    ) {
+        ArtifactsInfo componentArtifacts = collectComponentArtifacts(fetchedComponent);
+        ComponentTypeArtifacts componentArtifactsInfo = new ComponentTypeArtifacts();
+        if (componentArtifacts.isNotEmpty()) {
+            componentArtifactsInfo.setComponentArtifacts(componentArtifacts);
         }
         return componentArtifactsInfo;
     }
@@ -1327,7 +1325,17 @@ public class CsarUtils {
                Component fetchedComponent = component.left().value();
 
         //2. fill the artifacts for the current component parent type
-        ComponentTypeArtifacts componentParentArtifacts = collectComponentTypeArtifacts(resourcesTypeArtifacts, componentInstance, fetchedComponent);
+        String toscaComponentName =
+            componentInstance.getToscaComponentName() + "_v" + componentInstance.getComponentVersion();
+
+        // if there are no artifacts for this component type we need to fetch and build them
+        ComponentTypeArtifacts componentParentArtifacts = Optional
+            .ofNullable(resourcesTypeArtifacts.get(toscaComponentName))
+            .orElseGet(() -> collectComponentTypeArtifacts(fetchedComponent));
+
+        if (componentParentArtifacts.getComponentArtifacts().isNotEmpty()) {
+            resourcesTypeArtifacts.put(toscaComponentName, componentParentArtifacts);
+        }
 
         //3. find the artifacts specific to the instance
         Map<String, List<ArtifactDefinition>> componentInstanceSpecificInformationalArtifacts =