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.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
27 import java.io.IOException;
28 import java.util.List;
31 import org.codehaus.jackson.JsonParseException;
32 import org.codehaus.jackson.JsonProcessingException;
33 import org.codehaus.jackson.map.ObjectMapper;
34 import org.openecomp.sdc.be.model.ArtifactDefinition;
35 import org.openecomp.sdc.be.model.Component;
36 import org.openecomp.sdc.be.model.ComponentInstance;
37 import org.openecomp.sdc.be.resources.data.ESArtifactData;
38 import org.openecomp.sdc.ci.tests.config.Config;
39 import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
40 import org.openecomp.sdc.ci.tests.datatypes.enums.ArtifactTypeEnum;
41 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
42 import org.openecomp.sdc.ci.tests.utils.Decoder;
43 import org.openecomp.sdc.ci.tests.utils.Utils;
44 import org.openecomp.sdc.ci.tests.utils.general.FileUtils;
45 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
47 import com.google.gson.JsonElement;
48 import com.google.gson.JsonParser;
50 public class ArtifactValidationUtils {
52 private static String desc = "description";
53 private static String artifactType = "artifactType";
54 private static String artifactName = "artifactName";
55 private static String artifactChecksum = "artifactChecksum";
56 private static String uniqueId = "uniqueId";
57 protected Utils utils;
59 public static void validateInformationalArtifact(ArtifactReqDetails expectedArtifact,
60 Map<String, Object> actualArtifact) {
61 assertTrue("description is not as expected",
62 expectedArtifact.getDescription().equals(actualArtifact.get(desc).toString()));
63 assertTrue("artifactType is not as expected",
64 expectedArtifact.getArtifactType().toUpperCase().equals(actualArtifact.get(artifactType).toString()));
65 assertTrue("artifactName is not as expected",
66 expectedArtifact.getArtifactName().equals(actualArtifact.get(artifactName).toString()));
67 assertTrue("uniqueId is not as expected",
68 expectedArtifact.getUniqueId().equals(actualArtifact.get(uniqueId).toString()));
69 assertTrue("description is not as expected", expectedArtifact.getArtifactLabel().toLowerCase()
70 .equals(actualArtifact.get("artifactLabel").toString()));
73 public static void validateArtifactsNumberInComponent(Component component, ArtifactGroupTypeEnum artifactGroupType,
74 ArtifactTypeEnum artifactType, int expectedNumber) {
75 Map<String, ArtifactDefinition> deploymentArtifacts;
77 if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
78 deploymentArtifacts = component.getDeploymentArtifacts();
80 deploymentArtifacts = component.getArtifacts();
82 if (deploymentArtifacts != null) {
83 for (ArtifactDefinition artifactDefinition : deploymentArtifacts.values()) {
84 if (artifactDefinition.getArtifactType().equals(artifactType.getType())) {
89 assertEquals("Unexpected number of " + artifactGroupType.getType() + " artifacts in component", expectedNumber,
94 public static void validateArtifactsNumberInComponentInstance(ComponentInstance componentInstance,
95 ArtifactGroupTypeEnum artifactGroupType, ArtifactTypeEnum artifactType, int expectedNumber) {
96 Map<String, ArtifactDefinition> deploymentArtifacts = null;
98 if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
99 deploymentArtifacts = componentInstance.getDeploymentArtifacts();
101 if (deploymentArtifacts != null) {
102 for (ArtifactDefinition artifactDefinition : deploymentArtifacts.values()) {
103 if (artifactDefinition.getArtifactType().equals(artifactType.getType())) {
108 assertEquals("Unexpected number of " + artifactGroupType.getType() + " artifacts in component", expectedNumber,
112 public static ESArtifactData parseArtifactRespFromES(RestResponse resResponse)
113 throws JsonParseException, JsonProcessingException, Exception {
114 String bodyToParse = resResponse.getResponse();
115 JsonElement jElement = new JsonParser().parse(bodyToParse);
116 JsonElement jsourceElement = jElement.getAsJsonObject().get("_source");
118 ObjectMapper mapper = new ObjectMapper();
119 ESArtifactData esArtifactObject = mapper.readValue(jsourceElement.toString(), ESArtifactData.class);
121 return esArtifactObject;
124 public static void validateArtifactReqVsResp(ArtifactReqDetails expectedArtifactDetails,
125 ArtifactDefinition actualArtifactJavaObject) {
128 expected = expectedArtifactDetails.getArtifactName();
129 if (expected == null)
131 assertEquals("artifact name is not correct ", expected, actualArtifactJavaObject.getArtifactName());
133 expected = expectedArtifactDetails.getArtifactType();
134 if (expected == null)
136 assertEquals("artifact type is not correct ", expected, actualArtifactJavaObject.getArtifactType());
138 expected = expectedArtifactDetails.getDescription();
139 if (expected == null)
141 assertEquals("artifact description is not correct ", expected, actualArtifactJavaObject.getDescription());
143 expected = expectedArtifactDetails.getArtifactLabel();
144 if (expected == null || expected == "") {
145 expected = expectedArtifactDetails.getArtifactName().toLowerCase().substring(0,
146 expectedArtifactDetails.getArtifactName().lastIndexOf("."));
147 // expected = tmp.substring(0,
148 // artifactInfo.getArtifactName().lastIndexOf("."));
150 assertEquals("artifact label is not correct ", expected, actualArtifactJavaObject.getArtifactLabel());
152 expected = expectedArtifactDetails.getUrl();
153 if (expected != "") {
154 assertEquals(expected, actualArtifactJavaObject.getApiUrl());
155 assertEquals(expectedArtifactDetails.getArtifactDisplayName(),
156 actualArtifactJavaObject.getArtifactDisplayName());
159 // assertEquals(validChecksum,
160 // actualArtifactJavaObject.getArtifactChecksum());
162 // expected = expectedArtifactDetails.getArtifactDisplayName();
163 // if (expected != "")
165 // assertEquals(expected,
166 // actualArtifactJavaObject.getArtifactDisplayName());
169 boolean actual = actualArtifactJavaObject.getMandatory();
170 assertEquals(expectedArtifactDetails.isMandatory(), actual);
172 if (actualArtifactJavaObject.getServiceApi()) {
174 boolean actual2 = actualArtifactJavaObject.getServiceApi();
175 assertEquals(expectedArtifactDetails.isServiceApi(), actual2);
180 public static void validateEsArtifactReqVsResp(ArtifactReqDetails expectedArtifactInfo,
181 ESArtifactData esArtifactData) throws Exception {
182 String expectedArtifactUid = expectedArtifactInfo.getUniqueId();
183 if (expectedArtifactUid == null)
184 expectedArtifactUid = "";
185 assertEquals("artifact name is not correct ", expectedArtifactUid, esArtifactData.getId());
187 String actualPayload = Decoder.encode(esArtifactData.getData().array());
188 assertEquals("artifact payloadData is not correct ", expectedArtifactInfo.getPayload(), actualPayload);
191 public static List<String> getListOfArtifactFromFolder(String folderName) throws IOException, Exception {
192 Config config = Utils.getConfig();
193 String sourceDir = config.getResourceConfigDir();
194 String testResourcesPath = sourceDir + File.separator + folderName;
195 List<String> listofFiles = FileUtils.getFileListFromBaseDirectoryByTestName(testResourcesPath);
199 public static ArtifactReqDetails replaceDefaultArtWithArtFromList_(ArtifactReqDetails heatArtifactDetails,
200 String heatExtension, String folderName, int positionInlist) throws IOException, Exception {
202 Config config = Utils.getConfig();
203 String ext = heatExtension;
204 String sourceDir = config.getResourceConfigDir();
205 String testResourcesPath = sourceDir + File.separator + folderName;
206 List<String> listFileName = FileUtils.getFileListFromBaseDirectoryByTestName(testResourcesPath);
207 String payload = FileUtils.loadPayloadFile(listFileName, ext, true);
208 heatArtifactDetails.setPayload(payload);
209 heatArtifactDetails.setArtifactName(listFileName.get(positionInlist) + "." + ext);
210 return heatArtifactDetails;
213 public static ArtifactReqDetails replaceDefaultArtWithArtFromList(ArtifactReqDetails heatArtifactDetails,
214 String heatExtension, String folderName, int positionInlist) throws IOException, Exception {
215 List<String> listOfArtifactFromFolder = getListOfArtifactFromFolder(folderName);
216 String payload = FileUtils.loadPayloadFileFromListUsingPosition(listOfArtifactFromFolder, heatExtension, true,
218 heatArtifactDetails.setPayload(payload);
219 heatArtifactDetails.setArtifactName(heatArtifactDetails.getArtifactType()
220 + listOfArtifactFromFolder.get(positionInlist) + "." + heatExtension);
221 return heatArtifactDetails;