Catalog alignment
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / pages / ServiceGeneralPage.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.pages;
22
23 import com.aventstack.extentreports.Status;
24 import fj.data.Either;
25 import org.openecomp.sdc.be.model.Service;
26 import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum;
27 import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum.ServiceMetadataEnum;
28 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
29 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
30 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
31 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaDefinition;
32 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
33 import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
34 import org.openecomp.sdc.ci.tests.utils.ToscaParserUtils;
35 import org.openecomp.sdc.ci.tests.utils.Utils;
36 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
37 import org.openecomp.sdc.ci.tests.verificator.ToscaValidation;
38 import org.openqa.selenium.By;
39 import org.openqa.selenium.WebElement;
40
41 import java.io.File;
42 import java.util.ArrayList;
43 import java.util.Map;
44
45 public class ServiceGeneralPage extends ResourceGeneralPage {
46
47     public static ServiceLeftMenu getServiceLeftMenu() {
48         return new ServiceLeftMenu();
49     }
50
51     public static void defineName(String serviceName) {
52         WebElement serviceNameTextbox = GeneralUIUtils
53                 .getWebElementByTestID(DataTestIdEnum.ServiceMetadataEnum.SERVICE_NAME.getValue());
54         serviceNameTextbox.clear();
55         serviceNameTextbox.sendKeys(serviceName);
56     }
57
58
59     public static void defineServiceFunction(String serviceFunction) {
60         WebElement serviceFunctionTextbox = GeneralUIUtils
61                 .getWebElementByTestID(DataTestIdEnum.ServiceMetadataEnum.SERVICE_FUNCTION.getValue());
62         serviceFunctionTextbox.clear();
63         serviceFunctionTextbox.sendKeys(serviceFunction);
64     }
65
66     public static void defineProjectCode(String pmat) {
67         WebElement projectCodeTextbox = GeneralUIUtils
68                 .getWebElementByTestID(DataTestIdEnum.ServiceMetadataEnum.PROJECT_CODE.getValue());
69         projectCodeTextbox.clear();
70         projectCodeTextbox.sendKeys(pmat);
71     }
72     public static void defineNamingPolicy(String namingPolicyText) {
73         WebElement namingPolicyTextbox = GeneralUIUtils
74                 .getWebElementByTestID(DataTestIdEnum.ServiceMetadataEnum.NAMING_POLICY.getValue());
75         namingPolicyTextbox.clear();
76         namingPolicyTextbox.sendKeys(namingPolicyText);
77     }
78
79         public static String getServiceFunctionText(){
80                 return getServiceFunctionField().getAttribute("value");
81         }
82
83     public static String getNamingPolicyText(){
84         return getNamingPolicyField().getAttribute("value");
85     }
86
87     private static WebElement getNamingPolicyField() {
88         return GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ServiceMetadataEnum.NAMING_POLICY.getValue());
89     }
90
91
92     public static String getProjectCodeText() {
93         return getProjectCodeField().getAttribute("value");
94     }
95
96     public static String[] getTags() {
97         return ResourceGeneralPage.getElementsFromTagsTable().stream().map(WebElement::getText).toArray(String[]::new);
98     }
99     private static WebElement getServiceFunctionField() {
100         return GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ServiceMetadataEnum.SERVICE_FUNCTION.getValue());
101     }
102
103     private static WebElement getProjectCodeField() {
104         return GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ServiceMetadataEnum.PROJECT_CODE.getValue());
105     }
106
107     public static void deleteOldTags(ServiceReqDetails service) {
108         // Delete tag elements
109         int i = GeneralUIUtils.getWebElementsListByTestID("i-sdc-tag-delete").size();
110         while (i > 0) {
111             GeneralUIUtils.getWebElementByTestID("i-sdc-tag-delete").click();
112             i--;
113         }
114
115         service.setTags(new ArrayList<String>());
116     }
117
118     public static String getCategoryText() {
119         return GeneralUIUtils.getSelectedElementFromDropDown(getCategoryDataTestsIdAttribute()).getText();
120     }
121
122     public static String getInstantiationTypeChosenValue() {
123         return GeneralUIUtils.getSelectedElementFromDropDown(getInstantiationTypeIdAttribute()).getText();
124     }
125
126     private static String getInstantiationTypeIdAttribute() {
127         return ServiceMetadataEnum.INSTANTIATION_TYPE.getValue();
128     }
129
130     public static void clickAddWorkflow() {
131         SetupCDTest.getExtendTest().log(Status.INFO, "Adding workflow...");
132         GeneralUIUtils.clickOnElementByText("Add Workflow");
133     }
134
135     public static void fillAndAddNewWorkflow(String name, String description) throws InterruptedException {
136         GeneralUIUtils.ultimateWait();
137         SetupCDTest.getExtendTest().log(Status.INFO, String.format("Filling name field with %s", name));
138         insertText(name, "label + input");
139         SetupCDTest.getExtendTest().log(Status.INFO, String.format("Filling description filed with %s", name));
140         insertText(description, "label + textarea");
141         SetupCDTest.getExtendTest().log(Status.INFO, "Clicking save button ");
142         clickSave();
143     }
144
145     public static void insertText(String artifactDescriptoin, String element) throws InterruptedException {
146         GeneralUIUtils.getElementsByCSS("div[class='modal-content']");
147         WebElement artifactDescriptionTextbox = GeneralUIUtils.getWebElementBy(By.cssSelector(element));
148         artifactDescriptionTextbox.clear();
149         artifactDescriptionTextbox.sendKeys(artifactDescriptoin);
150         GeneralUIUtils.ultimateWait();
151     }
152
153     public static void clickSave() {
154         SetupCDTest.getExtendTest().log(Status.INFO, "Clicking on Save button");
155         GeneralUIUtils.clickOnElementByXpath("//*[@data-test-id='form-submit-button']");
156         GeneralUIUtils.ultimateWait();
157     }
158
159     public static void defineInstantiationType(String instantiationType) {
160         GeneralUIUtils.getSelectList(instantiationType, DataTestIdEnum.ServiceMetadataEnum.INSTANTIATION_TYPE.getValue());
161     }
162
163     public Service prepareServiceObject(ServiceReqDetails serviceMetadata) throws Exception {
164         return AtomicOperationUtils.getServiceObjectByNameAndVersion(UserRoleEnum.DESIGNER, serviceMetadata.getName(), "0.1");
165     }
166
167     public static boolean parseToscaFileIntoServiceAndValidateProperties(ServiceReqDetails serviceMetadata) throws Exception {
168         ServiceGeneralPage serviceGeneralPageObject = new ServiceGeneralPage();
169         Service service = serviceGeneralPageObject.prepareServiceObject(serviceMetadata);
170         Map<String, String> expectedMetadataMap = Utils.generateServiceMetadataToExpectedObject(serviceMetadata, service);
171         File latestFilefromDir = FileHandling.getLastModifiedFileNameFromDir();
172         ToscaDefinition toscaServiceDefinition = ToscaParserUtils.parseToscaMainYamlToJavaObjectByCsarLocation(latestFilefromDir);
173         Either<Boolean, Map<String, Object>> serviceToscaMetadataValidator = ToscaValidation.serviceToscaMetadataValidator(expectedMetadataMap, toscaServiceDefinition);
174         return serviceToscaMetadataValidator.isRight();
175     }
176
177 }