aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/main/java/vid/automation/test/sections/InstantiationStatusPage.java
blob: 1bb095fdaa9115168b960cb2f23f04c1481025c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package vid.automation.test.sections;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.onap.sdc.ci.tests.utilities.GeneralUIUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import vid.automation.test.Constants;
import vid.automation.test.infra.Click;
import vid.automation.test.infra.Get;
import vid.automation.test.infra.Wait;

import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toMap;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.testng.Assert.assertEquals;

public abstract class InstantiationStatusPage extends VidBasePage {

    public static final String refreshButtonId = "refresh-btn";

    private static final String NEW_VIEW_EDIT_RELATIVE_URL = "serviceModels.htm#/servicePlanning";


    public static String getWebTrTdSpanElementByParentID(WebElement tr, String id, int timeout) {
            return tr.findElements(By.xpath(".//*[@id='" + id + "']//span")).get(0).getText();
    }

    public static int getNumberOfTableRows(int timeout){
        WebDriverWait wait = waitUntilDriverIsReady(timeout);
        return wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[table]//tbody/tr"))).size();
    }

    public static WebElement assertInstantiationStatusRow(String spanIdSelector, Map<String, String> fieldsIdsAndExpected) {
        try {
            WebElement newTrRow = getInstantiationStatusRow(spanIdSelector);
            final Map<String, String> fieldIdAndActual = fieldsIdsAndExpected.entrySet().stream()
                    .collect(Collectors.toMap(
                            kv -> kv.getKey(),
                            kv -> getWebTrTdSpanElementByParentID(newTrRow, kv.getKey(), 1)
                    ));

            assertThat("failed comparing spanIdSelector " + spanIdSelector, fieldIdAndActual, is(fieldsIdsAndExpected));

            return newTrRow;
        } catch (Exception e) {
            throw new RuntimeException("error while assertInstantiationStatusRow with: String spanIdSelector=" +
                    spanIdSelector + ", fieldsIdsAndExpected=" + fieldsIdsAndExpected, e);
        }
    }

    public static WebElement getInstantiationStatusRow(String spanIdSelector) {
        GeneralUIUtils.ultimateWait();
        return Get.byXpath("//*[@id='" + spanIdSelector + "']/parent::*/parent::*/parent::*", 0);
    }

    public static void clickRefreshButton() {
        WebDriverWait wait = waitUntilDriverIsReady(0);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='"+ refreshButtonId + "']"))).click();
        GeneralUIUtils.ultimateWait();
    }

    public static void checkMenuItem(String actualInstanceName, String contextMenuItem, boolean shouldBeEnabled, Consumer<String> doIfEnabled) {
        Wait.waitFor(name -> {
            if (null == getInstantiationStatusRow(name)) {
                clickRefreshButton();
                return false;
            } else {
                return true;
            }
        }, actualInstanceName, 8, 1);
        final WebElement row = getInstantiationStatusRow(actualInstanceName);
        row.findElement(By.className("menu-div")).click();
        String clazz = Get.byXpath("//div[@data-tests-id='" + contextMenuItem + "']/ancestor::li").getAttribute("class");
        assertThat("item " + contextMenuItem + " of " + actualInstanceName +
                " should be " + (shouldBeEnabled ? "enabled" : "disabled"), !clazz.equals("disabled"), is(shouldBeEnabled));
        if (shouldBeEnabled) {
            doIfEnabled.accept(contextMenuItem);
        } else {
            // dismiss menu
            Get.byTestId("instantiation-status-title").click();
        }
    }

    public void showTooltipByHoverAboveStatusIcon(String elementTestId){
        WebElement selectedElement = GeneralUIUtils.getWebElementByTestID(elementTestId, 30);
    }

    public static void openDrawingBoardForRetry(String serviceInstanceName) {
        InstantiationStatusPage.checkMenuItem(serviceInstanceName, Constants.InstantiationStatus.CONTEXT_MENU_RETRY, true, contextMenuRetry -> {
            Click.byTestId(contextMenuRetry);
            VidBasePage.goOutFromIframe();
            verifyUrlPrefixMatchNewViewEdit("RETRY_EDIT");
        });
    }

    public static void verifyOpenNewViewEdit(String serviceInstanceName, String serviceInstanceId, String serviceModelId, String serviceType, String subscriberId, String mode) {
        InstantiationStatusPage.checkMenuItem(serviceInstanceName, Constants.InstantiationStatus.CONTEXT_MENU_HEADER_OPEN_ITEM, true, contextMenuOpen -> {
            Click.byTestId(contextMenuOpen);
            VidBasePage.goOutFromIframe();
            verifyUrlMatchNewViewEdit(serviceInstanceId, serviceModelId, serviceType, subscriberId, mode);
            SideMenu.navigateToMacroInstantiationStatus();
        });
    }

    public static void verifyOpenNewViewEdit(String serviceInstanceName, boolean openShouldBeEnabled, String expectedMode) {
        InstantiationStatusPage.checkMenuItem(serviceInstanceName, Constants.InstantiationStatus.CONTEXT_MENU_HEADER_OPEN_ITEM, openShouldBeEnabled, contextMenuOpen -> {
            Click.byTestId(contextMenuOpen);
            VidBasePage.goOutFromIframe();
            verifyUrlPrefixMatchNewViewEdit(expectedMode);
            SideMenu.navigateToMacroInstantiationStatus();
        });
    }

    public static void verifyUrlMatchNewViewEdit(String serviceInstanceId, String serviceModelId, String serviceType, String subscriberId, String expectedMode) {
        verifyUrlPrefixMatchNewViewEdit(expectedMode);
        Map<String, String> paramsMap = extractQueryParamsFromCurrentURL(NEW_VIEW_EDIT_RELATIVE_URL + "/" + expectedMode + "?");
        //assertEquals(paramsMap.get("mode"), expectedMode);
        assertEquals(paramsMap.get("serviceInstanceId"), serviceInstanceId);
        assertEquals(paramsMap.get("serviceModelId"), serviceModelId);
        assertEquals(paramsMap.get("serviceType"), serviceType);
        assertEquals(paramsMap.get("subscriberId"), subscriberId);
    }

    protected static Map<String, String> extractQueryParamsFromCurrentURL(String relativePath) {
        String currentUrl = GeneralUIUtils.getDriver().getCurrentUrl();
        //unfortunately parse(final URI uri, final String charset) can't handle with the #/ part of the uri
        String urlSuffix = currentUrl.substring(currentUrl.indexOf(relativePath)+relativePath.length());
        List<NameValuePair> params = URLEncodedUtils.parse(urlSuffix, Charset.forName("UTF-8"));
        return params.stream().collect(toMap(NameValuePair::getName, NameValuePair::getValue));
    }

    public static void verifyUrlPrefixMatchNewViewEdit(String expectedMode) {
        String currentUrl = GeneralUIUtils.getDriver().getCurrentUrl();
        assertThat(currentUrl, containsString(NEW_VIEW_EDIT_RELATIVE_URL  + "/" + expectedMode));
    }
}