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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.sdc.frontend.ci.tests.pages;
22 import static org.junit.jupiter.api.Assertions.fail;
24 import com.fasterxml.jackson.databind.json.JsonMapper;
25 import java.util.List;
27 import java.util.Map.Entry;
28 import java.util.stream.Collectors;
29 import lombok.AllArgsConstructor;
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;
38 * Represents the Service Dependencies Editor
40 public class ServiceDependenciesEditor extends AbstractPageObject {
42 public ServiceDependenciesEditor(final WebDriver webDriver) {
47 public void isLoaded() {
48 waitForElementVisibility(By.xpath(XpathSelector.SERVICE_DEPENDENCIES_EDITOR.xPath));
51 * Returns a list of strings based on the property UI Select
52 * @return List of property names which can be selected
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());
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());
68 addRuleAssignedValue(property);
69 } catch (Exception e) {
70 fail("Failed to add property due to exception while adding rule value :: {}", e);
72 webDriver.findElement(By.xpath(XpathSelector.CREATE_BUTTON.xPath)).click();
75 private void addRuleAssignedValue(final ServiceDependencyProperty property) throws Exception {
76 final var type = property.getType();
77 final var value = property.getValue();
80 addListInput(property.getName(), value);
83 addMapInput(property.getName(), value);
86 addStringInput(waitForElementVisibility(By.xpath(XpathSelector.RULE_ASSIGNED_VALUE.xPath)), value);
91 private void addStringInput(WebElement element, Object value) {
92 if ("select".equals(element.getTagName())) {
93 new Select(element).selectByVisibleText(value.toString());
95 element.sendKeys(value.toString());
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)));
108 private void addMapInput(final String name, final String value) throws Exception {
109 final Map<?, ?> values = new JsonMapper().readValue(value, Map.class);
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());
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']");
134 private final String xPath;
136 public String formatXpath(Object... values) {
137 return String.format(xPath, values);