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