[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / validation / ArtifactValidationUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.utils.validation;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.util.List;
29 import java.util.Map;
30
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;
46
47 import com.google.gson.Gson;
48 import com.google.gson.JsonElement;
49 import com.google.gson.JsonObject;
50 import com.google.gson.JsonParser;
51
52 public class ArtifactValidationUtils {
53
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;
60
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()));
73         }
74
75         public static void validateArtifactsNumberInComponent(Component component, ArtifactGroupTypeEnum artifactGroupType,
76                         ArtifactTypeEnum artifactType, int expectedNumber) {
77                 Map<String, ArtifactDefinition> deploymentArtifacts;
78                 int counter = 0;
79                 if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
80                         deploymentArtifacts = component.getDeploymentArtifacts();
81                 } else {
82                         deploymentArtifacts = component.getArtifacts();
83                 }
84                 if (deploymentArtifacts != null) {
85                         for (ArtifactDefinition artifactDefinition : deploymentArtifacts.values()) {
86                                 if (artifactDefinition.getArtifactType().equals(artifactType.getType())) {
87                                         counter++;
88                                 }
89                         }
90                 }
91                 assertEquals("Unexpected number of " + artifactGroupType.getType() + " artifacts in component", expectedNumber,
92                                 counter);
93         }
94
95         // Benny
96         public static void validateArtifactsNumberInComponentInstance(ComponentInstance componentInstance,
97                         ArtifactGroupTypeEnum artifactGroupType, ArtifactTypeEnum artifactType, int expectedNumber) {
98                 Map<String, ArtifactDefinition> deploymentArtifacts = null;
99                 int counter = 0;
100                 if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
101                         deploymentArtifacts = componentInstance.getDeploymentArtifacts();
102                 }
103                 if (deploymentArtifacts != null) {
104                         for (ArtifactDefinition artifactDefinition : deploymentArtifacts.values()) {
105                                 if (artifactDefinition.getArtifactType().equals(artifactType.getType())) {
106                                         counter++;
107                                 }
108                         }
109                 }
110                 assertEquals("Unexpected number of " + artifactGroupType.getType() + " artifacts in component", expectedNumber,
111                                 counter);
112         }
113
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");
119
120                 ObjectMapper mapper = new ObjectMapper();
121                 ESArtifactData esArtifactObject = mapper.readValue(jsourceElement.toString(), ESArtifactData.class);
122
123                 return esArtifactObject;
124
125         }
126
127         public static void validateArtifactReqVsResp(ArtifactReqDetails expectedArtifactDetails,
128                         ArtifactDefinition actualArtifactJavaObject) {
129                 String expected;
130
131                 expected = expectedArtifactDetails.getArtifactName();
132                 if (expected == null)
133                         expected = "";
134                 assertEquals("artifact name is not correct ", expected, actualArtifactJavaObject.getArtifactName());
135
136                 expected = expectedArtifactDetails.getArtifactType();
137                 if (expected == null)
138                         expected = "";
139                 assertEquals("artifact type is not correct ", expected, actualArtifactJavaObject.getArtifactType());
140
141                 expected = expectedArtifactDetails.getDescription();
142                 if (expected == null)
143                         expected = "";
144                 assertEquals("artifact description is not correct ", expected, actualArtifactJavaObject.getDescription());
145
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("."));
152                 }
153                 assertEquals("artifact label is not correct ", expected, actualArtifactJavaObject.getArtifactLabel());
154
155                 expected = expectedArtifactDetails.getUrl();
156                 if (expected != "") {
157                         assertEquals(expected, actualArtifactJavaObject.getApiUrl());
158                         assertEquals(expectedArtifactDetails.getArtifactDisplayName(),
159                                         actualArtifactJavaObject.getArtifactDisplayName());
160                 }
161
162                 // assertEquals(validChecksum,
163                 // actualArtifactJavaObject.getArtifactChecksum());
164
165                 // expected = expectedArtifactDetails.getArtifactDisplayName();
166                 // if (expected != "")
167                 // {
168                 // assertEquals(expected,
169                 // actualArtifactJavaObject.getArtifactDisplayName());
170                 // }
171
172                 boolean actual = actualArtifactJavaObject.getMandatory();
173                 assertEquals(expectedArtifactDetails.isMandatory(), actual);
174
175                 if (actualArtifactJavaObject.getServiceApi()) {
176
177                         boolean actual2 = actualArtifactJavaObject.getServiceApi();
178                         assertEquals(expectedArtifactDetails.isServiceApi(), actual2);
179                 }
180
181         }
182
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());
189
190                 String actualPayload = Decoder.encode(esArtifactData.getData().array());
191                 assertEquals("artifact payloadData is not correct ", expectedArtifactInfo.getPayload(), actualPayload);
192         }
193
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);
199                 return listofFiles;
200         }
201
202         public static ArtifactReqDetails replaceDefaultArtWithArtFromList_(ArtifactReqDetails heatArtifactDetails,
203                         String heatExtension, String folderName, int positionInlist) throws IOException, Exception {
204
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;
214         }
215
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,
220                                 positionInlist);
221                 heatArtifactDetails.setPayload(payload);
222                 heatArtifactDetails.setArtifactName(heatArtifactDetails.getArtifactType()
223                                 + listOfArtifactFromFolder.get(positionInlist) + "." + heatExtension);
224                 return heatArtifactDetails;
225         }
226 }