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;
47 public class ArtifactValidationUtils {
49 private static String desc = "description";
50 private static String artifactType = "artifactType";
51 private static String artifactName = "artifactName";
52 private static String artifactChecksum = "artifactChecksum";
53 private static String uniqueId = "uniqueId";
54 protected Utils utils;
56 public static void validateInformationalArtifact(ArtifactReqDetails expectedArtifact,
57 Map<String, Object> actualArtifact) {
58 assertTrue("description is not as expected",
59 expectedArtifact.getDescription().equals(actualArtifact.get(desc).toString()));
60 assertTrue("artifactType is not as expected",
61 expectedArtifact.getArtifactType().toUpperCase().equals(actualArtifact.get(artifactType).toString()));
62 assertTrue("artifactName is not as expected",
63 expectedArtifact.getArtifactName().equals(actualArtifact.get(artifactName).toString()));
64 assertTrue("uniqueId is not as expected",
65 expectedArtifact.getUniqueId().equals(actualArtifact.get(uniqueId).toString()));
66 assertTrue("description is not as expected", expectedArtifact.getArtifactLabel().toLowerCase()
67 .equals(actualArtifact.get("artifactLabel").toString()));
70 public static void validateArtifactsNumberInComponent(Component component, ArtifactGroupTypeEnum artifactGroupType,
71 ArtifactTypeEnum artifactType, int expectedNumber) {
72 Map<String, ArtifactDefinition> deploymentArtifacts;
74 if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
75 deploymentArtifacts = component.getDeploymentArtifacts();
77 deploymentArtifacts = component.getArtifacts();
79 if (deploymentArtifacts != null) {
80 for (ArtifactDefinition artifactDefinition : deploymentArtifacts.values()) {
81 if (artifactDefinition.getArtifactType().equals(artifactType.getType())) {
86 assertEquals("Unexpected number of " + artifactGroupType.getType() + " artifacts in component", expectedNumber,
91 public static void validateArtifactsNumberInComponentInstance(ComponentInstance componentInstance,
92 ArtifactGroupTypeEnum artifactGroupType, ArtifactTypeEnum artifactType, int expectedNumber) {
93 Map<String, ArtifactDefinition> deploymentArtifacts = null;
95 if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
96 deploymentArtifacts = componentInstance.getDeploymentArtifacts();
98 if (deploymentArtifacts != null) {
99 for (ArtifactDefinition artifactDefinition : deploymentArtifacts.values()) {
100 if (artifactDefinition.getArtifactType().equals(artifactType.getType())) {
105 assertEquals("Unexpected number of " + artifactGroupType.getType() + " artifacts in component", expectedNumber,
109 public static ESArtifactData parseArtifactRespFromES(RestResponse resResponse) throws Exception {
110 String bodyToParse = resResponse.getResponse();
111 JsonElement jElement = new JsonParser().parse(bodyToParse);
112 JsonElement jsourceElement = jElement.getAsJsonObject().get("_source");
114 ObjectMapper mapper = new ObjectMapper();
116 return mapper.readValue(jsourceElement.toString(), ESArtifactData.class);
120 public static void validateArtifactReqVsResp(ArtifactReqDetails expectedArtifactDetails,
121 ArtifactDefinition actualArtifactJavaObject) {
124 expected = expectedArtifactDetails.getArtifactName();
125 if (expected == null)
127 assertEquals("artifact name is not correct ", expected, actualArtifactJavaObject.getArtifactName());
129 expected = expectedArtifactDetails.getArtifactType();
130 if (expected == null)
132 assertEquals("artifact type is not correct ", expected, actualArtifactJavaObject.getArtifactType());
134 expected = expectedArtifactDetails.getDescription();
135 if (expected == null)
137 assertEquals("artifact description is not correct ", expected, actualArtifactJavaObject.getDescription());
139 expected = expectedArtifactDetails.getArtifactLabel();
140 if (expected == null || expected == "") {
141 expected = expectedArtifactDetails.getArtifactName().toLowerCase().substring(0,
142 expectedArtifactDetails.getArtifactName().lastIndexOf("."));
143 // expected = tmp.substring(0,
144 // artifactInfo.getArtifactName().lastIndexOf("."));
146 assertEquals("artifact label is not correct ", expected, actualArtifactJavaObject.getArtifactLabel());
148 expected = expectedArtifactDetails.getUrl();
149 if (expected != "") {
150 assertEquals(expected, actualArtifactJavaObject.getApiUrl());
151 assertEquals(expectedArtifactDetails.getArtifactDisplayName(),
152 actualArtifactJavaObject.getArtifactDisplayName());
155 // assertEquals(validChecksum,
156 // actualArtifactJavaObject.getArtifactChecksum());
158 // expected = expectedArtifactDetails.getArtifactDisplayName();
159 // if (expected != "")
161 // assertEquals(expected,
162 // actualArtifactJavaObject.getArtifactDisplayName());
165 boolean actual = actualArtifactJavaObject.getMandatory();
166 assertEquals(expectedArtifactDetails.isMandatory(), actual);
168 if (actualArtifactJavaObject.getServiceApi()) {
170 boolean actual2 = actualArtifactJavaObject.getServiceApi();
171 assertEquals(expectedArtifactDetails.isServiceApi(), actual2);
176 public static void validateEsArtifactReqVsResp(ArtifactReqDetails expectedArtifactInfo,
177 ESArtifactData esArtifactData) throws Exception {
178 String expectedArtifactUid = expectedArtifactInfo.getUniqueId();
179 if (expectedArtifactUid == null)
180 expectedArtifactUid = "";
181 assertEquals("artifact name is not correct ", expectedArtifactUid, esArtifactData.getId());
183 String actualPayload = Decoder.encode(esArtifactData.getData().array());
184 assertEquals("artifact payloadData is not correct ", expectedArtifactInfo.getPayload(), actualPayload);
187 public static List<String> getListOfArtifactFromFolder(String folderName) throws IOException, Exception {
188 Config config = Utils.getConfig();
189 String sourceDir = config.getResourceConfigDir();
190 String testResourcesPath = sourceDir + File.separator + folderName;
191 List<String> listofFiles = FileUtils.getFileListFromBaseDirectoryByTestName(testResourcesPath);
195 public static ArtifactReqDetails replaceDefaultArtWithArtFromList_(ArtifactReqDetails heatArtifactDetails,
196 String heatExtension, String folderName, int positionInlist) throws IOException, Exception {
198 Config config = Utils.getConfig();
199 String ext = heatExtension;
200 String sourceDir = config.getResourceConfigDir();
201 String testResourcesPath = sourceDir + File.separator + folderName;
202 List<String> listFileName = FileUtils.getFileListFromBaseDirectoryByTestName(testResourcesPath);
203 String payload = FileUtils.loadPayloadFile(listFileName, ext, true);
204 heatArtifactDetails.setPayload(payload);
205 heatArtifactDetails.setArtifactName(listFileName.get(positionInlist) + "." + ext);
206 return heatArtifactDetails;
209 public static ArtifactReqDetails replaceDefaultArtWithArtFromList(ArtifactReqDetails heatArtifactDetails,
210 String heatExtension, String folderName, int positionInlist) throws IOException, Exception {
211 List<String> listOfArtifactFromFolder = getListOfArtifactFromFolder(folderName);
212 String payload = FileUtils.loadPayloadFileFromListUsingPosition(listOfArtifactFromFolder, heatExtension, true,
214 heatArtifactDetails.setPayload(payload);
215 heatArtifactDetails.setArtifactName(heatArtifactDetails.getArtifactType()
216 + listOfArtifactFromFolder.get(positionInlist) + "." + heatExtension);
217 return heatArtifactDetails;