Update vulnerable package dependencies
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / frontend / ci / tests / execute / AmdocsComplexService / PathValidations.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.onap.sdc.frontend.ci.tests.execute.AmdocsComplexService;
22
23 import com.aventstack.extentreports.Status;
24 import com.clearspring.analytics.util.Pair;
25 import org.apache.commons.lang3.RandomStringUtils;
26 import org.json.JSONObject;
27 import org.onap.sdc.frontend.ci.tests.datatypes.DataTestIdEnum;
28 import org.onap.sdc.backend.ci.tests.datatypes.HeatMetaFirstLevelDefinition;
29 import org.onap.sdc.backend.ci.tests.datatypes.ServiceReqDetails;
30 import org.onap.sdc.backend.ci.tests.datatypes.VendorSoftwareProductObject;
31 import org.onap.sdc.backend.ci.tests.datatypes.http.RestResponse;
32 import org.onap.sdc.backend.ci.tests.execute.devCI.ArtifactFromCsar;
33 import org.onap.sdc.frontend.ci.tests.pages.DeploymentArtifactPage;
34 import org.onap.sdc.frontend.ci.tests.utilities.DownloadManager;
35 import org.onap.sdc.frontend.ci.tests.utilities.FileHandling;
36 import org.onap.sdc.frontend.ci.tests.utilities.GeneralUIUtils;
37 import org.onap.sdc.frontend.ci.tests.utilities.OnboardingUiUtils;
38 import org.onap.sdc.frontend.ci.tests.execute.setup.ArtifactsCorrelationManager;
39 import org.onap.sdc.frontend.ci.tests.execute.setup.ExtentTestActions;
40 import org.onap.sdc.frontend.ci.tests.execute.setup.SetupCDTest;
41 import org.onap.sdc.frontend.ci.tests.pages.ResourceGeneralPage;
42 import org.openqa.selenium.WebElement;
43
44 import java.io.File;
45 import java.util.*;
46 import java.util.stream.Collectors;
47
48 import static org.testng.AssertJUnit.assertEquals;
49 import static org.testng.AssertJUnit.assertNotSame;
50
51
52 public class PathValidations {
53
54
55     public static final int NUMBER_OF_LINKS = 5;
56     public static final int NUMBER_OF_LINES = 3;
57     public static final int NUMBER_OF_LINES_TO_DELETE = 1;
58
59     public static String[] validateServiceExtendedPath(String vspName) throws Exception {
60         PathUtilities.linkVFs(vspName, NUMBER_OF_LINKS);
61         PathUtilities.openCreatePath();
62         String pathName = "name1";
63         PathUtilities.insertValues(pathName, "pathProtocol1", "pathPortNumbers1");
64         PathUtilities.selectFirstLineParam();
65         PathValidations.extendPath(NUMBER_OF_LINES);
66         //delete line
67         PathUtilities.deleteLines(NUMBER_OF_LINES_TO_DELETE, NUMBER_OF_LINES);
68         GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ComplexServiceAmdocs.CREATE_BUTTON.getValue());
69         ExtentTestActions.log(Status.INFO, "path has been created");
70         PathValidations.checkPathFilter(pathName, true);
71         GeneralUIUtils.findElementsByXpath("//*[@data-tests-id='" + DataTestIdEnum.ComplexServiceAmdocs.PATH_MENU_BUTTON.getValue() + "']/parent::*").get(0).click();
72         GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ComplexServiceAmdocs.PATH_LIST_BUTTON.getValue());
73         String PathListName1 = GeneralUIUtils.findByText(pathName).getText();
74         // edit path
75         String newPathName = "name2";
76         PathUtilities.editPathName(pathName, newPathName);
77
78         GeneralUIUtils.findElementsByXpath("//*[@data-tests-id='" + DataTestIdEnum.ComplexServiceAmdocs.PATH_MENU_BUTTON.getValue() + "']/parent::*").get(0).click();
79         GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ComplexServiceAmdocs.PATH_LIST_BUTTON.getValue());
80         String PathListName2 = GeneralUIUtils.findByText(newPathName).getText();
81
82         // assert names changed
83         assertNotSame("path name expected to change after edit but did not", PathListName1, PathListName2);
84
85         // delete path
86         int paths_before_deletion = GeneralUIUtils.findElementsByXpath("//*[text()='" + newPathName + "']/parent::*//span").size();
87         GeneralUIUtils.findElementsByXpath(newPathName).get(1).click();
88         GeneralUIUtils.ultimateWait();
89         int paths_after_deletion = GeneralUIUtils.findElementsByXpath("//*[text()='" + newPathName + "']/parent::*//span").size();
90         assertNotSame("path expected to be deleted but did not", paths_after_deletion, paths_before_deletion);
91
92         GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.PropertiesAssignmentScreen.INPUT_DELETE_DIALOG_CLOSE.getValue());
93         SetupCDTest.getExtendTest().log(Status.INFO, "Path has been created");
94         // check that path got deleted in the path filter list
95         PathValidations.checkPathFilter(newPathName, false);
96         return new String[]{pathName, newPathName};
97     }
98
99     public static String[] validateComplexExtendedPath(String[] services) throws Exception {
100         PathUtilities.linkServices(services[0], services[1], NUMBER_OF_LINKS);
101         PathUtilities.openCreatePath();
102         String pathName = "name1";
103         PathUtilities.insertValues(pathName, "pathProtocol1", "pathPortNumbers");
104         PathUtilities.selectFirstLineParam();
105         PathValidations.extendPath(NUMBER_OF_LINES);
106         //delete line
107         PathUtilities.deleteLines(NUMBER_OF_LINES_TO_DELETE, NUMBER_OF_LINES);
108         GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ComplexServiceAmdocs.CREATE_BUTTON.getValue());
109         ExtentTestActions.log(Status.INFO, "path has been created");
110         PathUtilities.openPathList();
111         String PathListName1 = GeneralUIUtils.findByText(pathName).getText();
112         String newPathName = "name2";
113         PathUtilities.editPathName(pathName, newPathName);
114         GeneralUIUtils.findElementsByXpath("//*[@data-tests-id='" + DataTestIdEnum.ComplexServiceAmdocs.PATH_MENU_BUTTON.getValue() + "']/parent::*").get(0).click();
115         GeneralUIUtils.ultimateWait();
116         GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ComplexServiceAmdocs.PATH_LIST_BUTTON.getValue());
117         String PathListName2 = GeneralUIUtils.findByText(newPathName).getText();
118
119         // assert names changed
120         assertNotSame("path name expected to change after edit but did not", PathListName1, PathListName2);
121
122         // delete path
123         int paths_before_deletion = GeneralUIUtils.findElementsByXpath("//*[text()='" + newPathName + "']/parent::*//span").size();
124         GeneralUIUtils.findElementsByXpath("//*[text()='" + newPathName + "']/parent::*//span").get(1).click();
125         GeneralUIUtils.ultimateWait();
126
127         int paths_after_deletion = GeneralUIUtils.findElementsByXpath("//*[text()='" + newPathName + "']/parent::*//span").size();
128         assertNotSame("path expected to be deleted but did not", paths_after_deletion, paths_before_deletion);
129
130         GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.PropertiesAssignmentScreen.INPUT_DELETE_DIALOG_CLOSE.getValue());
131         PathValidations.validateServicePath(services[2], pathName);
132         PathValidations.validateServicePath(services[2], newPathName);
133         SetupCDTest.getExtendTest().log(Status.INFO, "Paths have been validated");
134         return new String[]{pathName, newPathName};
135     }
136
137     public static void createPathNumOfRows(int numOfPathRows) throws Exception {
138         final int numberOfRandomCharacters = 8;
139         PathUtilities.openCreatePath();
140         PathUtilities.insertValues("extended" + RandomStringUtils.randomAlphanumeric(numberOfRandomCharacters), "pathProtocol1", "pathPortNumbers1");
141         PathUtilities.selectFirstLineParam();
142         extendPath(numOfPathRows);
143         GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ComplexServiceAmdocs.CREATE_BUTTON.getValue());
144         SetupCDTest.getExtendTest().log(Status.INFO, "Path with " + numOfPathRows + " rows has been created");
145     }
146
147     public static void extendPath(int numOfLines) throws Exception {
148         for (int i = 0; i < numOfLines; i++) {
149             String check;
150             String index = Integer.toString(i + 2);
151             GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ComplexServiceAmdocs.EXTEND_BUTTON.getValue());
152
153             List<WebElement> linkSrcs = GeneralUIUtils.findElementsByXpath("//*[@data-tests-id='" + DataTestIdEnum.ComplexServiceAmdocs.LINK_SOURCE.getValue() + "']//select");
154             List<WebElement> linkSrcCPs = GeneralUIUtils.findElementsByXpath("//*[@data-tests-id='" + DataTestIdEnum.ComplexServiceAmdocs.LINK_SOURCE_CP.getValue() + "']//select");
155             List<WebElement> linkTargets = GeneralUIUtils.findElementsByXpath("//*[@data-tests-id='" + DataTestIdEnum.ComplexServiceAmdocs.LINK_TARGET.getValue() + "']//select");
156             List<WebElement> linkTargetCPs = GeneralUIUtils.findElementsByXpath("//*[@data-tests-id='" + DataTestIdEnum.ComplexServiceAmdocs.LINK_TARGET_CP.getValue() + "']//select");
157             for (int j = 0; j < i + 2; j++) {
158                 validateExtendedPathDisabledButtons(linkSrcs, j, "Source should be disabled. open bug to UI team");
159                 check = linkSrcCPs.get(j).getAttribute("class");
160                 validateElementDisabledAttribute(check, "Source connection point");
161             }
162             for (int j = 0; j < i + 1; j++) {
163                 check = linkTargets.get(j).getAttribute("class");
164                 validateElementDisabledAttribute(check, "Target");
165                 check = linkTargetCPs.get(j).getAttribute("class");
166                 validateElementDisabledAttribute(check, "Target connection point");
167             }
168             List<WebElement> choices = GeneralUIUtils.findElementsByXpath("//*[" + index + "]/*[@data-tests-id='" + DataTestIdEnum.ComplexServiceAmdocs.LINK_TARGET.getValue() + "']//option");
169             choices.get((new Random()).nextInt(choices.size())).click();
170             choices = GeneralUIUtils.findElementsByXpath("//*[" + index + "]/*[@data-tests-id='" + DataTestIdEnum.ComplexServiceAmdocs.LINK_TARGET_CP.getValue() + "']//option");
171             choices.get((new Random()).nextInt(choices.size())).click();
172         }
173     }
174
175     public static void validateElementDisabledAttribute(String check, String param) throws Exception {
176         assertEquals(param + " of last lines should be disabled", check.contains("disabled"), true);
177     }
178
179     public static void validateExtendedPathDisabledButtons(List<WebElement> linkSrcs, int i, String errMsg) throws Exception {
180         String check = linkSrcs.get(i).getAttribute("class");
181         assertEquals(errMsg, check.contains("disabled"), true);
182     }
183
184     public static void validatePathListIsEmpty() throws Exception {
185         PathUtilities.openPathList();
186         try {
187             GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ComplexServiceAmdocs.PATH_LIST_DELETE.getValue());
188         } catch (Exception e) {
189             SetupCDTest.getExtendTest().log(Status.INFO, "path list is empty");
190         }
191         GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ComplexServiceAmdocs.CLOSE.getValue());
192     }
193
194     public static void AssertNameChangeFromPathList(String pathListName1, String pathListName2) throws Exception {
195         assertNotSame("path name is expected to change after edit", pathListName1, pathListName2);
196     }
197
198     public static void ValidateAndDeletePathFromPathList(String pathName) throws Exception {
199         // count paths
200         List<WebElement> path_list_name = GeneralUIUtils.getWebElementsListByTestID(DataTestIdEnum.ComplexServiceAmdocs.PATH_LIST_NAME.getValue());
201         // delete paths
202         List<WebElement> path_delete = GeneralUIUtils.getWebElementsListByTestID(DataTestIdEnum.ComplexServiceAmdocs.PATH_LIST_DELETE.getValue());
203         int paths_before_deletion = 0;
204         for (int i = 0; i < path_list_name.size(); i++) {
205             if (path_list_name.get(i).getText().equals(pathName)) {
206                 paths_before_deletion++;
207                 path_delete.get(i).click();
208             }
209         }
210         int paths_after_deletion = 0;
211         List<WebElement> path_list_name2;
212         try {
213             path_list_name2 = GeneralUIUtils.getWebElementsListByTestID(DataTestIdEnum.ComplexServiceAmdocs.PATH_LIST_NAME.getValue());
214             for (WebElement path_list_element : path_list_name2) {
215                 if (path_list_element.getText().equals(pathName)) {
216                     paths_after_deletion++;
217                 }
218             }
219         } catch (Exception ignore) {
220         }
221         // assert deletion
222         assertNotSame("path is expected to be deleted", paths_after_deletion, paths_before_deletion);
223     }
224
225     public static void checkPathFilter(String pathName, boolean isFound) throws Exception {
226         List<WebElement> pathFilterList = GeneralUIUtils.findElementsByXpath("//*[@data-tests-id='" + DataTestIdEnum.ComplexServiceAmdocs.SERVICE_PATH_SELECTOR.getValue() + "']//option");
227         GeneralUIUtils.ultimateWait();
228         if (isFound) {
229             for (int i = 0; i < pathFilterList.size(); i++) {
230                 String element_text = pathFilterList.get(i).getText();
231                 if (element_text.equals(pathName)) {
232                     break;
233                 }
234                 assertNotSame("path filter list is missing a path", i, pathFilterList.size() - 1);
235             }
236         } else {
237             for (WebElement aPathFilterList : pathFilterList) {
238                 String element_text = aPathFilterList.getText();
239                 assertNotSame("path filter list is has a path that should be deleted", element_text, pathName);
240             }
241         }
242         SetupCDTest.getExtendTest().log(Status.INFO, "path list filter check passed");
243     }
244
245     public static void AssertCreatePath() throws Exception {
246         String check = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ComplexServiceAmdocs.CREATE_BUTTON.getValue()).getAttribute("disabled");
247         assertEquals("create button path should be disabled", check, "true");
248     }
249
250     public static void AssertCreatedPathExistInCompositionPage(String pathName) throws Exception {
251         checkPathFilter(pathName, true);
252         PathUtilities.openPathList();
253
254         // assert path is in the list
255         GeneralUIUtils.findByText(pathName).getText();
256         GeneralUIUtils.ultimateWait();
257         GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.PropertiesAssignmentScreen.INPUT_DELETE_DIALOG_CLOSE.getValue());
258 //        GeneralUIUtils.ultimateWait();
259         SetupCDTest.getExtendTest().log(Status.INFO, "Path is in the path list");
260 //        GeneralUIUtils.ultimateWait();
261     }
262
263     public static void AssertExtendPath() throws Exception {
264         String check = GeneralUIUtils.getWebElementsListByTestID(DataTestIdEnum.ComplexServiceAmdocs.EXTEND_BUTTON.getValue()).get(0).getAttribute("class");
265         assertEquals("Extend button should be disabled", check, "disabled");
266     }
267
268     public static Pair<RestResponse, ServiceReqDetails> validateServicePath(String serviceName, String name) throws Exception {
269         Pair<RestResponse, ServiceReqDetails> servicePaths = PathUtilities.getServicePathsAPI(serviceName);
270         String response = servicePaths.left.getResponse();
271
272         JSONObject jsonResponse = new JSONObject(response);
273         JSONObject forwardingPaths = jsonResponse.getJSONObject("forwardingPaths");
274         Boolean validation_complete = Boolean.FALSE;
275         for (Iterator it = forwardingPaths.keys(); it.hasNext(); ) {
276             Object key = it.next();
277             String keyStr = (String) key;
278             JSONObject forwardingPath = forwardingPaths.getJSONObject(keyStr);
279             if (forwardingPath.getString("name").equals(name)) {
280                 JSONObject pathElements = forwardingPath.getJSONObject("pathElements");
281                 Boolean empty = pathElements.getBoolean("empty");
282                 assertEquals("assert pathElements are not empty", empty, Boolean.FALSE);
283                 validation_complete = Boolean.TRUE;
284                 break;
285             }
286         }
287         assertEquals("validation success", validation_complete, Boolean.TRUE);
288         return servicePaths;
289     }
290
291     public static void ValidateThereIsNoErrorMessage() throws Exception {
292         try {
293             GeneralUIUtils.findElementsByXpath("//*[@data-tests-id='" + DataTestIdEnum.ComplexServiceAmdocs.OK.getValue() + "']");
294             throw new Exception("element should not be found");
295         } catch (Exception e) {
296             if (e.getMessage().equals("element should not be found")) {
297                 throw e;
298             }
299         }
300     }
301
302     public static void validateEditToExistingName(String firstPathName, String secondPathName) throws Exception {
303         PathUtilities.openPathList();
304         PathUtilities.editPathName(secondPathName, firstPathName);
305         try {
306             GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ComplexServiceAmdocs.OK.getValue());
307         } catch (Exception e) {
308             throw new Exception("when creating another path with duplicate name, expected error did not appear");
309         }
310         PathUtilities.openPathList();
311         PathUtilities.editPathName(secondPathName, firstPathName + "       ");
312         try {
313             GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ComplexServiceAmdocs.OK.getValue());
314         } catch (Exception e) {
315             throw new Exception("space in beggining or end does not count. when creating another path with duplicate name, expected error did not appear");
316         }
317         PathUtilities.openPathList();
318         PathUtilities.editPathName(secondPathName, "           " + firstPathName);
319         try {
320             GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ComplexServiceAmdocs.OK.getValue());
321         } catch (Exception e) {
322             throw new Exception("space in beggining or end does not count. when creating another path with duplicate name, expected error did not appear");
323         }
324         PathUtilities.openPathList();
325         PathUtilities.editPathProtocol(secondPathName, "pathProtocol2");
326     }
327
328     public static void validateNameWithSpaces(String pathName, String vspName) throws Exception {
329         PathUtilities.createPath(pathName + "           ", vspName);
330         PathUtilities.openPathList();
331         PathUtilities.editPathName(pathName, "newName");
332         PathUtilities.createPathWithoutLink("               " + pathName, vspName);
333         PathUtilities.openPathList();
334         PathUtilities.editPathName(pathName, "newName2");
335     }
336
337     public static void importAndVerifyVSP(VendorSoftwareProductObject createVendorSoftwareProduct, String filepath, String vnfFile) throws Exception {
338         DownloadManager.downloadCsarByNameFromVSPRepository(createVendorSoftwareProduct.getName(), createVendorSoftwareProduct.getVspId());
339         File latestFilefromDir = FileHandling.getLastModifiedFileNameFromDir();
340
341         OnboardingUiUtils.importVSP(createVendorSoftwareProduct);
342
343         ResourceGeneralPage.getLeftMenu().moveToDeploymentArtifactScreen();
344
345         // Verify deployment artifacts
346         Map<String, Object> combinedMap = ArtifactFromCsar.combineHeatArtifacstWithFolderArtifacsToMap(latestFilefromDir.getAbsolutePath());
347
348         LinkedList<HeatMetaFirstLevelDefinition> deploymentArtifacts = ((LinkedList<HeatMetaFirstLevelDefinition>) combinedMap.get("Deployment"));
349         ArtifactsCorrelationManager.addVNFartifactDetails(vnfFile, deploymentArtifacts);
350
351         List<String> heatEnvFilesFromCSAR = deploymentArtifacts.stream().filter(e -> e.getType().equals("HEAT_ENV")).
352                 map(e -> e.getFileName()).
353                 collect(Collectors.toList());
354
355         OnboardingUiUtils.validateDeploymentArtifactsVersion(deploymentArtifacts, heatEnvFilesFromCSAR);
356
357         DeploymentArtifactPage.verifyArtifactsExistInTable(filepath, vnfFile);
358     }
359
360 }