aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be
diff options
context:
space:
mode:
authorFrancis Toth <francis.toth@yoppworks.com>2020-05-01 12:42:58 -0400
committerOfir Sonsino <ofir.sonsino@intl.att.com>2020-05-18 10:58:18 +0000
commitbf37c21229e1b60937202c3962b24f14b45c96e5 (patch)
treef1c63a658f3b266d318bc0ffc385da53e706b77f /catalog-be
parent0ff3fcba8b7d645e497a7300776bde86590ec70c (diff)
Refactor CsarUtil::extractVfcArtifact
Signed-off-by: Francis Toth <francis.toth@yoppworks.com> Change-Id: Ica8d17d4f3c763920f406ccb89fb3e464d05eab4 Issue-ID: SDC-2812
Diffstat (limited to 'catalog-be')
-rw-r--r--catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java84
-rw-r--r--catalog-be/src/test/java/org/openecomp/sdc/be/tosca/CsarUtilsTest.java12
2 files changed, 58 insertions, 38 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java
index 8a8fff797f..21c626bf53 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java
@@ -753,7 +753,11 @@ public class CsarUtils {
// filter CSAR entry by node type artifact path
.filter(e -> Pattern.compile(VFC_NODE_TYPE_ARTIFACTS_PATH_PATTERN).matcher(e.getKey()).matches())
// extract ArtifactDefinition from CSAR entry for each entry with matching artifact path
- .forEach(e -> addExtractedVfcArtifact(extractVfcArtifact(e, collectedWarningMessages), artifacts));
+ .forEach(e ->
+ extractVfcArtifact(e, collectedWarningMessages).ifPresent(ip ->
+ addExtractedVfcArtifact(ip, artifacts)
+ )
+ );
// add counter suffix to artifact labels
handleWarningMessages(collectedWarningMessages);
@@ -775,30 +779,30 @@ public class CsarUtils {
}
- private static void addExtractedVfcArtifact(ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact, Map<String, List<ArtifactDefinition>> artifacts) {
- if (extractedVfcArtifact != null) {
- List<ArtifactDefinition> currArtifactsList;
- String vfcToscaNamespace = extractedVfcArtifact.getKey();
- if (artifacts.containsKey(vfcToscaNamespace)) {
- currArtifactsList = artifacts.get(vfcToscaNamespace);
- } else {
- currArtifactsList = new ArrayList<>();
- artifacts.put(vfcToscaNamespace, currArtifactsList);
- }
- currArtifactsList.add(extractedVfcArtifact.getValue());
- }
+ private static void addExtractedVfcArtifact(
+ ImmutablePair<String, ArtifactDefinition> extractedVfcArtifact,
+ Map<String, List<ArtifactDefinition>> artifacts
+ ) {
+ String vfcToscaNamespace = extractedVfcArtifact.getKey();
+
+ artifacts.computeIfAbsent(vfcToscaNamespace, k -> new ArrayList<>());
+ artifacts.get(vfcToscaNamespace).add(extractedVfcArtifact.getValue());
}
- private static ImmutablePair<String, ArtifactDefinition> extractVfcArtifact(Entry<String, byte[]> entry, Map<String, Set<List<String>>> collectedWarningMessages) {
- ArtifactDefinition artifact;
+ private static Optional<ImmutablePair<String, ArtifactDefinition>> extractVfcArtifact(
+ Entry<String, byte[]> entry,
+ Map<String, Set<List<String>>> collectedWarningMessages
+ ) {
String[] parsedCsarArtifactPath = entry.getKey().split(PATH_DELIMITER);
- Either<ArtifactGroupTypeEnum, Boolean> eitherArtifactGroupType = detectArtifactGroupType(parsedCsarArtifactPath[2].toUpperCase(), collectedWarningMessages);
- if (eitherArtifactGroupType.isLeft()) {
- artifact = buildArtifactDefinitionFromCsarArtifactPath(entry, collectedWarningMessages, parsedCsarArtifactPath, eitherArtifactGroupType.left().value());
- } else {
- return null;
- }
- return new ImmutablePair<>(parsedCsarArtifactPath[1], artifact);
+ String groupType = parsedCsarArtifactPath[2].toUpperCase();
+
+ return detectArtifactGroupType(groupType, collectedWarningMessages)
+ .left()
+ .map(buildArtifactDefinitionFromCsarArtifactPath(entry, collectedWarningMessages, parsedCsarArtifactPath))
+ .either(
+ ad -> Optional.of(new ImmutablePair<>(parsedCsarArtifactPath[1], ad)),
+ b -> Optional.empty()
+ );
}
private static Either<ArtifactGroupTypeEnum, Boolean> detectArtifactGroupType(String groupType, Map<String, Set<List<String>>> collectedWarningMessages) {
@@ -829,19 +833,29 @@ public class CsarUtils {
return result;
}
- private static ArtifactDefinition buildArtifactDefinitionFromCsarArtifactPath(Entry<String, byte[]> entry, Map<String, Set<List<String>>> collectedWarningMessages, String[] parsedCsarArtifactPath, ArtifactGroupTypeEnum artifactGroupType) {
- ArtifactDefinition artifact;
- artifact = new ArtifactDefinition();
- artifact.setArtifactGroupType(artifactGroupType);
- artifact.setArtifactType(detectArtifactTypeVFC(artifactGroupType, parsedCsarArtifactPath[3], parsedCsarArtifactPath[1], collectedWarningMessages));
- artifact.setArtifactName(ValidationUtils.normalizeFileName(parsedCsarArtifactPath[parsedCsarArtifactPath.length - 1]));
- artifact.setPayloadData(Base64.encodeBase64String(entry.getValue()));
- artifact.setArtifactDisplayName(artifact.getArtifactName().lastIndexOf('.') > 0 ? artifact.getArtifactName().substring(0, artifact.getArtifactName().lastIndexOf('.')) : artifact.getArtifactName());
- artifact.setArtifactLabel(ValidationUtils.normalizeArtifactLabel(artifact.getArtifactName()));
- artifact.setDescription(ARTIFACT_CREATED_FROM_CSAR);
- artifact.setIsFromCsar(true);
- artifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(entry.getValue()));
- return artifact;
+ private static F<ArtifactGroupTypeEnum, ArtifactDefinition> buildArtifactDefinitionFromCsarArtifactPath(
+ Entry<String, byte[]> entry,
+ Map<String, Set<List<String>>> collectedWarningMessages,
+ String[] parsedCsarArtifactPath
+ ) {
+ return artifactGroupType -> {
+ ArtifactDefinition artifact;
+ artifact = new ArtifactDefinition();
+ artifact.setArtifactGroupType(artifactGroupType);
+ artifact.setArtifactType(
+ detectArtifactTypeVFC(artifactGroupType, parsedCsarArtifactPath[3], parsedCsarArtifactPath[1],
+ collectedWarningMessages));
+ artifact.setArtifactName(
+ ValidationUtils.normalizeFileName(parsedCsarArtifactPath[parsedCsarArtifactPath.length - 1]));
+ artifact.setPayloadData(Base64.encodeBase64String(entry.getValue()));
+ artifact.setArtifactDisplayName(artifact.getArtifactName().lastIndexOf('.') > 0 ? artifact.getArtifactName()
+ .substring(0, artifact.getArtifactName().lastIndexOf('.')) : artifact.getArtifactName());
+ artifact.setArtifactLabel(ValidationUtils.normalizeArtifactLabel(artifact.getArtifactName()));
+ artifact.setDescription(ARTIFACT_CREATED_FROM_CSAR);
+ artifact.setIsFromCsar(true);
+ artifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(entry.getValue()));
+ return artifact;
+ };
}
@Getter
diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/CsarUtilsTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/CsarUtilsTest.java
index 092dd1d2b5..3ee2b89047 100644
--- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/CsarUtilsTest.java
+++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/CsarUtilsTest.java
@@ -26,6 +26,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import fj.data.Either;
import java.io.IOException;
@@ -40,6 +41,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Optional;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@@ -660,10 +662,14 @@ public class CsarUtilsTest extends BeConfDependentTest {
map.put(path, "value".getBytes());
Entry<String, byte[]> entry = map.entrySet().iterator().next();
- ImmutablePair<String, ArtifactDefinition> output = Deencapsulation.invoke(testSubject, "extractVfcArtifact", entry, new HashMap<>());
+ Optional<ImmutablePair<String, ArtifactDefinition>> output =
+ Deencapsulation.invoke(testSubject, "extractVfcArtifact", entry, new HashMap<>());
- assertNotNull(output);
- assertEquals("to",output.left);
+ if(output.isPresent()) {
+ assertEquals("to", output.get().left);
+ } else {
+ fail("`output` is empty!");
+ }
}
@Test