Refactor CsarUtils::validateNonMetaArtifact 99/106999/2
authorFrancis Toth <francis.toth@yoppworks.com>
Fri, 1 May 2020 17:26:03 +0000 (13:26 -0400)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Sun, 3 May 2020 08:15:29 +0000 (08:15 +0000)
This commit aims to refactor the CsarUtils::validateNonMetaArtifact function and make it up to standards regarding how fj.Either should be used.

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

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

index 22b655a..6ecb518 100644 (file)
@@ -38,6 +38,8 @@ import java.util.Map.Entry;
 import java.util.Objects;
 import java.util.Optional;
 import java.util.Set;
+import java.util.function.Function;
+import java.util.function.Supplier;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import java.util.zip.ZipEntry;
@@ -830,31 +832,31 @@ public class CsarUtils {
      * @param collectedWarningMessages
      * @return
      */
-    public static Either<NonMetaArtifactInfo, Boolean> validateNonMetaArtifact(String artifactPath, byte[] payloadData, Map<String, Set<List<String>>> collectedWarningMessages) {
-        Either<NonMetaArtifactInfo, Boolean> ret;
+    public static Either<NonMetaArtifactInfo, Boolean> validateNonMetaArtifact(
+        String artifactPath,
+        byte[] payloadData,
+        Map<String, Set<List<String>>> collectedWarningMessages
+    ) {
         try {
             String[] parsedArtifactPath = artifactPath.split(PATH_DELIMITER);
-            // Validate Artifact Group Type
-            Either<ArtifactGroupTypeEnum, Boolean> eitherGroupType = detectArtifactGroupType(parsedArtifactPath[1], collectedWarningMessages);
-            if (eitherGroupType.isLeft()) {
-                final ArtifactGroupTypeEnum groupTypeEnum = eitherGroupType.left().value();
-
-                // Validate Artifact Type
-                String artifactType = parsedArtifactPath[2];
-                artifactType = detectArtifactTypeVF(groupTypeEnum, artifactType, collectedWarningMessages);
-
-                String artifactFileNameType = parsedArtifactPath[3];
-                ret = Either.left(new NonMetaArtifactInfo(artifactFileNameType, artifactPath, artifactType, groupTypeEnum, payloadData, null, true));
-
-            } else {
-                ret = Either.right(eitherGroupType.right().value());
-            }
+            String groupType = parsedArtifactPath[1];
+            String receivedTypeName = parsedArtifactPath[2];
+            String artifactFileNameType = parsedArtifactPath[3];
+
+            return detectArtifactGroupType(groupType, collectedWarningMessages)
+                .left().bind(artifactGroupType -> {
+                    String artifactType =
+                        detectArtifactTypeVF(artifactGroupType, receivedTypeName, collectedWarningMessages);
+
+                    return Either.left(new NonMetaArtifactInfo(
+                        artifactFileNameType, artifactPath, artifactType,
+                        artifactGroupType, payloadData, null, true
+                    ));
+                });
         } catch (Exception e) {
             log.debug("detectArtifactGroupType failed with exception", e);
-            ret = Either.right(false);
+            return Either.right(false);
         }
-        return ret;
-
     }
 
     private static String detectArtifactTypeVFC(ArtifactGroupTypeEnum artifactGroupType, String receivedTypeName, String parentVfName, Map<String, Set<List<String>>> collectedWarningMessages) {