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 com.fasterxml.jackson.databind.ObjectMapper;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonParser;
26 import org.openecomp.sdc.be.model.ArtifactDefinition;
27 import org.openecomp.sdc.be.model.Component;
28 import org.openecomp.sdc.be.model.ComponentInstance;
29 import org.openecomp.sdc.be.resources.data.ESArtifactData;
30 import org.openecomp.sdc.ci.tests.config.Config;
31 import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
32 import org.openecomp.sdc.ci.tests.datatypes.enums.ArtifactTypeEnum;
33 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
34 import org.openecomp.sdc.ci.tests.utils.Decoder;
35 import org.openecomp.sdc.ci.tests.utils.Utils;
36 import org.openecomp.sdc.ci.tests.utils.general.FileUtils;
37 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
40 import java.io.IOException;
41 import java.util.List;
44 import static org.testng.AssertJUnit.assertEquals;
45 import static org.testng.AssertJUnit.assertTrue;
46 import static org.testng.AssertJUnit.assertTrue;
48 public class ArtifactValidationUtils {
50 private static String desc = "description";
51 private static String artifactType = "artifactType";
52 private static String artifactName = "artifactName";
53 private static String artifactChecksum = "artifactChecksum";
54 private static String uniqueId = "uniqueId";
55 protected Utils utils;
57 public static void validateInformationalArtifact(ArtifactReqDetails expectedArtifact,
58 Map<String, Object> actualArtifact) {
59 assertTrue("description is not as expected",
60 expectedArtifact.getDescription().equals(actualArtifact.get(desc).toString()));
61 assertTrue("artifactType is not as expected",
62 expectedArtifact.getArtifactType().toUpperCase().equals(actualArtifact.get(artifactType).toString()));
63 assertTrue("artifactName is not as expected",
64 expectedArtifact.getArtifactName().equals(actualArtifact.get(artifactName).toString()));
65 assertTrue("uniqueId is not as expected",
66 expectedArtifact.getUniqueId().equals(actualArtifact.get(uniqueId).toString()));
67 assertTrue("description is not as expected", expectedArtifact.getArtifactLabel().toLowerCase()
68 .equals(actualArtifact.get("artifactLabel").toString()));
71 public static void validateArtifactsNumberInComponent(Component component, ArtifactGroupTypeEnum artifactGroupType,
72 ArtifactTypeEnum artifactType, int expectedNumber) {
73 Map<String, ArtifactDefinition> deploymentArtifacts;
75 if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
76 deploymentArtifacts = component.getDeploymentArtifacts();
78 deploymentArtifacts = component.getArtifacts();
80 if (deploymentArtifacts != null) {
81 for (ArtifactDefinition artifactDefinition : deploymentArtifacts.values()) {
82 if (artifactDefinition.getArtifactType().equals(artifactType.getType())) {
87 assertEquals("Unexpected number of " + artifactGroupType.getType() + " artifacts in component", expectedNumber,
92 public static void validateArtifactsNumberInComponentInstance(ComponentInstance componentInstance,
93 ArtifactGroupTypeEnum artifactGroupType, ArtifactTypeEnum artifactType, int expectedNumber) {
94 Map<String, ArtifactDefinition> deploymentArtifacts = null;
96 if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
97 deploymentArtifacts = componentInstance.getDeploymentArtifacts();
99 if (deploymentArtifacts != null) {
100 for (ArtifactDefinition artifactDefinition : deploymentArtifacts.values()) {
101 if (artifactDefinition.getArtifactType().equals(artifactType.getType())) {
106 assertEquals("Unexpected number of " + artifactGroupType.getType() + " artifacts in component", expectedNumber,
110 public static ESArtifactData parseArtifactRespFromES(RestResponse resResponse) throws Exception {
111 String bodyToParse = resResponse.getResponse();
112 JsonElement jElement = new JsonParser().parse(bodyToParse);
113 JsonElement jsourceElement = jElement.getAsJsonObject().get("_source");
115 ObjectMapper mapper = new ObjectMapper();
117 return mapper.readValue(jsourceElement.toString(), ESArtifactData.class);
121 public static void validateArtifactReqVsResp(ArtifactReqDetails expectedArtifactDetails,
122 ArtifactDefinition actualArtifactJavaObject) {
125 expected = expectedArtifactDetails.getArtifactName();
126 if (expected == null)
128 assertEquals("artifact name is not correct ", expected, actualArtifactJavaObject.getArtifactName());
130 expected = expectedArtifactDetails.getArtifactType();
131 if (expected == null)
133 assertEquals("artifact type is not correct ", expected, actualArtifactJavaObject.getArtifactType());
135 expected = expectedArtifactDetails.getDescription();
136 if (expected == null)
138 assertEquals("artifact description is not correct ", expected, actualArtifactJavaObject.getDescription());
140 expected = expectedArtifactDetails.getArtifactLabel();
141 if (expected == null || expected == "") {
142 expected = expectedArtifactDetails.getArtifactName().toLowerCase().substring(0,
143 expectedArtifactDetails.getArtifactName().lastIndexOf("."));
144 // expected = tmp.substring(0,
145 // artifactInfo.getArtifactName().lastIndexOf("."));
147 assertEquals("artifact label is not correct ", expected, actualArtifactJavaObject.getArtifactLabel());
149 expected = expectedArtifactDetails.getUrl();
150 if (expected != "") {
151 assertEquals(expected, actualArtifactJavaObject.getApiUrl());
152 assertEquals(expectedArtifactDetails.getArtifactDisplayName(),
153 actualArtifactJavaObject.getArtifactDisplayName());
156 // assertEquals(validChecksum,
157 // actualArtifactJavaObject.getArtifactChecksum());
159 // expected = expectedArtifactDetails.getArtifactDisplayName();
160 // if (expected != "")
162 // assertEquals(expected,
163 // actualArtifactJavaObject.getArtifactDisplayName());
166 boolean actual = actualArtifactJavaObject.getMandatory();
167 assertEquals(expectedArtifactDetails.isMandatory(), actual);
169 if (actualArtifactJavaObject.getServiceApi()) {
171 boolean actual2 = actualArtifactJavaObject.getServiceApi();
172 assertEquals(expectedArtifactDetails.isServiceApi(), actual2);
177 public static void validateEsArtifactReqVsResp(ArtifactReqDetails expectedArtifactInfo,
178 ESArtifactData esArtifactData) throws Exception {
179 String expectedArtifactUid = expectedArtifactInfo.getUniqueId();
180 if (expectedArtifactUid == null)
181 expectedArtifactUid = "";
182 assertEquals("artifact name is not correct ", expectedArtifactUid, esArtifactData.getId());
184 String actualPayload = Decoder.encode(esArtifactData.getData().array());
185 assertEquals("artifact payloadData is not correct ", expectedArtifactInfo.getPayload(), actualPayload);
188 public static List<String> getListOfArtifactFromFolder(String folderName) throws IOException, Exception {
189 Config config = Utils.getConfig();
190 String sourceDir = config.getResourceConfigDir();
191 String testResourcesPath = sourceDir + File.separator + folderName;
192 List<String> listofFiles = FileUtils.getFileListFromBaseDirectoryByTestName(testResourcesPath);
196 public static ArtifactReqDetails replaceDefaultArtWithArtFromList_(ArtifactReqDetails heatArtifactDetails,
197 String heatExtension, String folderName, int positionInlist) throws IOException, Exception {
199 Config config = Utils.getConfig();
200 String ext = heatExtension;
201 String sourceDir = config.getResourceConfigDir();
202 String testResourcesPath = sourceDir + File.separator + folderName;
203 List<String> listFileName = FileUtils.getFileListFromBaseDirectoryByTestName(testResourcesPath);
204 String payload = FileUtils.loadPayloadFile(listFileName, ext, true);
205 heatArtifactDetails.setPayload(payload);
206 heatArtifactDetails.setArtifactName(listFileName.get(positionInlist) + "." + ext);
207 return heatArtifactDetails;
210 public static ArtifactReqDetails replaceDefaultArtWithArtFromList(ArtifactReqDetails heatArtifactDetails,
211 String heatExtension, String folderName, int positionInlist) throws IOException, Exception {
212 List<String> listOfArtifactFromFolder = getListOfArtifactFromFolder(folderName);
213 String payload = FileUtils.loadPayloadFileFromListUsingPosition(listOfArtifactFromFolder, heatExtension, true,
215 heatArtifactDetails.setPayload(payload);
216 heatArtifactDetails.setArtifactName(heatArtifactDetails.getArtifactType()
217 + listOfArtifactFromFolder.get(positionInlist) + "." + heatExtension);
218 return heatArtifactDetails;