Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / utils / YamlTemplateParsingHandlerTest.java
1 /*-
2  * ============LICENSE_START===============================================
3  * ONAP SDC
4  * ========================================================================
5  * Modifications Copyright (c) 2019 Samsung
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.utils;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.when;
27
28 import java.io.IOException;
29 import java.net.URISyntaxException;
30 import java.util.*;
31 import java.util.stream.Collectors;
32
33 import org.assertj.core.util.Lists;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.junit.MockitoJUnitRunner;
40
41 import org.openecomp.sdc.ZipUtil;
42 import org.openecomp.sdc.be.components.csar.CsarInfo;
43 import org.openecomp.sdc.be.components.csar.YamlTemplateParsingHandler;
44 import org.openecomp.sdc.be.components.impl.AnnotationBusinessLogic;
45 import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
46 import org.openecomp.sdc.be.components.validation.AnnotationValidator;
47 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
48 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
49 import org.openecomp.sdc.be.model.*;
50 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
51
52 @RunWith(MockitoJUnitRunner.class)
53 public class YamlTemplateParsingHandlerTest {
54
55     @Mock
56     private GroupTypeBusinessLogic groupTypeBusinessLogic;
57     @Mock
58     private AnnotationTypeOperations annotationTypeOperations;
59     @Mock
60     private AnnotationValidator annotationValidator;
61     @Mock
62     private JanusGraphDao janusGraphDao;
63     @Mock
64     private User user;
65
66     private YamlTemplateParsingHandler handler;
67
68     private static Map<String, byte[]> csar;
69     private static String resourceYml;
70
71     private final static String VFC_GROUP_TYPE = "org.openecomp.groups.VfcInstanceGroup";
72     private final static String HEAT_GROUP_TYPE = "org.openecomp.groups.heat.HeatStack";
73     private final static String ROOT_GROUP_TYPE = "tosca.groups.Root";
74     private final static GroupTypeDefinition VfcInstanceGroupType = buildVfcInstanceGroupType();
75     private final static GroupTypeDefinition heatGroupType = buildHeatStackGroupType();
76     private final static GroupTypeDefinition rootGroupType = buildRootGroupType();
77     private final static String CAPABILITY_TYPE = "org.openecomp.capabilities.VLANAssignment";
78     private final static String CAPABILITY_NAME = "vlan_assignment";
79     private static final String CSAR_FILE_PATH = "/csars/with_groups.csar";
80
81     private static final String FILE_NAME = "MainServiceTemplate.yaml";
82
83     private static final String CSAR_UUID = "csarUUID";
84     private static final String RESOURCE_NAME = "resourceName";
85     private static final String MAIN_TEMPLATE_NAME = "Definitions/MainServiceTemplate.yaml";
86     private static final String NODE_NAME = "org.openecomp.resource.abstract.nodes.heat.mg";
87     private static final String MAIN_GROUP_NAME = "x_group";
88     private static final String NESTED_GROUP_NAME = "nested_mg_vepdg_group";
89
90     @BeforeClass()
91     public static void prepareData() throws IOException, URISyntaxException {
92         csar = ZipUtil.readData(CSAR_FILE_PATH);
93
94         Optional<String> keyOp = csar.keySet().stream().filter(k -> k.endsWith(FILE_NAME)).findAny();
95         byte[] mainTemplateService = keyOp.map(csar::get).orElse(null);
96         assertNotNull(mainTemplateService);
97
98         resourceYml = new String(mainTemplateService);
99     }
100
101     @Before
102     public void setup() {
103
104         AnnotationBusinessLogic annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations,
105                 annotationValidator);
106         handler = new YamlTemplateParsingHandler(janusGraphDao, groupTypeBusinessLogic, annotationBusinessLogic);
107         stubGetGroupType();
108     }
109
110     @Test
111     public void parseResourceInfoFromOneNodeTest() {
112
113         String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
114         CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
115                 MAIN_TEMPLATE_NAME, main_template_content, true);
116
117         ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
118                 csarInfo.extractNodeTypesInfo(), NODE_NAME);
119
120         validateParsedYaml(parsedYaml, NESTED_GROUP_NAME,
121                 Lists.newArrayList("heat_file", "description"));
122     }
123
124     @Test
125     public void parseResourceInfoFromYAMLTest() {
126
127         ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
128                 new HashMap<>(), "");
129         validateParsedYamlWithCapability(parsedYaml);
130     }
131
132     private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml, String group, List<String> expectedProp) {
133         assertThat(parsedYaml).isNotNull();
134         assertThat(parsedYaml.getGroups()).isNotNull().containsKey(group);
135         assertThat(parsedYaml.getGroups().get(group)).isNotNull();
136
137         assertThat(parsedYaml.getGroups().get(group).getProperties()).isNotNull();
138         assertThat(parsedYaml.getGroups().get(group).getProperties()
139                 .stream()
140                 .map(PropertyDataDefinition::getName)
141                 .collect(Collectors.toList()))
142                 .containsAll(expectedProp);
143
144         assertThat(parsedYaml.getGroups().get(group).getMembers()).isNotNull();
145     }
146
147         private void validateParsedYamlWithCapability(ParsedToscaYamlInfo parsedYaml) {
148
149         final List<String> expectedProp = Lists.newArrayList("vfc_parent_port_role",
150                 "network_collection_function", "vfc_instance_group_function", "subinterface_role");
151
152         validateParsedYaml(parsedYaml, MAIN_GROUP_NAME, expectedProp);
153
154         assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()
155                 .get(CAPABILITY_TYPE)
156                 .get(0).getProperties().get(0).getValue()).isEqualTo("success");
157         assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()).isNotNull();
158         }
159
160         private void stubGetGroupType() {
161                 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(VFC_GROUP_TYPE))).thenReturn(VfcInstanceGroupType);
162                 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE))).thenReturn(heatGroupType);
163         when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(ROOT_GROUP_TYPE))).thenReturn(rootGroupType);
164     }
165
166     private static GroupTypeDefinition buildRootGroupType() {
167         return createGroupTypeDefinition(ROOT_GROUP_TYPE, null,
168                 "The TOSCA Group Type all other TOSCA Group Types derive from");
169         }
170
171         private static GroupTypeDefinition buildHeatStackGroupType() {
172         GroupTypeDefinition groupType = createGroupTypeDefinition(HEAT_GROUP_TYPE, "tosca.groups.Root",
173                 "Grouped all heat resources which are in the same heat stack");
174
175         GroupProperty property1 = createGroupProperty("heat_file",
176                 "Heat file which associate to this group/heat stack", "SUPPORTED");
177
178         GroupProperty property2 = createGroupProperty("description",
179                 "Group description", "SUPPORTED");
180
181         groupType.setProperties(Lists.newArrayList(property1, property2));
182         return groupType;
183         }
184
185         private static GroupTypeDefinition buildVfcInstanceGroupType() {
186         GroupTypeDefinition groupType = createGroupTypeDefinition(VFC_GROUP_TYPE, "tosca.groups.Root",
187                 "Groups of VFCs with same parent port role");
188
189         GroupProperty property1 = createGroupProperty("vfc_instance_group_function",
190                 "Function of this VFC group", null);
191
192         GroupProperty property2 = createGroupProperty("vfc_parent_port_role",
193                 "Common role of parent ports of VFCs in this group", null);
194
195         GroupProperty property3 = createGroupProperty("network_collection_function",
196                 "Network collection function assigned to this group", null);
197
198         GroupProperty property4 = createGroupProperty("subinterface_role",
199                 "Common role of subinterfaces of VFCs in this group, criteria the group is created", null);
200
201         groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));
202
203         CapabilityDefinition capability = new CapabilityDefinition();
204         capability.setType(CAPABILITY_TYPE);
205         capability.setName(CAPABILITY_NAME);
206         ComponentInstanceProperty capabilityProperty = new ComponentInstanceProperty();
207         capabilityProperty.setName("vfc_instance_group_reference");
208         capabilityProperty.setType("string");
209         capability.setProperties(Collections.singletonList(capabilityProperty));
210
211         Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
212         capabilityMap.put(CAPABILITY_NAME, capability);
213         groupType.setCapabilities(capabilityMap);
214         return groupType;
215     }
216
217     private static GroupTypeDefinition createGroupTypeDefinition(String type, String derivedFrom, String description){
218         GroupTypeDefinition property = new GroupTypeDefinition();
219
220         if (type != null)
221             property.setType(type);
222
223         if (derivedFrom != null) {
224             property.setDerivedFrom(derivedFrom);
225         }
226
227         if (description != null) {
228             property.setDescription(description);
229         }
230
231         return property;
232     }
233     private static GroupProperty createGroupProperty(String name, String description,
234                                                      String status){
235         GroupProperty property = new GroupProperty();
236         if (name != null)
237             property.setName(name);
238
239         if (description != null) {
240             property.setDescription(description);
241         }
242
243         if (status != null) {
244             property.setStatus(status);
245         }
246
247         property.setType("string");
248         property.setRequired(true);
249
250         return property;
251     }
252 }