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