From 5971be1a797d4ad232257c4bf5dd8c3359b56ede Mon Sep 17 00:00:00 2001 From: Francis Toth Date: Sun, 31 May 2020 08:42:14 -0400 Subject: Refactor ArtifactsBusinessLogic::getUpdatedGroupInstances Signed-off-by: Francis Toth Change-Id: I6d62c1f916aa820f84328d25d2afbc7470397b0d Issue-ID: SDC-2961 --- .../be/components/impl/ArtifactsBusinessLogic.java | 43 ++++++++++------------ 1 file changed, 20 insertions(+), 23 deletions(-) (limited to 'catalog-be') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java index 60b88cf5cb..d8f85617c0 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java @@ -27,7 +27,6 @@ import static org.openecomp.sdc.be.dao.api.ActionStatus.MISMATCH_BETWEEN_ARTIFAC import com.google.common.annotations.VisibleForTesting; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import fj.F; import fj.data.Either; import io.vavr.control.Option; import java.io.ByteArrayInputStream; @@ -69,11 +68,11 @@ import org.openecomp.sdc.be.components.impl.artifact.PayloadTypeEnum; import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException; import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException; import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; -import org.openecomp.sdc.be.components.utils.ArtifactUtils; import org.openecomp.sdc.be.components.impl.utils.ComponentUtils; import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic; import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction; import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum; +import org.openecomp.sdc.be.components.utils.ArtifactUtils; import org.openecomp.sdc.be.components.utils.InterfaceOperationUtils; import org.openecomp.sdc.be.config.ArtifactConfiguration; import org.openecomp.sdc.be.config.BeEcompErrorManager; @@ -1454,27 +1453,25 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { return updatedGroups; } - private List getUpdatedGroupInstances(String artifactId, ArtifactDefinition foundArtifact, List groupInstances) { - List updatedGroupInstances = new ArrayList<>(); - if (CollectionUtils.isNotEmpty(groupInstances)) { - boolean isUpdated = false; - for (GroupInstance groupInstance : groupInstances) { - isUpdated = false; - if (CollectionUtils.isNotEmpty(groupInstance.getGroupInstanceArtifacts()) && groupInstance.getGroupInstanceArtifacts().contains(artifactId)) { - groupInstance.getGroupInstanceArtifacts().remove(artifactId); - isUpdated = true; - } - if (CollectionUtils.isNotEmpty(groupInstance.getGroupInstanceArtifactsUuid()) && groupInstance.getGroupInstanceArtifactsUuid() - .contains(foundArtifact.getArtifactUUID())) { - groupInstance.getGroupInstanceArtifactsUuid().remove(foundArtifact.getArtifactUUID()); - isUpdated = true; - } - if (isUpdated) { - updatedGroupInstances.add(groupInstance); - } - } - } - return updatedGroupInstances; + private List getUpdatedGroupInstances( + String artifactId, ArtifactDefinition foundArtifact, List groupInstances + ) { + if (CollectionUtils.isEmpty(groupInstances)) { + return new ArrayList<>(); + } + // TODO: A defensive copy should be created here for groupInstances. Modifying + // arguments (aka output arguments) is overall a bad practice as explained in + // Clean Code by Robert Martin. + // A better approach would be to use Lenses. + + return groupInstances.stream().filter(gi -> { + boolean groupInstanceArtifactRemoved = gi.getGroupInstanceArtifacts() != null && + gi.getGroupInstanceArtifacts().remove(artifactId); + boolean groupInstanceArtifactUUIDRemoved = gi.getGroupInstanceArtifactsUuid() != null && + gi.getGroupInstanceArtifactsUuid().remove(foundArtifact.getArtifactUUID()); + + return groupInstanceArtifactRemoved || groupInstanceArtifactUUIDRemoved; + }).collect(Collectors.toList()); } private ArtifactDataDefinition deleteOrUpdateArtifactOnGraph(Component component, String parentId, String artifactId, NodeTypeEnum parentType, ArtifactDefinition foundArtifact, Boolean cloneIsNeeded) { -- cgit 1.2.3-korg