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