[SDC-29] rebase continue work to align source
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / verificator / VfModuleVerificator.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.ci.tests.verificator;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import java.io.File;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.stream.Collectors;
31
32 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
33 import org.openecomp.sdc.be.model.ComponentInstance;
34 import org.openecomp.sdc.be.model.GroupInstance;
35 import org.openecomp.sdc.be.model.GroupProperty;
36 import org.openecomp.sdc.be.model.Resource;
37 import org.openecomp.sdc.be.model.Service;
38 import org.openecomp.sdc.ci.tests.datatypes.TypeHeatMetaDefinition;
39 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
40 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaDefinition;
41 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaGroupsMetadataDefinition;
42 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaGroupsTopologyTemplateDefinition;
43 import org.openecomp.sdc.ci.tests.utils.ToscaParserUtils;
44
45 import com.aventstack.extentreports.Status;
46
47 public class VfModuleVerificator {
48
49         private static final String [] PROPERTY_TYPES = {"vf_module_label", "min_vf_module_instances", "max_vf_module_instances", "initial_count"};
50         private static final String VF_MODULE_TYPE = "org.openecomp.groups.VfModule";
51         
52         /**
53          * compare number of groups from HEAT.meta file vs TOSCA yaml groups generated by ASDC
54          * @param listTypeHeatMetaDefinition - java object created from HEAT.meta file
55          * @param toscaDefinition - java object created from TOSCA yaml
56          */
57         public static void compareNumberOfVfModules(List<TypeHeatMetaDefinition> listTypeHeatMetaDefinition, ToscaDefinition toscaDefinition) {
58
59                 int heatMetaGroupCount = 0;
60                 int toscaDefinitionGroupCount = 0;
61                 for (TypeHeatMetaDefinition typeHeatMetaDefinition : listTypeHeatMetaDefinition) {
62                         heatMetaGroupCount = typeHeatMetaDefinition.getGroupHeatMetaDefinition().size();
63                 }
64                 toscaDefinitionGroupCount = toscaDefinition.getTopology_template().getGroups().size();
65                 assertEquals("Expected num of groups in HEAT.meta file is " + heatMetaGroupCount + ", but was in TOSCA yaml file " + toscaDefinitionGroupCount, heatMetaGroupCount, toscaDefinitionGroupCount);
66         }
67
68         /**
69          * check group structure and "metadata" parameters vs data on the service object
70          * @param toscaDefinition
71          */
72         public static void verifyGroupMetadata(ToscaDefinition toscaDefinition, Service service) {
73
74                 Map<String, ToscaGroupsTopologyTemplateDefinition> groups = toscaDefinition.getTopology_template().getGroups();
75                 for (Map.Entry<String, ToscaGroupsTopologyTemplateDefinition> groupTopologyTemplateDefinition : groups.entrySet()) {
76                         String key = groupTopologyTemplateDefinition.getKey();
77                         GroupInstance groupInstanceObject = getGroupInstanceByKey(key, service);
78                         ToscaGroupsMetadataDefinition metadata = groupTopologyTemplateDefinition.getValue().getMetadata();
79                         assertNotNull("groupInstanceObject is null", groupInstanceObject);
80                         assertTrue("expected vfModuleModelName " + groupInstanceObject.getGroupName() + ", actual " + metadata.getVfModuleModelName(), groupInstanceObject.getGroupName().equals(metadata.getVfModuleModelName()));
81                         assertTrue("expected vfModuleModelInvariantUUID " + groupInstanceObject.getInvariantUUID() + ", actual " + metadata.getVfModuleModelInvariantUUID(), groupInstanceObject.getInvariantUUID().equals(metadata.getVfModuleModelInvariantUUID()));
82                         assertTrue("expected vfModuleModelCustomizationUUID " + groupInstanceObject.getCustomizationUUID() + ", actual " + metadata.getVfModuleModelCustomizationUUID(), groupInstanceObject.getCustomizationUUID().equals(metadata.getVfModuleModelCustomizationUUID()));
83                         assertTrue("expected vfModuleModelUUID " + groupInstanceObject.getGroupUUID() + ", actual " + metadata.getVfModuleModelUUID(), groupInstanceObject.getGroupUUID().equals(metadata.getVfModuleModelUUID()));
84                         assertTrue("expected vfModuleModelVersion " + groupInstanceObject.getVersion() + ", actual " + metadata.getVfModuleModelVersion(), groupInstanceObject.getVersion().equals(metadata.getVfModuleModelVersion()));
85                 }
86         }
87
88         
89         /**
90          * @param key
91          * @param service
92          * @return
93          */
94         public static GroupInstance getGroupInstanceByKey(String key, Service service) {
95                 for(ComponentInstance componentInstance : service.getComponentInstances()){
96                         for(GroupInstance groupInstance : componentInstance.getGroupInstances()){
97                                 if( key.equals(groupInstance.getName())){
98                                         return groupInstance;
99                                 }
100                         }
101                 }
102                 return null;
103         }
104
105         public static void validateSpecificModulePropertiesFromRequest(Resource resource) {
106                 List<List<PropertyDataDefinition>> allProperties = resource.getGroups().stream().
107                                                                                                              filter(e -> e.getType().equals(VF_MODULE_TYPE)).
108                                                                                                              map(e -> e.getProperties()).
109                                                                                                              collect(Collectors.toList());
110                 for(String propertyType :PROPERTY_TYPES){
111                         int numberOfTypes = getPropertyType(allProperties, propertyType).size();
112                         SetupCDTest.getExtendTest().log(Status.INFO, String.format("Validating VF property %s exist, Expected: %s, Actual: %s  ", propertyType, allProperties.size(), numberOfTypes));
113                         assertTrue(numberOfTypes == allProperties.size());
114                 }               
115         }
116
117         public static List<PropertyDataDefinition> getPropertyType(List<List<PropertyDataDefinition>> allProperties, String propertyType) {
118                 return allProperties.stream().
119                                              flatMap(List::stream).
120                                              filter(e -> e.getName().equals(propertyType)).
121                                              collect(Collectors.toList());
122         }
123         
124         public static void validateSpecificModulePropertiesFromFile(ToscaDefinition toscaDefinition){
125                 List<ToscaGroupsTopologyTemplateDefinition> vfModules = toscaDefinition.getTopology_template().getGroups().values().stream().
126                                                                                                                                                     filter(e -> e.getType().equals(VF_MODULE_TYPE)).
127                                                                                                                                                     collect(Collectors.toList());
128                 
129                 for(String propertyType :PROPERTY_TYPES){
130                         int numberOfTypes = (int) vfModules.stream().
131                                                                                                 filter(e -> e.getProperties().containsKey(propertyType)).
132                                                                                                 count();
133                         SetupCDTest.getExtendTest().log(Status.INFO, String.format("Validating VF property %s exist, Expected: %s, Actual: %s  ", propertyType, vfModules.size(), numberOfTypes));
134                         assertTrue(numberOfTypes == vfModules.size());
135                 }               
136         }
137
138         public static ToscaDefinition getToscaTemplate(String pathToCsar) throws Exception {
139                 String outputFolder = DeploymentViewVerificator.unzipCsarFile(pathToCsar);
140                 String templateFileName = VfModuleVerificator.getTemplateFilenname(pathToCsar);
141                 
142                 File pathToMainServiceTemplate = new File(outputFolder + File.separator + "Definitions" + File.separator + templateFileName);
143                 ToscaDefinition toscaDefinition = ToscaParserUtils.parseToscaYamlToJavaObject(pathToMainServiceTemplate);
144                 
145                 DeploymentViewVerificator.cleanFolders(new File(outputFolder).getParent());
146                 
147                 return toscaDefinition;
148         }
149
150         public static String getTemplateFilenname(String pathToCsar) {
151                 File csarFile = new File(pathToCsar);
152                 String templateFileName = csarFile.getName().replaceAll("-csar.csar", "-template.yml");
153                 return templateFileName;
154         }
155         
156 }