aboutsummaryrefslogtreecommitdiffstats
path: root/ui-ci/src/main/java/org/openecomp/sdc/ci/tests/verificator/VfVerificator.java
blob: db8d1b27e3a36e61ef0cdf7bb8ee2bbd06bb1d78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.ci.tests.verificator;


import com.aventstack.extentreports.Status;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.http.HttpStatus;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.openecomp.sdc.be.model.LifecycleStateEnum;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.category.CategoryDefinition;
import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum;
import org.openecomp.sdc.ci.tests.datatypes.LifeCycleStateEnum;
import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.VendorSoftwareProductObject;
import org.openecomp.sdc.ci.tests.datatypes.enums.PropertyTypeEnum;
import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
import org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest;
import org.openecomp.sdc.ci.tests.pages.ResourceGeneralPage;
import org.openecomp.sdc.ci.tests.utilities.FileHandling;
import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
import org.openecomp.sdc.ci.tests.utilities.ResourceUIUtils;
import org.openecomp.sdc.ci.tests.utilities.RestCDUtils;
import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

public final class VfVerificator {

    private VfVerificator() {
    }

    public static void verifyNumOfComponentInstances(ResourceReqDetails createResourceInUI, int numOfVFC, User user) {
        ServiceVerificator.verifyNumOfComponentInstances(createResourceInUI, createResourceInUI.getVersion(), numOfVFC, user);
    }

    public static void verifyRILocationChanged(ResourceReqDetails createResourceInUI,
                                               ImmutablePair<String, String> prevRIPos, User user) {

        ImmutablePair<String, String> currRIPos = ResourceUIUtils.getFirstRIPos(createResourceInUI, user);
        assertTrue(!prevRIPos.left.equals(currRIPos.left) || !prevRIPos.right.equals(currRIPos.right));
    }

    public static void verifyLinkCreated(ResourceReqDetails createResourceInUI, User user, int expectedRelationsSize) {
        SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verifying that a link was created on canvas"));
        String responseAfterDrag = RestCDUtils.getResource(createResourceInUI, user).getResponse();
        JSONObject jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
        assertTrue(((JSONArray) jsonResource.get("componentInstancesRelations")).size() == expectedRelationsSize);
        ExtentTestActions.log(Status.INFO, "The link was verified.");

    }

    public static void verifyVFMetadataInUI(ResourceReqDetails vf) {
        SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verifying fields on General screen through UI ..."));
        assertTrue(vf.getName().equals(ResourceGeneralPage.getNameText()));
        assertTrue(vf.getDescription().equals(ResourceGeneralPage.getDescriptionText()));
        assertTrue(vf.getCategories().get(0).getSubcategories().get(0).getName().equals(GeneralUIUtils.getSelectedElementFromDropDown(ResourceGeneralPage.getCategoryDataTestsIdAttribute()).getText()));
        assertTrue(vf.getVendorName().equals(ResourceGeneralPage.getVendorNameText()));
        assertTrue(vf.getVendorRelease().equals(ResourceGeneralPage.getVendorReleaseText()));
        assertTrue(vf.getContactId().equals(ResourceGeneralPage.getContactIdText()));
        List<WebElement> tagsList = ResourceGeneralPage.getElementsFromTagsTable();
        assertTrue(vf.getTags().size() == tagsList.size());
        for (int i = 0; i < vf.getTags().size(); i++) {
            assertTrue(vf.getTags().contains(tagsList.get(i).getText()));
        }
        assertTrue(vf.getContactId().equals(ResourceGeneralPage.getContactIdText()));
    }

    public static void verifyVFUpdated(ResourceReqDetails vf, User user) {
        SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verifying fields on General screen through Backend ..."));
        String response = RestCDUtils.getResource(vf, user).getResponse();
        Resource resource = ResponseParser.convertResourceResponseToJavaObject(response);
        assertTrue(vf.getName().equals(resource.getName()));
        assertTrue(vf.getDescription().equals(resource.getDescription()));
        assertTrue(vf.getVendorName().equals(resource.getVendorName()));
        assertTrue(vf.getVendorRelease().equals(resource.getVendorRelease()));
        assertTrue(vf.getContactId().equals(resource.getContactId()));

        assertTrue(vf.getCategories().size() == (resource.getCategories().size()));
        for (int i = 0; i < vf.getCategories().size(); i++) {
            CategoryDefinition expectedCategoryDefinition = vf.getCategories().get(i);
            CategoryDefinition actualCategoryDefinition = resource.getCategories().get(i);
            assertTrue(expectedCategoryDefinition.getName().equals(actualCategoryDefinition.getName()));
            assertTrue(expectedCategoryDefinition.getSubcategories().get(i).getName().equals(actualCategoryDefinition.getSubcategories().get(i).getName()));
        }

        assertTrue(vf.getTags().size() == (resource.getTags().size()));
        for (int i = 0; i < vf.getTags().size(); i++) {
            List<String> expectedTags = vf.getTags();
            List<String> actualTags = resource.getTags();

            assertTrue(actualTags.contains(expectedTags.get(i)));

        }
    }

