re base code
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / CompositionBusinessLogicTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  */
20
21 package org.openecomp.sdc.be.components.impl;
22
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;
30
31 import java.util.*;
32 import java.util.stream.Collectors;
33
34 import static org.junit.Assert.assertEquals;
35 import static org.junit.Assert.assertSame;
36 import static org.junit.Assert.assertTrue;
37
38 public class CompositionBusinessLogicTest {
39     CompositionBusinessLogic compBl = new CompositionBusinessLogic();
40
41     @Test
42     public void testBuildSpiralPatternPositioningForComponentInstances() {
43         int instancesNum = 10;
44         Resource createVF = FactoryUtils.createVF();
45         for (int i = 0; i < instancesNum; i++) {
46             FactoryUtils.addComponentInstanceToVF(createVF, FactoryUtils.createResourceInstance());
47         }
48         Map<ImmutablePair<Double, Double>, ComponentInstance> componentInstances = compBl.buildSpiralPatternPositioningForComponentInstances(createVF);
49         assertEquals(componentInstances.size(), instancesNum);
50         // Verify Spiral Pattern
51         ImmutablePair<Double, Double> key;
52         key = new ImmutablePair<>(0D, 0D);
53         assertTrue(componentInstances.containsKey(key));
54         key = new ImmutablePair<>(-1D, 0D);
55         assertTrue(componentInstances.containsKey(key));
56         key = new ImmutablePair<>(-1D, 1D);
57         assertTrue(componentInstances.containsKey(key));
58         key = new ImmutablePair<>(0D, 1D);
59         assertTrue(componentInstances.containsKey(key));
60         key = new ImmutablePair<>(1D, 1D);
61         assertTrue(componentInstances.containsKey(key));
62         key = new ImmutablePair<>(1D, 0D);
63         assertTrue(componentInstances.containsKey(key));
64         key = new ImmutablePair<>(1D, -1D);
65         assertTrue(componentInstances.containsKey(key));
66         key = new ImmutablePair<>(0D, -1D);
67         assertTrue(componentInstances.containsKey(key));
68         key = new ImmutablePair<>(-1D, -1D);
69         assertTrue(componentInstances.containsKey(key));
70         key = new ImmutablePair<>(-2D, -1D);
71         assertTrue(componentInstances.containsKey(key));
72     }
73
74     @Test
75     public void testGetCpsConnectedToVFC() {
76         List<ComponentInstance> allComponentInstances = new ArrayList<>();
77         Resource createVF = FactoryUtils.createVF();
78         ComponentInstance vfc = populateVfWithVfcAndCps(allComponentInstances, createVF);
79
80         Map<ComponentInstance, List<ComponentInstance>> cpsConnectedToVFC = compBl.getCpsConnectedToVFC(allComponentInstances, createVF);
81         assertEquals(1, cpsConnectedToVFC.size());
82         assertTrue(cpsConnectedToVFC.containsKey(vfc));
83         Set<ComponentInstance> cps = cpsConnectedToVFC.get(vfc).stream().collect(Collectors.toSet());
84         assertEquals(3, cps.size());
85         cps.stream().forEach(e -> assertSame(e.getOriginType(), OriginTypeEnum.CP));
86
87     }
88
89     @Test
90     public void testBuildCirclePatternForCps() {
91         List<ComponentInstance> allComponentInstances = new ArrayList<>();
92         Resource createVF = FactoryUtils.createVF();
93         ComponentInstance vfcInstance = populateVfWithVfcAndCps(allComponentInstances, createVF);
94         Map<ComponentInstance, List<ComponentInstance>> cpsConnectedToVFC = compBl.getCpsConnectedToVFC(allComponentInstances, createVF);
95
96         Map<ImmutablePair<Double, Double>, ComponentInstance> componentInstLocations = new HashMap<>();
97         componentInstLocations.put(new ImmutablePair<>(0D, 0D), vfcInstance);
98         compBl.buildCirclePatternForCps(componentInstLocations, cpsConnectedToVFC);
99         assertEquals(4, componentInstLocations.size());
100
101         Set<ImmutablePair<Double, Double>> cpsLocations = componentInstLocations.entrySet().stream().filter(entry -> entry.getValue().getOriginType() == OriginTypeEnum.CP).map(Map.Entry::getKey).collect(Collectors.toSet());
102         // Verify that all cps are located at different positions
103         assertEquals(3, cpsLocations.size());
104         Set<Double> distances = cpsLocations.stream().map(cpLocation -> Math.sqrt(Math.pow(cpLocation.left, 2) + Math.pow(cpLocation.right, 2))).collect(Collectors.toSet());
105         // Verify that all cps are at the same distance from center
106         assertEquals(1, distances.size());
107
108     }
109
110     /**
111      * Adds 4 instances to the vf.<br>
112      * vfc instance and 3 cps instances.<br>
113      * the cp instances are connected to the vfc instance.<br>
114      *
115      * @param allComponentInstances
116      * @param createVF
117      * @return vfc instance
118      */
119     private ComponentInstance populateVfWithVfcAndCps(List<ComponentInstance> allComponentInstances, Resource createVF) {
120         ComponentInstance vfc = FactoryUtils.createResourceInstance();
121         vfc.setOriginType(OriginTypeEnum.VFC);
122         FactoryUtils.addComponentInstanceToVF(createVF, vfc);
123         allComponentInstances.add(vfc);
124
125         connectCpToVfc(allComponentInstances, createVF, vfc);
126         connectCpToVfc(allComponentInstances, createVF, vfc);
127         connectCpToVfc(allComponentInstances, createVF, vfc);
128         return vfc;
129     }
130
131     private void connectCpToVfc(List<ComponentInstance> allComponentInstances, Resource createVF, ComponentInstance vfc) {
132         List<RequirementCapabilityRelDef> allRelations;
133         if (createVF.getComponentInstancesRelations() != null) {
134             allRelations = createVF.getComponentInstancesRelations();
135         } else {
136             allRelations = new ArrayList<>();
137             createVF.setComponentInstancesRelations(allRelations);
138         }
139         ComponentInstance cp1 = FactoryUtils.createResourceInstance();
140         cp1.setOriginType(OriginTypeEnum.CP);
141         addVfcCpRelation(vfc, cp1, allRelations);
142         FactoryUtils.addComponentInstanceToVF(createVF, cp1);
143         allComponentInstances.add(cp1);
144     }
145
146     private void addVfcCpRelation(ComponentInstance vfc, ComponentInstance cp, List<RequirementCapabilityRelDef> allRelations) {
147         RequirementCapabilityRelDef rel = new RequirementCapabilityRelDef();
148         rel.setToNode(vfc.getComponentUid());
149         rel.setFromNode(cp.getComponentUid());
150         allRelations.add(rel);
151     }
152 }