re base code
[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 ServiceGeneralPage() {
48                 super();
49         }
50         
51         public static ServiceLeftMenu getServiceLeftMenu() {
52         return new ServiceLeftMenu();
53     }
54
55         public static void defineName(String serviceName) {
56                 WebElement serviceNameTextbox = GeneralUIUtils
57                                 .getWebElementByTestID(DataTestIdEnum.ServiceMetadataEnum.SERVICE_NAME.getValue());
58                 serviceNameTextbox.clear();
59                 serviceNameTextbox.sendKeys(serviceName);
60         }
61
62         public static void defineProjectCode(String pmat) {
63                 WebElement projectCodeTextbox = GeneralUIUtils
64                                 .getWebElementByTestID(DataTestIdEnum.ServiceMetadataEnum.PROJECT_CODE.getValue());
65                 projectCodeTextbox.clear();
66                 projectCodeTextbox.sendKeys(pmat);
67         }
68         
69         public static String getProjectCodeText(){
70                 return getProjectCodeField().getAttribute("value");
71         }
72         
73         public static String[] getTags(){
74                 return ResourceGeneralPage.getElementsFromTagsTable().stream().map(WebElement::getText).toArray(String[]::new);
75         }
76         
77         private static WebElement getProjectCodeField() {
78                 return GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ServiceMetadataEnum.PROJECT_CODE.getValue());
79         }
80         
81         public static void deleteOldTags(ServiceReqDetails service){
82                 // Delete tag elements
83                 int i = GeneralUIUtils.getWebElementsListByTestID("i-sdc-tag-delete").size();
84                 while (i > 0){                  
85                         GeneralUIUtils.getWebElementByTestID("i-sdc-tag-delete").click();
86                         i--;
87                 }
88                 
89                 service.setTags(new ArrayList<String>());
90         }
91         
92         public static String getCategoryText() {
93                 return GeneralUIUtils.getSelectedElementFromDropDown(getCategoryDataTestsIdAttribute()).getText();
94         }
95         
96         public static String getInstantiationTypeChosenValue() {
97                 return GeneralUIUtils.getSelectedElementFromDropDown(getInstantiationTypeIdAttribute()).getText();
98         }
99         
100         private static String getInstantiationTypeIdAttribute() {
101                 return ServiceMetadataEnum.INSTANTIATION_TYPE.getValue();
102         }
103         
104         public static void clickAddWorkflow (){
105                 SetupCDTest.getExtendTest().log(Status.INFO, "Adding workflow...");
106                 GeneralUIUtils.clickOnElementByText("Add Workflow");
107         }
108         
109         public static void fillAndAddNewWorkflow(String name, String description ) throws InterruptedException{
110                 GeneralUIUtils.ultimateWait();
111                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Filling name field with %s", name));
112                 insertText(name, "label + input");
113                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Filling description filed with %s", name));
114                 insertText(description,"label + textarea");
115                 SetupCDTest.getExtendTest().log(Status.INFO, "Clicking save button ");
116                 clickSave();
117         }
118         
119         public static void insertText(String artifactDescriptoin, String element) throws InterruptedException {
120                 GeneralUIUtils.getElementsByCSS("div[class='modal-content']");
121                 WebElement artifactDescriptionTextbox = GeneralUIUtils.getWebElementBy(By.cssSelector(element));
122                 artifactDescriptionTextbox.clear();
123                 artifactDescriptionTextbox.sendKeys(artifactDescriptoin);
124                 GeneralUIUtils.ultimateWait();          
125         }
126         
127         public static void clickSave() {
128                 SetupCDTest.getExtendTest().log(Status.INFO, "Clicking on Save button");
129                 GeneralUIUtils.clickOnElementByXpath("//*[@data-test-id='form-submit-button']");
130                 GeneralUIUtils.ultimateWait();
131         }
132         
133         public static void defineInstantiationType(String instantiationType) {
134                 GeneralUIUtils.getSelectList(instantiationType, DataTestIdEnum.ServiceMetadataEnum.INSTANTIATION_TYPE.getValue());
135         }
136         
137         public Service prepareServiceObject(ServiceReqDetails serviceMetadata) throws Exception {
138                 return AtomicOperationUtils.getServiceObjectByNameAndVersion(UserRoleEnum.DESIGNER, serviceMetadata.getName(), "0.1");
139         }
140         
141         public static boolean parseToscaFileIntoServiceAndValidateProperties(ServiceReqDetails serviceMetadata) throws Exception {
142                 ServiceGeneralPage serviceGeneralPageObject = new ServiceGeneralPage();
143                 Service service = serviceGeneralPageObject.prepareServiceObject(serviceMetadata);
144                 Map<String, String> expectedMetadataMap = Utils.generateServiceMetadataToExpectedObject(serviceMetadata, service);
145                 File latestFilefromDir = FileHandling.getLastModifiedFileNameFromDir();
146                 ToscaDefinition toscaServiceDefinition = ToscaParserUtils.parseToscaMainYamlToJavaObjectByCsarLocation(latestFilefromDir);
147                 Either<Boolean,Map<String, Object>> serviceToscaMetadataValidator = ToscaValidation.serviceToscaMetadataValidator(expectedMetadataMap, toscaServiceDefinition);
148                 return serviceToscaMetadataValidator.isRight();
149         }
150         
151 }