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 / VspCreationModal.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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 lombok.AllArgsConstructor;
23 import lombok.Getter;
24 import org.onap.sdc.frontend.ci.tests.datatypes.CategorySelect;
25 import org.onap.sdc.frontend.ci.tests.datatypes.VspCreateData;
26 import org.onap.sdc.frontend.ci.tests.datatypes.VspOnboardingProcedure;
27 import org.openqa.selenium.By;
28 import org.openqa.selenium.Keys;
29 import org.openqa.selenium.WebDriver;
30 import org.openqa.selenium.WebElement;
31 import org.openqa.selenium.support.ui.Select;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Handles the VSP Creation Modal UI actions
37  */
38 public class VspCreationModal extends AbstractPageObject {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(VspCreationModal.class);
41
42     private WebElement wrappingElement;
43
44     public VspCreationModal(final WebDriver webDriver) {
45         super(webDriver);
46         timeoutInSeconds = 5;
47     }
48
49     @Override
50     public void isLoaded() {
51         LOGGER.debug("Finding element with xpath '{}'", XpathSelector.MODAL_XPATH.getXpath());
52         wrappingElement = waitForElementVisibility(XpathSelector.MODAL_XPATH.getXpath());
53     }
54
55     /**
56      * Fills the creation form with the given data.
57      *
58      * @param vspCreateData the data to fill the Vendor Software Product create form
59      */
60     public void fillCreationForm(final VspCreateData vspCreateData) {
61         fillName(vspCreateData.getName());
62         selectVendorOrElseAny(vspCreateData.getVendor());
63         selectCategory(vspCreateData.getCategory());
64         fillDescription(vspCreateData.getDescription());
65         selectOnboardingProcedure(vspCreateData.getOnboardingProcedure());
66         selectModel(vspCreateData.getModel());
67     }
68
69     /**
70      * Clicks on the create button.
71      *
72      * @return the next page object
73      */
74     public SoftwareProductOnboarding clickOnCreate() {
75         clickElement(XpathSelector.CREATE_BTN);
76         return new SoftwareProductOnboarding(webDriver, new VspCommitModal(webDriver));
77     }
78
79     /**
80      * Fills the VSP name.
81      *
82      * @param vspName the VSP name
83      */
84     public void fillName(final String vspName) {
85         setInputValue(XpathSelector.NAME_TXT, vspName);
86     }
87
88     /**
89      * Fills the VSP description.
90      *
91      * @param description the VSP description
92      */
93     public void fillDescription(final String description) {
94         setInputValue(XpathSelector.DESCRIPTION_TXT, description);
95     }
96
97     /**
98      * Selects the given vendor option. If a null value is given, selects the first option available.
99      *
100      * @param vendor the vendor option to select
101      */
102     public void selectVendorOrElseAny(final String vendor) {
103         if (vendor == null) {
104             selectVendorFirstVendor();
105             return;
106         }
107         setSelectValue(XpathSelector.VENDOR_SELECT, vendor);
108     }
109
110     /**
111      * Selects the first vendor in the vendor list.
112      */
113     public void selectVendorFirstVendor() {
114         setSelectIndex(XpathSelector.VENDOR_SELECT, 1);
115     }
116
117     /**
118      * Selects the default model.
119      */
120     public void selectDefaultModel() {
121         clickElement(XpathSelector.DEFAULT_MODEL_RADIO);
122     }
123
124     public void selectModel(final String model) {
125         if (model == null) {
126             selectDefaultModel();
127             return;
128         }
129         clickElement(XpathSelector.OTHER_MODEL_RADIO);
130         final WebElement modelSelect = findSubElement(wrappingElement, XpathSelector.MODEL_SELECT.getXpath());
131         modelSelect.sendKeys(model);
132         modelSelect.sendKeys(Keys.ENTER);
133     }
134
135     /**
136      * Selects a category in the category list based on the option value.
137      *
138      * @param categoryOptionValue the option value
139      */
140     public void selectCategory(final CategorySelect categoryOptionValue) {
141         setSelectValue(XpathSelector.CATEGORY_SELECT, categoryOptionValue.getOption());
142     }
143
144     /**
145      * Selects the network package onboarding procedure option.
146      *
147      * @param vspOnboardingProcedure the onboarding procedure to select
148      */
149     public void selectOnboardingProcedure(final VspOnboardingProcedure vspOnboardingProcedure) {
150         if (VspOnboardingProcedure.MANUAL == vspOnboardingProcedure) {
151             wrappingElement.findElement(By.xpath(XpathSelector.ONBOARDING_MANUAL_PROCEDURE_RADIO.getXpath())).click();
152             return;
153         }
154         if (VspOnboardingProcedure.NETWORK_PACKAGE == vspOnboardingProcedure) {
155             wrappingElement.findElement(By.xpath(XpathSelector.ONBOARDING_PACKAGE_PROCEDURE_RADIO.getXpath())).click();
156             return;
157         }
158         throw new UnsupportedOperationException(String.format("Onboarding procedure option '%s' not yet supported", vspOnboardingProcedure));
159     }
160
161     private void setInputValue(final XpathSelector inputTestId, final String value) {
162         findSubElement(wrappingElement, By.xpath(inputTestId.getXpath())).sendKeys(value);
163     }
164
165     private void setSelectIndex(final XpathSelector inputTestId, final int index) {
166         new Select(findSubElement(wrappingElement, By.xpath(inputTestId.getXpath()))).selectByIndex(index);
167     }
168
169     private void setSelectValue(final XpathSelector inputTestId, final String value) {
170         new Select(findSubElement(wrappingElement, By.xpath(inputTestId.getXpath()))).selectByValue(value);
171     }
172
173     private void clickElement(final XpathSelector elementTestId) {
174         wrappingElement.findElement(By.xpath(elementTestId.getXpath())).click();
175     }
176
177     /**
178      * Enum that contains identifiers and xpath expressions to elements related to the enclosing page object.
179      */
180     @AllArgsConstructor
181     private enum XpathSelector {
182         MODAL_XPATH("software-product-creation-page", "//div[@class='%s']"),
183         NAME_TXT("new-vsp-name", "//input[@data-test-id='%s']"),
184         VENDOR_SELECT("new-vsp-vendor", "//select[@data-test-id='%s']"),
185         CATEGORY_SELECT("new-vsp-category", "//select[@data-test-id='%s']"),
186         DESCRIPTION_TXT("new-vsp-description", "//textarea[@data-test-id='%s']"),
187         ONBOARDING_PACKAGE_PROCEDURE_RADIO("new-vsp-creation-procedure-heat", "//input[@data-test-id='%s']/parent::label"),
188         ONBOARDING_MANUAL_PROCEDURE_RADIO("new-vsp-creation-procedure-manual", "//input[@data-test-id='%s']/parent::label"),
189         DEFAULT_MODEL_RADIO("model-option-default", "//input[@data-test-id='%s']/parent::label"),
190         OTHER_MODEL_RADIO("model-option-other", "//input[@data-test-id='%s']/parent::label"),
191         MODEL_SELECT("model-option-select", "//div[@data-test-id='%s']//input"),
192         CREATE_BTN("form-submit-button", "//*[@data-test-id='%s']");
193
194         @Getter
195         private final String id;
196         private final String xpathFormat;
197
198         public String getXpath() {
199             return String.format(xpathFormat, id);
200         }
201     }
202
203 }