Sync Integ to Master
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / verificator / VfVerificator.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.apache.commons.lang3.tuple.ImmutablePair;
25 import org.json.simple.JSONArray;
26 import org.json.simple.JSONObject;
27 import org.json.simple.JSONValue;
28 import org.openecomp.sdc.be.model.LifecycleStateEnum;
29 import org.openecomp.sdc.be.model.Resource;
30 import org.openecomp.sdc.be.model.User;
31 import org.openecomp.sdc.be.model.category.CategoryDefinition;
32 import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum;
33 import org.openecomp.sdc.ci.tests.datatypes.LifeCycleStateEnum;
34 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
35 import org.openecomp.sdc.ci.tests.datatypes.VendorSoftwareProductObject;
36 import org.openecomp.sdc.ci.tests.datatypes.enums.PropertyTypeEnum;
37 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
38 import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
39 import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
40 import org.openecomp.sdc.ci.tests.pages.ResourceGeneralPage;
41 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
42 import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
43 import org.openecomp.sdc.ci.tests.utilities.ResourceUIUtils;
44 import org.openecomp.sdc.ci.tests.utilities.RestCDUtils;
45 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
46 import org.openqa.selenium.By;
47 import org.openqa.selenium.WebElement;
48 import org.testng.Assert;
49
50 import java.io.File;
51 import java.io.IOException;
52 import java.util.List;
53 import java.util.Map;
54
55 import static org.testng.Assert.*;
56
57 public final class VfVerificator {
58         
59         public static void verifyNumOfComponentInstances(ResourceReqDetails createResourceInUI, int numOfVFC, User user) {
60                 ServiceVerificator.verifyNumOfComponentInstances(createResourceInUI, createResourceInUI.getVersion(), numOfVFC, user);
61         }
62
63         public static void verifyRILocationChanged(ResourceReqDetails createResourceInUI,
64                         ImmutablePair<String, String> prevRIPos, User user) {
65
66                 ImmutablePair<String, String> currRIPos = ResourceUIUtils.getFirstRIPos(createResourceInUI, user);
67                 assertTrue(!prevRIPos.left.equals(currRIPos.left) || !prevRIPos.right.equals(currRIPos.right));
68         }
69
70         public static void verifyLinkCreated(ResourceReqDetails createResourceInUI, User user, int expectedRelationsSize) {
71                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verifing that a link was created on canvas"));
72                 String responseAfterDrag = RestCDUtils.getResource(createResourceInUI, user).getResponse();
73                 JSONObject jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
74                 assertTrue(((JSONArray) jsonResource.get("componentInstancesRelations")).size() == expectedRelationsSize);
75                 ExtentTestActions.log(Status.INFO, "The link was verified.");
76
77         }
78     
79 //      @Step(description="Verifying fields on General screen through UI ...")
80         public static void verifyVFMetadataInUI(ResourceReqDetails vf) {
81                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verifying fields on General screen through UI ..."));
82                 assertTrue(vf.getName().equals(ResourceGeneralPage.getNameText()));
83                 assertTrue(vf.getDescription().equals(ResourceGeneralPage.getDescriptionText()));
84                 assertTrue(vf.getCategories().get(0).getSubcategories().get(0).getName().equals(GeneralUIUtils.getSelectedElementFromDropDown(ResourceGeneralPage.getCategoryDataTestsIdAttribute()).getText()));
85                 assertTrue(vf.getVendorName().equals(ResourceGeneralPage.getVendorNameText()));
86                 assertTrue(vf.getVendorRelease().equals(ResourceGeneralPage.getVendorReleaseText()));
87                 assertTrue(vf.getContactId().equals(ResourceGeneralPage.getContactIdText()));
88                 List<WebElement> tagsList = ResourceGeneralPage.getElementsFromTagsTable();
89                 assertTrue(vf.getTags().size() == tagsList.size());
90                 for (int i = 0 ; i < vf.getTags().size(); i ++ ){
91                         assertTrue(vf.getTags().contains(tagsList.get(i).getText()));
92                 }
93                 assertTrue(vf.getContactId().equals(ResourceGeneralPage.getContactIdText()));
94         }
95         
96 //      @Step(description="Verifying fields on General screen through Backend ...")
97         public static void verifyVFUpdated(ResourceReqDetails vf, User user) {
98                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verifying fields on General screen through Backend ..."));
99                 String response = RestCDUtils.getResource(vf, user).getResponse();
100                 Resource resource = ResponseParser.convertResourceResponseToJavaObject(response);
101                 assertTrue(vf.getName().equals(resource.getName()));
102                 assertTrue(vf.getDescription().equals(resource.getDescription()));
103                 assertTrue(vf.getVendorName().equals(resource.getVendorName()));
104                 assertTrue(vf.getVendorRelease().equals(resource.getVendorRelease()));
105                 assertTrue(vf.getContactId().equals(resource.getContactId()));
106                 
107                 assertTrue(vf.getCategories().size() == (resource.getCategories().size()));
108                 for (int i = 0 ; i < vf.getCategories().size() ; i ++)
109                 {
110                         CategoryDefinition expectedCategoryDefinition = vf.getCategories().get(i);
111                         CategoryDefinition actualCategoryDefinition = resource.getCategories().get(i);
112                         assertTrue(expectedCategoryDefinition.getName().equals(actualCategoryDefinition.getName()));
113                         assertTrue(expectedCategoryDefinition.getSubcategories().get(i).getName().equals(actualCategoryDefinition.getSubcategories().get(i).getName()));
114                 }
115                 
116                 assertTrue(vf.getTags().size() == (resource.getTags().size()));
117                 for (int i = 0 ; i < vf.getTags().size() ; i ++){
118                         List<String> expectedTags = vf.getTags();
119                         List<String> actualTags = resource.getTags();
120                         
121                         assertTrue(actualTags.contains(expectedTags.get(i)));
122                         
123                 }                               
124         }
125
126         public static void verifyVFLifecycle(ResourceReqDetails vf, User user, LifecycleStateEnum expectedLifecycleState) {
127                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verfiying that object %s version is %s", vf.getName(),expectedLifecycleState));
128                 String responseAfterDrag = RestCDUtils.getResource(vf, user).getResponse();
129                 JSONObject jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
130                 String actualLifecycleState = jsonResource.get("lifecycleState").toString();
131                 assertTrue(expectedLifecycleState.name().equals(actualLifecycleState), "actual: " + actualLifecycleState + "-- expected: " + expectedLifecycleState);
132         }
133
134         public static void verifyVfLifecycleInUI(LifeCycleStateEnum lifecycleState){
135                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verfiying that object version is %s", lifecycleState.getValue()));
136                 GeneralUIUtils.ultimateWait();
137                 assertTrue(ResourceGeneralPage.getLifeCycleState().equals(lifecycleState.getValue()));
138         }
139         
140         public static void verifyInstanceVersion(ResourceReqDetails vf, User user, String instanceName, String instanceVersion){
141                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verfiying that instance %s version is %s", instanceName,instanceVersion));
142                 String responseAfterDrag = RestCDUtils.getResource(vf, user).getResponse();
143                 JSONObject jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
144                 JSONArray jsonArrayResource = (JSONArray) jsonResource.get("componentInstances");
145                 for (int i = 0; i < jsonArrayResource.size(); i++){
146                         Object object = jsonArrayResource.get(i);
147                         try{
148                                 JSONObject jRes = (JSONObject) JSONValue.parse(object.toString());
149                                 String componentName = jRes.get("componentName").toString();
150                                 if (componentName.equals(instanceName)){
151                                         String componentVersion = jRes.get("componentVersion").toString();
152                                         assertTrue(componentVersion.equals(instanceVersion));
153                                 }
154                         }
155                         catch(Exception e){
156                                 System.out.println("Can't test object in componentInstances array");
157                                 Assert.fail("Can't test object in componentInstances array");
158                         }
159                 }
160         }
161         
162         public static void verifyVfDeleted(ResourceReqDetails vf, User user){
163                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Validating resource %s was deleted", vf.getName()));
164                 RestResponse response = RestCDUtils.getResource(vf, user);
165                 assertTrue(response.getErrorCode().intValue() == 404);
166         }
167         
168         public static void verifyPropertiesInUI(List<PropertyTypeEnum> propertyList){
169                 
170                 for (PropertyTypeEnum prop : propertyList){
171                         String propName = prop.getName();
172                         
173                         String actualName = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.PropertiesPageEnum.PROPERTY_NAME.getValue() + propName).getText();
174                         String actualType = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.PropertiesPageEnum.PROPERTY_TYPE.getValue() + propName).getText();
175                         String actualDesciprtion = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.PropertiesPageEnum.PROPERTY_DESCRIPTION.getValue() + propName).getText();
176                         
177                         assertTrue(propName.equals(actualName), String.format("Property name is not correct. expected:%s ; actual %s", propName, actualName));
178                         assertTrue(prop.getType().equals(actualType), String.format("Property type is not correct. expected:%s ; actual %s", prop.getType(), actualType));
179                         assertTrue(prop.getDescription().equals(actualDesciprtion), String.format("Property description is not correct. expected:%s ; actual %s", prop.getDescription(), actualDesciprtion));
180                         
181                         if (prop.getSchemaDefinition() != null){
182                                 String actualSchema = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.PropertiesPageEnum.ENTRY_SCHEMA.getValue() + propName).getText();
183                                 assertTrue(prop.getSchemaDefinition().equals(actualSchema), String.format("Property schema is not correct. expected:%s ; actual %s", prop.getSchemaDefinition(), actualSchema));
184                         }
185                 }
186         }
187         
188         public static void verifyToscaArtifactsInfo(ResourceReqDetails vf, User user){
189                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Validating Tosca Aritfact Info of resource %s", vf.getName()));
190                 String responseAfterDrag = RestCDUtils.getResource(vf, user).getResponse();
191                 JSONObject jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
192                 JSONObject toscaArtifacts = (JSONObject) jsonResource.get("toscaArtifacts");
193                 
194                 assertEquals(2, toscaArtifacts.size());
195                 for (Object artifactObj : toscaArtifacts.keySet()){
196                         JSONObject artifact = (JSONObject) JSONValue.parse(toscaArtifacts.get(artifactObj).toString());
197                         assertFalse(artifact.get("artifactUUID").toString().isEmpty(), "artifactUUID field is empty");
198                         assertFalse(artifact.get("artifactChecksum").toString().isEmpty(), "artifactChecksum filed is empty");
199                         assertFalse(artifact.get("payloadUpdateDate").toString().isEmpty(), "payloadUpdateDate field is empty");
200                         assertFalse(artifact.get("artifactVersion").toString().equals("0"), "artifactVersion field is 0");
201                 }
202         }
203
204         public static void verifyVfInputs(String instanceName, Map<String, String> instancePropertiesMapFromJson,List<WebElement> propertyRowsFromTable) {
205                 
206                 for (int i = 0 ; i < propertyRowsFromTable.size() ; i++){
207                         WebElement row = propertyRowsFromTable.get(i);
208                         String propertyNameFromTable = row.findElement(By.xpath(".//*[@data-tests-id='" + "propertyName']")).getText();
209                         String propertyTypeFromTable = row.findElement(By.xpath(".//*[@data-tests-id='" + "propertyType']")).getText();
210                         String instanceNameFromTable = row.findElement(By.xpath(".//*[@data-tests-id='" + "instanceName']")).getText();
211                         String propertySchemaFromTable = row.findElement(By.xpath(".//*[@data-tests-id='" + "propertySchema']")).getText();
212                         
213                         assertTrue(instancePropertiesMapFromJson.containsKey(propertyNameFromTable), "No property named : " + propertyNameFromTable + "for instance " + instanceName);
214                         String expectedType = instancePropertiesMapFromJson.get(propertyNameFromTable);
215                         assertTrue(expectedType.equals(propertyTypeFromTable.toLowerCase()), propertyNameFromTable + "type is incorrect");
216                         assertTrue(instanceName.equals(instanceNameFromTable), "Instance name of property named " + propertyNameFromTable + "is incorrect");
217                 }
218         }
219
220         public static void verifyOnboardedVnfMetadata(String vspName, VendorSoftwareProductObject vspMetadata) {
221                 SetupCDTest.getExtendTest().log(Status.INFO, "Verifying metadata");
222                 assertTrue(vspName.equals(ResourceGeneralPage.getNameText()), "VSP name is not valid.");
223                 assertTrue(vspMetadata.getDescription().equals(ResourceGeneralPage.getDescriptionText()), "VSP description is not valid.");
224                 
225                 String [] splitedSubCategorey = vspMetadata.getSubCategory().split("\\.");
226                 String expectedSubCategory = splitedSubCategorey[splitedSubCategorey.length-1];
227                 String actualSubCategory = GeneralUIUtils.getSelectedElementFromDropDown(ResourceGeneralPage.getCategoryDataTestsIdAttribute()).getText().trim().toLowerCase();
228                 
229                 assertTrue(expectedSubCategory.equals(actualSubCategory), "VSP category is not valid.");
230                 assertTrue(vspMetadata.getVendorName().equals(ResourceGeneralPage.getVendorNameText()), "VSP vendor name is not valid.");
231                 assertTrue("1.0".equals(ResourceGeneralPage.getVendorReleaseText()), "VSP version is not valid.");
232                 List<WebElement> tagsList = ResourceGeneralPage.getElementsFromTagsTable();
233                 assertTrue(tagsList.size() == 1, "VSP tags size is not equal to 1.");
234                 assertTrue(vspName.equals(tagsList.get(0).getText()), "VSP tag is not its name.");
235                 assertTrue(vspMetadata.getAttContact().equals(ResourceGeneralPage.getContactIdText()), "VSP attContact is not valid.");
236         }
237         
238         public static void verifyIsElementDisabled(String elementLocator, String elementName){
239                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Checking if %s is disabled", elementName));
240                 assertTrue(GeneralUIUtils.isElementReadOnly(elementLocator));
241         }
242         
243         public static void verifyFilesChecksum(File actual, File expected){
244                 try {
245                         String actualMd5OfFile = FileHandling.getMD5OfFile(actual);
246                         String expectedMd5OfFile = FileHandling.getMD5OfFile(expected);
247                         Assert.assertEquals(expectedMd5OfFile, actualMd5OfFile, "File does not exist");
248                 } catch (IOException e) {
249                         e.printStackTrace();
250                 }
251                  
252         }
253         
254 }