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=========================================================
21 package org.openecomp.sdc.be.components.impl;
23 import org.apache.commons.lang3.tuple.ImmutablePair;
24 import org.junit.Test;
25 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
26 import org.openecomp.sdc.be.model.ComponentInstance;
27 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
28 import org.openecomp.sdc.be.model.Resource;
29 import org.openecomp.sdc.be.unittests.utils.FactoryUtils;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
36 import java.util.stream.Collectors;
38 import static org.junit.Assert.assertTrue;
40 public class CompositionBusinessLogicTest {
41 CompositionBusinessLogic compBl = new CompositionBusinessLogic();
44 public void testBuildSpiralPatternPositioningForComponentInstances() {
45 int instancesNum = 10;
46 Resource createVF = FactoryUtils.createVF();
47 for (int i = 0; i < instancesNum; i++) {
48 FactoryUtils.addComponentInstanceToVF(createVF, FactoryUtils.createResourceInstance());
50 Map<ImmutablePair<Double, Double>, ComponentInstance> componentInstances = compBl.buildSpiralPatternPositioningForComponentInstances(createVF);
51 assertTrue(componentInstances.size() == instancesNum);
52 // Verify Spiral Pattern
53 ImmutablePair<Double, Double> key;
54 key = new ImmutablePair<>(0D, 0D);
55 assertTrue(componentInstances.containsKey(key));
56 key = new ImmutablePair<>(-1D, 0D);
57 assertTrue(componentInstances.containsKey(key));
58 key = new ImmutablePair<>(-1D, 1D);
59 assertTrue(componentInstances.containsKey(key));
60 key = new ImmutablePair<>(0D, 1D);
61 assertTrue(componentInstances.containsKey(key));
62 key = new ImmutablePair<>(1D, 1D);
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<>(-2D, -1D);
73 assertTrue(componentInstances.containsKey(key));
77 public void testGetCpsConnectedToVFC() {
78 List<ComponentInstance> allComponentInstances = new ArrayList<>();
79 Resource createVF = FactoryUtils.createVF();
80 ComponentInstance vfc = populateVfWithVfcAndCps(allComponentInstances, createVF);
82 Map<ComponentInstance, List<ComponentInstance>> cpsConnectedToVFC = compBl.getCpsConnectedToVFC(allComponentInstances, createVF);
83 assertTrue(cpsConnectedToVFC.size() == 1);
84 assertTrue(cpsConnectedToVFC.containsKey(vfc));
85 Set<ComponentInstance> cps = cpsConnectedToVFC.get(vfc).stream().collect(Collectors.toSet());
86 assertTrue(cps.size() == 3);
87 cps.stream().forEach(e -> assertTrue(e.getOriginType() == OriginTypeEnum.CP));
92 public void testBuildCirclePatternForCps() {
93 List<ComponentInstance> allComponentInstances = new ArrayList<>();
94 Resource createVF = FactoryUtils.createVF();
95 ComponentInstance vfcInstance = populateVfWithVfcAndCps(allComponentInstances, createVF);
96 Map<ComponentInstance, List<ComponentInstance>> cpsConnectedToVFC = compBl.getCpsConnectedToVFC(allComponentInstances, createVF);
98 Map<ImmutablePair<Double, Double>, ComponentInstance> componentInstLocations = new HashMap<>();
99 componentInstLocations.put(new ImmutablePair<Double, Double>(0D, 0D), vfcInstance);
100 compBl.buildCirclePatternForCps(componentInstLocations, cpsConnectedToVFC);
101 assertTrue(componentInstLocations.size() == 4);
103 Set<ImmutablePair<Double, Double>> cpsLocations = componentInstLocations.entrySet().stream().filter(entry -> entry.getValue().getOriginType() == OriginTypeEnum.CP).map(e -> e.getKey()).collect(Collectors.toSet());
104 // Verify that all cps are located at different positions
105 assertTrue(cpsLocations.size() == 3);
106 Set<Double> distances = cpsLocations.stream().map(cpLocation -> Math.sqrt(Math.pow(cpLocation.left, 2) + Math.pow(cpLocation.right, 2))).collect(Collectors.toSet());
107 // Verify that all cps are at the same distance from center
108 assertTrue(distances.size() == 1);
113 * Adds 4 instances to the vf.<br>
114 * vfc instance and 3 cps instances.<br>
115 * the cp instances are connected to the vfc instance.<br>
117 * @param allComponentInstances
119 * @return vfc instance
121 private ComponentInstance populateVfWithVfcAndCps(List<ComponentInstance> allComponentInstances, Resource createVF) {
122 ComponentInstance vfc = FactoryUtils.createResourceInstance();
123 vfc.setOriginType(OriginTypeEnum.VFC);
124 FactoryUtils.addComponentInstanceToVF(createVF, vfc);
125 allComponentInstances.add(vfc);
127 connectCpToVfc(allComponentInstances, createVF, vfc);
128 connectCpToVfc(allComponentInstances, createVF, vfc);
129 connectCpToVfc(allComponentInstances, createVF, vfc);
133 private void connectCpToVfc(List<ComponentInstance> allComponentInstances, Resource createVF, ComponentInstance vfc) {
134 List<RequirementCapabilityRelDef> allRelations;
135 if (createVF.getComponentInstancesRelations() != null) {
136 allRelations = createVF.getComponentInstancesRelations();
138 allRelations = new ArrayList<>();
139 createVF.setComponentInstancesRelations(allRelations);
141 ComponentInstance cp1 = FactoryUtils.createResourceInstance();
142 cp1.setOriginType(OriginTypeEnum.CP);
143 addVfcCpRelation(vfc, cp1, allRelations);
144 FactoryUtils.addComponentInstanceToVF(createVF, cp1);
145 allComponentInstances.add(cp1);
148 private void addVfcCpRelation(ComponentInstance vfc, ComponentInstance cp, List<RequirementCapabilityRelDef> allRelations) {
149 RequirementCapabilityRelDef rel = new RequirementCapabilityRelDef();
150 rel.setToNode(vfc.getComponentUid());
151 rel.setFromNode(cp.getComponentUid());
152 allRelations.add(rel);