From 08cb42224a5d20783e525cadbab1b7ae40a86f72 Mon Sep 17 00:00:00 2001 From: Chris André Date: Tue, 21 Apr 2020 14:40:07 -0400 Subject: Handle null case in `GroupVersionUpdater` - change in the final condition of `isGenerateGroupUUID` - Added tests related to modified code Issue-ID: SDC-2913 Signed-off-by: Chris Andre Change-Id: I22738f9f4922467209a16a4d6b3b78d7bfac11c9 --- .../components/impl/group/GroupVersionUpdater.java | 38 ++- .../impl/group/GroupVersionUpdaterTest.java | 305 +++++++++++++++++++++ 2 files changed, 330 insertions(+), 13 deletions(-) create mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/group/GroupVersionUpdaterTest.java diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/group/GroupVersionUpdater.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/group/GroupVersionUpdater.java index bb4d884811..8e9ba7a88b 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/group/GroupVersionUpdater.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/group/GroupVersionUpdater.java @@ -20,7 +20,8 @@ package org.openecomp.sdc.be.components.impl.group; - +import java.util.Collections; +import java.util.stream.Collectors; import org.openecomp.sdc.be.components.impl.version.OnChangeVersionCommand; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datatypes.enums.GroupTypeEnum; @@ -37,7 +38,6 @@ import org.openecomp.sdc.common.log.wrappers.Logger; import java.util.List; import java.util.Map; import java.util.function.Consumer; -import java.util.stream.Collectors; import static org.apache.commons.collections.CollectionUtils.isEmpty; @@ -84,27 +84,39 @@ public class GroupVersionUpdater implements OnChangeVersionCommand { } group.setVersion(String.valueOf(newVersion)); } + } + static List emptyIfNull(final List list) { + return (list == null? Collections.emptyList() : list); } - private boolean isGenerateGroupUUID(GroupDefinition group, Component container) { - if(GroupTypeEnum.VF_MODULE.getGroupTypeName().equals(group.getType())){ - List artifactsUuid = group.getArtifactsUuid(); - List heatArtifactUniqueIDs = group.getArtifacts().stream().filter(a->!a.endsWith("env")).collect(Collectors.toList()); - Map deploymentArtifacts = container.getDeploymentArtifacts(); - for (String heatArtifactUniqueID : heatArtifactUniqueIDs){ - ArtifactDefinition artifactDefinition = deploymentArtifacts.get(heatArtifactUniqueID.split("\\.", -1)[1]); - if((artifactDefinition == null || artifactDefinition.isEmpty()) - && !artifactsUuid.contains(artifactDefinition.getArtifactUUID()) ){ + static Map emptyIfNull(final Map list) { + return (list == null? Collections.emptyMap() : list); + } + + static boolean isGenerateGroupUUID(GroupDefinition group, Component container) { + if (!GroupTypeEnum.VF_MODULE.getGroupTypeName().equals(group.getType())) { + return true; + } else { + List artifactsUuid = emptyIfNull(group.getArtifactsUuid()); + Map deploymentArtifacts = emptyIfNull(container.getDeploymentArtifacts()); + List heatArtifactUniqueIDs = emptyIfNull(group.getArtifacts()).stream().filter(a -> !a.endsWith("env")).collect( + Collectors.toList()); + + for (String heatArtifactUniqueID : heatArtifactUniqueIDs) { + ArtifactDefinition artifactDefinition = deploymentArtifacts + .get(heatArtifactUniqueID.split("\\.", -1)[1]); + + if ((artifactDefinition == null + || artifactDefinition.isEmpty()) + || !artifactsUuid.contains(artifactDefinition.getArtifactUUID())) { return true; } } return false; } - return true; } - private ActionStatus updateGroupsVersion(Component groupsContainer, Consumer> updateGroupVersion) { List groups = groupsContainer.getGroups(); if (isEmpty(groups)) { diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/group/GroupVersionUpdaterTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/group/GroupVersionUpdaterTest.java new file mode 100644 index 0000000000..4e08545acb --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/group/GroupVersionUpdaterTest.java @@ -0,0 +1,305 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.components.impl.group; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import io.vavr.collection.HashMap; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.function.Supplier; +import java.util.stream.Collectors; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.datatypes.enums.GroupTypeEnum; +import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.GroupDefinition; + +public class GroupVersionUpdaterTest { + + @Test + @DisplayName("emptyIfNull(List) - should return the same List passed as an argument if it is not null") + public void safeAccessList_NotNull() { + //Given + List arg = defaultArtifactsUuid.get(); + + //When + List result = GroupVersionUpdater.emptyIfNull(arg); + + //Then + assertSame(arg, result); + } + + @Test + @DisplayName("emptyIfNull(List) - should return an empty List if we pass it a null value") + public void safeAccessList_Null() { + //Given + List arg = null; + + //When + List result = GroupVersionUpdater.emptyIfNull(arg); + + //Then + assertSame(Collections.EMPTY_LIST, result); + } + + @Test + @DisplayName("emptyIfNull(Map) - should return the same Map passed as an argument if it is not null") + public void safeAccessMap_NotNull() { + //Given + Map arg = defaultDeploymentArtifacts.get(); + + //When + Map result = GroupVersionUpdater.emptyIfNull(arg); + + //Then + assertSame(arg, result); + } + + @Test + @DisplayName("emptyIfNull(Map) - should return an empty Map if we pass it a null value") + public void safeAccessMap_Null() { + //Given + Map arg = null; + + //When + Map result = GroupVersionUpdater.emptyIfNull(arg); + + //Then + assertSame(Collections.EMPTY_MAP, result); + } + + + @Test + @DisplayName("isGenerateGroupUUID - Return true if there is a deployment artifact in the container that is not" + + " part of the group") + public void isGenerateGroupUUID_GoldenPath() { + // Given + Supplier type = defaultGroupType; + Supplier> artifactsUuid = defaultArtifactsUuid; + GroupDefinition group = getGroup( + type, + artifactsUuid + ); + + Supplier> deploymentArtifacts = defaultDeploymentArtifacts; + Component container = getContainer(deploymentArtifacts); + + // When + boolean result = GroupVersionUpdater.isGenerateGroupUUID(group, container); + + // Then + assertTrue(result); + } + + @Test + @DisplayName("isGenerateGroupUUID - `group`'s type is not GroupTypeEnum.VF_MODULE") + public void isGenerateGroupUUID_GroupIsNotVFModule() { + // Given + Supplier type = GroupTypeEnum.HEAT_STACK::getGroupTypeName; + Supplier> artifactsUuid = defaultArtifactsUuid; + GroupDefinition group = getGroup( + type, + artifactsUuid + ); + + Supplier> deploymentArtifacts = defaultDeploymentArtifacts; + Component container = getContainer(deploymentArtifacts); + + // When + boolean result = GroupVersionUpdater.isGenerateGroupUUID(group, container); + + // Then + assertTrue(result); + } + + @Test + @DisplayName("isGenerateGroupUUID - `group`'s type is null") + public void isGenerateGroupUUID_NullGroupType() { + // Given + Supplier type = () -> null; + Supplier> artifactsUuid = defaultArtifactsUuid; + GroupDefinition group = getGroup( + type, + artifactsUuid + ); + + Supplier> deploymentArtifacts = defaultDeploymentArtifacts; + Component container = getContainer(deploymentArtifacts); + + // When + boolean result = GroupVersionUpdater.isGenerateGroupUUID(group, container); + + // Then + assertTrue(result); + } + + @Test + @DisplayName("isGenerateGroupUUID - `container`'s DeploymentArtifacts is null") + public void isGenerateGroupUUID_NullDeploymentArtifacts() { + // Given + Supplier type = defaultGroupType; + Supplier> artifactsUuid = defaultArtifactsUuid; + GroupDefinition group = getGroup( + type, + artifactsUuid + ); + + Supplier> deploymentArtifacts = () -> null; + Component container = getContainer(deploymentArtifacts); + + // When + boolean result = GroupVersionUpdater.isGenerateGroupUUID(group, container); + + // Then + assertTrue(result); + } + + @Test + @DisplayName("isGenerateGroupUUID - `group`'s artifactUuid is null") + public void isGenerateGroupUUID_NullGroupArtifactUuid() { + // Given + Supplier type = defaultGroupType; + Supplier> artifactsUuid = () -> null; + GroupDefinition group = getGroup( + type, + artifactsUuid + ); + + Supplier> deploymentArtifacts = defaultDeploymentArtifacts; + Component container = getContainer(deploymentArtifacts); + + // When + boolean result = GroupVersionUpdater.isGenerateGroupUUID(group, container); + + // Then + assertFalse(result); + } + + @Test + @DisplayName("isGenerateGroupUUID - `group`'s artifactUuid only has artifactUuid ending with 'env'") + public void isGenerateGroupUUID_OnlyArtifactEndingWithEnv() { + // Given + Supplier type = defaultGroupType; + Supplier> artifactsUuid = () -> + defaultArtifactsUuid.get().stream() + .map(str -> str + ".env") + .collect(Collectors.toList()); + GroupDefinition group = getGroup( + type, + artifactsUuid + ); + + Supplier> deploymentArtifacts = defaultDeploymentArtifacts; + Component container = getContainer(deploymentArtifacts); + + // When + boolean result = GroupVersionUpdater.isGenerateGroupUUID(group, container); + + // Then + assertFalse(result); + } + + @Test + @DisplayName("isGenerateGroupUUID - `group`'s artifactUuid is not part of the container's list of deployment artifacts") + public void isGenerateGroupUUID_NoMatchingUuidInContainer() { + // Given + Supplier type = defaultGroupType; + Supplier> artifactsUuid = defaultArtifactsUuid; + GroupDefinition group = getGroup( + type, + artifactsUuid + ); + + Supplier> deploymentArtifacts = defaultDeploymentArtifacts; + Component container = getContainer(deploymentArtifacts); + + // When + boolean result = GroupVersionUpdater.isGenerateGroupUUID(group, container); + + // Then + assertTrue(result); + } + + Supplier defaultGroupType = GroupTypeEnum.VF_MODULE::getGroupTypeName; + + Supplier> defaultArtifactsUuid = () -> Arrays.asList( + "uniqueId1.1", + "uniqueId2.2", + "uniqueId3.3", + "uniqueId4.4", + "uniqueId5.5", + "uniqueId6.6", + "uniqueId7.7", + "uniqueId8.8", + "uniqueId9.9" + ); + + Supplier> defaultDeploymentArtifacts = () -> HashMap.of( + "uniqueId1.1", getArtifactDefinition("uniqueId1.1"), + "uniqueId2.2", getArtifactDefinition("uniqueId2.2"), + "uniqueId3.3", getArtifactDefinition("uniqueId3.3"), + "uniqueId44", getArtifactDefinition("uniqueId4.4"), + "uniqueId5.5", getArtifactDefinition("uniqueId5.5"), + "uniqueId6.6", getArtifactDefinition("uniqueId6.6"), + "uniqueId7.7", getArtifactDefinition("uniqueId7.7"), + "uniqueId8.8", getArtifactDefinition("uniqueId8.8"), + "uniqueId9.9", getArtifactDefinition("uniqueId9.9"), + "uniqueId.not.found", getArtifactDefinition("uniqueId.not.found") + ).toJavaMap(); + + ArtifactDefinition getArtifactDefinition(String artifactUuid) { + return new ArtifactDefinition() { + @Override + public String getArtifactUUID() { + return artifactUuid; + } + }; + } + + GroupDefinition getGroup(Supplier type, Supplier> artifacts) { + return new GroupDefinition() { + @Override + public String getType() { + return type.get(); + } + + @Override + public List getArtifacts() { + return artifacts.get(); + } + }; + } + + Component getContainer(Supplier> deploymentArtifacts) { + return new Component(null) { + @Override + public Map getDeploymentArtifacts() { + return deploymentArtifacts.get(); + } + }; + } +} -- cgit 1.2.3-korg