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