Fix for radio buttons
[sdc.git] / asdc-tests / 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.JsonElement;
48 import com.google.gson.JsonParser;
49
50 public class ArtifactValidationUtils {
51
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;
58
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()));
71         }
72
73         public static void validateArtifactsNumberInComponent(Component component, ArtifactGroupTypeEnum artifactGroupType,
74                         ArtifactTypeEnum artifactType, int expectedNumber) {
75                 Map<String, ArtifactDefinition> deploymentArtifacts;
76                 int counter = 0;
77                 if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
78                         deploymentArtifacts = component.getDeploymentArtifacts();
79                 } else {
80                         deploymentArtifacts = component.getArtifacts();
81                 }
82                 if (deploymentArtifacts != null) {
83                         for (ArtifactDefinition artifactDefinition : deploymentArtifacts.values()) {
84                                 if (artifactDefinition.getArtifactType().equals(artifactType.getType())) {
85                                         counter++;
86                                 }
87                         }
88                 }
89                 assertEquals("Unexpected number of " + artifactGroupType.getType() + " artifacts in component", expectedNumber,
90                                 counter);
91         }
92
93         // Benny
94         public static void validateArtifactsNumberInComponentInstance(ComponentInstance componentInstance,
95                         ArtifactGroupTypeEnum artifactGroupType, ArtifactTypeEnum artifactType, int expectedNumber) {
96                 Map<String, ArtifactDefinition> deploymentArtifacts = null;
97                 int counter = 0;
98                 if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
99                         deploymentArtifacts = componentInstance.getDeploymentArtifacts();
100                 }
101                 if (deploymentArtifacts != null) {
102                         for (ArtifactDefinition artifactDefinition : deploymentArtifacts.values()) {
103                                 if (artifactDefinition.getArtifactType().equals(artifactType.getType())) {
104                                         counter++;
105                                 }
106                         }
107                 }
108                 assertEquals("Unexpected number of " + artifactGroupType.getType() + " artifacts in component", expectedNumber,
109                                 counter);
110         }
111
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");
117
118                 ObjectMapper mapper = new ObjectMapper();
119                 ESArtifactData esArtifactObject = mapper.readValue(jsourceElement.toString(), ESArtifactData.class);
120
121                 return esArtifactObject;
122         }
123
124         public static void validateArtifactReqVsResp(ArtifactReqDetails expectedArtifactDetails,
125                         ArtifactDefinition actualArtifactJavaObject) {
126                 String expected;
127
128                 expected = expectedArtifactDetails.getArtifactName();
129                 if (expected == null)
130                         expected = "";
131                 assertEquals("artifact name is not correct ", expected, actualArtifactJavaObject.getArtifactName());
132
133                 expected = expectedArtifactDetails.getArtifactType();
134                 if (expected == null)
135                         expected = "";
136                 assertEquals("artifact type is not correct ", expected, actualArtifactJavaObject.getArtifactType());
137
138                 expected = expectedArtifactDetails.getDescription();
139                 if (expected == null)
140                         expected = "";
141                 assertEquals("artifact description is not correct ", expected, actualArtifactJavaObject.getDescription());
142
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("."));
149                 }
150                 assertEquals("artifact label is not correct ", expected, actualArtifactJavaObject.getArtifactLabel());
151
152                 expected = expectedArtifactDetails.getUrl();
153                 if (expected != "") {
154                         assertEquals(expected, actualArtifactJavaObject.getApiUrl());
155                         assertEquals(expectedArtifactDetails.getArtifactDisplayName(),
156                                         actualArtifactJavaObject.getArtifactDisplayName());
157                 }
158
159                 // assertEquals(validChecksum,
160                 // actualArtifactJavaObject.getArtifactChecksum());
161
162                 // expected = expectedArtifactDetails.getArtifactDisplayName();
163                 // if (expected != "")
164                 // {
165                 // assertEquals(expected,
166                 // actualArtifactJavaObject.getArtifactDisplayName());
167                 // }
168
169                 boolean actual = actualArtifactJavaObject.getMandatory();
170                 assertEquals(expectedArtifactDetails.isMandatory(), actual);
171
172                 if (actualArtifactJavaObject.getServiceApi()) {
173
174                         boolean actual2 = actualArtifactJavaObject.getServiceApi();
175                         assertEquals(expectedArtifactDetails.isServiceApi(), actual2);
176                 }
177
178         }
179
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());
186
187                 String actualPayload = Decoder.encode(esArtifactData.getData().array());
188                 assertEquals("artifact payloadData is not correct ", expectedArtifactInfo.getPayload(), actualPayload);
189         }
190
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);
196                 return listofFiles;
197         }
198
199         public static ArtifactReqDetails replaceDefaultArtWithArtFromList_(ArtifactReqDetails heatArtifactDetails,
200                         String heatExtension, String folderName, int positionInlist) throws IOException, Exception {
201
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;
211         }
212
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,
217                                 positionInlist);
218                 heatArtifactDetails.setPayload(payload);
219                 heatArtifactDetails.setArtifactName(heatArtifactDetails.getArtifactType()
220                                 + listOfArtifactFromFolder.get(positionInlist) + "." + heatExtension);
221                 return heatArtifactDetails;
222         }
223 }