    public static void verifyVFLifecycle(ResourceReqDetails vf, User user, LifecycleStateEnum expectedLifecycleState) {
        SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verfiying that object %s state is %s", vf.getName(), expectedLifecycleState));
        String responseAfterDrag = RestCDUtils.getResource(vf, user).getResponse();
        JSONObject jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
        String actualLifecycleState = jsonResource.get("lifecycleState").toString();
        assertTrue(expectedLifecycleState.name().equals(actualLifecycleState), "actual: " + actualLifecycleState + "-- expected: " + expectedLifecycleState);
    }

    public static void verifyVfLifecycleInUI(LifeCycleStateEnum lifecycleState) {
        SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verfiying that object state is %s", lifecycleState.getValue()));
        GeneralUIUtils.ultimateWait();
        assertTrue(ResourceGeneralPage.getLifeCycleState().equals(lifecycleState.getValue()));
    }

    public static void verifyVersionUI(String expected) {
        SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verifying that version is %s", expected));
        String actualVersion = GeneralUIUtils.getSelectedElementFromDropDown(DataTestIdEnum.
                GeneralElementsEnum.VERSION_HEADER.getValue()).getText().replace("V", "");
        assertTrue(actualVersion.equals(expected), String.format("Expected version: %s, Actual version: %s", expected, actualVersion));
    }

    public static void verifyInstanceVersion(ResourceReqDetails vf, User user, String instanceName, String instanceVersion) {
        SetupCDTest.getExtendTest().log(Status.INFO, String.format("Verfiying that instance %s version is %s", instanceName, instanceVersion));
        String responseAfterDrag = RestCDUtils.getResource(vf, user).getResponse();
        JSONObject jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
        JSONArray jsonArrayResource = (JSONArray) jsonResource.get("componentInstances");
        for (int i = 0; i < jsonArrayResource.size(); i++) {
            Object object = jsonArrayResource.get(i);
            try {
                JSONObject jRes = (JSONObject) JSONValue.parse(object.toString());
                String componentName = jRes.get("componentName").toString();
                if (componentName.equals(instanceName)) {
                    String componentVersion = jRes.get("componentVersion").toString();
                    assertTrue(componentVersion.equals(instanceVersion));
                }
            } catch (Exception e) {
                System.out.println("Can't test object in componentInstances array");
                Assert.fail("Can't test object in componentInstances array");
            }
        }
    }

    public static void verifyVfDeleted(ResourceReqDetails vf, User user) {
        SetupCDTest.getExtendTest().log(Status.INFO, String.format("Validating resource %s was deleted", vf.getName()));
        RestResponse response = RestCDUtils.getResource(vf, user);
        assertTrue(response.getErrorCode().intValue() == HttpStatus.SC_NOT_FOUND);
    }

