Support for multiple directives
[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.ArrayList;
23 import java.util.List;
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 ng-select web element
51      *
52      */
53     public void selectDirective() {
54         waitToBeClickable(By.xpath(XpathSelector.NODE_FILTER_DIRECTIVE_DROPDOWN.getXPath())).click();
55         waitToBeClickable(By.xpath(XpathSelector.NODE_FILTER_DIRECTIVE_OPTIONS_SELECTABLE.getXPath())).click();
56         saveDirectivesSelected();
57     }
58     /**
59      * Select multiple options from the directive ng-select web element
60      *
61      */
62     public void updateDirectives() {
63         waitToBeClickable(By.xpath(XpathSelector.NODE_FILTER_DIRECTIVE_EDIT_BTN.getXPath())).click();
64         waitToBeClickable(By.xpath(XpathSelector.NODE_FILTER_DIRECTIVE_DROPDOWN.getXPath())).click();
65         waitToBeClickable(By.xpath(XpathSelector.NODE_FILTER_DIRECTIVE_OPTIONS_SUBSTITUTABLE.getXPath())).click();
66         saveDirectivesSelected();
67     }
68
69     /**
70      * Returns a ServiceDependenciesEditor when the add node filter button is clicked
71      *
72      * @param index an index indicating which add node filter button to click
73      * @return a new ServiceDependenciesEditor component instance
74      */
75     public ServiceDependenciesEditor clickAddNodeFilter(final int index) {
76         waitForElementInvisibility(By.xpath(XpathSelector.LOADER_HELPER.getXPath()));
77         waitForElementVisibility(By.xpath(XpathSelector.ADD_RULE_BUTTON.formatXPath(index))).click();
78         return new ServiceDependenciesEditor(webDriver);
79     }
80
81     /**
82      * Verify a rule has been created
83      *
84      * @param propertyName name of the created rule
85      * @return true if the rule is present on screen otherwise false
86      */
87     public boolean isRulePresent(final String propertyName) {
88         try {
89             return waitForElementVisibility(By.xpath(XpathSelector.RULE_DESC.formatXPath(propertyName))) != null;
90         } catch (final Exception ignored) {
91             return false;
92         }
93     }
94
95     /**
96      * Return all available directive types from the directive ng-select web element
97      *
98      * @return list of strings in lower case based on visible text of the ng-select's web element options.
99      * The List values should correspond to {@link DirectiveType}
100      */
101     public List<String> getDirectiveSelectOptions() {
102         final List<String> directiveOptions =  new ArrayList<>();
103         directiveOptions.add(DirectiveType.SELECT.getName());
104         directiveOptions.add(DirectiveType.SELECTABLE.getName());
105         directiveOptions.add(DirectiveType.SUBSTITUTE.getName());
106         directiveOptions.add(DirectiveType.SUBSTITUTABLE.getName());
107         return directiveOptions;
108     }
109
110     /**
111      * Verify a directive has been selected
112      *
113      * @param type which directive type to verify
114      * @return true if the directive type is selected on screen otherwise false
115      */
116     public boolean isDirectiveSelected(final DirectiveType type) {
117         try {
118             return waitForElementVisibility(
119                     By.xpath(XpathSelector.NODE_FILTER_DIRECTIVE_SELECTED
120                             .formatXPath(type.getName().toUpperCase())), 2) != null;
121         } catch (final Exception ignored) {
122             return false;
123         }
124     }
125
126     private Select getDirectiveSelect() {
127         return new Select(findElement(By.xpath(XpathSelector.NODE_FILTER_DIRECTIVE_SELECT.xPath)));
128     }
129
130     private void saveDirectivesSelected() {
131         waitToBeClickable(By.xpath(XpathSelector.NODE_FILTER_DIRECTIVE_APPLY_BTN.formatXPath("Apply"))).click();
132     }
133
134     @AllArgsConstructor
135     @Getter
136     private enum XpathSelector {
137         NODE_FILTER_TAB("//service-dependencies-tab"),
138         NODE_FILTER_DIRECTIVE_DROPDOWN("//div[@class='select-directives']//div[@class='ng-input']"),
139         NODE_FILTER_DIRECTIVE_SELECT("//select[@id='singleSelect']"),
140         NODE_FILTER_DIRECTIVE_SELECTED("//label[contains(text(),': %s')]"),
141         NODE_FILTER_DIRECTIVE_OPTIONS_SELECTABLE("//div[@class='select-directives']//*[contains(text(), 'selectable')]"),
142         NODE_FILTER_DIRECTIVE_APPLY_BTN("//*[@id=\"multi-select-apply\"]"),
143         NODE_FILTER_DIRECTIVE_EDIT_BTN("//*[@data-tests-id='directive-edit-icon']"),
144         NODE_FILTER_DIRECTIVE_OPTIONS_SUBSTITUTABLE("//div[@class='select-directives']//*[contains(text(), 'substitutable')]"),
145         ADD_RULE_BUTTON("(//*[@data-tests-id='add-rule-button'])[%d]"),
146         RULE_DESC("//*[contains(text(),'%s')]"),
147         LOADER_HELPER("//div[@class='tlv-loader-back tlv-loader-relative']");
148
149         private final String xPath;
150
151         public String formatXPath(Object value) {
152             return String.format(xPath, value);
153         }
154     }
155 }