diff options
author | Francis Toth <francis.toth@yoppworks.com> | 2020-05-04 18:42:58 -0400 |
---|---|---|
committer | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2020-05-28 07:48:14 +0000 |
commit | 1de7b75829b3bf0c06ee7a8b3006b0edfb239420 (patch) | |
tree | 7c45b2830a80d92de1b5d2f1d65a0b84984f52d4 /catalog-be/src/test | |
parent | 9f7430db214092bba954fa07fd3e72e333116cfa (diff) |
Extract artifact component belonging logic
This commit extracts checkArtifactInComponent and checkArtifactInResourceInstance from ArtifactsBusinessLogic. Tests have been added along with a some utility functions designed to generate randomized test data. The former tests were deleted as they were not doing any assertions.
Signed-off-by: Francis Toth <francis.toth@yoppworks.com>
Change-Id: I1b27c55d064cf4e9a40cd7b152e7ea6032a09276
Issue-ID: SDC-2961
Diffstat (limited to 'catalog-be/src/test')
3 files changed, 315 insertions, 15 deletions
diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogicTest.java index 6f0b83b0d5..c51456547c 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogicTest.java @@ -845,20 +845,6 @@ public class ArtifactsBusinessLogicTest extends BaseBusinessLogicMock { } @Test - public void testCheckArtifactInComponent() throws Exception { - ArtifactsBusinessLogic testSubject; - Component component = new Resource(); - component.setComponentType(ComponentTypeEnum.RESOURCE); - String artifactId = ""; - boolean result; - - // default test - testSubject = createTestSubject(); - result = Deencapsulation.invoke(testSubject, "checkArtifactInComponent", - new Object[]{component, artifactId}); - } - - @Test public void testCheckCreateFields() throws Exception { ArtifactsBusinessLogic testSubject; // User user = USER; @@ -936,7 +922,6 @@ public class ArtifactsBusinessLogicTest extends BaseBusinessLogicMock { artifactId, component, artifacts); } - @Test public void testValidateArtifact() throws Exception { ArtifactsBusinessLogic testSubject; diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/ComponentUtilsTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/ComponentUtilsTest.java new file mode 100644 index 0000000000..bf9ecaf1bb --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/ComponentUtilsTest.java @@ -0,0 +1,130 @@ +/*- + * ============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.utils; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.openecomp.sdc.be.components.impl.utils.ComponentUtils.checkArtifactInComponent; +import static org.openecomp.sdc.be.components.impl.utils.ComponentUtils.checkArtifactInResourceInstance; +import static org.openecomp.sdc.be.components.impl.utils.TestDataUtils.aComponent; +import static org.openecomp.sdc.be.components.impl.utils.TestDataUtils.aComponentInstance; +import static org.openecomp.sdc.be.components.impl.utils.TestDataUtils.aResourceInstanceId; +import static org.openecomp.sdc.be.components.impl.utils.TestDataUtils.aUniqueId; +import static org.openecomp.sdc.be.components.impl.utils.TestDataUtils.anArtifactDefinition; +import static org.openecomp.sdc.be.components.impl.utils.TestDataUtils.anArtifactId; +import static org.openecomp.sdc.be.components.impl.utils.TestDataUtils.anOperation; +import static org.openecomp.sdc.be.components.impl.utils.TestDataUtils.someArtifacts; +import static org.openecomp.sdc.be.components.impl.utils.TestDataUtils.someComponentInstances; +import static org.openecomp.sdc.be.components.impl.utils.TestDataUtils.someInterfaces; +import static org.openecomp.sdc.be.components.impl.utils.TestDataUtils.someOperations; + +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.InterfaceDefinition; +import org.openecomp.sdc.be.model.Operation; +import org.openecomp.sdc.be.model.Service; + +public class ComponentUtilsTest { + + private static final String ARTIFACT_ID = "SomeArtifactId"; + + @Test + public void checkArtifactInComponentShouldReturnFalseWhenNoConditionsAreMet() { + assertFalse(checkArtifactInComponent(aComponent(), ARTIFACT_ID)); + } + + @Test + public void checkArtifactInComponentShouldReturnTrueWhenOneArtifactHasQueriedId() { + Component component = aComponent(); + component.setArtifacts(someArtifacts(anArtifactDefinition(ARTIFACT_ID))); + assertTrue(checkArtifactInComponent(component, ARTIFACT_ID)); + } + + @Test + public void checkArtifactInComponentShouldReturnTrueWhenOneDeploymentArtifactHasQueriedId() { + Component component = aComponent(); + component.setDeploymentArtifacts(someArtifacts(anArtifactDefinition(ARTIFACT_ID))); + assertTrue(checkArtifactInComponent(component, ARTIFACT_ID)); + } + + @Test + public void checkArtifactInComponentShouldReturnTrueWhenOneToscaArtifactHasQueriedId() { + Component component = aComponent(); + component.setToscaArtifacts(someArtifacts(anArtifactDefinition(ARTIFACT_ID))); + assertTrue(checkArtifactInComponent(component, ARTIFACT_ID)); + } + + @Test + public void checkArtifactInComponentShouldReturnTrueWhenOneOperationHasQueriedId() { + Component component = aComponent(); + component.setInterfaces(someInterfaces(anInterfaceDefinition(ARTIFACT_ID))); + assertTrue(checkArtifactInComponent(component, ARTIFACT_ID)); + } + + private static InterfaceDefinition anInterfaceDefinition(String artifactUniqueId) { + Map<String, Operation> operations = someOperations( + anOperation(aUniqueId(), anArtifactDefinition(artifactUniqueId))); + + InterfaceDefinition id = new InterfaceDefinition(); + id.setOperationsMap(operations); + + return id; + } + + @Test + public void checkArtifactInComponentShouldReturnTrueWhenServiceHasArtifactsWithQueriedId() { + Service service = new Service(); + service.setComponentType(ComponentTypeEnum.SERVICE); + service.setServiceApiArtifacts(someArtifacts(anArtifactDefinition(ARTIFACT_ID))); + + assertTrue(checkArtifactInComponent(service, ARTIFACT_ID)); + } + + @Test + public void checkArtifactInResourceInstanceShouldReturnFalseWhenNoConditionIsMet() { + assertFalse(checkArtifactInResourceInstance(aComponent(), aResourceInstanceId(), anArtifactId())); + } + + @Test + public void checkArtifactInResourceInstanceShouldReturnTrueWhenResourceHasQueriedIdAndDeploymentArtifactId() { + ComponentInstance ci = aComponentInstance(aResourceInstanceId()); + ci.setDeploymentArtifacts(someArtifacts(anArtifactDefinition(ARTIFACT_ID))); + + Component component = aComponent(); + component.setComponentInstances(someComponentInstances(ci)); + + assertTrue(checkArtifactInResourceInstance(component, ci.getUniqueId(), ARTIFACT_ID)); + } + + @Test + public void checkArtifactInResourceInstanceShouldReturnTrueWhenResourceHasQueriedIdAndArtifactId() { + ComponentInstance ci = aComponentInstance(aResourceInstanceId()); + ci.setArtifacts(someArtifacts(anArtifactDefinition(ARTIFACT_ID))); + + Component component = aComponent(); + component.setComponentInstances(someComponentInstances(ci)); + + assertTrue(checkArtifactInResourceInstance(component, ci.getUniqueId(), ARTIFACT_ID)); + } +} diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/TestDataUtils.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/TestDataUtils.java new file mode 100644 index 0000000000..bc7d57a8cc --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/TestDataUtils.java @@ -0,0 +1,185 @@ +/*- + * ============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.utils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.function.Function; +import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; +import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.InterfaceDefinition; +import org.openecomp.sdc.be.model.Operation; +import org.openecomp.sdc.be.model.Product; +import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.Service; + +/** + * This class provides some utility functions to generate close to randomized data for testing purpose. + */ +public class TestDataUtils { + + private TestDataUtils() { + } + + /** + * Generates a random {@code Component} + */ + public static Component aComponent() { + return oneOf(new Resource(), new Service(), new Product()); + } + + /** + * Generates a random map of {@code ArtifactDefinition} + */ + public static Map<String, ArtifactDefinition> someArtifacts(ArtifactDefinition a, ArtifactDefinition... as) { + return some(ArtifactDataDefinition::getUniqueId, a, as); + } + + /** + * Generates a random {@code ArtifactDefinition} provided with a unique id + */ + public static ArtifactDefinition anArtifactDefinition(String uniqueId) { + ArtifactDefinition ad = new ArtifactDefinition(); + ad.setUniqueId(uniqueId); + return ad; + } + + /** + * Converts n {@code ComponentInstance} to {@code List<ComponentInstance>} + */ + public static List<ComponentInstance> someComponentInstances(ComponentInstance ci, ComponentInstance... cis) { + return varargs(ci, cis); + } + + /** + * Generates a random {@code ComponentInstance} provided with a unique id + */ + public static ComponentInstance aComponentInstance(String uniqueId) { + ComponentInstance ci = new ComponentInstance(); + ci.setUniqueId(uniqueId); + return ci; + } + + /** + * Converts n {@code Operation} to {@code List<Operation>} + */ + public static Map<String, Operation> someOperations(Operation o, Operation... os) { + return some(Operation::getUniqueId, o, os); + } + + /** + * Generates a random {@code Operation} provided with a unique id and an implementation + */ + public static Operation anOperation(String uniqueId, ArtifactDefinition implementation) { + Operation o = new Operation(); + o.setUniqueId(uniqueId); + o.setImplementation(implementation); + return o; + } + + /** + * Converts n {@code InterfaceDefinition} to a {@code Map<String, InterfaceDefinition>} + */ + public static Map<String, InterfaceDefinition> someInterfaces(InterfaceDefinition a, InterfaceDefinition... as) { + return some(InterfaceDefinition::getUniqueId, a, as); + } + + /** + * Generates a {@code Map} given a function to generate a key from a value, along with the values to put in the + * resulting {@code Map}. + * + * @param keyMapper A function to generate a key from a value + * @param v0 The first element to put in the {@code Map} + * @param vs The other elements to put on the {@code Map} + * @param <K> The type of a key + * @param <V> The type of a value + */ + @SafeVarargs + public static <K, V> Map<K, V> some(Function<V, K> keyMapper, V v0, V... vs) { + HashMap<K, V> m = new HashMap<>(); + for (V v : varargs(v0, vs)) { + m.put(keyMapper.apply(v), v); + } + return m; + } + + /** + * Alias for {@code aUniqueId} + */ + public static String anArtifactId() { + return alphaNum(10); + } + + /** + * Alias for {@code aUniqueId} + */ + public static String aResourceInstanceId() { + return alphaNum(10); + } + + /** + * Generates a uniqueId + */ + public static String aUniqueId() { + return alphaNum(10); + } + + /** + * Generates an alphanumeric string + * + * @param size The size of the {@code String} returned + */ + public static String alphaNum(int size) { + return alphaNum("", size, new Random()); + } + + private static String alphaNum(String acc, int rem, Random r) { + if (rem == 0) { + return acc; + } else { + String alphaNum = "0123456789abcdefghijklmnopqrstuvwxyz"; + return alphaNum(acc + alphaNum.charAt(r.nextInt(alphaNum.length())), rem - 1, r); + } + } + + /** + * Randomly selects one value among the ones provided + */ + @SafeVarargs + public static <A> A oneOf(A a, A... as) { + List<A> as0 = varargs(a, as); + Random r = new Random(); + return as0.get(r.nextInt(as0.size())); + } + + @SafeVarargs + private static <A> List<A> varargs(A a, A... as) { + ArrayList<A> as0 = new ArrayList<>(Arrays.asList(as)); + as0.add(a); + return as0; + } +} |