2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.utils.validation;
23 import static org.testng.AssertJUnit.assertTrue;
25 import java.nio.charset.StandardCharsets;
26 import java.util.ArrayList;
27 import java.util.List;
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;
51 public class CsarValidationUtils {
52 private static Logger log = LoggerFactory.getLogger(CsarValidationUtils.class.getName());
54 public static String getCsarPayload(String csarName, String fileLocation) throws Exception {
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);
67 byte[] artifactsBs = readZip.get(fileLocation);
68 String str = new String(artifactsBs, StandardCharsets.UTF_8);
74 public static List<TypeHeatMetaDefinition> getListTypeHeatMetaDefinition(String csarUUID) throws Exception {
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;
91 protected static List<TypeHeatMetaDefinition> getArtifactsByGroup(JSONObject jsonObjectImportStructure,
92 List<TypeHeatMetaDefinition> listHeatMetaDefenition) {
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);
106 return listHeatMetaDefenition;
109 protected static List<GroupHeatMetaDefinition> fetchArtifactByGroup(JSONArray array,
110 List<GroupHeatMetaDefinition> listGroupHeatMetaDefinition, Boolean openNewGroup) {
112 GroupHeatMetaDefinition groupHeatMetaDefinition;
115 for (int i = 0; i < array.size(); i++) {
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);
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));
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);
149 if (!groupKeyStr.equals("isBase")) {
150 fetchArtifactByGroup((JSONArray) jsonObject.get(groupKeyStr), listGroupHeatMetaDefinition,
157 return listGroupHeatMetaDefinition;
160 private static Integer getArtifactCount(List<TypeHeatMetaDefinition> listHeatMetaDefenition,
161 Boolean isEnvIncluded) {
163 List<String> uniqeArtifactList = new ArrayList<>();
165 for (TypeHeatMetaDefinition typeHeatMetaDefinition : listHeatMetaDefenition) {
166 for (GroupHeatMetaDefinition groupHeatMetaDefinition : typeHeatMetaDefinition
167 .getGroupHeatMetaDefinition()) {
169 count = count + groupHeatMetaDefinition.getArtifactList().size();
171 for (String fileName : groupHeatMetaDefinition.getArtifactList()) {
172 if (!fileName.contains(".env") && !uniqeArtifactList.contains(fileName)) {
173 uniqeArtifactList.add(fileName);
183 private static Integer getGroupCount(List<TypeHeatMetaDefinition> listHeatMetaDefenition) {
185 for (TypeHeatMetaDefinition typeHeatMetaDefinition : listHeatMetaDefenition) {
186 count = count + typeHeatMetaDefinition.getGroupHeatMetaDefinition().size();
191 private static String groupNameBuilder(Resource resource) {
192 String separator = "::";
193 String module = "module-";
194 String groupName = resource.getSystemName() + separator + module;
198 public static void validateCsarVfArtifact(String csarUUID, Resource resource) throws Exception {
200 List<TypeHeatMetaDefinition> listTypeHeatMetaDefinition = getListTypeHeatMetaDefinition(csarUUID);
202 "check group count, expected: " + getGroupCount(listTypeHeatMetaDefinition) + ", actual: "
203 + resource.getGroups().size(),
204 getGroupCount(listTypeHeatMetaDefinition) == resource.getGroups().size());
206 "check artifact count, expected: " + getArtifactCount(listTypeHeatMetaDefinition, false) + ", actual: "
207 + resource.getDeploymentArtifacts().size(),
208 getArtifactCount(listTypeHeatMetaDefinition, false) == resource.getDeploymentArtifacts().size());
212 public static void validateToscaDefinitonObjectVsResource(ToscaDefinition toscaDefinition, Resource resource)
216 "check resource instance count, expected: " + getResourceInstanceCount(toscaDefinition) + ", actual: "
217 + resource.getComponentInstances().size(),
218 getResourceInstanceCount(toscaDefinition) == resource.getComponentInstances().size());
220 "check resource instance relation count, expected: " + getResourceInstanceRelationCount(toscaDefinition)
221 + ", actual: " + resource.getComponentInstancesRelations().size(),
222 getResourceInstanceRelationCount(toscaDefinition) == resource.getComponentInstancesRelations().size());
226 public static Integer getResourceInstanceCount(ToscaDefinition toscaDefinition) {
228 return toscaDefinition.getToscaTopologyTemplate().getToscaNodeTemplatesTopologyTemplateDefinition().size();
231 public static Integer getResourceInstanceRelationCount(ToscaDefinition toscaDefinition) {
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) {
250 private static void validateCsarVfgroup(String csarUUID, Resource resource) {
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();
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()));
267 String expectedCsarUUID = csarUUID;
268 // String expectedToscaResourceName = "org.openecomp.resource.vf." +
269 // WordUtils.capitalize(resourceDetails.getName().toLowerCase());
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()));
279 // RestResponse getResourceResponse =
280 // ResourceRestUtils.getResource(resource.getUniqueId());
281 // Resource getResource =
282 // ResponseParser.parseToObjectUsingMapper(getResourceResponse.getResponse(),
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()));
294 private static String getGroupType() {
295 return "org.openecomp.groups.VfModule";