Fix for radio buttons
[sdc.git] / asdc-tests / src / main / java / org / openecomp / sdc / ci / tests / utils / validation / CsarValidationUtils.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.utils.validation;
22
23 import static org.testng.AssertJUnit.assertTrue;
24
25 import java.nio.charset.StandardCharsets;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30
31 import org.json.simple.JSONArray;
32 import org.json.simple.JSONObject;
33 import org.json.simple.parser.JSONParser;
34 import org.openecomp.sdc.be.model.GroupDefinition;
35 import org.openecomp.sdc.be.model.Resource;
36 import org.openecomp.sdc.ci.tests.datatypes.GroupHeatMetaDefinition;
37 import org.openecomp.sdc.ci.tests.datatypes.PropertyHeatMetaDefinition;
38 import org.openecomp.sdc.ci.tests.datatypes.TypeHeatMetaDefinition;
39 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
40 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaDefinition;
41 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaNodeTemplatesTopologyTemplateDefinition;
42 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaRequirementsNodeTemplatesDefinition;
43 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
44 import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
45 import org.openecomp.sdc.ci.tests.utils.rest.ImportRestUtils;
46 import org.openecomp.sdc.common.rest.api.RestResponseAsByteArray;
47 import org.openecomp.sdc.common.util.ZipUtil;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 public class CsarValidationUtils {
52         private static Logger log = LoggerFactory.getLogger(CsarValidationUtils.class.getName());
53
54         public static String getCsarPayload(String csarName, String fileLocation) throws Exception {
55
56                 RestResponseAsByteArray csar = ImportRestUtils.getCsar(csarName,
57                                 ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
58                 assertTrue("Return response code different from 200",
59                                 csar.getHttpStatusCode() == BaseRestUtils.STATUS_CODE_SUCCESS);
60                 Map<String, byte[]> readZip = null;
61                 byte[] data = csar.getResponse();
62                 if (data != null && data.length > 0) {
63                         readZip = ZipUtil.readZip(data);
64
65                 }
66
67                 byte[] artifactsBs = readZip.get(fileLocation);
68                 String str = new String(artifactsBs, StandardCharsets.UTF_8);
69
70                 return str;
71
72         }
73
74         public static List<TypeHeatMetaDefinition> getListTypeHeatMetaDefinition(String csarUUID) throws Exception {
75
76                 String artifactHeatMetaLocation = "Artifacts/HEAT.meta";
77                 JSONParser parser = new JSONParser();
78                 String csarPayload = getCsarPayload(csarUUID, artifactHeatMetaLocation);
79                 if (csarPayload != null) {
80                         Object parse = parser.parse(csarPayload);
81                         JSONObject jsonObject = (JSONObject) parse;
82                         JSONObject jsonObjectImportStructure = (JSONObject) jsonObject.get("importStructure");
83                         List<TypeHeatMetaDefinition> listHeatMetaDefenition = new ArrayList<TypeHeatMetaDefinition>();
84                         listHeatMetaDefenition = getArtifactsByGroup(jsonObjectImportStructure, listHeatMetaDefenition);
85                         return listHeatMetaDefenition;
86                 }
87                 return null;
88
89         }
90
91         protected static List<TypeHeatMetaDefinition> getArtifactsByGroup(JSONObject jsonObjectImportStructure,
92                         List<TypeHeatMetaDefinition> listHeatMetaDefenition) {
93
94                 @SuppressWarnings("unchecked")
95                 Set<Object> typeSet = jsonObjectImportStructure.keySet();
96                 for (Object type : typeSet) {
97                         TypeHeatMetaDefinition heatMetaDefenition = new TypeHeatMetaDefinition();
98                         log.debug(type.toString());
99                         log.debug("{}", jsonObjectImportStructure.get(type));
100                         JSONArray array = (JSONArray) jsonObjectImportStructure.get(type);
101                         heatMetaDefenition.setTypeName((String) type);
102                         List<GroupHeatMetaDefinition> groupHeatMetaDefinitions = new ArrayList<GroupHeatMetaDefinition>();
103                         heatMetaDefenition.setGroupHeatMetaDefinition(fetchArtifactByGroup(array, groupHeatMetaDefinitions, true));
104                         listHeatMetaDefenition.add(heatMetaDefenition);
105                 }
106                 return listHeatMetaDefenition;
107         }
108
109         protected static List<GroupHeatMetaDefinition> fetchArtifactByGroup(JSONArray array,
110                         List<GroupHeatMetaDefinition> listGroupHeatMetaDefinition, Boolean openNewGroup) {
111
112                 GroupHeatMetaDefinition groupHeatMetaDefinition;
113
114                 if (array != null) {
115                         for (int i = 0; i < array.size(); i++) {
116                                 if (openNewGroup) {
117                                         groupHeatMetaDefinition = new GroupHeatMetaDefinition();
118                                         int groupNumber = listGroupHeatMetaDefinition.size() + 1;
119                                         log.debug("groupName={}", groupNumber);
120                                         groupHeatMetaDefinition.setGroup(groupNumber);
121                                         listGroupHeatMetaDefinition.add(groupHeatMetaDefinition);
122                                         PropertyHeatMetaDefinition propertyHeatMetaDefinition = new PropertyHeatMetaDefinition();
123                                         propertyHeatMetaDefinition.setName("isBase");
124                                         propertyHeatMetaDefinition.setValue(false);
125                                         groupHeatMetaDefinition.setPropertyHeatMetaDefinition(propertyHeatMetaDefinition);
126                                 }
127                                 groupHeatMetaDefinition = listGroupHeatMetaDefinition.get(listGroupHeatMetaDefinition.size() - 1);
128                                 JSONObject jsonObject = (JSONObject) array.get(i);
129                                 @SuppressWarnings("unchecked")
130                                 Set<Object> groupsKey = jsonObject.keySet();
131                                 for (Object groupKey : groupsKey) {
132                                         String groupKeyStr = (String) groupKey;
133                                         if (groupKeyStr.equals("isBase")) {
134                                                 PropertyHeatMetaDefinition propertyHeatMetaDefinition = new PropertyHeatMetaDefinition();
135                                                 propertyHeatMetaDefinition.setName(groupKeyStr);
136                                                 propertyHeatMetaDefinition.setValue((boolean) jsonObject.get(groupKeyStr));
137                                                 if (!groupHeatMetaDefinition.getPropertyHeatMetaDefinition()
138                                                                 .equals(propertyHeatMetaDefinition)) {
139                                                         groupHeatMetaDefinition.getPropertyHeatMetaDefinition()
140                                                                         .setValue((boolean) jsonObject.get(groupKeyStr));
141                                                 }
142                                         }
143                                         if (groupKeyStr.equals("fileName") || groupKeyStr.equals("env")) {
144                                                 String artifactName = (String) jsonObject.get(groupKeyStr);
145                                                 List<String> listArtifactNames = groupHeatMetaDefinition.getArtifactList();
146                                                 listArtifactNames.add(artifactName);
147                                                 groupHeatMetaDefinition.setArtifactList(listArtifactNames);
148                                         } else {
149                                                 if (!groupKeyStr.equals("isBase")) {
150                                                         fetchArtifactByGroup((JSONArray) jsonObject.get(groupKeyStr), listGroupHeatMetaDefinition,
151                                                                         false);
152                                                 }
153                                         }
154                                 }
155                         }
156                 }
157                 return listGroupHeatMetaDefinition;
158         }
159
160         private static Integer getArtifactCount(List<TypeHeatMetaDefinition> listHeatMetaDefenition,
161                         Boolean isEnvIncluded) {
162                 int count = 0;
163                 List<String> uniqeArtifactList = new ArrayList<>();
164
165                 for (TypeHeatMetaDefinition typeHeatMetaDefinition : listHeatMetaDefenition) {
166                         for (GroupHeatMetaDefinition groupHeatMetaDefinition : typeHeatMetaDefinition
167                                         .getGroupHeatMetaDefinition()) {
168                                 if (isEnvIncluded) {
169                                         count = count + groupHeatMetaDefinition.getArtifactList().size();
170                                 } else {
171                                         for (String fileName : groupHeatMetaDefinition.getArtifactList()) {
172                                                 if (!fileName.contains(".env") && !uniqeArtifactList.contains(fileName)) {
173                                                         uniqeArtifactList.add(fileName);
174                                                         count = count + 1;
175                                                 }
176                                         }
177                                 }
178                         }
179                 }
180                 return count;
181         }
182
183         private static Integer getGroupCount(List<TypeHeatMetaDefinition> listHeatMetaDefenition) {
184                 int count = 0;
185                 for (TypeHeatMetaDefinition typeHeatMetaDefinition : listHeatMetaDefenition) {
186                         count = count + typeHeatMetaDefinition.getGroupHeatMetaDefinition().size();
187                 }
188                 return count;
189         }
190
191         private static String groupNameBuilder(Resource resource) {
192                 String separator = "::";
193                 String module = "module-";
194                 String groupName = resource.getSystemName() + separator + module;
195                 return groupName;
196         }
197
198         public static void validateCsarVfArtifact(String csarUUID, Resource resource) throws Exception {
199
200                 List<TypeHeatMetaDefinition> listTypeHeatMetaDefinition = getListTypeHeatMetaDefinition(csarUUID);
201                 assertTrue(
202                                 "check group count, expected: " + getGroupCount(listTypeHeatMetaDefinition) + ", actual: "
203                                                 + resource.getGroups().size(),
204                                 getGroupCount(listTypeHeatMetaDefinition) == resource.getGroups().size());
205                 assertTrue(
206                                 "check artifact count, expected: " + getArtifactCount(listTypeHeatMetaDefinition, false) + ", actual: "
207                                                 + resource.getDeploymentArtifacts().size(),
208                                 getArtifactCount(listTypeHeatMetaDefinition, false) == resource.getDeploymentArtifacts().size());
209
210         }
211
212         public static void validateToscaDefinitonObjectVsResource(ToscaDefinition toscaDefinition, Resource resource)
213                         throws Exception {
214
215                 assertTrue(
216                                 "check resource instance count, expected: " + getResourceInstanceCount(toscaDefinition) + ", actual: "
217                                                 + resource.getComponentInstances().size(),
218                                 getResourceInstanceCount(toscaDefinition) == resource.getComponentInstances().size());
219                 assertTrue(
220                                 "check resource instance relation count, expected: " + getResourceInstanceRelationCount(toscaDefinition)
221                                                 + ", actual: " + resource.getComponentInstancesRelations().size(),
222                                 getResourceInstanceRelationCount(toscaDefinition) == resource.getComponentInstancesRelations().size());
223
224         }
225
226         public static Integer getResourceInstanceCount(ToscaDefinition toscaDefinition) {
227
228                 return toscaDefinition.getToscaTopologyTemplate().getToscaNodeTemplatesTopologyTemplateDefinition().size();
229         }
230
231         public static Integer getResourceInstanceRelationCount(ToscaDefinition toscaDefinition) {
232                 int count = 0;
233                 List<ToscaNodeTemplatesTopologyTemplateDefinition> toscaNodeTemplatesTopologyTemplateDefinition = toscaDefinition
234                                 .getToscaTopologyTemplate().getToscaNodeTemplatesTopologyTemplateDefinition();
235                 for (int i = 0; i < toscaNodeTemplatesTopologyTemplateDefinition.size(); i++) {
236                         List<ToscaRequirementsNodeTemplatesDefinition> requirements = toscaNodeTemplatesTopologyTemplateDefinition
237                                         .get(i).getRequirements();
238                         if (requirements != null) {
239                                 for (ToscaRequirementsNodeTemplatesDefinition requirement : requirements) {
240                                         if (requirement.getNode() != null) {
241                                                 count = count + 1;
242                                         }
243                                 }
244                         }
245                 }
246                 return count;
247         }
248
249         // not finished yet
250         private static void validateCsarVfgroup(String csarUUID, Resource resource) {
251
252                 List<GroupDefinition> groups = resource.getGroups();
253                 for (GroupDefinition groupDefinition : groups) {
254                         List<String> artifacts = groupDefinition.getArtifacts();
255                         assertTrue("group description is null", groupDefinition.getDescription() != null);
256                         assertTrue("InvariantUUID is null", groupDefinition.getInvariantUUID() != null);
257                         // groupDefinition.getMembers();
258                         assertTrue(
259                                         "name format mismatch, expected: " + groupNameBuilder(resource) + "[0-9], actual: "
260                                                         + groupDefinition.getName(),
261                                         groupDefinition.getName().contains(groupNameBuilder(resource)));
262                         // groupDefinition.getProperties();
263                         // groupDefinition.getPropertyValueCounter();
264                         assertTrue(groupDefinition.getType().equals(getGroupType()));
265                 }
266
267                 String expectedCsarUUID = csarUUID;
268                 // String expectedToscaResourceName = "org.openecomp.resource.vf." +
269                 // WordUtils.capitalize(resourceDetails.getName().toLowerCase());
270                 //
271                 // assertTrue("csarUUID : " + buildAssertMessage(expectedCsarUUID,
272                 // resource.getCsarUUID()),
273                 // expectedCsarUUID.equals(resource.getCsarUUID()));
274                 // assertTrue("toscaResourceName : " +
275                 // buildAssertMessage(expectedToscaResourceName,
276                 // resource.getToscaResourceName()),
277                 // expectedToscaResourceName.equals(resource.getToscaResourceName()));
278                 //
279                 // RestResponse getResourceResponse =
280                 // ResourceRestUtils.getResource(resource.getUniqueId());
281                 // Resource getResource =
282                 // ResponseParser.parseToObjectUsingMapper(getResourceResponse.getResponse(),
283                 // Resource.class);
284                 // assertTrue("csarUUID : " + buildAssertMessage(expectedCsarUUID,
285                 // getResource.getCsarUUID()),
286                 // expectedCsarUUID.equals(getResource.getCsarUUID()));
287                 // assertTrue("toscaResourceName : " +
288                 // buildAssertMessage(expectedToscaResourceName,
289                 // getResource.getToscaResourceName()),
290                 // expectedToscaResourceName.equals(getResource.getToscaResourceName()));
291
292         }
293
294         private static String getGroupType() {
295                 return "org.openecomp.groups.VfModule";
296         }
297
298 }