Sync Integ to Master
[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 static org.testng.Assert.assertTrue;
24
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Random;
30 import java.util.function.Predicate;
31 import java.util.stream.Collectors;
32
33 import org.json.simple.JSONArray;
34 import org.json.simple.JSONObject;
35 import org.json.simple.JSONValue;
36 import org.openecomp.sdc.be.model.GroupInstance;
37 import org.openecomp.sdc.be.model.GroupInstanceProperty;
38 import org.openecomp.sdc.be.model.LifecycleStateEnum;
39 import org.openecomp.sdc.be.model.Service;
40 import org.openecomp.sdc.be.model.User;
41 import org.openecomp.sdc.ci.tests.datatypes.ComponentReqDetails;
42 import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum;
43 import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum.PropertiesPopupEnum;
44 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
45 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
46 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
47 import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
48 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
49 import org.openecomp.sdc.ci.tests.pages.CompositionPage;
50 import org.openecomp.sdc.ci.tests.pages.DeploymentPage;
51 import org.openecomp.sdc.ci.tests.pages.PropertyPopup;
52 import org.openecomp.sdc.ci.tests.pages.ResourceGeneralPage;
53 import org.openecomp.sdc.ci.tests.pages.ServiceGeneralPage;
54 import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
55 import org.openecomp.sdc.ci.tests.utilities.RestCDUtils;
56 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
57 import org.openqa.selenium.By;
58 import org.openqa.selenium.WebElement;
59 import org.openqa.selenium.support.ui.Select;
60
61 import com.aventstack.extentreports.Status;
62
63 public class ServiceVerificator {
64
65         private ServiceVerificator() {  
66         }       
67
68         public static void verifyNumOfComponentInstances(ComponentReqDetails component, String version, int numOfVFC,
69                         User user) {
70                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verifing the number of components on the canvas; should be %s", numOfVFC));  
71                 String responseAfterDrag = null;
72                 component.setVersion(version);
73                 if (component instanceof ServiceReqDetails) {
74                         responseAfterDrag = RestCDUtils.getService((ServiceReqDetails) component, user).getResponse();
75                 } else if (component instanceof ResourceReqDetails) {
76                         responseAfterDrag = RestCDUtils.getResource((ResourceReqDetails) component, user).getResponse();
77                 }
78                 int size = 0;
79                 JSONObject jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
80                 if(jsonResource.get("componentInstances")!= null){
81                         size = ((JSONArray) jsonResource.get("componentInstances")).size();
82                         assertTrue(size == numOfVFC, "Expected number of componenet instances is " + numOfVFC + ", but actual is " + size);
83                         ExtentTestActions.log(Status.INFO, "The number of components on the canvas was verified.");
84                 }else{
85                         assertTrue(false, "Expected number of componenet instances is " + numOfVFC + ", but actual is " + size);
86                 }
87         }
88         
89         public static void verifyServiceUpdatedInUI(ServiceReqDetails service) {
90                 assertTrue(service.getName().equals(ResourceGeneralPage.getNameText()));
91                 assertTrue(service.getDescription().equals(ResourceGeneralPage.getDescriptionText()));
92                 assertTrue(service.getCategory().equals(ServiceGeneralPage.getCategoryText()));
93                 assertTrue(service.getProjectCode().equals(ServiceGeneralPage.getProjectCodeText()));
94                 for(String tag: ServiceGeneralPage.getTags()){
95                         assertTrue(service.getTags().contains(tag));
96                 }
97                 assertTrue(service.getContactId().equals(ResourceGeneralPage.getContactIdText()));              
98         }
99         
100         public static void verifyServiceDeletedInUI(ServiceReqDetails service) throws InterruptedException {
101                 Thread.sleep(1000);
102                 List<WebElement> cardElements = GeneralUIUtils.getElementsByCSS(DataTestIdEnum.DashboardCardEnum.DASHBOARD_CARD.getValue());
103                 if (!(cardElements.isEmpty())){
104                         for (WebElement cardElement: cardElements){
105                                 WebElement componentName = GeneralUIUtils.getElementfromElementByCSS(cardElement, 
106                         DataTestIdEnum.DashboardCardEnum.INFO_NAME.getValue());
107                                 WebElement componentType = GeneralUIUtils.getElementfromElementByCSS(cardElement,
108                         DataTestIdEnum.DashboardCardEnum.ASSET_TYPE_CSS.getValue());
109                                 
110                                 String componentNameStr    = componentName.getAttribute("textContent").trim(), 
111                                            componentTypeStr    = componentType.getAttribute("class");
112                                 
113                                 if(componentTypeStr.equals("S")){
114                                         assertTrue( !(componentNameStr.equals(service.getName())), "Deleted service was found !!!");
115                                 }
116                         }
117                 }
118         }
119         
120         public static void verifyServiceLifecycle(ServiceReqDetails service, User user, LifecycleStateEnum expectedLifecycleState) {
121                 String responseAfterDrag = RestCDUtils.getService(service, user).getResponse();
122                 JSONObject jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
123                 String actualLifecycleState = jsonResource.get("lifecycleState").toString();
124                 assertTrue(expectedLifecycleState.name().equals(actualLifecycleState), "actual: " + actualLifecycleState + "-- expected: " + expectedLifecycleState);
125         }
126         
127         public static void verifyLinkCreated(ServiceReqDetails createServiceInUI, User user, int expectedRelationsSize) {
128                 String responseAfterDrag = RestCDUtils.getService(createServiceInUI, user).getResponse();
129                 JSONObject jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
130                 assertTrue(((JSONArray) jsonResource.get("componentInstancesRelations")).size() == expectedRelationsSize);
131
132         }
133         
134         public static void verifyManagmentWorkflow(String expectedName, String expectedDescription){
135                 String actualName = GeneralUIUtils.getWebElementBy(By.cssSelector("div[class='text name']")).getText();
136                 String actualDescription = GeneralUIUtils.getWebElementBy(By.cssSelector("div[class='text description']")).getText();
137                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verifing name ( should be %s ) and description ( should be %s ) ", expectedName, expectedDescription));
138                 assertTrue(actualName.equals(expectedName) && actualDescription.equals(expectedDescription));
139         }
140         
141         public static void verifyVersionUI(String expected){
142                 String actualVersion = GeneralUIUtils.getSelectedElementFromDropDown(DataTestIdEnum.GeneralElementsEnum.VERSION_HEADER.getValue()).getText().replace("V", "");
143                 assertTrue(actualVersion.equals(expected), String.format( "Expected version: %s, Actual version: %s", expected, actualVersion));
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());
328                 assertTrue(DeploymentPage.getPropertyErrorValidationMessdge().size() == 1);
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 }