9f32aec8aa9d4dc4a431277f7f08c30b900f0ca2
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation;
18
19 import java.util.Arrays;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import org.junit.Assert;
24 import org.junit.Test;
25
26 import org.onap.sdc.tosca.datatypes.model.RequirementAssignment;
27
28 public class ComputeTemplateConsolidationDataTest {
29
30     private static final String COMPUTE_NODE_TEMPLATE_ID_1 = "computeNodeTemplateId1";
31     private static final String COMPUTE_NODE_TEMPLATE_ID_2 = "computeNodeTemplateId2";
32     private static final String REQUIREMENT_ID_1 = "requirementId1";
33     private static final String REQUIREMENT_ID_2 = "requirementId2";
34
35     private static final String PORT_TYPE_1 = "portType1";
36     private static final String PORT_TYPE_2 = "portType2";
37     private static final String PORT_NODE_TEMPLATE_ID_1 = "portNodeTemplateId1";
38     private static final String PORT_NODE_TEMPLATE_ID_2 = "portNodeTemplateId2";
39
40
41     private final ComputeTemplateConsolidationData consolidationData
42             = new ComputeTemplateConsolidationData();
43
44     @Test
45     public void testAddPort_SamePortType() {
46         Map<String, String[]> expectedPorts = new HashMap<>();
47         expectedPorts.put(PORT_TYPE_1, new String[]{PORT_NODE_TEMPLATE_ID_1});
48         addAndCheckPorts(PORT_TYPE_1, PORT_NODE_TEMPLATE_ID_1, expectedPorts);
49
50         expectedPorts.put(PORT_TYPE_1, new String[]{PORT_NODE_TEMPLATE_ID_1, PORT_NODE_TEMPLATE_ID_2});
51         addAndCheckPorts(PORT_TYPE_1, PORT_NODE_TEMPLATE_ID_2, expectedPorts);
52     }
53
54     @Test
55     public void testAddPort_DiffPortType() {
56         Map<String, String[]> expectedPorts = new HashMap<>();
57         expectedPorts.put(PORT_TYPE_1, new String[]{PORT_NODE_TEMPLATE_ID_1});
58         addAndCheckPorts(PORT_TYPE_1, PORT_NODE_TEMPLATE_ID_1, expectedPorts);
59
60         expectedPorts.put(PORT_TYPE_2, new String[]{PORT_NODE_TEMPLATE_ID_2});
61         addAndCheckPorts(PORT_TYPE_2, PORT_NODE_TEMPLATE_ID_2, expectedPorts);
62     }
63
64     @Test
65     public void testAddVolume_SameComputeNode() {
66         Map<String, String[]> expectedVolumes = new HashMap<>();
67         expectedVolumes.put(COMPUTE_NODE_TEMPLATE_ID_1, new String[]{REQUIREMENT_ID_1});
68         addAndCheckVolume(REQUIREMENT_ID_1, COMPUTE_NODE_TEMPLATE_ID_1, expectedVolumes);
69
70         expectedVolumes.put(COMPUTE_NODE_TEMPLATE_ID_1, new String[]{REQUIREMENT_ID_1, REQUIREMENT_ID_2});
71         addAndCheckVolume(REQUIREMENT_ID_2, COMPUTE_NODE_TEMPLATE_ID_1, expectedVolumes);
72     }
73
74     @Test
75     public void testAddVolume_DiffComputeNode() {
76         Map<String, String[]> expectedVolumes = new HashMap<>();
77         expectedVolumes.put(COMPUTE_NODE_TEMPLATE_ID_1, new String[]{REQUIREMENT_ID_1});
78         addAndCheckVolume(REQUIREMENT_ID_1, COMPUTE_NODE_TEMPLATE_ID_1, expectedVolumes);
79
80         expectedVolumes.put(COMPUTE_NODE_TEMPLATE_ID_2, new String[]{REQUIREMENT_ID_2});
81         addAndCheckVolume(REQUIREMENT_ID_2, COMPUTE_NODE_TEMPLATE_ID_2, expectedVolumes);
82     }
83
84     private void addAndCheckPorts(String portType, String portNodeTemplateId,
85             Map<String, String[]> expectedPorts) {
86         consolidationData.addPort(portType, portNodeTemplateId);
87         checkPorts(consolidationData.getPorts(), expectedPorts);
88     }
89
90     private void checkPorts(Map<String, List<String>> actualAllPorts,
91             Map<String, String[]> expectedAllPorts) {
92         Assert.assertNotNull(actualAllPorts);
93         expectedAllPorts.keySet().forEach(expectedPortType -> {
94             Assert.assertTrue(actualAllPorts.containsKey(expectedPortType));
95             Assert.assertEquals(expectedAllPorts.size(), actualAllPorts.size());
96             checkPortsPerType(actualAllPorts, expectedAllPorts, expectedPortType);
97         });
98     }
99
100     private void checkPortsPerType(Map<String, List<String>>  actualAllPorts,  Map<String, String[]> expectedAllPorts,
101                                    String expectedPortType) {
102         List<String> actualPorts = actualAllPorts.get(expectedPortType);
103         List<String> expectedPortList = Arrays.asList(expectedAllPorts.get(expectedPortType));
104         Assert.assertEquals(expectedPortList.size(), actualPorts.size());
105         actualPorts.forEach(actualPort ->
106                 Assert.assertTrue(expectedPortList.contains(actualPort)));
107     }
108
109     private void addAndCheckVolume(String requirementId, String computeNodeTemplateId,
110                                           Map<String, String[]> expectedVolumes) {
111         RequirementAssignment requirementAssignment1 = createRequirement(computeNodeTemplateId);
112         consolidationData.addVolume(requirementId, requirementAssignment1);
113         checkVolumes(consolidationData.getVolumes(), expectedVolumes);
114     }
115
116     private void checkVolumes(Map<String, List<RequirementAssignmentData>> actualVolumes,
117             Map<String, String[]> expectedVolumes) {
118         Assert.assertNotNull(actualVolumes);
119         expectedVolumes.keySet().forEach(nodeTemplateId -> {
120             Assert.assertTrue(actualVolumes.containsKey(nodeTemplateId));
121             Assert.assertEquals(expectedVolumes.size(), actualVolumes.size());
122             checkVolumesPerType(actualVolumes, expectedVolumes, nodeTemplateId);
123         });
124     }
125
126     private void checkVolumesPerType(Map<String, List<RequirementAssignmentData>>
127             actualVolumes,  Map<String, String[]> expectedVolumes, String nodeTemplateId) {
128         List<RequirementAssignmentData> actualRequirementAssignmentData = actualVolumes.get(nodeTemplateId);
129         List<String> requirementIds = Arrays.asList(expectedVolumes.get(nodeTemplateId));
130         Assert.assertEquals(requirementIds.size(), actualRequirementAssignmentData.size());
131         actualRequirementAssignmentData.forEach(actualRequirementAssignment ->
132                 Assert.assertTrue(requirementIds.contains(actualRequirementAssignment.getRequirementId())));
133     }
134
135     private RequirementAssignment createRequirement(String nodeTemplateId) {
136         RequirementAssignment requirementAssignment = new RequirementAssignment();
137         requirementAssignment.setNode(nodeTemplateId);
138         return requirementAssignment;
139     }
140 }