1bb095fdaa9115168b960cb2f23f04c1481025c1
[vid.git] / vid-automation / src / main / java / vid / automation / test / sections / InstantiationStatusPage.java
1 package vid.automation.test.sections;
2
3 import org.apache.http.NameValuePair;
4 import org.apache.http.client.utils.URLEncodedUtils;
5 import org.onap.sdc.ci.tests.utilities.GeneralUIUtils;
6 import org.openqa.selenium.By;
7 import org.openqa.selenium.WebElement;
8 import org.openqa.selenium.support.ui.ExpectedConditions;
9 import org.openqa.selenium.support.ui.WebDriverWait;
10 import vid.automation.test.Constants;
11 import vid.automation.test.infra.Click;
12 import vid.automation.test.infra.Get;
13 import vid.automation.test.infra.Wait;
14
15 import java.nio.charset.Charset;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.function.Consumer;
19 import java.util.stream.Collectors;
20
21 import static java.util.stream.Collectors.toMap;
22 import static org.hamcrest.CoreMatchers.containsString;
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.testng.Assert.assertEquals;
26
27 public abstract class InstantiationStatusPage extends VidBasePage {
28
29     public static final String refreshButtonId = "refresh-btn";
30
31     private static final String NEW_VIEW_EDIT_RELATIVE_URL = "serviceModels.htm#/servicePlanning";
32
33
34     public static String getWebTrTdSpanElementByParentID(WebElement tr, String id, int timeout) {
35             return tr.findElements(By.xpath(".//*[@id='" + id + "']//span")).get(0).getText();
36     }
37
38     public static int getNumberOfTableRows(int timeout){
39         WebDriverWait wait = waitUntilDriverIsReady(timeout);
40         return wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[table]//tbody/tr"))).size();
41     }
42
43     public static WebElement assertInstantiationStatusRow(String spanIdSelector, Map<String, String> fieldsIdsAndExpected) {
44         try {
45             WebElement newTrRow = getInstantiationStatusRow(spanIdSelector);
46             final Map<String, String> fieldIdAndActual = fieldsIdsAndExpected.entrySet().stream()
47                     .collect(Collectors.toMap(
48                             kv -> kv.getKey(),
49                             kv -> getWebTrTdSpanElementByParentID(newTrRow, kv.getKey(), 1)
50                     ));
51
52             assertThat("failed comparing spanIdSelector " + spanIdSelector, fieldIdAndActual, is(fieldsIdsAndExpected));
53
54             return newTrRow;
55         } catch (Exception e) {
56             throw new RuntimeException("error while assertInstantiationStatusRow with: String spanIdSelector=" +
57                     spanIdSelector + ", fieldsIdsAndExpected=" + fieldsIdsAndExpected, e);
58         }
59     }
60
61     public static WebElement getInstantiationStatusRow(String spanIdSelector) {
62         GeneralUIUtils.ultimateWait();
63         return Get.byXpath("//*[@id='" + spanIdSelector + "']/parent::*/parent::*/parent::*", 0);
64     }
65
66     public static void clickRefreshButton() {
67         WebDriverWait wait = waitUntilDriverIsReady(0);
68         wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='"+ refreshButtonId + "']"))).click();
69         GeneralUIUtils.ultimateWait();
70     }
71
72     public static void checkMenuItem(String actualInstanceName, String contextMenuItem, boolean shouldBeEnabled, Consumer<String> doIfEnabled) {
73         Wait.waitFor(name -> {
74             if (null == getInstantiationStatusRow(name)) {
75                 clickRefreshButton();
76                 return false;
77             } else {
78                 return true;
79             }
80         }, actualInstanceName, 8, 1);
81         final WebElement row = getInstantiationStatusRow(actualInstanceName);
82         row.findElement(By.className("menu-div")).click();
83         String clazz = Get.byXpath("//div[@data-tests-id='" + contextMenuItem + "']/ancestor::li").getAttribute("class");
84         assertThat("item " + contextMenuItem + " of " + actualInstanceName +
85                 " should be " + (shouldBeEnabled ? "enabled" : "disabled"), !clazz.equals("disabled"), is(shouldBeEnabled));
86         if (shouldBeEnabled) {
87             doIfEnabled.accept(contextMenuItem);
88         } else {
89             // dismiss menu
90             Get.byTestId("instantiation-status-title").click();
91         }
92     }
93
94     public void showTooltipByHoverAboveStatusIcon(String elementTestId){
95         WebElement selectedElement = GeneralUIUtils.getWebElementByTestID(elementTestId, 30);
96     }
97
98     public static void openDrawingBoardForRetry(String serviceInstanceName) {
99         InstantiationStatusPage.checkMenuItem(serviceInstanceName, Constants.InstantiationStatus.CONTEXT_MENU_RETRY, true, contextMenuRetry -> {
100             Click.byTestId(contextMenuRetry);
101             VidBasePage.goOutFromIframe();
102             verifyUrlPrefixMatchNewViewEdit("RETRY_EDIT");
103         });
104     }
105
106     public static void verifyOpenNewViewEdit(String serviceInstanceName, String serviceInstanceId, String serviceModelId, String serviceType, String subscriberId, String mode) {
107         InstantiationStatusPage.checkMenuItem(serviceInstanceName, Constants.InstantiationStatus.CONTEXT_MENU_HEADER_OPEN_ITEM, true, contextMenuOpen -> {
108             Click.byTestId(contextMenuOpen);
109             VidBasePage.goOutFromIframe();
110             verifyUrlMatchNewViewEdit(serviceInstanceId, serviceModelId, serviceType, subscriberId, mode);
111             SideMenu.navigateToMacroInstantiationStatus();
112         });
113     }
114
115     public static void verifyOpenNewViewEdit(String serviceInstanceName, boolean openShouldBeEnabled, String expectedMode) {
116         InstantiationStatusPage.checkMenuItem(serviceInstanceName, Constants.InstantiationStatus.CONTEXT_MENU_HEADER_OPEN_ITEM, openShouldBeEnabled, contextMenuOpen -> {
117             Click.byTestId(contextMenuOpen);
118             VidBasePage.goOutFromIframe();
119             verifyUrlPrefixMatchNewViewEdit(expectedMode);
120             SideMenu.navigateToMacroInstantiationStatus();
121         });
122     }
123
124     public static void verifyUrlMatchNewViewEdit(String serviceInstanceId, String serviceModelId, String serviceType, String subscriberId, String expectedMode) {
125         verifyUrlPrefixMatchNewViewEdit(expectedMode);
126         Map<String, String> paramsMap = extractQueryParamsFromCurrentURL(NEW_VIEW_EDIT_RELATIVE_URL + "/" + expectedMode + "?");
127         //assertEquals(paramsMap.get("mode"), expectedMode);
128         assertEquals(paramsMap.get("serviceInstanceId"), serviceInstanceId);
129         assertEquals(paramsMap.get("serviceModelId"), serviceModelId);
130         assertEquals(paramsMap.get("serviceType"), serviceType);
131         assertEquals(paramsMap.get("subscriberId"), subscriberId);
132     }
133
134     protected static Map<String, String> extractQueryParamsFromCurrentURL(String relativePath) {
135         String currentUrl = GeneralUIUtils.getDriver().getCurrentUrl();
136         //unfortunately parse(final URI uri, final String charset) can't handle with the #/ part of the uri
137         String urlSuffix = currentUrl.substring(currentUrl.indexOf(relativePath)+relativePath.length());
138         List<NameValuePair> params = URLEncodedUtils.parse(urlSuffix, Charset.forName("UTF-8"));
139         return params.stream().collect(toMap(NameValuePair::getName, NameValuePair::getValue));
140     }
141
142     public static void verifyUrlPrefixMatchNewViewEdit(String expectedMode) {
143         String currentUrl = GeneralUIUtils.getDriver().getCurrentUrl();
144         assertThat(currentUrl, containsString(NEW_VIEW_EDIT_RELATIVE_URL  + "/" + expectedMode));
145     }
146 }