    public static void verifyPropertiesInUI(List<PropertyTypeEnum> propertyList) {

        for (PropertyTypeEnum prop : propertyList) {
            String propName = prop.getName();

            String actualName = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.PropertiesPageEnum.PROPERTY_NAME.getValue() + propName).getText();
            String actualType = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.PropertiesPageEnum.PROPERTY_TYPE.getValue() + propName).getText();
            String actualDesciprtion = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.PropertiesPageEnum.PROPERTY_DESCRIPTION.getValue() + propName).getText();

            assertTrue(propName.equals(actualName), String.format("Property name is not correct. expected:%s ; actual %s", propName, actualName));
            assertTrue(prop.getType().equals(actualType), String.format("Property type is not correct. expected:%s ; actual %s", prop.getType(), actualType));
            assertTrue(prop.getDescription().equals(actualDesciprtion), String.format("Property description is not correct. expected:%s ; actual %s", prop.getDescription(), actualDesciprtion));

            if (prop.getSchemaDefinition() != null) {
                String actualSchema = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.PropertiesPageEnum.ENTRY_SCHEMA.getValue() + propName).getText();
                assertTrue(prop.getSchemaDefinition().equals(actualSchema), String.format("Property schema is not correct. expected:%s ; actual %s", prop.getSchemaDefinition(), actualSchema));
            }
        }
    }

    public static void verifyToscaArtifactsInfo(ResourceReqDetails vf, User user) {
        SetupCDTest.getExtendTest().log(Status.INFO, String.format("Validating Tosca Aritfact Info of resource %s", vf.getName()));
        String responseAfterDrag = RestCDUtils.getResource(vf, user).getResponse();
        JSONObject jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
        JSONObject toscaArtifacts = (JSONObject) jsonResource.get("toscaArtifacts");

        assertEquals(2, toscaArtifacts.size());
        for (Object artifactObj : toscaArtifacts.keySet()) {
            JSONObject artifact = (JSONObject) JSONValue.parse(toscaArtifacts.get(artifactObj).toString());
            assertFalse(artifact.get("artifactUUID").toString().isEmpty(), "artifactUUID field is empty");
            assertFalse(artifact.get("artifactChecksum").toString().isEmpty(), "artifactChecksum filed is empty");
            assertFalse(artifact.get("payloadUpdateDate").toString().isEmpty(), "payloadUpdateDate field is empty");
            assertFalse(artifact.get("artifactVersion").toString().equals("0"), "artifactVersion field is 0");
        }
    }

    public static void verifyVfInputs(String instanceName, Map<String, String> instancePropertiesMapFromJson, List<WebElement> propertyRowsFromTable) {

        for (int i = 0; i < propertyRowsFromTable.size(); i++) {
            WebElement row = propertyRowsFromTable.get(i);
            String propertyNameFromTable = row.findElement(By.xpath(".//*[@data-tests-id='" + "propertyName']")).getText();
            String propertyTypeFromTable = row.findElement(By.xpath(".//*[@data-tests-id='" + "propertyType']")).getText();
            String instanceNameFromTable = row.findElement(By.xpath(".//*[@data-tests-id='" + "instanceName']")).getText();
            String propertySchemaFromTable = row.findElement(By.xpath(".//*[@data-tests-id='" + "propertySchema']")).getText();

            assertTrue(instancePropertiesMapFromJson.containsKey(propertyNameFromTable), "No property named : " + propertyNameFromTable + "for instance " + instanceName);
            String expectedType = instancePropertiesMapFromJson.get(propertyNameFromTable);
            assertTrue(expectedType.equals(propertyTypeFromTable.toLowerCase()), propertyNameFromTable + "type is incorrect");
            assertTrue(instanceName.equals(instanceNameFromTable), "Instance name of property named " + propertyNameFromTable + "is incorrect");
        }
    }

    public static void verifyOnboardedVnfMetadata(String vspName, VendorSoftwareProductObject vspMetadata) {
        SetupCDTest.getExtendTest().log(Status.INFO, "Verifying metadata");

        assertTrue(vspName.equals(ResourceGeneralPage.getNameText()), "VSP name is not valid.");
        List<WebElement> tagsList = ResourceGeneralPage.getElementsFromTagsTable();
        assertTrue(tagsList.size() == 1, "VSP tags size is not equal to 1.");
        assertTrue(vspName.equals(tagsList.get(0).getText()), "VSP tag is not its name.");

        verifyMetadataIndifferentToFlow(vspMetadata);
    }

    public static void verifyOnboardedVnfMetadataAfterUpdateVNF(String vspName, VendorSoftwareProductObject vspMetadata) {
        SetupCDTest.getExtendTest().log(Status.INFO, "Verifying metadata after update VNF");

        // VF name should be updated only only if VF not certified
        if (Double.parseDouble(ResourceGeneralPage.getVersionUI()) < 1.0) {
            assertTrue(vspName.equals(ResourceGeneralPage.getNameText()), "VSP name is not valid.");
            List<WebElement> tagsList = ResourceGeneralPage.getElementsFromTagsTable();
            assertTrue(tagsList.size() == 1, "VSP tags size is not equal to 1.");
            assertTrue(vspName.equals(tagsList.get(0).getText()), "VSP tag is not its name.");
        }

        verifyMetadataIndifferentToFlow(vspMetadata);
    }

    public static void verifyMetadataIndifferentToFlow(VendorSoftwareProductObject vspMetadata) {
        assertTrue(vspMetadata.getDescription().equals(ResourceGeneralPage.getDescriptionText()), "VSP description is not valid.");

        String[] splitedSubCategorey = vspMetadata.getSubCategory().split("\\.");
        String expectedSubCategory = splitedSubCategorey[splitedSubCategorey.length - 1];
        String actualSubCategory = GeneralUIUtils.getSelectedElementFromDropDown(ResourceGeneralPage.getCategoryDataTestsIdAttribute()).getText().trim().toLowerCase();

        assertTrue(expectedSubCategory.equals(actualSubCategory), "VSP category is not valid.");
        assertTrue(vspMetadata.getVendorName().equals(ResourceGeneralPage.getVendorNameText()), "VSP vendor name is not valid.");
        assertTrue("1.0".equals(ResourceGeneralPage.getVendorReleaseText()), "VSP version is not valid.");
        assertTrue(vspMetadata.getAttContact().equals(ResourceGeneralPage.getContactIdText()), "VSP attContact is not valid.");
    }

    public static void verifyIsElementDisabled(String elementLocator, String elementName) {
        SetupCDTest.getExtendTest().log(Status.INFO, String.format("Checking if %s is disabled", elementName));
        assertTrue(GeneralUIUtils.isElementReadOnly(elementLocator));
    }

    public static void verifyFilesChecksum(File actual, File expected) {
        try {
            String actualMd5OfFile = FileHandling.getMD5OfFile(actual);
            String expectedMd5OfFile = FileHandling.getMD5OfFile(expected);
            Assert.assertEquals(expectedMd5OfFile, actualMd5OfFile, "File does not exist");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}