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 static org.testng.Assert.assertTrue;
25 import java.util.ArrayList;
26 import java.util.LinkedList;
27 import java.util.List;
29 import java.util.stream.Collectors;
31 import org.json.simple.JSONArray;
32 import org.json.simple.JSONObject;
33 import org.json.simple.JSONValue;
34 import org.openecomp.sdc.be.model.User;
35 import org.openecomp.sdc.ci.tests.datatypes.HeatMetaFirstLevelDefinition;
36 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
37 import org.openecomp.sdc.ci.tests.datatypes.VFCArtifact;
38 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
39 import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
40 import org.openecomp.sdc.ci.tests.utilities.RestCDUtils;
41 import org.testng.Assert;
43 import com.aventstack.extentreports.Status;
45 public class VFCArtifactVerificator {
47 private static final String ARTIFACTS = "artifacts";
48 private static final String DEPLOYMENT_ARTIFACTS = "deploymentArtifacts";
49 private static List<VFCArtifact> vfcArtifactList = new ArrayList<VFCArtifact>();
50 private static JSONObject jsonResource;
52 public static void verifyVFCArtifactsNotInVFArtifactList(ResourceReqDetails resource , User user, RestResponse optionalGetResponse, Map<String, LinkedList<HeatMetaFirstLevelDefinition>> expectedArtifactMap){
53 ExtentTestActions.log(Status.INFO, "Verifying that VFC artifacts are not as part of VF artifacts.");
54 LinkedList<HeatMetaFirstLevelDefinition> expectedDeploymentArtifacts = expectedArtifactMap.get(DEPLOYMENT_ARTIFACTS);
55 LinkedList<HeatMetaFirstLevelDefinition> expectedInformationalArtifacts = expectedArtifactMap.get(ARTIFACTS);
57 Map<String, Object> vfDepArtifacts = getVFDeploymentArtifacts(resource, user, optionalGetResponse);
58 for (Object artifact : vfDepArtifacts.values()){
59 JSONObject acArtifact = ((JSONObject) JSONValue.parse(artifact.toString()));
60 String acArtifactName = acArtifact.get("artifactName").toString();
62 for(HeatMetaFirstLevelDefinition exDepArtifact : expectedDeploymentArtifacts){
63 assertTrue(!exDepArtifact.getType().equals(acArtifactName));
67 Map<String, Object> vfInfoArtifacts = getVFInforamtionalArtifacts(resource, user, optionalGetResponse);
68 for (Object artifact : vfInfoArtifacts.values()){
69 JSONObject acArtifact = ((JSONObject) JSONValue.parse(artifact.toString()));
70 if (acArtifact.containsKey("artifactName")){
71 String acArtifactName = acArtifact.get("artifactName").toString();
73 for(HeatMetaFirstLevelDefinition exInfoArtifact : expectedInformationalArtifacts){
74 assertTrue(!exInfoArtifact.getType().equals(acArtifactName));
85 public static void verifyVfcArtifactUpdated(String instanceName, ResourceReqDetails resource, User user){
86 ExtentTestActions.log(Status.INFO, "Verifying VFC artifacts are updated.");
87 List<VFCArtifact> vfcArtifactsBeforeUpdate = getVfcArtifactList();
89 setVfcArtifactList(new ArrayList<VFCArtifact>());
90 setActualVfcArtifactList(instanceName, resource, user);
92 for (VFCArtifact artifact : vfcArtifactsBeforeUpdate){
93 String artifactnameBeforeUpdate = artifact.getArtifactname();
94 for (VFCArtifact newArtifact : vfcArtifactList){
95 String artifactnameAfterUpdate = newArtifact.getArtifactname();
96 if (artifactnameBeforeUpdate.equals(artifactnameAfterUpdate)){
97 String artifactUUIDAfterUpdate = newArtifact.getArtifactUUID();
98 assertTrue(!artifactUUIDAfterUpdate.equals(artifact.getArtifactUUID()));
100 int artifactVersionAfterUpdate = Integer.parseInt(newArtifact.getArtifactVersion());
101 int artifactVersionBeforeUpdate = Integer.parseInt(artifact.getArtifactVersion());
102 assertTrue(artifactVersionAfterUpdate == artifactVersionBeforeUpdate + 1);
105 vfcArtifactList.remove(newArtifact);
107 ExtentTestActions.log(Status.INFO, "VFC artifacts are updated and verified.");
115 assertTrue(vfcArtifactList.size() == 0);
119 public static void verifyVFCArtifactNotChanged(String instanceName, ResourceReqDetails resource, User user){
120 ExtentTestActions.log(Status.INFO, "Verifying VFC artifacts are not chaned after update.");
121 List<VFCArtifact> vfcArtifactsBeforeUpdate = getVfcArtifactList();
123 setVfcArtifactList(new ArrayList<VFCArtifact>());
124 setActualVfcArtifactList(instanceName, resource, user);
126 for (VFCArtifact artifact : vfcArtifactsBeforeUpdate){
127 String artifactnameBeforeUpdate = artifact.getArtifactname();
128 for (VFCArtifact newArtifact : vfcArtifactList){
129 String artifactnameAfterUpdate = newArtifact.getArtifactname();
130 if (artifactnameBeforeUpdate.equals(artifactnameAfterUpdate)){
131 String artifactUUIDAfterUpdate = newArtifact.getArtifactUUID();
132 assertTrue(artifactUUIDAfterUpdate.equals(artifact.getArtifactUUID()));
134 int artifactVersionAfterUpdate = Integer.parseInt(newArtifact.getArtifactVersion());
135 int artifactVersionBeforeUpdate = Integer.parseInt(artifact.getArtifactVersion());
136 assertTrue(artifactVersionAfterUpdate == artifactVersionBeforeUpdate);
138 vfcArtifactList.remove(newArtifact);
145 assertTrue(vfcArtifactList.size() == 0);
149 public static void verifyNoVfcArtifacts(ResourceReqDetails resource , User user, RestResponse optionalGetResponse){
150 ExtentTestActions.log(Status.INFO, "Verifying that there are no VFC artifacts at all.");
151 JSONArray jArr = getVFInstances(resource, user, optionalGetResponse);
152 for (Object instanceObj : jArr){
153 JSONObject instance = (JSONObject) JSONValue.parse(instanceObj.toString());
154 List<String> actualDeploymentArtifacts = getActualVfcInstanceArtifactsFromJson(DEPLOYMENT_ARTIFACTS, instance);
156 assertTrue(actualDeploymentArtifacts == null || actualDeploymentArtifacts.size() == 0);
160 public static void verifyVfcArtifacts(ResourceReqDetails resource , User user, String instanceName, Map<String, LinkedList<HeatMetaFirstLevelDefinition>> expectedArtifactMap,
161 RestResponse optionalGetResponse){
162 ExtentTestActions.log(Status.INFO, "Verifying VFC artifacts for instance named " + instanceName);
164 String exCompName = instanceName.split(".vfc.")[1].toLowerCase();
165 String exName = instanceName.split(".heat.")[1].toLowerCase();
167 JSONArray jArr = getVFInstances(resource, user, optionalGetResponse);
168 int jArrSize = jArr.size();
170 for (Object instanceObj : jArr){
171 JSONObject instance = (JSONObject) JSONValue.parse(instanceObj.toString());
172 String componentName = instance.get("componentName").toString().toLowerCase();
173 String name = instance.get("name").toString().toLowerCase();
175 if (componentName.contains(exCompName) || name.toLowerCase().equals(exName)){
177 List<String> actualDeploymentArtifacts = getActualVfcInstanceArtifactsFromJson(DEPLOYMENT_ARTIFACTS, instance);
178 LinkedList<HeatMetaFirstLevelDefinition> expectedDeploymentArtifacts = expectedArtifactMap.get(DEPLOYMENT_ARTIFACTS);
179 checkVFCArtifactsExist(expectedDeploymentArtifacts, actualDeploymentArtifacts);
182 List<String> actualInformationalArtifacts = getActualVfcInstanceArtifactsFromJson(ARTIFACTS, instance);
183 LinkedList<HeatMetaFirstLevelDefinition> expectedInformationalArtifacts = expectedArtifactMap.get(ARTIFACTS);
184 checkVFCArtifactsExist(expectedInformationalArtifacts, actualInformationalArtifacts);
186 jArr.remove(instanceObj);
188 ExtentTestActions.log(Status.INFO, "VFC artifacts for instance named " + instanceName + "are verified.");
194 assertTrue(jArr.size() == jArrSize - 1, "Instance " + instanceName + " was not found and tested");
200 private static JSONArray getVFInstances(ResourceReqDetails resource, User user, RestResponse response) {
202 jsonResource = getVFAsJsonObject(resource, user, response);
203 JSONArray jArr = (JSONArray) jsonResource.get("componentInstances");
207 private static Map<String, Object> getVFDeploymentArtifacts(ResourceReqDetails resource, User user, RestResponse response) {
209 jsonResource = getVFAsJsonObject(resource, user, response);
210 Map<String, Object> jArr = (Map<String, Object>) jsonResource.get(DEPLOYMENT_ARTIFACTS);
214 private static Map<String, Object> getVFInforamtionalArtifacts(ResourceReqDetails resource, User user, RestResponse response) {
216 jsonResource = getVFAsJsonObject(resource, user, response);
217 Map<String, Object> jArr = (Map<String, Object>) jsonResource.get(ARTIFACTS);
221 private static JSONObject getVFAsJsonObject(ResourceReqDetails resource, User user, RestResponse response) {
222 if (response == null){
223 resource.setUniqueId(null);
224 response = RestCDUtils.getResource(resource, user);
225 assertTrue(response.getErrorCode().intValue() == 200);
226 getVFAsJsonObject(resource, user, response);
229 String responseAfterDrag = response.getResponse();
230 jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
236 private static List<String> getActualVfcInstanceArtifactsFromJson(String artifactKind, JSONObject instanceFromJson){
237 Object actualtObject = instanceFromJson.get(artifactKind);
238 if (actualtObject != null){
239 JSONObject actualJsonObject = (JSONObject) JSONValue.parse(actualtObject.toString());
240 List<String> actualArtifacts = (List<String>) actualJsonObject.keySet().stream().map(e -> actualJsonObject.get(e).toString()).collect(Collectors.toList());
241 return actualArtifacts;
246 private static void checkVFCArtifactsExist(LinkedList<HeatMetaFirstLevelDefinition> expectedArtifacts, List<String> actualArtifacts) {
247 if (expectedArtifacts == null){
251 if (expectedArtifacts.size() != actualArtifacts.size()){
252 ExtentTestActions.log(Status.FAIL, "Expected and actual VFC artifacts lists size are not the same. Expected size: " + expectedArtifacts.size() + " , actual size: " + actualArtifacts.size());
253 Assert.fail("Expected and actual VFC artifacts lists size are not the same. Expected size: " + expectedArtifacts.size() + " , actual size: " + actualArtifacts.size());
256 List<String> types = new ArrayList<String>();
257 List<String> fileNames = new ArrayList<String>();
258 for (HeatMetaFirstLevelDefinition exArtifact : expectedArtifacts){
260 fileNames.add(exArtifact.getFileName());
261 types.add(exArtifact.getType());
265 for (int i = 0 ; i < actualArtifacts.size() ; i++){
266 String actualArtifactsString = actualArtifacts.get(i);
267 JSONObject acArtifact = ((JSONObject) JSONValue.parse(actualArtifactsString));
269 String acArtifactFileName = acArtifact.get("artifactName").toString();
270 String acArtifactType = acArtifact.get("artifactType").toString();
272 assertTrue(types.contains(acArtifactType), "List does not contain " + acArtifactType);
273 assertTrue(fileNames.contains(acArtifactFileName), "List does not contain " + acArtifactFileName);
275 types.remove(acArtifactType);
276 fileNames.remove(acArtifactFileName);
280 assertTrue(types.size() == 0);
281 assertTrue(fileNames.size() == 0);
285 public static List<VFCArtifact> getVfcArtifactList(){
286 return vfcArtifactList;
290 public static void setVfcArtifactList(List<VFCArtifact> vfcArtifactList) {
291 VFCArtifactVerificator.vfcArtifactList = vfcArtifactList;
294 public static void setActualVfcArtifactList(String instanceName, ResourceReqDetails resource , User user) {
295 String exCompName = instanceName.split(".vfc.")[1].toLowerCase();
296 String exName = instanceName.split(".heat.")[1].toLowerCase();
298 JSONArray jArr = getVFInstances(resource, user, null);
300 for (Object instanceObj : jArr){
301 JSONObject instance = (JSONObject) JSONValue.parse(instanceObj.toString());
302 String componentName = instance.get("componentName").toString().toLowerCase();
303 String name = instance.get("name").toString().toLowerCase();
305 if (componentName.contains(exCompName) || name.toLowerCase().equals(exName)){
306 List<String> actualDeploymentArtifacts = getActualVfcInstanceArtifactsFromJson(DEPLOYMENT_ARTIFACTS, instance);
307 List<String> actualInformationalArtifacts = getActualVfcInstanceArtifactsFromJson(ARTIFACTS, instance);
309 if (actualDeploymentArtifacts != null){
310 for (int i = 0 ; i < actualDeploymentArtifacts.size() ; i++){
311 String actualArtifactsString = actualDeploymentArtifacts.get(i);
312 JSONObject acArtifact = ((JSONObject) JSONValue.parse(actualArtifactsString));
314 if (acArtifact.containsKey("artifactName")){
315 String acArtifactType = acArtifact.get("artifactName").toString();
316 String acArtifactFileName = acArtifact.get("artifactType").toString();
317 String acArtifactUUID = acArtifact.get("artifactUUID").toString();
318 String acArtifactVersion = acArtifact.get("artifactVersion").toString();
320 vfcArtifactList.add(new VFCArtifact(acArtifactType, acArtifactFileName, acArtifactUUID, acArtifactVersion));
324 if (actualInformationalArtifacts != null){
325 for (int i = 0 ; i < actualInformationalArtifacts.size() ; i++){
326 String actualArtifactsString = actualInformationalArtifacts.get(i);
327 JSONObject acArtifact = ((JSONObject) JSONValue.parse(actualArtifactsString));
329 if (acArtifact.containsKey("artifactName")){
330 String acArtifactType = acArtifact.get("artifactName").toString();
331 String acArtifactFileName = acArtifact.get("artifactType").toString();
332 String acArtifactUUID = acArtifact.get("artifactUUID").toString();
333 String acArtifactVersion = acArtifact.get("artifactVersion").toString();
334 vfcArtifactList.add(new VFCArtifact(acArtifactType, acArtifactFileName, acArtifactUUID, acArtifactVersion));