Sync Integ to Master
[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.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;
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 import static org.testng.AssertJUnit.assertTrue;
47
48 public class ArtifactValidationUtils {
49
50         private static String desc = "description";
51         private static String artifactType = "artifactType";
52         private static String artifactName = "artifactName";
53         private static String artifactChecksum = "artifactChecksum";
54         private static String uniqueId = "uniqueId";
55         protected Utils utils;
56
57         public static void validateInformationalArtifact(ArtifactReqDetails expectedArtifact,
58                         Map<String, Object> actualArtifact) {
59                 assertTrue("description is not as expected",
60                                 expectedArtifact.getDescription().equals(actualArtifact.get(desc).toString()));
61                 assertTrue("artifactType is not as expected",
62                                 expectedArtifact.getArtifactType().toUpperCase().equals(actualArtifact.get(artifactType).toString()));
63                 assertTrue("artifactName is not as expected",
64                                 expectedArtifact.getArtifactName().equals(actualArtifact.get(artifactName).toString()));
65                 assertTrue("uniqueId is not as expected",
66                                 expectedArtifact.getUniqueId().equals(actualArtifact.get(uniqueId).toString()));
67                 assertTrue("description is not as expected", expectedArtifact.getArtifactLabel().toLowerCase()
68                                 .equals(actualArtifact.get("artifactLabel").toString()));
69         }
70
71         public static void validateArtifactsNumberInComponent(Component component, ArtifactGroupTypeEnum artifactGroupType,
72                         ArtifactTypeEnum artifactType, int expectedNumber) {
73                 Map<String, ArtifactDefinition> deploymentArtifacts;
74                 int counter = 0;
75                 if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
76                         deploymentArtifacts = component.getDeploymentArtifacts();
77                 } else {
78                         deploymentArtifacts = component.getArtifacts();
79                 }
80                 if (deploymentArtifacts != null) {
81                         for (ArtifactDefinition artifactDefinition : deploymentArtifacts.values()) {
82                                 if (artifactDefinition.getArtifactType().equals(artifactType.getType())) {
83                                         counter++;
84                                 }
85                         }
86                 }
87                 assertEquals("Unexpected number of " + artifactGroupType.getType() + " artifacts in component", expectedNumber,
88                                 counter);
89         }
90
91         // Benny
92         public static void validateArtifactsNumberInComponentInstance(ComponentInstance componentInstance,
93                         ArtifactGroupTypeEnum artifactGroupType, ArtifactTypeEnum artifactType, int expectedNumber) {
94                 Map<String, ArtifactDefinition> deploymentArtifacts = null;
95                 int counter = 0;
96                 if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
97                         deploymentArtifacts = componentInstance.getDeploymentArtifacts();
98                 }
99                 if (deploymentArtifacts != null) {
100                         for (ArtifactDefinition artifactDefinition : deploymentArtifacts.values()) {
101                                 if (artifactDefinition.getArtifactType().equals(artifactType.getType())) {
102                                         counter++;
103                                 }
104                         }
105                 }
106                 assertEquals("Unexpected number of " + artifactGroupType.getType() + " artifacts in component", expectedNumber,
107                                 counter);
108         }
109
110         public static ESArtifactData parseArtifactRespFromES(RestResponse resResponse) throws Exception {
111                 String bodyToParse = resResponse.getResponse();
112                 JsonElement jElement = new JsonParser().parse(bodyToParse);
113                 JsonElement jsourceElement = jElement.getAsJsonObject().get("_source");
114
115                 ObjectMapper mapper = new ObjectMapper();
116
117                 return mapper.readValue(jsourceElement.toString(), ESArtifactData.class);
118
119         }
120
121         public static void validateArtifactReqVsResp(ArtifactReqDetails expectedArtifactDetails,
122                         ArtifactDefinition actualArtifactJavaObject) {
123                 String expected;
124
125                 expected = expectedArtifactDetails.getArtifactName();
126                 if (expected == null)
127                         expected = "";
128                 assertEquals("artifact name is not correct ", expected, actualArtifactJavaObject.getArtifactName());
129
130                 expected = expectedArtifactDetails.getArtifactType();
131                 if (expected == null)
132                         expected = "";
133                 assertEquals("artifact type is not correct ", expected, actualArtifactJavaObject.getArtifactType());
134
135                 expected = expectedArtifactDetails.getDescription();
136                 if (expected == null)
137                         expected = "";
138                 assertEquals("artifact description is not correct ", expected, actualArtifactJavaObject.getDescription());
139
140                 expected = expectedArtifactDetails.getArtifactLabel();
141                 if (expected == null || expected == "") {
142                         expected = expectedArtifactDetails.getArtifactName().toLowerCase().substring(0,
143                                         expectedArtifactDetails.getArtifactName().lastIndexOf("."));
144                         // expected = tmp.substring(0,
145                         // artifactInfo.getArtifactName().lastIndexOf("."));
146                 }
147                 assertEquals("artifact label is not correct ", expected, actualArtifactJavaObject.getArtifactLabel());
148
149                 expected = expectedArtifactDetails.getUrl();
150                 if (expected != "") {
151                         assertEquals(expected, actualArtifactJavaObject.getApiUrl());
152                         assertEquals(expectedArtifactDetails.getArtifactDisplayName(),
153                                         actualArtifactJavaObject.getArtifactDisplayName());
154                 }
155
156                 // assertEquals(validChecksum,
157                 // actualArtifactJavaObject.getArtifactChecksum());
158
159                 // expected = expectedArtifactDetails.getArtifactDisplayName();
160                 // if (expected != "")
161                 // {
162                 // assertEquals(expected,
163                 // actualArtifactJavaObject.getArtifactDisplayName());
164                 // }
165
166                 boolean actual = actualArtifactJavaObject.getMandatory();
167                 assertEquals(expectedArtifactDetails.isMandatory(), actual);
168
169                 if (actualArtifactJavaObject.getServiceApi()) {
170
171                         boolean actual2 = actualArtifactJavaObject.getServiceApi();
172                         assertEquals(expectedArtifactDetails.isServiceApi(), actual2);
173                 }
174
175         }
176
177         public static void validateEsArtifactReqVsResp(ArtifactReqDetails expectedArtifactInfo,
178                         ESArtifactData esArtifactData) throws Exception {
179                 String expectedArtifactUid = expectedArtifactInfo.getUniqueId();
180                 if (expectedArtifactUid == null)
181                         expectedArtifactUid = "";
182                 assertEquals("artifact name is not correct ", expectedArtifactUid, esArtifactData.getId());
183
184                 String actualPayload = Decoder.encode(esArtifactData.getData().array());
185                 assertEquals("artifact payloadData is not correct ", expectedArtifactInfo.getPayload(), actualPayload);
186         }
187
188         public static List<String> getListOfArtifactFromFolder(String folderName) throws IOException, Exception {
189                 Config config = Utils.getConfig();
190                 String sourceDir = config.getResourceConfigDir();
191                 String testResourcesPath = sourceDir + File.separator + folderName;
192                 List<String> listofFiles = FileUtils.getFileListFromBaseDirectoryByTestName(testResourcesPath);
193                 return listofFiles;
194         }
195
196         public static ArtifactReqDetails replaceDefaultArtWithArtFromList_(ArtifactReqDetails heatArtifactDetails,
197                         String heatExtension, String folderName, int positionInlist) throws IOException, Exception {
198
199                 Config config = Utils.getConfig();
200                 String ext = heatExtension;
201                 String sourceDir = config.getResourceConfigDir();
202                 String testResourcesPath = sourceDir + File.separator + folderName;
203                 List<String> listFileName = FileUtils.getFileListFromBaseDirectoryByTestName(testResourcesPath);
204                 String payload = FileUtils.loadPayloadFile(listFileName, ext, true);
205                 heatArtifactDetails.setPayload(payload);
206                 heatArtifactDetails.setArtifactName(listFileName.get(positionInlist) + "." + ext);
207                 return heatArtifactDetails;
208         }
209
210         public static ArtifactReqDetails replaceDefaultArtWithArtFromList(ArtifactReqDetails heatArtifactDetails,
211                         String heatExtension, String folderName, int positionInlist) throws IOException, Exception {
212                 List<String> listOfArtifactFromFolder = getListOfArtifactFromFolder(folderName);
213                 String payload = FileUtils.loadPayloadFileFromListUsingPosition(listOfArtifactFromFolder, heatExtension, true,
214                                 positionInlist);
215                 heatArtifactDetails.setPayload(payload);
216                 heatArtifactDetails.setArtifactName(heatArtifactDetails.getArtifactType()
217                                 + listOfArtifactFromFolder.get(positionInlist) + "." + heatExtension);
218                 return heatArtifactDetails;
219         }
220 }