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