2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 * Modifications copyright (c) 2019 Nokia
20 * ================================================================================
23 package org.openecomp.sdc.be.components.impl;
25 import org.apache.commons.lang3.tuple.ImmutablePair;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
29 import org.openecomp.sdc.be.model.ComponentInstance;
30 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
31 import org.openecomp.sdc.be.model.Resource;
32 import org.openecomp.sdc.be.unittests.utils.FactoryUtils;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.List;
39 import java.util.stream.Collectors;
41 import static org.junit.Assert.assertEquals;
42 import static org.junit.Assert.assertSame;
43 import static org.junit.Assert.assertTrue;
45 public class CompositionBusinessLogicTest {
47 private ComponentInstanceBusinessLogic componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class);
49 CompositionBusinessLogic compBl = new CompositionBusinessLogic(componentInstanceBusinessLogic);
52 public void testBuildSpiralPatternPositioningForComponentInstances() {
53 int instancesNum = 10;
54 Resource createVF = FactoryUtils.createVF();
55 for (int i = 0; i < instancesNum; i++) {
56 FactoryUtils.addComponentInstanceToVF(createVF, FactoryUtils.createResourceInstance());
58 Map<ImmutablePair<Double, Double>, ComponentInstance> componentInstances = compBl.buildSpiralPatternPositioningForComponentInstances(createVF);
59 assertEquals(componentInstances.size(), instancesNum);
60 // Verify Spiral Pattern
61 ImmutablePair<Double, Double> key;
62 key = new ImmutablePair<>(0D, 0D);
63 assertTrue(componentInstances.containsKey(key));
64 key = new ImmutablePair<>(-1D, 0D);
65 assertTrue(componentInstances.containsKey(key));
66 key = new ImmutablePair<>(-1D, 1D);
67 assertTrue(componentInstances.containsKey(key));
68 key = new ImmutablePair<>(0D, 1D);
69 assertTrue(componentInstances.containsKey(key));
70 key = new ImmutablePair<>(1D, 1D);
71 assertTrue(componentInstances.containsKey(key));
72 key = new ImmutablePair<>(1D, 0D);
73 assertTrue(componentInstances.containsKey(key));
74 key = new ImmutablePair<>(1D, -1D);
75 assertTrue(componentInstances.containsKey(key));
76 key = new ImmutablePair<>(0D, -1D);
77 assertTrue(componentInstances.containsKey(key));
78 key = new ImmutablePair<>(-1D, -1D);
79 assertTrue(componentInstances.containsKey(key));
80 key = new ImmutablePair<>(-2D, -1D);
81 assertTrue(componentInstances.containsKey(key));
85 public void testGetCpsConnectedToVFC() {
86 List<ComponentInstance> allComponentInstances = new ArrayList<>();
87 Resource createVF = FactoryUtils.createVF();
88 ComponentInstance vfc = populateVfWithVfcAndCps(allComponentInstances, createVF);
90 Map<ComponentInstance, List<ComponentInstance>> cpsConnectedToVFC = compBl.getCpsConnectedToVFC(allComponentInstances, createVF);
91 assertEquals(1, cpsConnectedToVFC.size());
92 assertTrue(cpsConnectedToVFC.containsKey(vfc));
93 Set<ComponentInstance> cps = cpsConnectedToVFC.get(vfc).stream().collect(Collectors.toSet());
94 assertEquals(3, cps.size());
95 cps.stream().forEach(e -> assertSame(e.getOriginType(), OriginTypeEnum.CP));
100 public void testBuildCirclePatternForCps() {
101 List<ComponentInstance> allComponentInstances = new ArrayList<>();
102 Resource createVF = FactoryUtils.createVF();
103 ComponentInstance vfcInstance = populateVfWithVfcAndCps(allComponentInstances, createVF);
104 Map<ComponentInstance, List<ComponentInstance>> cpsConnectedToVFC = compBl.getCpsConnectedToVFC(allComponentInstances, createVF);
106 Map<ImmutablePair<Double, Double>, ComponentInstance> componentInstLocations = new HashMap<>();
107 componentInstLocations.put(new ImmutablePair<>(0D, 0D), vfcInstance);
108 compBl.buildCirclePatternForCps(componentInstLocations, cpsConnectedToVFC);
109 assertEquals(4, componentInstLocations.size());
111 Set<ImmutablePair<Double, Double>> cpsLocations = componentInstLocations.entrySet().stream().filter(entry -> entry.getValue().getOriginType() == OriginTypeEnum.CP).map(Map.Entry::getKey).collect(Collectors.toSet());
112 // Verify that all cps are located at different positions
113 assertEquals(3, cpsLocations.size());
114 Set<Double> distances = cpsLocations.stream().map(cpLocation -> Math.sqrt(Math.pow(cpLocation.left, 2) + Math.pow(cpLocation.right, 2))).collect(Collectors.toSet());
115 // Verify that all cps are at the same distance from center
116 assertEquals(1, distances.size());
121 * Adds 4 instances to the vf.<br>
122 * vfc instance and 3 cps instances.<br>
123 * the cp instances are connected to the vfc instance.<br>
125 * @param allComponentInstances
127 * @return vfc instance
129 private ComponentInstance populateVfWithVfcAndCps(List<ComponentInstance> allComponentInstances, Resource createVF) {
130 ComponentInstance vfc = FactoryUtils.createResourceInstance();
131 vfc.setOriginType(OriginTypeEnum.VFC);
132 FactoryUtils.addComponentInstanceToVF(createVF, vfc);
133 allComponentInstances.add(vfc);
135 connectCpToVfc(allComponentInstances, createVF, vfc);
136 connectCpToVfc(allComponentInstances, createVF, vfc);
137 connectCpToVfc(allComponentInstances, createVF, vfc);
141 private void connectCpToVfc(List<ComponentInstance> allComponentInstances, Resource createVF, ComponentInstance vfc) {
142 List<RequirementCapabilityRelDef> allRelations;
143 if (createVF.getComponentInstancesRelations() != null) {
144 allRelations = createVF.getComponentInstancesRelations();
146 allRelations = new ArrayList<>();
147 createVF.setComponentInstancesRelations(allRelations);
149 ComponentInstance cp1 = FactoryUtils.createResourceInstance();
150 cp1.setOriginType(OriginTypeEnum.CP);
151 addVfcCpRelation(vfc, cp1, allRelations);
152 FactoryUtils.addComponentInstanceToVF(createVF, cp1);
153 allComponentInstances.add(cp1);
156 private void addVfcCpRelation(ComponentInstance vfc, ComponentInstance cp, List<RequirementCapabilityRelDef> allRelations) {
157 RequirementCapabilityRelDef rel = new RequirementCapabilityRelDef();
158 rel.setToNode(vfc.getComponentUid());
159 rel.setFromNode(cp.getComponentUid());
160 allRelations.add(rel);