Merge "Change ServiceInstance's top-level rollbackOnFailure serialization to String"
[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 void verifyUrlMatchInstantiationStatusWithFilterSearchParam(String serviceModelId) {
44
45         Map<String, String> paramsMap = extractQueryParamsFromCurrentURL("instantiationStatus?");
46         assertEquals(paramsMap.get("filterText"), serviceModelId);
47
48     }
49
50     public static WebElement assertInstantiationStatusRow(String spanIdSelector, Map<String, String> fieldsIdsAndExpected) {
51         try {
52             WebElement newTrRow = getInstantiationStatusRow(spanIdSelector);
53             final Map<String, String> fieldIdAndActual = fieldsIdsAndExpected.entrySet().stream()
54                     .collect(Collectors.toMap(
55                             kv -> kv.getKey(),
56                             kv -> getWebTrTdSpanElementByParentID(newTrRow, kv.getKey(), 1)
57                     ));
58
59             assertThat("failed comparing spanIdSelector " + spanIdSelector, fieldIdAndActual, is(fieldsIdsAndExpected));
60
61             return newTrRow;
62         } catch (Exception e) {
63             throw new RuntimeException("error while assertInstantiationStatusRow with: String spanIdSelector=" +
64                     spanIdSelector + ", fieldsIdsAndExpected=" + fieldsIdsAndExpected, e);
65         }
66     }
67
68     public static WebElement getInstantiationStatusRow(String spanIdSelector) {
69         GeneralUIUtils.ultimateWait();
70         return Get.byXpath("//*[@id='" + spanIdSelector + "']/parent::*/parent::*/parent::*", 0);
71     }
72
73     public static void clickRefreshButton() {
74         WebDriverWait wait = waitUntilDriverIsReady(0);
75         wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='"+ refreshButtonId + "']"))).click();
76         GeneralUIUtils.ultimateWait();
77     }
78
79     public static void checkMenuItem(String actualInstanceName, String contextMenuItem, boolean shouldBeEnabled, Consumer<String> doIfEnabled) {
80         Wait.waitFor(name -> {
81             if (null == getInstantiationStatusRow(name)) {
82                 clickRefreshButton();
83                 return false;
84             } else {
85                 return true;
86             }
87         }, actualInstanceName, 8, 1);
88         final WebElement row = getInstantiationStatusRow(actualInstanceName);
89         row.findElement(By.className("menu-div")).click();
90         String clazz = Get.byXpath("//div[@data-tests-id='" + contextMenuItem + "']/ancestor::li").getAttribute("class");
91         assertThat("item " + contextMenuItem + " of " + actualInstanceName +
92                 " should be " + (shouldBeEnabled ? "enabled" : "disabled"), !clazz.equals("disabled"), is(shouldBeEnabled));
93         if (shouldBeEnabled) {
94             doIfEnabled.accept(contextMenuItem);
95         } else {
96             // dismiss menu
97             Get.byTestId("instantiation-status-title").click();
98         }
99     }
100
101     public void showTooltipByHoverAboveStatusIcon(String elementTestId){
102         WebElement selectedElement = GeneralUIUtils.getWebElementByTestID(elementTestId, 30);
103     }
104
105     public static void openDrawingBoardForRetry(String serviceInstanceName) {
106         InstantiationStatusPage.checkMenuItem(serviceInstanceName, Constants.InstantiationStatus.CONTEXT_MENU_RETRY, true, contextMenuRetry -> {
107             Click.byTestId(contextMenuRetry);
108             VidBasePage.goOutFromIframe();
109             verifyUrlPrefixMatchNewViewEdit("RETRY_EDIT");
110         });
111     }
112
113     public static void verifyOpenNewViewEdit(String serviceInstanceName, String serviceInstanceId, String serviceModelId, String serviceType, String subscriberId, String mode) {
114         InstantiationStatusPage.checkMenuItem(serviceInstanceName, Constants.InstantiationStatus.CONTEXT_MENU_HEADER_OPEN_ITEM, true, contextMenuOpen -> {
115             Click.byTestId(contextMenuOpen);
116             VidBasePage.goOutFromIframe();
117             verifyUrlMatchNewViewEdit(serviceInstanceId, serviceModelId, serviceType, subscriberId, mode);
118             SideMenu.navigateToMacroInstantiationStatus();
119         });
120     }
121
122     public static void verifyOpenNewViewEdit(String serviceInstanceName, boolean openShouldBeEnabled, String expectedMode) {
123         InstantiationStatusPage.checkMenuItem(serviceInstanceName, Constants.InstantiationStatus.CONTEXT_MENU_HEADER_OPEN_ITEM, openShouldBeEnabled, contextMenuOpen -> {
124             Click.byTestId(contextMenuOpen);
125             VidBasePage.goOutFromIframe();
126             verifyUrlPrefixMatchNewViewEdit(expectedMode);
127             SideMenu.navigateToMacroInstantiationStatus();
128         });
129     }
130
131     public static void verifyUrlMatchNewViewEdit(String serviceInstanceId, String serviceModelId, String serviceType, String subscriberId, String expectedMode) {
132         verifyUrlPrefixMatchNewViewEdit(expectedMode);
133         Map<String, String> paramsMap = extractQueryParamsFromCurrentURL(NEW_VIEW_EDIT_RELATIVE_URL + "/" + expectedMode + "?");
134         //assertEquals(paramsMap.get("mode"), expectedMode);
135         assertEquals(paramsMap.get("serviceInstanceId"), serviceInstanceId);
136         assertEquals(paramsMap.get("serviceModelId"), serviceModelId);
137         assertEquals(paramsMap.get("serviceType"), serviceType);
138         assertEquals(paramsMap.get("subscriberId"), subscriberId);
139     }
140
141     protected static Map<String, String> extractQueryParamsFromCurrentURL(String relativePath) {
142         String currentUrl = GeneralUIUtils.getDriver().getCurrentUrl();
143         //unfortunately parse(final URI uri, final String charset) can't handle with the #/ part of the uri
144         String urlSuffix = currentUrl.substring(currentUrl.indexOf(relativePath)+relativePath.length());
145         List<NameValuePair> params = URLEncodedUtils.parse(urlSuffix, Charset.forName("UTF-8"));
146         return params.stream().collect(toMap(NameValuePair::getName, NameValuePair::getValue));
147     }
148
149     public static void verifyUrlPrefixMatchNewViewEdit(String expectedMode) {
150         String currentUrl = GeneralUIUtils.getDriver().getCurrentUrl();
151         assertThat(currentUrl, containsString(NEW_VIEW_EDIT_RELATIVE_URL  + "/" + expectedMode));
152     }
153 }