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