re base code
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / verificator / ServiceVerificator.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.verificator;
22
23 import com.aventstack.extentreports.Status;
24 import org.json.simple.JSONArray;
25 import org.json.simple.JSONObject;
26 import org.json.simple.JSONValue;
27 import org.openecomp.sdc.be.model.*;
28 import org.openecomp.sdc.ci.tests.datatypes.*;
29 import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum.PropertiesPopupEnum;
30 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
31 import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
32 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
33 import org.openecomp.sdc.ci.tests.pages.*;
34 import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
35 import org.openecomp.sdc.ci.tests.utilities.RestCDUtils;
36 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
37 import org.openqa.selenium.By;
38 import org.openqa.selenium.WebElement;
39 import org.openqa.selenium.support.ui.Select;
40
41 import java.util.*;
42 import java.util.function.Predicate;
43 import java.util.stream.Collectors;
44
45 import static org.testng.Assert.assertTrue;
46
47 public class ServiceVerificator {
48
49         private ServiceVerificator() {  
50         }       
51
52         public static void verifyNumOfComponentInstances(ComponentReqDetails component, String version, int numOfVFC,
53                         User user) {
54                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verifing the number of components on the canvas; should be %s", numOfVFC));  
55                 String responseAfterDrag = null;
56                 component.setVersion(version);
57                 if (component instanceof ServiceReqDetails) {
58                         responseAfterDrag = RestCDUtils.getService((ServiceReqDetails) component, user).getResponse();
59                 } else if (component instanceof ResourceReqDetails) {
60                         responseAfterDrag = RestCDUtils.getResource((ResourceReqDetails) component, user).getResponse();
61                 }
62                 int size = 0;
63                 JSONObject jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
64                 if(jsonResource.get("componentInstances")!= null){
65                         size = ((JSONArray) jsonResource.get("componentInstances")).size();
66                         assertTrue(size == numOfVFC, "Expected number of componenet instances is " + numOfVFC + ", but actual is " + size);
67                         ExtentTestActions.log(Status.INFO, "The number of components on the canvas was verified.");
68                 }else{
69                         assertTrue(false, "Expected number of componenet instances is " + numOfVFC + ", but actual is " + size);
70                 }
71         }
72         
73         public static void verifyServiceUpdatedInUI(ServiceReqDetails service) {
74                 assertTrue(service.getName().equals(ResourceGeneralPage.getNameText()));
75                 assertTrue(service.getDescription().equals(ResourceGeneralPage.getDescriptionText()));
76                 assertTrue(service.getCategory().equals(ServiceGeneralPage.getCategoryText()));
77                 assertTrue(service.getProjectCode().equals(ServiceGeneralPage.getProjectCodeText()));
78                 for(String tag: ServiceGeneralPage.getTags()){
79                         assertTrue(service.getTags().contains(tag));
80                 }
81                 assertTrue(service.getContactId().equals(ResourceGeneralPage.getContactIdText()));              
82         }
83         
84         public static void verifyServiceDeletedInUI(ServiceReqDetails service) throws InterruptedException {
85                 Thread.sleep(1000);
86                 List<WebElement> cardElements = GeneralUIUtils.getElementsByCSS(DataTestIdEnum.DashboardCardEnum.DASHBOARD_CARD.getValue());
87                 if (!(cardElements.isEmpty())){
88                         for (WebElement cardElement: cardElements){
89                                 WebElement componentName = GeneralUIUtils.getElementfromElementByCSS(cardElement, 
90                         DataTestIdEnum.DashboardCardEnum.INFO_NAME.getValue());
91                                 WebElement componentType = GeneralUIUtils.getElementfromElementByCSS(cardElement,
92                         DataTestIdEnum.DashboardCardEnum.ASSET_TYPE_CSS.getValue());
93                                 
94                                 String componentNameStr    = componentName.getAttribute("textContent").trim(), 
95                                            componentTypeStr    = componentType.getAttribute("class");
96                                 
97                                 if(componentTypeStr.equals("S")){
98                                         assertTrue( !(componentNameStr.equals(service.getName())), "Deleted service was found !!!");
99                                 }
100                         }
101                 }
102         }
103         
104         public static void verifyServiceLifecycle(ServiceReqDetails service, User user, LifecycleStateEnum expectedLifecycleState) {
105                 String responseAfterDrag = RestCDUtils.getService(service, user).getResponse();
106                 JSONObject jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
107                 String actualLifecycleState = jsonResource.get("lifecycleState").toString();
108                 assertTrue(expectedLifecycleState.name().equals(actualLifecycleState), "actual: " + actualLifecycleState + "-- expected: " + expectedLifecycleState);
109         }
110
111         public static void verifyServiceLifecycleInUI(LifeCycleStateEnum lifecycleState){
112                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verfiying that service state is %s", lifecycleState.getValue()));
113                 GeneralUIUtils.ultimateWait();
114                 assertTrue(ResourceGeneralPage.getLifeCycleState().equals(lifecycleState.getValue()));
115         }
116         
117         public static void verifyLinkCreated(ServiceReqDetails createServiceInUI, User user, int expectedRelationsSize) {
118                 String responseAfterDrag = RestCDUtils.getService(createServiceInUI, user).getResponse();
119                 JSONObject jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
120                 assertTrue(((JSONArray) jsonResource.get("componentInstancesRelations")).size() == expectedRelationsSize);
121
122         }
123         
124         public static void verifyManagmentWorkflow(String expectedName, String expectedDescription){
125                 String actualName = GeneralUIUtils.getWebElementBy(By.cssSelector("div[class='text name']")).getText();
126                 String actualDescription = GeneralUIUtils.getWebElementBy(By.cssSelector("div[class='text description']")).getText();
127                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verifing name ( should be %s ) and description ( should be %s ) ", expectedName, expectedDescription));
128                 assertTrue(actualName.equals(expectedName) && actualDescription.equals(expectedDescription));
129         }
130         
131         public static void verifyVersionUI(String expected){
132                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verifying that version is %s", expected));
133                 String actualVersion = GeneralUIUtils.getSelectedElementFromDropDown
134                                 (DataTestIdEnum.GeneralElementsEnum.VERSION_HEADER.getValue()).getText().replace("V", "");
135                 assertTrue(actualVersion.equals(expected), String.format( "Expected version: %s, Actual version: %s", expected, actualVersion));
136         }
137
138         public static void verifyResourceInstanceVersionUI(String expected){
139                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verifying that instance version is %s", expected));
140         List<WebElement> selectedVersion = GeneralUIUtils.findElementsByXpath
141                 ("//option[contains(@class,\"select-instance-version\") and contains (@selected, \"selected\")]");
142         String actual = selectedVersion.get(0).getText();
143         assertTrue(expected.equals(actual), String.format( "Expected version: %s, Actual version: %s", expected, actual));
144     }
145         
146         public static void verifyOpenTabTitle(DataTestIdEnum.CompositionScreenEnum currentTab) throws Exception{
147                 List<String> expectedTitles  = new ArrayList<String>();
148                 for(String expectedTitle: currentTab.getTitle()){
149                         expectedTitles.add(expectedTitle.toLowerCase());
150                 }               
151                 for (WebElement actualTitle: CompositionPage.getOpenTabTitle()){
152                         int indexOfTitle = expectedTitles.indexOf(actualTitle.getText().trim().toLowerCase());
153                         assertTrue(indexOfTitle >= 0, "Wrong title");
154                         expectedTitles.remove(indexOfTitle);
155                 }
156                 assertTrue(expectedTitles.size() == 0, "Missing titles in " + currentTab.getValue());
157         }
158         
159         public static void verifyDeploymentPageSubElements(String moduleName, DeploymentViewVerificator verificatorObj) throws Exception{
160                 HashMap<String, List<String>> moduleProperties = verificatorObj.getDeploymentViewData().get(moduleName);
161                 
162                 ServiceVerificator.moveMetadataPropertiesArtifactSection(-700);
163                 
164                 List<WebElement> artifacts, properties;
165                 artifacts = DeploymentPage.getArtifactNames();
166                 properties = DeploymentPage.getPropertyNames();
167                 assertTrue(moduleProperties.get("artifacts").size() == artifacts.size(), "Artifacts amount not as expected, expected " + moduleProperties.get("artifacts").size());
168                 assertTrue(moduleProperties.get("artifacts").containsAll(artifacts.stream().
169                                                                                 map(e -> e.getAttribute("textContent")).
170                                                                                 collect(Collectors.toList())));
171                 assertTrue(moduleProperties.get("properties").size() == properties.size(), "Properties amount not as expected, expected " + moduleProperties.get("properties").size());
172                 assertTrue(moduleProperties.get("properties").containsAll(properties.stream().
173                                                                                                                 map(e -> e.getAttribute("textContent")).
174                                                                                                                 collect(Collectors.toList())));
175                 
176                 DeploymentPage.clickOnProperties();
177                 DeploymentPage.clickOnArtifacts();
178                 ServiceVerificator.moveMetadataPropertiesArtifactSection(700);
179         }
180         
181         public static void verifyVFModuleCustomizationUUID(ServiceReqDetails service) throws Exception {
182                 Predicate<String> componentInstancePredicate = e -> e.length() > 35;
183                 List<String> customizationUUIDList = getAllVFModuleCustomizationUUIDs(service);
184                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Validating vfModuleCustomizationUUID uniqness ... "));
185                 assertTrue(customizationUUIDList.stream().allMatch(componentInstancePredicate), "vfModuleCustomizationUUID is less then 35 chars");
186                 CustomizationUUIDVerificator.validateCustomizationUUIDuniqueness(customizationUUIDList);
187         }
188
189         public static List<String> getAllVFModuleCustomizationUUIDs(ServiceReqDetails service) throws Exception {
190                 Service serviceObj = AtomicOperationUtils.getServiceObjectByNameAndVersion(UserRoleEnum.DESIGNER, service.getName(), service.getVersion());
191                 List<String> customizationUUIDList = serviceObj.getComponentInstances().get(0).getGroupInstances().stream().
192                                                                                      map(e -> e.getCustomizationUUID()).
193                                                                                      collect(Collectors.toList());
194                 
195                 return customizationUUIDList;
196         }
197         
198         public static String getVFModulePropertyValue(ServiceReqDetails service, String propertyName, String moduleName) throws Exception {
199                 Service serviceObj = AtomicOperationUtils.getServiceObjectByNameAndVersion(UserRoleEnum.DESIGNER, service.getName(), service.getVersion());     
200                 List<GroupInstance> groupInstances = serviceObj.getComponentInstances().get(0).getGroupInstances();             
201                 List<GroupInstanceProperty> groupInstancesProperties = groupInstances.stream().
202                                                                                               filter(e -> e.getName().equals(moduleName)).
203                                                                                               findFirst().
204                                                                                               get().
205                                                                                               convertToGroupInstancesProperties();              
206                 String propertyValue = groupInstancesProperties.stream().
207                                                                          filter(e -> e.getName().equals(propertyName)).
208                                                                          findFirst().
209                                                                          get().
210                                                                          getValue();
211                 return propertyValue;
212         }
213         
214         public static boolean isEqualCustomizationUUIDsAfterChanges(List<String> listBefore, List<String> listAfter){
215                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Validating if vfModuleCustomizationUUID changed after certification ... "));
216                 return (listBefore.size() == listAfter.size()) && (listBefore.containsAll(listAfter));
217         }
218
219         public static void verifyDisabledServiceProperties() throws Exception{
220                 List<String> propertiesForCheck = Arrays.asList("isBase", "vf_module_type", "vf_module_label", "vf_module_description");
221                 List<PropertiesPopupEnum> popupElementsForCheck = Arrays.asList(PropertiesPopupEnum.PROPERTY_NAME, 
222                                                                                         PropertiesPopupEnum.PROPERTY_DESCRIPTION, 
223                                                                                         PropertiesPopupEnum.PROPERTY_TYPE, 
224                                                                                         PropertiesPopupEnum.PROPERTY_VALUE);
225                 ServiceVerificator.moveMetadataPropertiesArtifactSection(-700);
226                 List<WebElement> properties = DeploymentPage.getPropertyNames();
227                 
228                 for(WebElement property : properties){
229                         if (propertiesForCheck.contains(property.getAttribute("textContent"))){
230                                 DeploymentPage.clickOnProperty(property);
231                                 Select propertTypeElement = new Select(GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.PropertiesPopupEnum.PROPERTY_TYPE.getValue()));
232                                 boolean isTypeBoolean = propertTypeElement.getFirstSelectedOption().getText().contains("boolean");
233                                 for (PropertiesPopupEnum popupElement: popupElementsForCheck){
234                                         if (isTypeBoolean && popupElement == PropertiesPopupEnum.PROPERTY_VALUE){
235                                                 assertTrue(GeneralUIUtils.checkForDisabledAttribute(DataTestIdEnum.PropertiesPopupEnum.PROPERTY_BOOLEAN_VALUE.getValue()), String.format("Element %s not disabled ", property.getText()));
236                                         } else {
237                                                 assertTrue(GeneralUIUtils.checkForDisabledAttribute(popupElement.getValue()), String.format("Element %s not disabled ", property.getText()));
238                                         }                                       
239                                 }
240                                 new PropertyPopup().clickCancel();
241                         }
242                 }
243                 
244                 DeploymentPage.clickOnProperties();
245                 ServiceVerificator.moveMetadataPropertiesArtifactSection(700);
246         }
247
248         public static void verifyEnabledServiceProperties() throws Exception{
249                 List<String> propertiesForCheck = Arrays.asList("initial_count", "max_vf_module_instances", "min_vf_module_instances");
250                 
251                 ServiceVerificator.moveMetadataPropertiesArtifactSection(-700);
252                 List<WebElement> properties = DeploymentPage.getPropertyNames();
253                 
254                 ServiceVerificator.positiveFlow(propertiesForCheck, properties);
255                 ServiceVerificator.negativeFlow(propertiesForCheck, properties);
256                 
257                 DeploymentPage.clickOnProperties();
258                 ServiceVerificator.moveMetadataPropertiesArtifactSection(700);                  
259         }
260
261         public static void positiveFlow(List<String> propertiesForCheck, List<WebElement> properties)
262                         throws InterruptedException {
263                 int baseNumber = new Random().nextInt(100) + 2;
264                 for(WebElement property : properties){
265                         String propertyName = property.getAttribute("textContent");
266                         if (propertiesForCheck.contains(propertyName)){
267                                 DeploymentPage.clickOnProperty(property);                               
268                                 int actualNumber = 0;
269                                 if (propertyName.equals("initial_count")){
270                                         actualNumber = baseNumber;
271                                 } else if (propertyName.equals("max_vf_module_instances")) {
272                                         actualNumber = baseNumber + 1;
273                                 } else if (propertyName.equals("min_vf_module_instances")){
274                                         actualNumber = baseNumber - 1;                          
275                                 }
276                                 
277                                 new PropertyPopup().insertPropertyDefaultValue(String.valueOf(actualNumber));
278                                 new PropertyPopup().clickSave();
279                                 assertTrue(DeploymentPage.getPropertyValueFromPropertiesList(propertyName).equals(String.valueOf(actualNumber)));
280                         }
281                 }
282         }
283
284         public static void negativeFlow(List<String> propertiesForCheck, List<WebElement> properties)
285                                 throws Exception {
286                         int currentMaxValue = Integer.valueOf(DeploymentPage.getPropertyValueFromPropertiesList("max_vf_module_instances"));
287                         int currentMinValue = Integer.valueOf(DeploymentPage.getPropertyValueFromPropertiesList("min_vf_module_instances"));
288                         int currentInitialValue = Integer.valueOf(DeploymentPage.getPropertyValueFromPropertiesList("initial_count"));          
289                         PropertyPopup propertyPopupObj = new PropertyPopup();
290                         
291                         for(WebElement property : properties){
292                                 String propertyName = property.getAttribute("textContent");
293                                 if (propertiesForCheck.contains(propertyName)){
294                                         DeploymentPage.clickOnProperty(property);                               
295                                         if (propertyName.equals("initial_count")){
296                                                 
297                                                 propertyPopupObj.insertPropertyDefaultValue(String.valueOf(currentMaxValue + 1));
298                                                 ServiceVerificator.verifyErrorPresentAndSaveDisabled();
299                                                 propertyPopupObj.insertPropertyDefaultValue(String.valueOf(currentMinValue - 1));
300                                                 ServiceVerificator.verifyErrorPresentAndSaveDisabled();
301                                                 propertyPopupObj.insertPropertyDefaultValue(String.valueOf(0));
302                                                 ServiceVerificator.verifyErrorPresentAndSaveDisabled();
303                                                 
304                                         } else if (propertyName.equals("max_vf_module_instances")) {
305                                                 
306                                                 propertyPopupObj.insertPropertyDefaultValue(String.valueOf(currentInitialValue - 1));
307                                                 ServiceVerificator.verifyErrorPresentAndSaveDisabled();
308                                                 propertyPopupObj.insertPropertyDefaultValue(String.valueOf(currentMinValue - 1));
309                                                 ServiceVerificator.verifyErrorPresentAndSaveDisabled();
310                                                 propertyPopupObj.insertPropertyDefaultValue(String.valueOf(0));
311                                                 verifyErrorPresentAndSaveDisabled();
312                                                 
313                                         } else if (propertyName.equals("min_vf_module_instances")){
314                                                 
315                                                 propertyPopupObj.insertPropertyDefaultValue(String.valueOf(currentInitialValue + 1));
316                                                 ServiceVerificator.verifyErrorPresentAndSaveDisabled();
317                                                 propertyPopupObj.insertPropertyDefaultValue(String.valueOf(currentMaxValue + 1));
318                                                 ServiceVerificator.verifyErrorPresentAndSaveDisabled();                         
319                                         }                               
320                                                                         
321                                         new PropertyPopup().clickCancel();
322                                 }
323                         }
324                 }
325
326         public static void verifyErrorPresentAndSaveDisabled() throws Exception{
327                 assertTrue(DeploymentPage.isPropertySaveButtonDisabled(), "Property Save button enabled, should be disabled");
328                 assertTrue(DeploymentPage.getPropertyErrorValidationMessdge().size() == 1, "Error msg missing for input");
329         }
330
331         public static void moveMetadataPropertiesArtifactSection(int offset) throws InterruptedException {
332                 WebElement dragLineElement = GeneralUIUtils.getElementByCSS("div.rg-top");
333                 GeneralUIUtils.dragAndDropElementByY(dragLineElement, offset);
334         }
335         
336     
337 }