92f8056e3b2707991b7f909847fc20d2126e76ea
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / frontend / ci / tests / pages / component / workspace / CompositionDirectiveNodeFilterTab.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.sdc.frontend.ci.tests.pages.component.workspace;
21
22 import java.util.List;
23 import java.util.stream.Collectors;
24
25 import org.onap.sdc.frontend.ci.tests.datatypes.DirectiveType;
26 import org.onap.sdc.frontend.ci.tests.pages.AbstractPageObject;
27 import org.onap.sdc.frontend.ci.tests.pages.ServiceDependenciesEditor;
28 import org.openqa.selenium.By;
29 import org.openqa.selenium.WebDriver;
30 import org.openqa.selenium.support.ui.Select;
31
32 import lombok.AllArgsConstructor;
33 import lombok.Getter;
34
35 /**
36  * Represents the composition page, details panel, Directives and Node Filters tab
37  */
38 public class CompositionDirectiveNodeFilterTab extends AbstractPageObject {
39
40     public CompositionDirectiveNodeFilterTab(final WebDriver webDriver) {
41         super(webDriver);
42     }
43
44     @Override
45     public void isLoaded() {
46         waitForElementVisibility(By.xpath(XpathSelector.NODE_FILTER_TAB.xPath));
47     }
48
49     /**
50      * Select option from the directive select web element
51      *
52      * @param type which directive type to select from options
53      */
54     public void selectDirective(final DirectiveType type) {
55         final Select directiveSelect = getDirectiveSelect();
56         directiveSelect.selectByVisibleText(type.getName());
57     }
58
59     /**
60      * Returns a ServiceDependenciesEditor when the add node filter button is clicked
61      *
62      * @param index an index indicating which add node filter button to click
63      * @return a new ServiceDependenciesEditor component instance
64      */
65     public ServiceDependenciesEditor clickAddNodeFilter(final int index) {
66         waitForElementVisibility(By.xpath(XpathSelector.ADD_RULE_BUTTON.formatXPath(index))).click();
67         return new ServiceDependenciesEditor(webDriver);
68     }
69
70     /**
71      * Verify a rule has been created
72      *
73      * @param propertyName name of the created rule
74      * @return true if the rule is present on screen otherwise false
75      */
76     public boolean isRulePresent(final String propertyName) {
77         try {
78             return waitForElementVisibility(By.xpath(XpathSelector.RULE_DESC.formatXPath(propertyName))) != null;
79         } catch (final Exception ignored) {
80             return false;
81         }
82     }
83
84     /**
85      * Return all available directive types from the directive select web element
86      *
87      * @return list of strings in lower case based on visible text of the select's web element options.
88      * The List values should correspond to {@link DirectiveType}
89      */
90     public List<String> getDirectiveSelectOptions() {
91         final Select directiveSelect = getDirectiveSelect();
92         final List<String> directiveOptions =  directiveSelect.getOptions().stream()
93                 .map(option -> option.getText().toLowerCase()).collect(Collectors.toList());
94         directiveOptions.remove("select directive");
95         return directiveOptions;
96     }
97
98     /**
99      * Verify a directive has been selected
100      *
101      * @param type which directive type to verify
102      * @return true if the directive type is selected on screen otherwise false
103      */
104     public boolean isDirectiveSelected(final DirectiveType type) {
105         try {
106             return waitForElementVisibility(
107                     By.xpath(XpathSelector.NODE_FILTER_DIRECTIVE_SELECTED
108                             .formatXPath(type.getName().toUpperCase())), 2) != null;
109         } catch (final Exception ignored) {
110             return false;
111         }
112     }
113
114     private Select getDirectiveSelect() {
115         return new Select(findElement(By.xpath(XpathSelector.NODE_FILTER_DIRECTIVE_SELECT.xPath)));
116     }
117
118     @AllArgsConstructor
119     @Getter
120     private enum XpathSelector {
121         NODE_FILTER_TAB("//service-dependencies-tab"),
122         NODE_FILTER_DIRECTIVE_SELECT("//select[@id='singleSelect']"),
123         NODE_FILTER_DIRECTIVE_SELECTED("//label[contains(text(),': %s')]"),
124         ADD_RULE_BUTTON("(//*[@data-tests-id='add-rule-button'])[%d]"),
125         RULE_DESC("//*[contains(text(),'%s')]");
126
127         private final String xPath;
128
129         public String formatXPath(Object value) {
130             return String.format(xPath, value);
131         }
132     }
133 }