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.Gson;
48 import com.google.gson.JsonElement;
49 import com.google.gson.JsonObject;
50 import com.google.gson.JsonParser;
52 public class ArtifactValidationUtils {
54 private static String desc = "description";
55 private static String artifactType = "artifactType";
56 private static String artifactName = "artifactName";
57 private static String artifactChecksum = "artifactChecksum";
58 private static String uniqueId = "uniqueId";
59 protected Utils utils;
61 public static void validateInformationalArtifact(ArtifactReqDetails expectedArtifact,
62 Map<String, Object> actualArtifact) {
63 assertTrue("description is not as expected",
64 expectedArtifact.getDescription().equals(actualArtifact.get(desc).toString()));
65 assertTrue("artifactType is not as expected",
66 expectedArtifact.getArtifactType().toUpperCase().equals(actualArtifact.get(artifactType).toString()));
67 assertTrue("artifactName is not as expected",
68 expectedArtifact.getArtifactName().equals(actualArtifact.get(artifactName).toString()));
69 assertTrue("uniqueId is not as expected",
70 expectedArtifact.getUniqueId().equals(actualArtifact.get(uniqueId).toString()));
71 assertTrue("description is not as expected", expectedArtifact.getArtifactLabel().toLowerCase()
72 .equals(actualArtifact.get("artifactLabel").toString()));
75 public static void validateArtifactsNumberInComponent(Component component, ArtifactGroupTypeEnum artifactGroupType,
76 ArtifactTypeEnum artifactType, int expectedNumber) {
77 Map<String, ArtifactDefinition> deploymentArtifacts;
79 if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
80 deploymentArtifacts = component.getDeploymentArtifacts();
82 deploymentArtifacts = component.getArtifacts();
84 if (deploymentArtifacts != null) {
85 for (ArtifactDefinition artifactDefinition : deploymentArtifacts.values()) {
86 if (artifactDefinition.getArtifactType().equals(artifactType.getType())) {
91 assertEquals("Unexpected number of " + artifactGroupType.getType() + " artifacts in component", expectedNumber,
96 public static void validateArtifactsNumberInComponentInstance(ComponentInstance componentInstance,
97 ArtifactGroupTypeEnum artifactGroupType, ArtifactTypeEnum artifactType, int expectedNumber) {
98 Map<String, ArtifactDefinition> deploymentArtifacts = null;
100 if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
101 deploymentArtifacts = componentInstance.getDeploymentArtifacts();
103 if (deploymentArtifacts != null) {
104 for (ArtifactDefinition artifactDefinition : deploymentArtifacts.values()) {
105 if (artifactDefinition.getArtifactType().equals(artifactType.getType())) {
110 assertEquals("Unexpected number of " + artifactGroupType.getType() + " artifacts in component", expectedNumber,
114 public static ESArtifactData parseArtifactRespFromES(RestResponse resResponse)
115 throws JsonParseException, JsonProcessingException, Exception {
116 String bodyToParse = resResponse.getResponse();
117 JsonElement jElement = new JsonParser().parse(bodyToParse);
118 JsonElement jsourceElement = jElement.getAsJsonObject().get("_source");
120 ObjectMapper mapper = new ObjectMapper();
121 ESArtifactData esArtifactObject = mapper.readValue(jsourceElement.toString(), ESArtifactData.class);
123 return esArtifactObject;
127 public static void validateArtifactReqVsResp(ArtifactReqDetails expectedArtifactDetails,
128 ArtifactDefinition actualArtifactJavaObject) {
131 expected = expectedArtifactDetails.getArtifactName();
132 if (expected == null)
134 assertEquals("artifact name is not correct ", expected, actualArtifactJavaObject.getArtifactName());
136 expected = expectedArtifactDetails.getArtifactType();
137 if (expected == null)
139 assertEquals("artifact type is not correct ", expected, actualArtifactJavaObject.getArtifactType());
141 expected = expectedArtifactDetails.getDescription();
142 if (expected == null)
144 assertEquals("artifact description is not correct ", expected, actualArtifactJavaObject.getDescription());
146 expected = expectedArtifactDetails.getArtifactLabel();
147 if (expected == null || expected == "") {
148 expected = expectedArtifactDetails.getArtifactName().toLowerCase().substring(0,
149 expectedArtifactDetails.getArtifactName().lastIndexOf("."));
150 // expected = tmp.substring(0,
151 // artifactInfo.getArtifactName().lastIndexOf("."));
153 assertEquals("artifact label is not correct ", expected, actualArtifactJavaObject.getArtifactLabel());
155 expected = expectedArtifactDetails.getUrl();
156 if (expected != "") {
157 assertEquals(expected, actualArtifactJavaObject.getApiUrl());
158 assertEquals(expectedArtifactDetails.getArtifactDisplayName(),
159 actualArtifactJavaObject.getArtifactDisplayName());
162 // assertEquals(validChecksum,
163 // actualArtifactJavaObject.getArtifactChecksum());
165 // expected = expectedArtifactDetails.getArtifactDisplayName();
166 // if (expected != "")
168 // assertEquals(expected,
169 // actualArtifactJavaObject.getArtifactDisplayName());
172 boolean actual = actualArtifactJavaObject.getMandatory();
173 assertEquals(expectedArtifactDetails.isMandatory(), actual);
175 if (actualArtifactJavaObject.getServiceApi()) {
177 boolean actual2 = actualArtifactJavaObject.getServiceApi();
178 assertEquals(expectedArtifactDetails.isServiceApi(), actual2);
183 public static void validateEsArtifactReqVsResp(ArtifactReqDetails expectedArtifactInfo,
184 ESArtifactData esArtifactData) throws Exception {
185 String expectedArtifactUid = expectedArtifactInfo.getUniqueId();
186 if (expectedArtifactUid == null)
187 expectedArtifactUid = "";
188 assertEquals("artifact name is not correct ", expectedArtifactUid, esArtifactData.getId());
190 String actualPayload = Decoder.encode(esArtifactData.getData().array());
191 assertEquals("artifact payloadData is not correct ", expectedArtifactInfo.getPayload(), actualPayload);
194 public static List<String> getListOfArtifactFromFolder(String folderName) throws IOException, Exception {
195 Config config = Utils.getConfig();
196 String sourceDir = config.getResourceConfigDir();
197 String testResourcesPath = sourceDir + File.separator + folderName;
198 List<String> listofFiles = FileUtils.getFileListFromBaseDirectoryByTestName(testResourcesPath);
202 public static ArtifactReqDetails replaceDefaultArtWithArtFromList_(ArtifactReqDetails heatArtifactDetails,
203 String heatExtension, String folderName, int positionInlist) throws IOException, Exception {
205 Config config = Utils.getConfig();
206 String ext = heatExtension;
207 String sourceDir = config.getResourceConfigDir();
208 String testResourcesPath = sourceDir + File.separator + folderName;
209 List<String> listFileName = FileUtils.getFileListFromBaseDirectoryByTestName(testResourcesPath);
210 String payload = FileUtils.loadPayloadFile(listFileName, ext, true);
211 heatArtifactDetails.setPayload(payload);
212 heatArtifactDetails.setArtifactName(listFileName.get(positionInlist) + "." + ext);
213 return heatArtifactDetails;
216 public static ArtifactReqDetails replaceDefaultArtWithArtFromList(ArtifactReqDetails heatArtifactDetails,
217 String heatExtension, String folderName, int positionInlist) throws IOException, Exception {
218 List<String> listOfArtifactFromFolder = getListOfArtifactFromFolder(folderName);
219 String payload = FileUtils.loadPayloadFileFromListUsingPosition(listOfArtifactFromFolder, heatExtension, true,
221 heatArtifactDetails.setPayload(payload);
222 heatArtifactDetails.setArtifactName(heatArtifactDetails.getArtifactType()
223 + listOfArtifactFromFolder.get(positionInlist) + "." + heatExtension);
224 return heatArtifactDetails;