Catalog alignment
[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 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.DAOArtifactData;
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;
38
39 import java.io.File;
40 import java.io.IOException;
41 import java.util.List;
42 import java.util.Map;
43
44 import static org.testng.AssertJUnit.assertEquals;
45 import static org.testng.AssertJUnit.assertTrue;
46
47 public class ArtifactValidationUtils {
48
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;
55
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()));
68         }
69
70         public static void validateArtifactsNumberInComponent(Component component, ArtifactGroupTypeEnum artifactGroupType,
71                         ArtifactTypeEnum artifactType, int expectedNumber) {
72                 Map<String, ArtifactDefinition> deploymentArtifacts;
73                 int counter = 0;
74                 if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
75                         deploymentArtifacts = component.getDeploymentArtifacts();
76                 } else {
77                         deploymentArtifacts = component.getArtifacts();
78                 }
79                 if (deploymentArtifacts != null) {
80                         for (ArtifactDefinition artifactDefinition : deploymentArtifacts.values()) {
81                                 if (artifactDefinition.getArtifactType().equals(artifactType.getType())) {
82                                         counter++;
83                                 }
84                         }
85                 }
86                 assertEquals("Unexpected number of " + artifactGroupType.getType() + " artifacts in component", expectedNumber,
87                                 counter);
88         }
89
90         // Benny
91         public static void validateArtifactsNumberInComponentInstance(ComponentInstance componentInstance,
92                         ArtifactGroupTypeEnum artifactGroupType, ArtifactTypeEnum artifactType, int expectedNumber) {
93                 Map<String, ArtifactDefinition> deploymentArtifacts = null;
94                 int counter = 0;
95                 if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
96                         deploymentArtifacts = componentInstance.getDeploymentArtifacts();
97                 }
98                 if (deploymentArtifacts != null) {
99                         for (ArtifactDefinition artifactDefinition : deploymentArtifacts.values()) {
100                                 if (artifactDefinition.getArtifactType().equals(artifactType.getType())) {
101                                         counter++;
102                                 }
103                         }
104                 }
105                 assertEquals("Unexpected number of " + artifactGroupType.getType() + " artifacts in component", expectedNumber,
106                                 counter);
107         }
108
109         public static DAOArtifactData parseArtifactRespFromES(RestResponse resResponse) throws Exception {
110                 String bodyToParse = resResponse.getResponse();
111                 JsonElement jElement = new JsonParser().parse(bodyToParse);
112                 JsonElement jsourceElement = jElement.getAsJsonObject().get("_source");
113
114                 ObjectMapper mapper = new ObjectMapper();
115
116                 return mapper.readValue(jsourceElement.toString(), DAOArtifactData.class);
117
118         }
119
120         public static void validateArtifactReqVsResp(ArtifactReqDetails expectedArtifactDetails,
121                         ArtifactDefinition actualArtifactJavaObject) {
122                 String expected;
123
124                 expected = expectedArtifactDetails.getArtifactName();
125                 if (expected == null)
126                         expected = "";
127                 assertEquals("artifact name is not correct ", expected, actualArtifactJavaObject.getArtifactName());
128
129                 expected = expectedArtifactDetails.getArtifactType();
130                 if (expected == null)
131                         expected = "";
132                 assertEquals("artifact type is not correct ", expected, actualArtifactJavaObject.getArtifactType());
133
134                 expected = expectedArtifactDetails.getDescription();
135                 if (expected == null)
136                         expected = "";
137                 assertEquals("artifact description is not correct ", expected, actualArtifactJavaObject.getDescription());
138
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("."));
145                 }
146                 assertEquals("artifact label is not correct ", expected, actualArtifactJavaObject.getArtifactLabel());
147
148                 expected = expectedArtifactDetails.getUrl();
149                 if (expected != "") {
150                         assertEquals(expected, actualArtifactJavaObject.getApiUrl());
151                         assertEquals(expectedArtifactDetails.getArtifactDisplayName(),
152                                         actualArtifactJavaObject.getArtifactDisplayName());
153                 }
154
155                 // assertEquals(validChecksum,
156                 // actualArtifactJavaObject.getArtifactChecksum());
157
158                 // expected = expectedArtifactDetails.getArtifactDisplayName();
159                 // if (expected != "")
160                 // {
161                 // assertEquals(expected,
162                 // actualArtifactJavaObject.getArtifactDisplayName());
163                 // }
164
165                 boolean actual = actualArtifactJavaObject.getMandatory();
166                 assertEquals(expectedArtifactDetails.isMandatory(), actual);
167
168                 if (actualArtifactJavaObject.getServiceApi()) {
169
170                         boolean actual2 = actualArtifactJavaObject.getServiceApi();
171                         assertEquals(expectedArtifactDetails.isServiceApi(), actual2);
172                 }
173
174         }
175
176         public static void validateEsArtifactReqVsResp(ArtifactReqDetails expectedArtifactInfo,
177                         DAOArtifactData DAOArtifactData) throws Exception {
178                 String expectedArtifactUid = expectedArtifactInfo.getUniqueId();
179                 if (expectedArtifactUid == null)
180                         expectedArtifactUid = "";
181                 assertEquals("artifact name is not correct ", expectedArtifactUid, DAOArtifactData.getId());
182
183                 String actualPayload = Decoder.encode(DAOArtifactData.getData().array());
184                 assertEquals("artifact payloadData is not correct ", expectedArtifactInfo.getPayload(), actualPayload);
185         }
186
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);
192                 return listofFiles;
193         }
194
195         public static ArtifactReqDetails replaceDefaultArtWithArtFromList_(ArtifactReqDetails heatArtifactDetails,
196                         String heatExtension, String folderName, int positionInlist) throws IOException, Exception {
197
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;
207         }
208
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,
213                                 positionInlist);
214                 heatArtifactDetails.setPayload(payload);
215                 heatArtifactDetails.setArtifactName(heatArtifactDetails.getArtifactType()
216                                 + listOfArtifactFromFolder.get(positionInlist) + "." + heatExtension);
217                 return heatArtifactDetails;
218         }
219 }