Revert "Fix 'Wrong Inputs creation on (Add Service)'"
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / frontend / ci / tests / pages / ResourcePropertiesAssignmentInputTab.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.assertTrue;
23
24 import java.util.List;
25 import java.util.Map;
26 import java.util.stream.Collectors;
27
28 import org.onap.sdc.frontend.ci.tests.execute.setup.ExtentTestActions;
29 import org.openqa.selenium.By;
30 import org.openqa.selenium.WebDriver;
31 import org.openqa.selenium.WebElement;
32
33 import com.aventstack.extentreports.Status;
34
35 import lombok.AllArgsConstructor;
36 import lombok.Getter;
37
38 /**
39  * Handles the Resource Properties Assignment Input Tab UI actions
40  */
41 public class ResourcePropertiesAssignmentInputTab extends AbstractPageObject {
42
43     public ResourcePropertiesAssignmentInputTab(final WebDriver webDriver) {
44         super(webDriver);
45     }
46
47     @Override
48     public void isLoaded() {
49         waitForElementVisibility(XpathSelector.INPUT_TAB.getXpath());
50         isInputPropertiesTableLoaded();
51     }
52
53     /**
54      * Creates a List of property names from the inputs tab
55      */
56     public List<String> getInputPropertyNames() {
57         isInputPropertiesTableLoaded();
58         final List<WebElement> propertyNames = findElements(By.xpath(XpathSelector.INPUT_PROPERTY_NAME.getXpath()));
59         return propertyNames.stream().map(propertyName -> propertyName.getAttribute("innerText")).collect(Collectors.toList());
60     }
61
62     /**
63      * Adds metadata to a property within the inputs tab based on a property name
64      * @param name used to determine which property to add metadata
65      * @param key the metadata key to add
66      * @param value the metadata value to add
67      */
68     public void setInputPropertyMetadata(String name, String key, String value) {
69         isInputPropertiesTableLoaded();
70         findElement(By.xpath(XpathSelector.INPUT_PROPERTY_ADD_METADATA_BUTTON.formatXpath(name))).click();
71         waitForElementVisibility(XpathSelector.INPUT_PROPERTY_METADATA_KEY_VALUE_PAIR.formatXpath(name));
72         List<WebElement> keyValueInputs = findElements(By.xpath(XpathSelector.INPUT_PROPERTY_METADATA_KEY_VALUE_PAIR.formatXpath(name)));
73         keyValueInputs.get(0).sendKeys(key);
74         keyValueInputs.get(1).sendKeys(value);
75         saveInputProperties();
76         ExtentTestActions.takeScreenshot(Status.INFO, name, String.format("Added metadata for property %s", name));
77     }
78
79     private void isInputPropertiesTableLoaded() {
80         waitForElementVisibility(XpathSelector.PROPERTIES_TABLE.getXpath());
81         waitForElementInvisibility(By.xpath(XpathSelector.NO_DATA_MESSAGE.getXpath()));
82     }
83
84     public void saveInputProperties() {
85         findElement(By.xpath(XpathSelector.PROPERTY_SAVE_BTN.getXpath())).click();
86         waitForElementVisibility(XpathSelector.PROPERTY_SAVE_MESSAGE.getXpath());
87         waitForElementInvisibility(By.xpath(XpathSelector.PROPERTY_SAVE_MESSAGE.getXpath()));
88     }
89
90     /**
91      * Adds a input
92      * @param inputsMap the inputs map to be added
93      */
94     public void addInputs(final Map<String, String> inputsMap) {
95         isInputPropertiesTableLoaded();
96         inputsMap.forEach((inputName, inputType) -> {
97             WebElement inputAddButton = findElement(By.xpath(XpathSelector.INPUT_ADD_BTN.getXpath()));
98             assertTrue(inputAddButton.isDisplayed());
99             inputAddButton.click();
100             createInput(inputName, inputType);
101             waitForElementInvisibility(By.xpath(XpathSelector.MODAL_BACKGROUND.getXpath()), 5);
102             ExtentTestActions.takeScreenshot(Status.INFO, "added-input",
103                 String.format("Input '%s' was created on component", inputName));
104         });
105     }
106
107     /**
108      * Fills the creation input modal.
109      * @param inputName the input name to be created
110      * @param inputType the input type to be selected
111      */
112     private void createInput(final String inputName, final String inputType) {
113         final AddPropertyModal addInputModal = new AddPropertyModal(webDriver);
114         addInputModal.isLoaded();
115         addInputModal.fillPropertyForm(inputName, inputType);
116         addInputModal.clickOnCreate();
117     }
118
119     /**
120      * Verifies if the added input is displayed on the UI.
121      * @param inputsMap the input name to be found
122      */
123     public void verifyInputs(final Map<String, String> inputsMap ) {
124         for (Map.Entry<String, String> input : inputsMap.entrySet()) {
125             assertTrue(this.getInputPropertyNames().contains(input.getKey()),
126                 String.format("%s Input should be listed but found %s", input.getKey(),
127                     this.getInputPropertyNames().toString()));
128         }
129     }
130
131     /**
132      * Checks if a input exists.
133      * @param inputName the input name
134      * @return the value of the input
135      */
136     public boolean isInputPresent(final String inputName) {
137         isInputPropertiesTableLoaded();
138         try {
139             waitForElementVisibility(By.xpath(XpathSelector.INPUT_CHECKBOX.formatXpath(inputName)), 5);
140         } catch (final Exception ignored) {
141             return false;
142         }
143         return true;
144     }
145
146     /**
147      * Enum that contains identifiers and xpath expressions to elements related to the enclosing page object.
148      */
149     @AllArgsConstructor
150     @Getter
151     private enum XpathSelector {
152         INPUT_TAB("//*[contains(@data-tests-id, 'Inputs') and contains(@class, 'active')]"),
153         PROPERTIES_TABLE("//div[contains(@class,'properties-table')]"),
154         INPUT_CHECKBOX("//checkbox[@data-tests-id='%s']"),
155         NO_DATA_MESSAGE("//div[contains(@class,'no-data') and text()='No data to display']"),
156         PROPERTY_SAVE_BTN("//button[@data-tests-id='properties-save-button']"),
157         PROPERTY_SAVE_MESSAGE("//div[contains(text(), 'Successfully saved')]"),
158         INPUT_PROPERTY_NAME("//*[contains(@class, 'property-name')]"),
159         INPUT_PROPERTY_TABLE_ROW("//div[contains(@class, 'table-row') and descendant::*[text() = '%s']]"),
160         INPUT_PROPERTY_ADD_METADATA_BUTTON(INPUT_PROPERTY_TABLE_ROW.getXpath().concat("//a")),
161         INPUT_PROPERTY_METADATA_KEY_VALUE_PAIR(INPUT_PROPERTY_TABLE_ROW.getXpath().concat("//input")),
162         INPUT_ADD_BTN("//div[contains(@class,'add-btn')]"),
163         MODAL_BACKGROUND("//div[@class='modal-background']");
164
165         @Getter
166         private final String xpath;
167
168         public String formatXpath(Object... params) {
169             return String.format(xpath, params);
170         }
171     }
172
173 }