Support complex types in interface operation inputs
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / frontend / ci / tests / pages / component / workspace / InterfaceOperationInputListComponent.java
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2022 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.sdc.frontend.ci.tests.pages.component.workspace;
23
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.Optional;
30 import lombok.AllArgsConstructor;
31 import org.onap.sdc.frontend.ci.tests.pages.AbstractPageObject;
32 import org.onap.sdc.frontend.ci.tests.pages.component.workspace.InterfaceDefinitionOperationsModal.InterfaceOperationsData.InputData;
33 import org.openqa.selenium.By;
34 import org.openqa.selenium.WebDriver;
35 import org.openqa.selenium.WebElement;
36
37 /**
38  * Handles the input list inside the interface operation modal.
39  *
40  * @see "catalog-ui app-input-list and app-input-list-item ui components"
41  */
42 public class InterfaceOperationInputListComponent extends AbstractPageObject {
43
44     private WebElement wrappingElement;
45     private List<WebElement> inputList = new ArrayList<>();
46
47     public InterfaceOperationInputListComponent(final WebDriver webDriver) {
48         super(webDriver);
49     }
50
51     @Override
52     public void isLoaded() {
53         this.wrappingElement = waitForElementVisibility(XpathSelector.WRAPPING_ELEMENT.getXPath());
54         loadInputList();
55     }
56
57     /**
58      * Loads the input list
59      */
60     public void loadInputList() {
61         this.inputList = findSubElements(wrappingElement, XpathSelector.INPUT_LIST.getXPath());
62     }
63
64     /**
65      * Fill an input value.
66      *
67      * @param inputName the input name
68      * @param value     the value
69      */
70     public void fillInputValue(final String inputName, final Object value) {
71         if (value == null) {
72             return;
73         }
74         if (value instanceof String || value instanceof Integer || value instanceof Boolean) {
75             fillSimpleValue(inputName, String.valueOf(value));
76             return;
77         }
78         throw new UnsupportedOperationException("Set input value not yet implemented for value type: " + value.getClass().getName());
79     }
80
81     /**
82      * Expands or retracts an input in the input list.
83      *
84      * @param name the input name
85      */
86     public void toggleInputExpansion(final String name) {
87         final Optional<WebElement> inputOpt = findInput(name);
88         final By expandIconSelector = By.xpath(XpathSelector.EXPAND_ICON.getXPath());
89         inputOpt.ifPresent(webElement ->
90             webElement.findElement(expandIconSelector).click()
91         );
92     }
93
94     /**
95      * Deletes an input from the input list.
96      *
97      * @param name the name of the input to delete
98      */
99     public void deleteInput(final String name) {
100         final Optional<WebElement> inputOpt = findInput(name);
101         final By deleteIconSelector = By.xpath(XpathSelector.DELETE_ICON.getXPath());
102         inputOpt.ifPresent(webElement ->
103             webElement.findElement(deleteIconSelector).click()
104         );
105         loadInputList();
106         assertTrue(findInput(name).isEmpty());
107     }
108
109     public List<InputData> getInputList() {
110         if (inputList.isEmpty()) {
111             return Collections.emptyList();
112         }
113         final List<InputData> inputDataList = new ArrayList<>();
114         final By inputLabelSelector = By.xpath(XpathSelector.INPUT_LABEL.getXPath());
115         final By inputTypeSelector = By.xpath(XpathSelector.INPUT_TYPE.getXPath());
116         inputList.forEach(inputWebElement -> {
117             String inputLabel = inputWebElement.findElement(inputLabelSelector).getText();
118             inputLabel = inputLabel.substring(0, inputLabel.length() -1);
119             final String inputType = inputWebElement.findElement(inputTypeSelector).getText();
120             var inputData = new InputData(inputLabel, inputType, null);
121             inputDataList.add(inputData);
122         });
123
124         return inputDataList;
125     }
126
127     private Optional<WebElement> findInput(final String name) {
128         final String label = name + ":";
129         final By inputLabelSelector = By.xpath(XpathSelector.INPUT_LABEL.getXPath());
130         return inputList.stream().filter(webElement -> {
131             final WebElement inputLabel = webElement.findElement(inputLabelSelector);
132             return label.equals(inputLabel.getText());
133         }).findFirst();
134     }
135
136     private void fillSimpleValue(final String inputName, final String inputValue) {
137         toggleInputExpansion(inputName);
138         final Optional<WebElement> inputOpt = findInput(inputName);
139         assertTrue(inputOpt.isPresent(), String.format("Could not set value for input '%s'. The input was not found.", inputName));
140         final By simpleInputValueSelector = By.xpath(XpathSelector.SIMPLE_VALUE_INPUT_RELATIVE_FROM_INPUT_INFO.getXPath());
141         inputOpt.ifPresent(webElement -> webElement.findElement(simpleInputValueSelector).sendKeys(inputValue));
142     }
143
144     @AllArgsConstructor
145     private enum XpathSelector {
146         WRAPPING_ELEMENT("//div[@class='input-tree']"),
147         INPUT_LIST("//div[@class='input-tree']/*/*/*/span[@class='input-info']"),
148         INPUT_LABEL("label[@class='input-label']"),
149         INPUT_TYPE("em[@data-tests-id='input-type']"),
150         SIMPLE_VALUE_INPUT_RELATIVE_FROM_INPUT_INFO("..//li[@class='input-value']/input"),
151         EXPAND_ICON("em[contains(concat(' ',normalize-space(@class),' '),' round-expand-icon ')]"),
152         DELETE_ICON("span[contains(concat(' ',normalize-space(@class),' '),' delete-btn ')]");
153
154         private final String xPath;
155
156         public String getXPath(final String... xpathParams) {
157             return String.format(xPath, xpathParams);
158         }
159     }
160 }