re base code
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / utilities / ArtifactUIUtils.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.utilities;
22
23 import com.aventstack.extentreports.Status;
24 import org.openecomp.sdc.be.datatypes.elements.HeatParameterDataDefinition;
25 import org.openecomp.sdc.ci.tests.datatypes.ArtifactInfo;
26 import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum;
27 import org.openecomp.sdc.ci.tests.datatypes.HeatWithParametersDefinition;
28 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
29 import org.openecomp.sdc.ci.tests.pages.CompositionPage;
30 import org.openecomp.sdc.ci.tests.pages.InformationalArtifactPage;
31 import org.openecomp.sdc.ci.tests.pages.UploadArtifactPopup;
32 import org.openqa.selenium.By;
33 import org.openqa.selenium.WebElement;
34 import org.testng.Assert;
35
36 import java.io.File;
37 import java.io.IOException;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41
42 import static org.junit.Assert.assertTrue;
43
44 public final class ArtifactUIUtils {
45
46     private static final String PARAMETERS = "parameters";
47     private static final String DATA_TESTS_ID = "//*[@data-tests-id='";
48
49     private ArtifactUIUtils() {
50     }
51
52     public static void fillAndAddNewArtifactParameters(ArtifactInfo artifactInfo) throws Exception {
53         UploadArtifactPopup artifactPopup = new UploadArtifactPopup();
54         fillAndAddNewArtifactParameters(artifactInfo, artifactPopup);
55     }
56
57     public static void fillAndAddNewArtifactParameters(ArtifactInfo artifactInfo, UploadArtifactPopup artifactPopup) throws Exception {
58         artifactPopup.defineArtifactLabel(artifactInfo.getArtifactLabel());
59         artifactPopup.selectArtifactType(artifactInfo.getArtifactType());
60         artifactPopup.insertDescription(artifactInfo.getDescription());
61         artifactPopup.loadFile(artifactInfo.getFilepath(), artifactInfo.getFilename());
62         artifactPopup.clickDoneButton();
63         SetupCDTest.getExtendTest().log(Status.INFO, String.format("A new artifact of type %s was added", artifactInfo.getArtifactType()));
64     }
65
66     public static void fillAndAddNewEnvArtifactParameters(ArtifactInfo artifactInfo, UploadArtifactPopup artifactPopup) throws Exception {
67         artifactPopup.insertDescription(artifactInfo.getDescription());
68         artifactPopup.loadFile(artifactInfo.getFilepath(), artifactInfo.getFilename());
69         artifactPopup.clickDoneButton();
70         SetupCDTest.getExtendTest().log(Status.INFO, String.format("ENV parameters %s artifact updated ", artifactInfo.getArtifactType()));
71     }
72
73     public static void fillPlaceHolderInformationalArtifact(DataTestIdEnum.InformationalArtifactsPlaceholders artifactLabel, String filepath, String filename, String description) throws Exception {
74         GeneralUIUtils.clickOnElementByTestId(artifactLabel.getValue());
75         InformationalArtifactPage.artifactPopup().loadFile(filepath, filename);
76         InformationalArtifactPage.artifactPopup().insertDescription(description);
77         InformationalArtifactPage.artifactPopup().clickDoneButton();
78     }
79
80     public static void fillPlaceHolderInformationalArtifact(DataTestIdEnum.InformationalArtifactsService artifactLabel,
81                                                             String filepath, String filename, String description) throws Exception {
82         GeneralUIUtils.clickOnElementByTestId(artifactLabel.getValue());
83         UploadArtifactPopup artifactPopup = new UploadArtifactPopup(true);
84         artifactPopup.loadFile(filepath, filename);
85         artifactPopup.insertDescription(description);
86         artifactPopup.clickDoneButton();
87     }
88
89     public static void fillPlaceHolderAPIArtifact(DataTestIdEnum.APIArtifactsService artifactLabel,
90                                                   String filepath, String filename, String description, String url) throws Exception {
91         GeneralUIUtils.clickOnElementByTestId(artifactLabel.getValue());
92         UploadArtifactPopup artifactPopup = new UploadArtifactPopup(true);
93         artifactPopup.loadFile(filepath, filename);
94         artifactPopup.insertURL(url);
95         artifactPopup.insertDescription(description);
96         artifactPopup.clickDoneButton();
97     }
98
99     public static void validateArtifactNameVersionType(String artifactLabel, String artifactVersion, String artifactType) {
100         if (!GeneralUIUtils.getDriver().findElement(By.xpath(DATA_TESTS_ID + DataTestIdEnum.ArtifactPageEnum.ARTIFACT_NAME.getValue() + artifactLabel + "']")).getAttribute("textContent").trim().equals(artifactLabel)) {
101             SetupCDTest.getExtendTest().log(Status.WARNING, "Artifact label not equal - this warning represent defect.");
102         }
103         if (artifactVersion != null) {
104             if (!GeneralUIUtils.getDriver().findElement(By.xpath(DATA_TESTS_ID + DataTestIdEnum.ArtifactPageEnum.VERSION.getValue() + artifactLabel + "']")).getAttribute("textContent").trim().equals(artifactVersion)) {
105                 SetupCDTest.getExtendTest().log(Status.WARNING, "Artifact version not equal - this warning represent defect.");
106             }
107         }
108         if (artifactType != null) {
109             if (!GeneralUIUtils.getDriver().findElement(By.xpath(DATA_TESTS_ID + DataTestIdEnum.ArtifactPageEnum.TYPE.getValue() + artifactLabel + "']")).getAttribute("textContent").trim().equals(artifactType)) {
110                 SetupCDTest.getExtendTest().log(Status.WARNING, "Artifact type not equal - this warning represent defect.");
111             }
112         }
113     }
114
115     public static void validateExistArtifactOnDeploymentInformationPage(String expectedArtifactLabel, String artifactUUID, String artifactVersion, String artifactType, boolean isDownloadable, boolean isEditable, boolean isDeletable, boolean isArtifactParametersEditable) {
116
117         String dataTestId = DataTestIdEnum.ArtifactPageEnum.ARTIFACT_NAME.getValue() + expectedArtifactLabel;
118
119         List<WebElement> artifactElements = GeneralUIUtils.getWebElementsListByContainTestID(dataTestId);
120         Assert.assertEquals(artifactElements.size(), 1, "There are more then one artifact named " + expectedArtifactLabel);
121
122         WebElement artifact = artifactElements.get(0);
123         String actualArtifactLabel = GeneralUIUtils.getTextContentAttributeValue(artifact).trim();
124         Assert.assertEquals(actualArtifactLabel, expectedArtifactLabel);
125
126         if (artifactUUID != null) {
127             WebElement uuid = GeneralUIUtils.getInputElement(DataTestIdEnum.ArtifactPageEnum.UUID.getValue() + expectedArtifactLabel);
128             Assert.assertEquals(GeneralUIUtils.getTextContentAttributeValue(uuid).trim(), artifactUUID, "Artifact uuid not equal.");
129         }
130         if (artifactVersion != null) {
131             WebElement version = GeneralUIUtils.getInputElement(DataTestIdEnum.ArtifactPageEnum.VERSION.getValue() + expectedArtifactLabel);
132             Assert.assertEquals(GeneralUIUtils.getTextContentAttributeValue(version).trim(), artifactVersion, "Artifact version not equal.");
133         }
134         if (artifactType != null) {
135             WebElement type = GeneralUIUtils.getInputElement(DataTestIdEnum.ArtifactPageEnum.TYPE.getValue() + expectedArtifactLabel);
136             Assert.assertEquals(GeneralUIUtils.getTextContentAttributeValue(type).trim(), artifactType, "Artifact type not equal.");
137         }
138         if (isArtifactParametersEditable) {
139             Assert.assertNotNull(GeneralUIUtils.getInputElement(DataTestIdEnum.ArtifactPageEnum.EDIT_PARAMETERS_OF_ARTIFACT.getValue() + expectedArtifactLabel), "Expect that parameters edit button enabled.");
140         } else {
141             Assert.assertNull(GeneralUIUtils.getInputElement(DataTestIdEnum.ArtifactPageEnum.EDIT_PARAMETERS_OF_ARTIFACT.getValue() + expectedArtifactLabel), "Expect that parameters edit button disabled.");
142         }
143         if (isDownloadable) {
144             Assert.assertNotNull(GeneralUIUtils.getInputElement(DataTestIdEnum.ArtifactPageEnum.DOWNLOAD_ARTIFACT.getValue() + expectedArtifactLabel), "Expect that download button enabled.");
145         } else {
146             Assert.assertNull(GeneralUIUtils.getInputElement(DataTestIdEnum.ArtifactPageEnum.DOWNLOAD_ARTIFACT.getValue() + expectedArtifactLabel), "Expect that download button disabled.");
147         }
148         if (isEditable) {
149             Assert.assertNotNull(GeneralUIUtils.getInputElement(DataTestIdEnum.ArtifactPageEnum.EDIT_ARTIFACT.getValue() + expectedArtifactLabel), "Expect that edit button enabled.");
150         } else {
151             Assert.assertNull(GeneralUIUtils.getInputElement(DataTestIdEnum.ArtifactPageEnum.EDIT_ARTIFACT.getValue() + expectedArtifactLabel), "Expect that edit button disabled.");
152         }
153         if (isDeletable) {
154             Assert.assertNotNull(GeneralUIUtils.getInputElement(DataTestIdEnum.ArtifactPageEnum.DELETE_ARTIFACT.getValue() + expectedArtifactLabel), "Expect that delete button enabled.");
155         } else {
156             Assert.assertNull(GeneralUIUtils.getInputElement(DataTestIdEnum.ArtifactPageEnum.DELETE_ARTIFACT.getValue() + expectedArtifactLabel), "Expect that delete button disabled.");
157         }
158     }
159
160     public static void validateNotExistArtifactOnDeploymentInformationPage(String artifactLabel) {
161         Assert.assertEquals(GeneralUIUtils.isWebElementExistByTestId(DataTestIdEnum.ArtifactPageEnum.ARTIFACT_NAME.getValue() + artifactLabel), false);
162     }
163
164     public static void validateExistArtifactOnCompositionRightMenuDeploymentInformationPage(String fileName, String artifactDisplayedName,
165                                                                                             boolean isUpdateable, boolean isParametersEditable, boolean isDownloadable, boolean isDeleteable) {
166         Assert.assertEquals(GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.DeploymentArtifactCompositionRightMenu.ARTIFACT_NAME.getValue() + artifactDisplayedName).getText(), fileName);
167         Assert.assertEquals(GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.DeploymentArtifactCompositionRightMenu.ARTIFACT_DISPLAY_NAME.getValue() + artifactDisplayedName).getText(), artifactDisplayedName);
168
169         GeneralUIUtils.hoverOnAreaByTestId(DataTestIdEnum.DeploymentArtifactCompositionRightMenu.ARTIFACT_DISPLAY_NAME.getValue() + artifactDisplayedName);
170
171         if (isParametersEditable) {
172             Assert.assertEquals(GeneralUIUtils.isWebElementExistByTestId(DataTestIdEnum.DeploymentArtifactCompositionRightMenu.EDIT_PARAMETERS_OF_ARTIFACT.getValue() + artifactDisplayedName), true, "Expect that parameters edit button enabled.");
173         } else {
174             Assert.assertEquals(GeneralUIUtils.isWebElementExistByTestId(DataTestIdEnum.DeploymentArtifactCompositionRightMenu.EDIT_PARAMETERS_OF_ARTIFACT.getValue() + artifactDisplayedName), false, "Expect that parameters edit button disabled.");
175         }
176         if (isDownloadable) {
177             Assert.assertEquals(GeneralUIUtils.isWebElementExistByTestId(DataTestIdEnum.DeploymentArtifactCompositionRightMenu.DOWNLOAD.getValue() + artifactDisplayedName), true, "Expect that download button enabled.");
178         } else {
179             Assert.assertEquals(GeneralUIUtils.isWebElementExistByTestId(DataTestIdEnum.DeploymentArtifactCompositionRightMenu.DOWNLOAD.getValue() + artifactDisplayedName), false, "Expect that download button disabled.");
180         }
181         if (isDeleteable) {
182             Assert.assertEquals(GeneralUIUtils.isWebElementExistByTestId(DataTestIdEnum.DeploymentArtifactCompositionRightMenu.DELETE.getValue() + artifactDisplayedName), true, "Expect that delete button enabled.");
183         } else {
184             Assert.assertEquals(GeneralUIUtils.isWebElementExistByTestId(DataTestIdEnum.DeploymentArtifactCompositionRightMenu.DELETE.getValue() + artifactDisplayedName), false, "Expect that delete button disabled.");
185         }
186         if (isUpdateable) {
187             GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.DeploymentArtifactCompositionRightMenu.ARTIFACT_DISPLAY_NAME.getValue() + artifactDisplayedName);
188             Assert.assertEquals(GeneralUIUtils.isWebElementExistByTestId(DataTestIdEnum.ArtifactPopup.MODAL_WINDOW.getValue()), true, "Expect that edit button enabled.");
189             GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ArtifactPopup.DONE_BUTTON.getValue());
190             GeneralUIUtils.waitForElementInVisibilityByTestId(DataTestIdEnum.ArtifactPopup.DONE_BUTTON.getValue());
191         } else {
192             GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.DeploymentArtifactCompositionRightMenu.ARTIFACT_DISPLAY_NAME.getValue() + artifactDisplayedName);
193             Assert.assertEquals(GeneralUIUtils.isWebElementExistByTestId(DataTestIdEnum.ArtifactPopup.MODAL_WINDOW.getValue()), false, "Expect that edit button disabled.");
194         }
195
196     }
197
198     public static void validateNotExistArtifactOnCompositionRightMenuDeploymentInformationPage(String artifactDisplayedName) {
199         Assert.assertEquals(GeneralUIUtils.isWebElementExistByTestId(DataTestIdEnum.DeploymentArtifactCompositionRightMenu.ARTIFACT_NAME.getValue() + artifactDisplayedName), false);
200     }
201
202     public static File verifyUpdatedEnvParameters(HeatWithParametersDefinition pairToUpdate, File updateEnvFile, String dataTestId) throws Exception {
203         GeneralUIUtils.hoverOnAreaByTestId(dataTestId);
204         return verifyUpdatedEnvParameters(pairToUpdate, updateEnvFile);
205     }
206
207     public static File verifyUpdatedEnvParameters(HeatWithParametersDefinition pairToUpdate, File updateEnvFile) throws Exception {
208
209         String heatDisplayName = pairToUpdate.getHeatArtifactDisplayName();
210         GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ArtifactPageEnum.DOWNLOAD_ARTIFACT_ENV.getValue() + heatDisplayName);
211         File latestFilefromDir = FileHandling.getLastModifiedFileNameFromDir();
212
213         String pattern = PARAMETERS;
214         Map<String, Object> mapUpdetedEnvFile = FileHandling.parseYamlFileToMapByPattern(updateEnvFile, pattern);
215         Map<String, Object> mapDownloadedEnvFile = FileHandling.parseYamlFileToMapByPattern(latestFilefromDir, pattern);
216
217         SetupCDTest.getExtendTest().log(Status.INFO, "Going to check, that ENV file was updated ...");
218         assertTrue("File" + latestFilefromDir.getName() + " contains different parameters number from expected file", mapDownloadedEnvFile.size() == mapUpdetedEnvFile.size());
219         assertTrue("Updated file contains not updated parameters value", mapDownloadedEnvFile.entrySet().containsAll(mapUpdetedEnvFile.entrySet()));
220         return latestFilefromDir;
221     }
222
223     /**
224      * compare heat env files by pattern ("parameters")
225      *
226      * @param expectedFile
227      * @param actualFile
228      * @param pattern
229      * @throws Exception
230      */
231     public static void compareYamlFilesByPattern(File expectedFile, File actualFile, String pattern) throws Exception {
232
233         Map<String, Object> mapExpectedFile = FileHandling.parseYamlFileToMapByPattern(expectedFile, pattern);
234         Map<String, Object> mapActualFile = FileHandling.parseYamlFileToMapByPattern(actualFile, pattern);
235         SetupCDTest.getExtendTest().log(Status.INFO, "Going to compare file " + expectedFile.getName());
236         assertTrue("File " + actualFile.getName() + " contains different parameters number from expected file", mapActualFile.size() == mapExpectedFile.size());
237         assertTrue("File " + actualFile.getName() + " does not contains all expected parameters", mapActualFile.entrySet().containsAll(mapExpectedFile.entrySet()));
238     }
239
240     public static void compareYamlParametersByPattern(Map<String, Object> mapExpectedProperties, File actualFileProperties, String pattern) throws Exception {
241
242         Map<String, Object> mapActualFileProerties = FileHandling.parseYamlFileToMapByPattern(actualFileProperties, pattern);
243         SetupCDTest.getExtendTest().log(Status.INFO, "Going to compare files ...");
244         assertTrue("Actual file contains different parameters number from expected file", mapActualFileProerties.size() == mapExpectedProperties.size());
245         Map<String, Object> newMap = new HashMap<>(mapActualFileProerties);
246         assertTrue("Actual file does not contains all expected parameters", newMap.entrySet().containsAll(mapExpectedProperties.entrySet()));
247     }
248
249
250     public static File uploadCreatedUpdateParametersEnvFile(HeatWithParametersDefinition heatEnvDetails, String directoryPath) throws Exception {
251 //              created env file to upload
252         File pathToEnvParametersFile = prepareEnvParametersFile(heatEnvDetails, directoryPath);
253         ArtifactInfo heatEnvArtifactInfo = new ArtifactInfo(directoryPath, heatEnvDetails.getHeatEnvLabel() + ".env", "heatEnvDesc", heatEnvDetails.getHeatEnvLabel(), heatEnvDetails.getHeatEnvArtifactType());
254         ArtifactUIUtils.fillAndAddNewEnvArtifactParameters(heatEnvArtifactInfo, CompositionPage.artifactPopup());
255         return pathToEnvParametersFile;
256     }
257
258     public static File prepareEnvParametersFile(HeatWithParametersDefinition heatEnvDetails, String directoryPath) throws IOException {
259         File pathToEnvParametersFile = FileHandling.createEmptyFile(directoryPath + heatEnvDetails.getHeatEnvLabel() + ".env");
260 //              fill file
261         FileHandling.writeToFile(pathToEnvParametersFile, "parameters:", 0);
262         for (HeatParameterDataDefinition paramDefinition : heatEnvDetails.getHeatParameterDefinition()) {
263             Object data = getDataToWrite(paramDefinition);
264             FileHandling.writeToFile(pathToEnvParametersFile, data, 2);
265         }
266
267         return pathToEnvParametersFile;
268     }
269
270     public static Object getDataToWrite(HeatParameterDataDefinition paramDefinition) {
271         Object data = "";
272         switch (paramDefinition.getType()) {
273             case "string":
274                 String text = "\"string\"";
275                 data = getFormatedData(paramDefinition.getName(), text);
276                 break;
277             case "number":
278                 data = getFormatedData(paramDefinition.getName(), 666);
279                 break;
280             case "json":
281                 String jsonText = "{\"param1\":\"param1\", \"param2\":2}";
282                 data = getFormatedData(paramDefinition.getName(), jsonText);
283                 break;
284             case "boolean":
285                 if (paramDefinition.getCurrentValue().equals("true")) {
286                     data = getFormatedData(paramDefinition.getName(), false);
287                 } else {
288                     data = getFormatedData(paramDefinition.getName(), true);
289                 }
290                 break;
291             case "comma_delimited_list":
292                 String commaDelimitedListText = "127.0.0.10, 127.0.0.15, 127.0.0.20";
293                 data = getFormatedData(paramDefinition.getName(), commaDelimitedListText);
294                 break;
295             default:
296                 break;
297         }
298         return data;
299     }
300
301
302     public static Map<String, Object> getDataToWriteInUI(List<HeatParameterDataDefinition> paramDefinitionFromGetResourceResponse) {
303         Map<String, Object> newValuesToUpdateInUI = new HashMap<>();
304         for (HeatParameterDataDefinition param : paramDefinitionFromGetResourceResponse) {
305             switch (param.getType()) {
306
307                 case "string":
308                     String text = "string";
309                     newValuesToUpdateInUI.put(param.getName(), text);
310                     break;
311                 case "number":
312                     newValuesToUpdateInUI.put(param.getName(), 666);
313                     break;
314                 case "json":
315                     String jsonText = "{\"param1\":\"param1\", \"param2\":2}";
316                     newValuesToUpdateInUI.put(param.getName(), jsonText);
317                     break;
318                 case "boolean":
319                     if (param.getCurrentValue().equals(true)) {
320                         newValuesToUpdateInUI.put(param.getName(), false);
321                     } else {
322                         newValuesToUpdateInUI.put(param.getName(), true);
323                     }
324                     break;
325                 case "comma_delimited_list":
326                     String commaDelimitedListText = "127.0.0.10, 127.0.0.15, 127.0.0.20";
327                     newValuesToUpdateInUI.put(param.getName(), commaDelimitedListText);
328                     break;
329                 default:
330                     break;
331
332             }
333
334         }
335         return newValuesToUpdateInUI;
336     }
337
338     public static Object getValue(HeatParameterDataDefinition param) {
339         String type = param.getType();
340         Object result = null;
341         switch (type) {
342             case "string":
343                 result = param.getCurrentValue();
344                 break;
345             case "number":
346                 result = new Integer(param.getCurrentValue());
347                 break;
348             case "json":
349                 result = param.getCurrentValue();
350                 break;
351             case "boolean":
352                 result = new Boolean(param.getCurrentValue());
353                 break;
354             case "comma_delimited_list":
355                 result = param.getCurrentValue();
356                 break;
357             default:
358                 break;
359         }
360         return result;
361     }
362
363     public static Object getFormatedData(String name, Object text) {
364         return name + ": " + text;
365     }
366
367
368 }