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.verificator;
23 import com.aventstack.extentreports.Status;
24 import org.json.simple.JSONArray;
25 import org.json.simple.JSONObject;
26 import org.json.simple.JSONValue;
27 import org.openecomp.sdc.be.model.User;
28 import org.openecomp.sdc.ci.tests.datatypes.HeatMetaFirstLevelDefinition;
29 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
30 import org.openecomp.sdc.ci.tests.datatypes.VFCArtifact;
31 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
32 import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
33 import org.openecomp.sdc.ci.tests.utilities.RestCDUtils;
34 import org.testng.Assert;
36 import java.util.ArrayList;
37 import java.util.LinkedList;
38 import java.util.List;
40 import java.util.stream.Collectors;
42 import static org.testng.Assert.assertTrue;
44 public class VFCArtifactVerificator {
46 private static final String ARTIFACTS = "artifacts";
47 private static final String DEPLOYMENT_ARTIFACTS = "deploymentArtifacts";
48 private static List<VFCArtifact> vfcArtifactList = new ArrayList<VFCArtifact>();
49 private static JSONObject jsonResource;
51 public static void verifyVFCArtifactsNotInVFArtifactList(ResourceReqDetails resource , User user, RestResponse optionalGetResponse, Map<String, LinkedList<HeatMetaFirstLevelDefinition>> expectedArtifactMap){
52 ExtentTestActions.log(Status.INFO, "Verifying that VFC artifacts are not as part of VF artifacts.");
53 LinkedList<HeatMetaFirstLevelDefinition> expectedDeploymentArtifacts = expectedArtifactMap.get(DEPLOYMENT_ARTIFACTS);
54 LinkedList<HeatMetaFirstLevelDefinition> expectedInformationalArtifacts = expectedArtifactMap.get(ARTIFACTS);
56 Map<String, Object> vfDepArtifacts = getVFDeploymentArtifacts(resource, user, optionalGetResponse);
57 for (Object artifact : vfDepArtifacts.values()){
58 JSONObject acArtifact = ((JSONObject) JSONValue.parse(artifact.toString()));
59 String acArtifactName = acArtifact.get("artifactName").toString();
61 for(HeatMetaFirstLevelDefinition exDepArtifact : expectedDeploymentArtifacts){
62 assertTrue(!exDepArtifact.getType().equals(acArtifactName));
66 Map<String, Object> vfInfoArtifacts = getVFInforamtionalArtifacts(resource, user, optionalGetResponse);
67 for (Object artifact : vfInfoArtifacts.values()){
68 JSONObject acArtifact = ((JSONObject) JSONValue.parse(artifact.toString()));
69 if (acArtifact.containsKey("artifactName")){
70 String acArtifactName = acArtifact.get("artifactName").toString();
72 for(HeatMetaFirstLevelDefinition exInfoArtifact : expectedInformationalArtifacts){
73 assertTrue(!exInfoArtifact.getType().equals(acArtifactName));
84 public static void verifyVfcArtifactUpdated(String instanceName, ResourceReqDetails resource, User user){
85 ExtentTestActions.log(Status.INFO, "Verifying VFC artifacts are updated.");
86 List<VFCArtifact> vfcArtifactsBeforeUpdate = getVfcArtifactList();
88 setVfcArtifactList(new ArrayList<VFCArtifact>());
89 setActualVfcArtifactList(instanceName, resource, user);
91 for (VFCArtifact artifact : vfcArtifactsBeforeUpdate){
92 String artifactnameBeforeUpdate = artifact.getArtifactname();
93 for (VFCArtifact newArtifact : vfcArtifactList){
94 String artifactnameAfterUpdate = newArtifact.getArtifactname();
95 if (artifactnameBeforeUpdate.equals(artifactnameAfterUpdate)){
96 String artifactUUIDAfterUpdate = newArtifact.getArtifactUUID();
97 assertTrue(!artifactUUIDAfterUpdate.equals(artifact.getArtifactUUID()));
99 int artifactVersionAfterUpdate = Integer.parseInt(newArtifact.getArtifactVersion());
100 int artifactVersionBeforeUpdate = Integer.parseInt(artifact.getArtifactVersion());
101 assertTrue(artifactVersionAfterUpdate == artifactVersionBeforeUpdate + 1);
104 vfcArtifactList.remove(newArtifact);
106 ExtentTestActions.log(Status.INFO, "VFC artifacts are updated and verified.");
114 assertTrue(vfcArtifactList.size() == 0);
118 public static void verifyVFCArtifactNotChanged(String instanceName, ResourceReqDetails resource, User user){
119 ExtentTestActions.log(Status.INFO, "Verifying VFC artifacts are not chaned after update.");
120 List<VFCArtifact> vfcArtifactsBeforeUpdate = getVfcArtifactList();
122 setVfcArtifactList(new ArrayList<VFCArtifact>());
123 setActualVfcArtifactList(instanceName, resource, user);
125 for (VFCArtifact artifact : vfcArtifactsBeforeUpdate){
126 String artifactnameBeforeUpdate = artifact.getArtifactname();
127 for (VFCArtifact newArtifact : vfcArtifactList){
128 String artifactnameAfterUpdate = newArtifact.getArtifactname();
129 if (artifactnameBeforeUpdate.equals(artifactnameAfterUpdate)){
130 String artifactUUIDAfterUpdate = newArtifact.getArtifactUUID();
131 assertTrue(artifactUUIDAfterUpdate.equals(artifact.getArtifactUUID()));
133 int artifactVersionAfterUpdate = Integer.parseInt(newArtifact.getArtifactVersion());
134 int artifactVersionBeforeUpdate = Integer.parseInt(artifact.getArtifactVersion());
135 assertTrue(artifactVersionAfterUpdate == artifactVersionBeforeUpdate);
137 vfcArtifactList.remove(newArtifact);
144 assertTrue(vfcArtifactList.size() == 0);
148 public static void verifyNoVfcArtifacts(ResourceReqDetails resource , User user, RestResponse optionalGetResponse){
149 ExtentTestActions.log(Status.INFO, "Verifying that there are no VFC artifacts at all.");
150 JSONArray jArr = getVFInstances(resource, user, optionalGetResponse);
151 for (Object instanceObj : jArr){
152 JSONObject instance = (JSONObject) JSONValue.parse(instanceObj.toString());
153 List<String> actualDeploymentArtifacts = getActualVfcInstanceArtifactsFromJson(DEPLOYMENT_ARTIFACTS, instance);
155 assertTrue(actualDeploymentArtifacts == null || actualDeploymentArtifacts.size() == 0);
159 public static void verifyVfcArtifacts(ResourceReqDetails resource , User user, String instanceName, Map<String, LinkedList<HeatMetaFirstLevelDefinition>> expectedArtifactMap,
160 RestResponse optionalGetResponse){
161 ExtentTestActions.log(Status.INFO, "Verifying VFC artifacts for instance named " + instanceName);
163 String exCompName = instanceName.split(".vfc.")[1].toLowerCase();
164 String exName = instanceName.split(".heat.")[1].toLowerCase();
166 JSONArray jArr = getVFInstances(resource, user, optionalGetResponse);
167 int jArrSize = jArr.size();
169 for (Object instanceObj : jArr){
170 JSONObject instance = (JSONObject) JSONValue.parse(instanceObj.toString());
171 String componentName = instance.get("componentName").toString().toLowerCase();
172 String name = instance.get("name").toString().toLowerCase();
174 if (componentName.contains(exCompName) || name.toLowerCase().equals(exName)){
176 List<String> actualDeploymentArtifacts = getActualVfcInstanceArtifactsFromJson(DEPLOYMENT_ARTIFACTS, instance);
177 LinkedList<HeatMetaFirstLevelDefinition> expectedDeploymentArtifacts = expectedArtifactMap.get(DEPLOYMENT_ARTIFACTS);
178 checkVFCArtifactsExist(expectedDeploymentArtifacts, actualDeploymentArtifacts);
181 List<String> actualInformationalArtifacts = getActualVfcInstanceArtifactsFromJson(ARTIFACTS, instance);
182 LinkedList<HeatMetaFirstLevelDefinition> expectedInformationalArtifacts = expectedArtifactMap.get(ARTIFACTS);
183 checkVFCArtifactsExist(expectedInformationalArtifacts, actualInformationalArtifacts);
185 jArr.remove(instanceObj);
187 ExtentTestActions.log(Status.INFO, "VFC artifacts for instance named " + instanceName + "are verified.");
193 assertTrue(jArr.size() == jArrSize - 1, "Instance " + instanceName + " was not found and tested");
199 private static JSONArray getVFInstances(ResourceReqDetails resource, User user, RestResponse response) {
201 jsonResource = getVFAsJsonObject(resource, user, response);
202 JSONArray jArr = (JSONArray) jsonResource.get("componentInstances");
206 private static Map<String, Object> getVFDeploymentArtifacts(ResourceReqDetails resource, User user, RestResponse response) {
208 jsonResource = getVFAsJsonObject(resource, user, response);
209 Map<String, Object> jArr = (Map<String, Object>) jsonResource.get(DEPLOYMENT_ARTIFACTS);
213 private static Map<String, Object> getVFInforamtionalArtifacts(ResourceReqDetails resource, User user, RestResponse response) {
215 jsonResource = getVFAsJsonObject(resource, user, response);
216 Map<String, Object> jArr = (Map<String, Object>) jsonResource.get(ARTIFACTS);
220 private static JSONObject getVFAsJsonObject(ResourceReqDetails resource, User user, RestResponse response) {
221 if (response == null){
222 resource.setUniqueId(null);
223 response = RestCDUtils.getResource(resource, user);
224 assertTrue(response.getErrorCode().intValue() == 200);
225 getVFAsJsonObject(resource, user, response);
228 String responseAfterDrag = response.getResponse();
229 jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
235 private static List<String> getActualVfcInstanceArtifactsFromJson(String artifactKind, JSONObject instanceFromJson){
236 Object actualtObject = instanceFromJson.get(artifactKind);
237 if (actualtObject != null){
238 JSONObject actualJsonObject = (JSONObject) JSONValue.parse(actualtObject.toString());
239 List<String> actualArtifacts = (List<String>) actualJsonObject.keySet().stream().map(e -> actualJsonObject.get(e).toString()).collect(Collectors.toList());
240 return actualArtifacts;
245 private static void checkVFCArtifactsExist(LinkedList<HeatMetaFirstLevelDefinition> expectedArtifacts, List<String> actualArtifacts) {
246 if (expectedArtifacts == null){
250 if (expectedArtifacts.size() != actualArtifacts.size()){
251 ExtentTestActions.log(Status.FAIL, "Expected and actual VFC artifacts lists size are not the same. Expected size: " + expectedArtifacts.size() + " , actual size: " + actualArtifacts.size());
252 Assert.fail("Expected and actual VFC artifacts lists size are not the same. Expected size: " + expectedArtifacts.size() + " , actual size: " + actualArtifacts.size());
255 List<String> types = new ArrayList<String>();
256 List<String> fileNames = new ArrayList<String>();
257 for (HeatMetaFirstLevelDefinition exArtifact : expectedArtifacts){
259 fileNames.add(exArtifact.getFileName());
260 types.add(exArtifact.getType());
264 for (int i = 0 ; i < actualArtifacts.size() ; i++){
265 String actualArtifactsString = actualArtifacts.get(i);
266 JSONObject acArtifact = ((JSONObject) JSONValue.parse(actualArtifactsString));
268 String acArtifactFileName = acArtifact.get("artifactName").toString();
269 String acArtifactType = acArtifact.get("artifactType").toString();
271 assertTrue(types.contains(acArtifactType), "List does not contain " + acArtifactType);
272 assertTrue(fileNames.contains(acArtifactFileName), "List does not contain " + acArtifactFileName);
274 types.remove(acArtifactType);
275 fileNames.remove(acArtifactFileName);
279 assertTrue(types.size() == 0);
280 assertTrue(fileNames.size() == 0);
284 public static List<VFCArtifact> getVfcArtifactList(){
285 return vfcArtifactList;
289 public static void setVfcArtifactList(List<VFCArtifact> vfcArtifactList) {
290 VFCArtifactVerificator.vfcArtifactList = vfcArtifactList;
293 public static void setActualVfcArtifactList(String instanceName, ResourceReqDetails resource , User user) {
294 String exCompName = instanceName.split(".vfc.")[1].toLowerCase();
295 String exName = instanceName.split(".heat.")[1].toLowerCase();
297 JSONArray jArr = getVFInstances(resource, user, null);
299 for (Object instanceObj : jArr){
300 JSONObject instance = (JSONObject) JSONValue.parse(instanceObj.toString());
301 String componentName = instance.get("componentName").toString().toLowerCase();
302 String name = instance.get("name").toString().toLowerCase();
304 if (componentName.contains(exCompName) || name.toLowerCase().equals(exName)){
305 List<String> actualDeploymentArtifacts = getActualVfcInstanceArtifactsFromJson(DEPLOYMENT_ARTIFACTS, instance);
306 List<String> actualInformationalArtifacts = getActualVfcInstanceArtifactsFromJson(ARTIFACTS, instance);
308 if (actualDeploymentArtifacts != null){
309 for (int i = 0 ; i < actualDeploymentArtifacts.size() ; i++){
310 String actualArtifactsString = actualDeploymentArtifacts.get(i);
311 JSONObject acArtifact = ((JSONObject) JSONValue.parse(actualArtifactsString));
313 if (acArtifact.containsKey("artifactName")){
314 String acArtifactType = acArtifact.get("artifactName").toString();
315 String acArtifactFileName = acArtifact.get("artifactType").toString();
316 String acArtifactUUID = acArtifact.get("artifactUUID").toString();
317 String acArtifactVersion = acArtifact.get("artifactVersion").toString();
319 vfcArtifactList.add(new VFCArtifact(acArtifactType, acArtifactFileName, acArtifactUUID, acArtifactVersion));
323 if (actualInformationalArtifacts != null){
324 for (int i = 0 ; i < actualInformationalArtifacts.size() ; i++){
325 String actualArtifactsString = actualInformationalArtifacts.get(i);
326 JSONObject acArtifact = ((JSONObject) JSONValue.parse(actualArtifactsString));
328 if (acArtifact.containsKey("artifactName")){
329 String acArtifactType = acArtifact.get("artifactName").toString();
330 String acArtifactFileName = acArtifact.get("artifactType").toString();
331 String acArtifactUUID = acArtifact.get("artifactUUID").toString();
332 String acArtifactVersion = acArtifact.get("artifactVersion").toString();
333 vfcArtifactList.add(new VFCArtifact(acArtifactType, acArtifactFileName, acArtifactUUID, acArtifactVersion));