Provide user to specify the ouput name while declaring the atrributes
[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.Objects;
29 import java.util.stream.Collectors;
30 import lombok.AllArgsConstructor;
31 import lombok.Getter;
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;
37
38 /**
39  * Represents the Service Dependencies Editor
40  */
41 public class ServiceDependenciesEditor extends AbstractPageObject {
42
43     public ServiceDependenciesEditor(final WebDriver webDriver) {
44         super(webDriver);
45     }
46
47     @Override
48     public void isLoaded() {
49         waitForElementVisibility(By.xpath(XpathSelector.SERVICE_DEPENDENCIES_EDITOR.xPath));
50     }
51     /**
52      * Returns a list of strings based on the property UI Select
53      * @return List of property names which can be selected
54      */
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());
62     }
63
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();
70         try {
71             addRuleAssignedValue(property);
72         } catch (Exception e) {
73             fail("Failed to add property due to exception while adding rule value :: {}", e);
74         }
75         webDriver.findElement(By.xpath(XpathSelector.CREATE_BUTTON.xPath)).click();
76     }
77
78     private void addRuleAssignedValue(final ServiceDependencyProperty property) throws Exception {
79         final var type = property.getType();
80         final var value = property.getValue();
81         switch (type) {
82             case "list":
83                 addListInput(property.getName(), value);
84                 break;
85             case "map":
86                 addMapInput(property.getName(), value);
87                 break;
88             default:
89                 addStringInput(waitForElementVisibility(By.xpath(XpathSelector.RULE_ASSIGNED_VALUE.xPath)), value);
90                 break;
91         }
92     }
93
94     private void addStringInput(WebElement element, Object value) {
95         if ("select".equals(element.getTagName())) {
96             new Select(element).selectByVisibleText(value.toString());
97         } else {
98             element.sendKeys(value.toString());
99         }
100     }
101
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)));
108         }
109     }
110
111     private void addMapInput(final String name, final String value) throws Exception {
112         final Map<?, ?> values = new JsonMapper().readValue(value, Map.class);
113         int i = 0;
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());
120         }
121     }
122
123     @AllArgsConstructor
124     @Getter
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']");
136
137         private final String xPath;
138
139         public String formatXpath(Object... values) {
140             return String.format(xPath, values);
141         }
142     }
143 }