2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation;
19 import java.util.Arrays;
20 import java.util.HashMap;
21 import java.util.List;
23 import org.junit.Assert;
24 import org.junit.Test;
26 import org.onap.sdc.tosca.datatypes.model.RequirementAssignment;
28 public class ComputeTemplateConsolidationDataTest {
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";
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";
41 private final ComputeTemplateConsolidationData consolidationData
42 = new ComputeTemplateConsolidationData();
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);
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);
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);
60 expectedPorts.put(PORT_TYPE_2, new String[]{PORT_NODE_TEMPLATE_ID_2});
61 addAndCheckPorts(PORT_TYPE_2, PORT_NODE_TEMPLATE_ID_2, expectedPorts);
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);
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);
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);
80 expectedVolumes.put(COMPUTE_NODE_TEMPLATE_ID_2, new String[]{REQUIREMENT_ID_2});
81 addAndCheckVolume(REQUIREMENT_ID_2, COMPUTE_NODE_TEMPLATE_ID_2, expectedVolumes);
84 private void addAndCheckPorts(String portType, String portNodeTemplateId,
85 Map<String, String[]> expectedPorts) {
86 consolidationData.addPort(portType, portNodeTemplateId);
87 checkPorts(consolidationData.getPorts(), expectedPorts);
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);
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)));
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);
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);
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())));
135 private RequirementAssignment createRequirement(String nodeTemplateId) {
136 RequirementAssignment requirementAssignment = new RequirementAssignment();
137 requirementAssignment.setNode(nodeTemplateId);
138 return requirementAssignment;