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.Objects;
29 import java.util.stream.Collectors;
30 import lombok.AllArgsConstructor;
32 import org.onap.sdc.frontend.ci.tests.datatypes.ServiceDependencyProperty;
33 import org.openqa.selenium.By;
34 import org.openqa.selenium.WebDriver;
35 import org.openqa.selenium.WebElement;
36 import org.openqa.selenium.support.ui.Select;
39 * Represents the Service Dependencies Editor
41 public class ServiceDependenciesEditor extends AbstractPageObject {
43 public ServiceDependenciesEditor(final WebDriver webDriver) {
48 public void isLoaded() {
49 waitForElementVisibility(By.xpath(XpathSelector.SERVICE_DEPENDENCIES_EDITOR.xPath));
52 * Returns a list of strings based on the property UI Select
53 * @return List of property names which can be selected
55 public List<String> getPropertySelectOptions() {
56 return new Select(webDriver.findElement(By.xpath(XpathSelector.SERVICE_PROPERTY_NAME.xPath)))
57 .getOptions().stream()
58 .map(option -> option.getAttribute("innerText"))
59 .filter(Objects::nonNull)
60 .filter(option -> !option.startsWith("Select"))
61 .collect(Collectors.toList());
64 public void addProperty(final ServiceDependencyProperty property) {
65 final Select properties = new Select(webDriver.findElement(By.xpath(XpathSelector.SERVICE_PROPERTY_NAME.xPath)));
66 properties.selectByVisibleText(property.getName());
67 final Select logicalOperator = new Select(webDriver.findElement(By.xpath(XpathSelector.CONSTRAINT_OPERATOR.xPath)));
68 logicalOperator.selectByVisibleText(property.getLogicalOperator().getOperator());
69 findElement(XpathSelector.VALUE_TYPE_STATIC.getXPath()).click();
71 addRuleAssignedValue(property);
72 } catch (Exception e) {
73 fail("Failed to add property due to exception while adding rule value :: {}", e);
75 webDriver.findElement(By.xpath(XpathSelector.CREATE_BUTTON.xPath)).click();
78 private void addRuleAssignedValue(final ServiceDependencyProperty property) throws Exception {
79 final var type = property.getType();
80 final var value = property.getValue();
83 addListInput(property.getName(), value);
86 addMapInput(property.getName(), value);
89 addStringInput(waitForElementVisibility(By.xpath(XpathSelector.RULE_ASSIGNED_VALUE.xPath)), value);
94 private void addStringInput(WebElement element, Object value) {
95 if ("select".equals(element.getTagName())) {
96 new Select(element).selectByVisibleText(value.toString());
98 element.sendKeys(value.toString());
102 private void addListInput(final String name, final String value) throws Exception {
103 final List<?> values = new JsonMapper().readValue(value, List.class);
104 final WebElement addToListElement = waitForElementVisibility(By.xpath(XpathSelector.RULE_ASSIGNED_VALUE_ADD_TO_LIST.formatXpath(name)));
105 for (int i=0;i<values.size();i++) {
106 addToListElement.click();
107 addStringInput(waitForElementVisibility(By.xpath(XpathSelector.RULE_ASSIGNED_LIST_VALUE.formatXpath(name, i))), values.get((i)));
111 private void addMapInput(final String name, final String value) throws Exception {
112 final Map<?, ?> values = new JsonMapper().readValue(value, Map.class);
114 final WebElement addToListElement = waitForElementVisibility(By.xpath(XpathSelector.RULE_ASSIGNED_VALUE_ADD_TO_LIST.formatXpath(name)));
115 for(Entry<?, ?> entry : values.entrySet()) {
116 addToListElement.click();
117 final List<WebElement> KeyValueInputs = waitForAllElementsVisibility(By.xpath(XpathSelector.RULE_ASSIGNED_MAP_KEY_VALUE.formatXpath(name, i++)));
118 addStringInput(KeyValueInputs.get(0), entry.getKey());
119 addStringInput(KeyValueInputs.get(1), entry.getValue());
125 private enum XpathSelector {
126 SERVICE_DEPENDENCIES_EDITOR("//service-dependencies-editor"),
127 SERVICE_PROPERTY_NAME("//*[@data-tests-id='servicePropertyName']/select"),
128 CONSTRAINT_OPERATOR("//*[@data-tests-id='constraintOperator']/select"),
129 VALUE_TYPE_STATIC("//*[@data-tests-id='value-type-static']"),
130 VALUE_TYPE_TOSCA_FUNCTION("//*[@data-tests-id='value-type-tosca-function']"),
131 RULE_ASSIGNED_VALUE("//*[@data-tests-id='ruleAssignedValue']//*[self::input or self::select]"),
132 RULE_ASSIGNED_VALUE_ADD_TO_LIST("//a[@data-tests-id = 'add-to-list-%s']"),
133 RULE_ASSIGNED_LIST_VALUE("//*[@data-tests-id='value-prop-%s.%d']"),
134 RULE_ASSIGNED_MAP_KEY_VALUE("//*[contains(@data-tests-id, 'value-prop') and contains(@data-tests-id, '%s.%d')]"),
135 CREATE_BUTTON("//button[text()='Create']");
137 private final String xPath;
139 public String formatXpath(Object... values) {
140 return String.format(xPath, values);