re base code
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / utils / YamlTemplateParsingHandlerTest.java
1 package org.openecomp.sdc.be.components.impl.utils;
2
3 import org.assertj.core.util.Lists;
4 import org.junit.Before;
5 import org.junit.Test;
6 import org.junit.runner.RunWith;
7 import org.mockito.Mock;
8 import org.mockito.junit.MockitoJUnitRunner;
9 import org.openecomp.sdc.be.components.csar.YamlTemplateParsingHandler;
10 import org.openecomp.sdc.be.components.impl.AnnotationBusinessLogic;
11 import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
12 import org.openecomp.sdc.be.components.validation.AnnotationValidator;
13 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
14 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
15 import org.openecomp.sdc.be.impl.ComponentsUtils;
16 import org.openecomp.sdc.be.model.*;
17 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
18 import org.openecomp.sdc.common.util.ZipUtil;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.StringReader;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.util.*;
27 import java.util.stream.Collectors;
28
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.mockito.ArgumentMatchers.eq;
31 import static org.mockito.Mockito.when;
32 @RunWith(MockitoJUnitRunner.class)
33 public class YamlTemplateParsingHandlerTest {
34
35     private final static String VFC_GROUP_TYPE = "org.openecomp.groups.VfcInstanceGroup";
36     private final static String HEAT_GROUP_TYPE = "org.openecomp.groups.heat.HeatStack";
37     private final static String ROOT_GROUP_TYPE = "tosca.groups.Root";
38     private final static GroupTypeDefinition VfcInstanceGroupType = buildVfcInstanceGroupType();
39     private final static GroupTypeDefinition heatGroupType = buildHeatStackGroupType();
40     private final static GroupTypeDefinition rootGroupType = buildRootGroupType();
41     private final static String CAPABILITY_TYPE = "org.openecomp.capabilities.VLANAssignment";
42     private final static String CAPABILITY_NAME = "vlan_assignment";
43     public static final String csarsFilePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator + "csars" ;
44
45     private YamlTemplateParsingHandler handler;
46     private AnnotationBusinessLogic annotationBusinessLogic;
47     @Mock
48     private ComponentsUtils componentsUtils;
49     @Mock
50     private GroupTypeBusinessLogic groupTypeBusinessLogic;
51     @Mock
52     private AnnotationTypeOperations annotationTypeOperations;
53     @Mock
54     private AnnotationValidator annotationValidator;
55     @Mock
56     private TitanDao titanDao;
57
58     @Before
59     public void init(){
60         annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations, annotationValidator);
61         handler = new YamlTemplateParsingHandler(titanDao, groupTypeBusinessLogic, annotationBusinessLogic);
62     }
63
64     @Test
65     public void parseResourceInfoFromYAMLTest(){
66         Path path = Paths.get(csarsFilePath + File.separator + "with_groups.csar");
67         try {
68             Map<String, byte[]> csar = ZipUtil.readZip(Files.readAllBytes(path));
69             String fileName = "MainServiceTemplate.yaml";
70             Optional<String> keyOp = csar.keySet().stream().filter(k -> k.endsWith(fileName)).findAny();
71             byte[] mainTemplateService = csar.get(keyOp.get());
72             Properties props = new Properties();
73             String resourceYml = new String(mainTemplateService);
74             props.load(new StringReader(resourceYml.replace("\\","\\\\")));
75             Resource resource = new Resource();
76             
77             stubGetGroupType();
78             
79             ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(fileName, resourceYml, new HashMap<>(), new HashMap<>(), "");
80             
81             validateParsedYaml(parsedYaml);
82             
83         } catch (IOException e) {
84             e.printStackTrace();
85         }
86     }
87
88
89
90         private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml) {
91                 assertThat(parsedYaml).isNotNull();
92                 assertThat(parsedYaml.getGroups()).isNotNull().containsKey("x_group");
93                 assertThat(parsedYaml.getGroups().get("x_group")).isNotNull();
94                 assertThat(parsedYaml.getGroups().get("x_group").getProperties()).isNotNull();
95                 assertThat(parsedYaml.getGroups().get("x_group").getProperties()
96                                 .stream()
97                                 .map(PropertyDataDefinition::getName)
98                                 .collect(Collectors.toList()))
99                 .containsAll(Lists.newArrayList("vfc_parent_port_role", "network_collection_function", "vfc_instance_group_function", "subinterface_role"));
100                 assertThat(parsedYaml.getGroups().get("x_group").getCapabilities()
101                                 .get(CAPABILITY_TYPE)
102                                 .get(0).getProperties().get(0).getValue()).isEqualTo("success");
103                 assertThat(parsedYaml.getGroups().get("x_group").getProperties()
104                                 .stream()
105                                 .map(PropertyDataDefinition::getName)
106                                 .collect(Collectors.toList()))
107                 .containsAll(Lists.newArrayList("vfc_parent_port_role", "network_collection_function", "vfc_instance_group_function", "subinterface_role"));
108                 assertThat(parsedYaml.getGroups().get("x_group").getCapabilities()).isNotNull();
109                 assertThat(parsedYaml.getGroups().get("x_group").getMembers()).isNotNull();
110         }
111
112         private void stubGetGroupType() {
113                 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(VFC_GROUP_TYPE))).thenReturn(VfcInstanceGroupType);
114                 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE))).thenReturn(heatGroupType);
115         when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(ROOT_GROUP_TYPE))).thenReturn(rootGroupType);
116 //        when(annotationBusinessLogic.validateAndMergeAnnotationsAndAssignToInput(any(Map.class))).thenReturn(null);
117     }
118
119     private static GroupTypeDefinition buildRootGroupType() {
120         GroupTypeDefinition groupType = new GroupTypeDefinition();
121         groupType.setType(ROOT_GROUP_TYPE);
122         groupType.setDescription("The TOSCA Group Type all other TOSCA Group Types derive from");
123         return groupType;
124         }
125
126         private static GroupTypeDefinition buildHeatStackGroupType() {
127         GroupTypeDefinition groupType = new GroupTypeDefinition();
128         groupType.setType(HEAT_GROUP_TYPE);
129         groupType.setDerivedFrom("tosca.groups.Root");
130         groupType.setDescription("Grouped all heat resources which are in the same heat stack");
131                       
132         GroupProperty property1 = new GroupProperty();
133         property1.setName("heat_file");
134         property1.setType("string");
135         property1.setRequired(true);
136         property1.setDescription("Heat file which associate to this group/heat stack");
137         property1.setStatus("SUPPORTED");
138
139         GroupProperty property2 = new GroupProperty();
140         property2.setName("description");
141         property2.setType("string");
142         property2.setRequired(true);
143         property2.setDescription("group description");
144         property2.setStatus("SUPPORTED");
145         groupType.setProperties(Lists.newArrayList(property1, property2));
146         return groupType;
147         }
148
149         private static GroupTypeDefinition buildVfcInstanceGroupType() {
150         GroupTypeDefinition groupType = new GroupTypeDefinition();
151         groupType.setType(VFC_GROUP_TYPE);
152         groupType.setDerivedFrom("tosca.groups.Root");
153         groupType.setDescription("groups VFCs with same parent port role");
154         GroupProperty property1 = new GroupProperty();
155         property1.setName("vfc_instance_group_function");
156         property1.setType("string");
157         property1.setRequired(true);
158         property1.setDescription("function of this VFC group");
159
160         GroupProperty property2 = new GroupProperty();
161         property2.setName("vfc_parent_port_role");
162         property2.setType("string");
163         property2.setRequired(true);
164         property2.setDescription("common role of parent ports of VFCs in this group");
165
166         GroupProperty property3 = new GroupProperty();
167         property3.setName("network_collection_function");
168         property3.setType("string");
169         property3.setRequired(true);
170         property3.setDescription("network collection function assigned to this group");
171
172         GroupProperty property4 = new GroupProperty();
173         property4.setName("subinterface_role");
174         property4.setType("string");
175         property4.setRequired(true);
176         property4.setDescription("common role of subinterfaces of VFCs in this group, criteria the group is created");
177
178         groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));
179
180         CapabilityDefinition capability = new CapabilityDefinition();
181         capability.setType(CAPABILITY_TYPE);
182         capability.setName(CAPABILITY_NAME);
183         ComponentInstanceProperty capabilityProperty = new ComponentInstanceProperty();
184         capabilityProperty.setName("vfc_instance_group_reference");
185         capabilityProperty.setType("string");
186         capability.setProperties(Arrays.asList(capabilityProperty));
187
188         Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
189         capabilityMap.put(CAPABILITY_NAME, capability);
190         groupType.setCapabilities(capabilityMap);
191         return groupType;
192     }
193
194 }