diff options
author | Chris André <chris.andre@yoppworks.com> | 2020-04-21 14:40:07 -0400 |
---|---|---|
committer | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2020-04-28 12:19:02 +0000 |
commit | 08cb42224a5d20783e525cadbab1b7ae40a86f72 (patch) | |
tree | 4f8c31dcc56cb0d9b9dc01c33b965dbaec41db7c /catalog-be | |
parent | 33a4512b6eb6204e2209a788b06153a889629f4e (diff) |
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 <chris.andre@yoppworks.com>
Change-Id: I22738f9f4922467209a16a4d6b3b78d7bfac11c9
Diffstat (limited to 'catalog-be')
2 files changed, 330 insertions, 13 deletions
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 <T> List<T> emptyIfNull(final List<T> list) { + return (list == null? Collections.emptyList() : list); } - private boolean isGenerateGroupUUID(GroupDefinition group, Component container) { - if(GroupTypeEnum.VF_MODULE.getGroupTypeName().equals(group.getType())){ - List<String> artifactsUuid = group.getArtifactsUuid(); - List<String> heatArtifactUniqueIDs = group.getArtifacts().stream().filter(a->!a.endsWith("env")).collect(Collectors.toList()); - Map<String, ArtifactDefinition> 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 <T, U> Map<T, U> emptyIfNull(final Map<T, U> 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<String> artifactsUuid = emptyIfNull(group.getArtifactsUuid()); + Map<String, ArtifactDefinition> deploymentArtifacts = emptyIfNull(container.getDeploymentArtifacts()); + List<String> 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<List<GroupDefinition>> updateGroupVersion) { List<GroupDefinition> 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<T>) - should return the same List passed as an argument if it is not null") + public void safeAccessList_NotNull() { + //Given + List<String> arg = defaultArtifactsUuid.get(); + + //When + List<String> result = GroupVersionUpdater.emptyIfNull(arg); + + //Then + assertSame(arg, result); + } + + @Test + @DisplayName("emptyIfNull(List<T>) - should return an empty List if we pass it a null value") + public void safeAccessList_Null() { + //Given + List<String> arg = null; + + //When + List<String> result = GroupVersionUpdater.emptyIfNull(arg); + + //Then + assertSame(Collections.EMPTY_LIST, result); + } + + @Test + @DisplayName("emptyIfNull(Map<T, U>) - should return the same Map passed as an argument if it is not null") + public void safeAccessMap_NotNull() { + //Given + Map<String, ArtifactDefinition> arg = defaultDeploymentArtifacts.get(); + + //When + Map<String, ArtifactDefinition> result = GroupVersionUpdater.emptyIfNull(arg); + + //Then + assertSame(arg, result); + } + + @Test + @DisplayName("emptyIfNull(Map<T, U>) - should return an empty Map if we pass it a null value") + public void safeAccessMap_Null() { + //Given + Map<String, ArtifactDefinition> arg = null; + + //When + Map<String, ArtifactDefinition> 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<String> type = defaultGroupType; + Supplier<List<String>> artifactsUuid = defaultArtifactsUuid; + GroupDefinition group = getGroup( + type, + artifactsUuid + ); + + Supplier<Map<String, ArtifactDefinition>> 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<String> type = GroupTypeEnum.HEAT_STACK::getGroupTypeName; + Supplier<List<String>> artifactsUuid = defaultArtifactsUuid; + GroupDefinition group = getGroup( + type, + artifactsUuid + ); + + Supplier<Map<String, ArtifactDefinition>> 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<String> type = () -> null; + Supplier<List<String>> artifactsUuid = defaultArtifactsUuid; + GroupDefinition group = getGroup( + type, + artifactsUuid + ); + + Supplier<Map<String, ArtifactDefinition>> 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<String> type = defaultGroupType; + Supplier<List<String>> artifactsUuid = defaultArtifactsUuid; + GroupDefinition group = getGroup( + type, + artifactsUuid + ); + + Supplier<Map<String, ArtifactDefinition>> 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<String> type = defaultGroupType; + Supplier<List<String>> artifactsUuid = () -> null; + GroupDefinition group = getGroup( + type, + artifactsUuid + ); + + Supplier<Map<String, ArtifactDefinition>> 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<String> type = defaultGroupType; + Supplier<List<String>> artifactsUuid = () -> + defaultArtifactsUuid.get().stream() + .map(str -> str + ".env") + .collect(Collectors.toList()); + GroupDefinition group = getGroup( + type, + artifactsUuid + ); + + Supplier<Map<String, ArtifactDefinition>> 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<String> type = defaultGroupType; + Supplier<List<String>> artifactsUuid = defaultArtifactsUuid; + GroupDefinition group = getGroup( + type, + artifactsUuid + ); + + Supplier<Map<String, ArtifactDefinition>> deploymentArtifacts = defaultDeploymentArtifacts; + Component container = getContainer(deploymentArtifacts); + + // When + boolean result = GroupVersionUpdater.isGenerateGroupUUID(group, container); + + // Then + assertTrue(result); + } + + Supplier<String> defaultGroupType = GroupTypeEnum.VF_MODULE::getGroupTypeName; + + Supplier<List<String>> defaultArtifactsUuid = () -> Arrays.asList( + "uniqueId1.1", + "uniqueId2.2", + "uniqueId3.3", + "uniqueId4.4", + "uniqueId5.5", + "uniqueId6.6", + "uniqueId7.7", + "uniqueId8.8", + "uniqueId9.9" + ); + + Supplier<Map<String, ArtifactDefinition>> 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<String> type, Supplier<List<String>> artifacts) { + return new GroupDefinition() { + @Override + public String getType() { + return type.get(); + } + + @Override + public List<String> getArtifacts() { + return artifacts.get(); + } + }; + } + + Component getContainer(Supplier<Map<String, ArtifactDefinition>> deploymentArtifacts) { + return new Component(null) { + @Override + public Map<String, ArtifactDefinition> getDeploymentArtifacts() { + return deploymentArtifacts.get(); + } + }; + } +} |