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