aba5f740638ec80087881337287ffdcc01652ef2
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / frontend / ci / tests / pages / ServiceDependenciesEditor.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;
21
22 import static org.junit.jupiter.api.Assertions.fail;
23
24 import com.fasterxml.jackson.databind.json.JsonMapper;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.stream.Collectors;
29 import lombok.AllArgsConstructor;
30 import lombok.Getter;
31 import org.onap.sdc.frontend.ci.tests.datatypes.ServiceDependencyProperty;
32 import org.openqa.selenium.By;
33 import org.openqa.selenium.WebDriver;
34 import org.openqa.selenium.WebElement;
35 import org.openqa.selenium.support.ui.Select;
36
37 /**
38  * Represents the Service Dependencies Editor
39  */
40 public class ServiceDependenciesEditor extends AbstractPageObject {
41
42     public ServiceDependenciesEditor(final WebDriver webDriver) {
43         super(webDriver);
44     }
45
46     @Override
47     public void isLoaded() {
48         waitForElementVisibility(By.xpath(XpathSelector.SERVICE_DEPENDENCIES_EDITOR.xPath));
49     }
50     /**
51      * Returns a list of strings based on the property UI Select
52      * @return List of property names which can be selected
53      */
54     public List<String> getPropertySelectOptions() {
55         return new Select(webDriver.findElement(By.xpath(XpathSelector.SERVICE_PROPERTY_NAME.xPath)))
56                 .getOptions().stream()
57                 .map(option -> option.getAttribute("innerText")).collect(Collectors.toList());
58     }
59
60     public void addProperty(final ServiceDependencyProperty property) {
61         final Select properties = new Select(webDriver.findElement(By.xpath(XpathSelector.SERVICE_PROPERTY_NAME.xPath)));
62         properties.selectByVisibleText(property.getName());
63         final Select logicalOperator = new Select(webDriver.findElement(By.xpath(XpathSelector.CONSTRAINT_OPERATOR.xPath)));
64         logicalOperator.selectByVisibleText(property.getLogicalOperator().getOperator());
65         final Select functionType = new Select(webDriver.findElement(By.xpath(XpathSelector.FUNCTION_TYPE.xPath)));
66         functionType.selectByVisibleText(property.getSource());
67         try {
68             addRuleAssignedValue(property);
69         } catch (Exception e) {
70             fail("Failed to add property due to exception while adding rule value :: {}", e);
71         }
72         webDriver.findElement(By.xpath(XpathSelector.CREATE_BUTTON.xPath)).click();
73     }
74
75     private void addRuleAssignedValue(final ServiceDependencyProperty property) throws Exception {
76         final var type = property.getType();
77         final var value = property.getValue();
78         switch (type) {
79             case "list":
80                 addListInput(property.getName(), value);
81                 break;
82             case "map":
83                 addMapInput(property.getName(), value);
84                 break;
85             default:
86                 addStringInput(waitForElementVisibility(By.xpath(XpathSelector.RULE_ASSIGNED_VALUE.xPath)), value);
87                 break;
88         }
89     }
90
91     private void addStringInput(WebElement element, Object value) {
92         if ("select".equals(element.getTagName())) {
93             new Select(element).selectByVisibleText(value.toString());
94         } else {
95             element.sendKeys(value.toString());
96         }
97     }
98
99     private void addListInput(final String name, final String value) throws Exception {
100         final List<?> values = new JsonMapper().readValue(value, List.class);
101         final WebElement addToListElement = waitForElementVisibility(By.xpath(XpathSelector.RULE_ASSIGNED_VALUE_ADD_TO_LIST.formatXpath(name)));
102         for (int i=0;i<values.size();i++) {
103             addToListElement.click();
104             addStringInput(waitForElementVisibility(By.xpath(XpathSelector.RULE_ASSIGNED_LIST_VALUE.formatXpath(name, i))), values.get((i)));
105         }
106     }
107
108     private void addMapInput(final String name, final String value) throws Exception {
109         final Map<?, ?> values = new JsonMapper().readValue(value, Map.class);
110         int i = 0;
111         final WebElement addToListElement = waitForElementVisibility(By.xpath(XpathSelector.RULE_ASSIGNED_VALUE_ADD_TO_LIST.formatXpath(name)));
112         for(Entry<?, ?> entry : values.entrySet()) {
113             addToListElement.click();
114             final List<WebElement> KeyValueInputs = waitForAllElementsVisibility(By.xpath(XpathSelector.RULE_ASSIGNED_MAP_KEY_VALUE.formatXpath(name, i++)));
115             addStringInput(KeyValueInputs.get(0), entry.getKey());
116             addStringInput(KeyValueInputs.get(1), entry.getValue());
117         }
118     }
119
120     @AllArgsConstructor
121     @Getter
122     private enum XpathSelector {
123         SERVICE_DEPENDENCIES_EDITOR("//service-dependencies-editor"),
124         SERVICE_PROPERTY_NAME("//*[@data-tests-id='servicePropertyName']/select"),
125         CONSTRAINT_OPERATOR("//*[@data-tests-id='constraintOperator']/select"),
126         FUNCTION_TYPE("//*[@data-tests-id='functionType']/select"),
127         SOURCE_TYPE("//*[@data-tests-id='sourceType']/select"),
128         RULE_ASSIGNED_VALUE("//*[@data-tests-id='ruleAssignedValue']//*[self::input or self::select]"),
129         RULE_ASSIGNED_VALUE_ADD_TO_LIST("//a[@data-tests-id = 'add-to-list-%s']"),
130         RULE_ASSIGNED_LIST_VALUE("//*[@data-tests-id='value-prop-%s.%d']"),
131         RULE_ASSIGNED_MAP_KEY_VALUE("//*[contains(@data-tests-id, 'value-prop') and contains(@data-tests-id, '%s.%d')]"),
132         CREATE_BUTTON("//button[text()='Create']");
133
134         private final String xPath;
135
136         public String formatXpath(Object... values) {
137             return String.format(xPath, values);
138         }
139     }
140 }