bac613861b2e7fc847be8ca535101091d5c0c421
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / frontend / ci / tests / execute / sanity / ImportVfcUiTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19 package org.onap.sdc.frontend.ci.tests.execute.sanity;
20
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.hamcrest.Matchers.empty;
23 import static org.hamcrest.Matchers.emptyString;
24 import static org.hamcrest.Matchers.not;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.assertTrue;
29 import static org.junit.jupiter.api.Assertions.fail;
30
31 import com.aventstack.extentreports.Status;
32 import java.util.Arrays;
33 import java.util.Map;
34 import java.util.Optional;
35 import org.apache.commons.collections.MapUtils;
36 import org.onap.sdc.backend.ci.tests.datatypes.enums.ComponentType;
37 import org.onap.sdc.backend.ci.tests.datatypes.enums.ResourceCategoryEnum;
38 import org.onap.sdc.backend.ci.tests.utils.general.ElementFactory;
39 import org.onap.sdc.frontend.ci.tests.datatypes.ComponentData;
40 import org.onap.sdc.frontend.ci.tests.datatypes.ModelName;
41 import org.onap.sdc.frontend.ci.tests.datatypes.ResourceCreateData;
42 import org.onap.sdc.frontend.ci.tests.exception.UnzipException;
43 import org.onap.sdc.frontend.ci.tests.execute.setup.DriverFactory;
44 import org.onap.sdc.frontend.ci.tests.execute.setup.ExtentTestActions;
45 import org.onap.sdc.frontend.ci.tests.execute.setup.SetupCDTest;
46 import org.onap.sdc.frontend.ci.tests.flow.AddNodeToCompositionFlow;
47 import org.onap.sdc.frontend.ci.tests.flow.CreateVfFlow;
48 import org.onap.sdc.frontend.ci.tests.flow.CreateVfcFlow;
49 import org.onap.sdc.frontend.ci.tests.flow.DownloadCsarArtifactFlow;
50 import org.onap.sdc.frontend.ci.tests.flow.InterfaceDefinitionFlow;
51 import org.onap.sdc.frontend.ci.tests.flow.exception.UiTestFlowRuntimeException;
52 import org.onap.sdc.frontend.ci.tests.pages.AttributeModal;
53 import org.onap.sdc.frontend.ci.tests.pages.AttributesPage;
54 import org.onap.sdc.frontend.ci.tests.pages.ComponentPage;
55 import org.onap.sdc.frontend.ci.tests.pages.ResourceCreatePage;
56 import org.onap.sdc.frontend.ci.tests.pages.component.workspace.CompositionDetailSideBarComponent;
57 import org.onap.sdc.frontend.ci.tests.pages.component.workspace.CompositionDetailSideBarComponent.CompositionDetailTabName;
58 import org.onap.sdc.frontend.ci.tests.pages.component.workspace.CompositionInformationTab;
59 import org.onap.sdc.frontend.ci.tests.pages.component.workspace.CompositionInterfaceOperationsTab;
60 import org.onap.sdc.frontend.ci.tests.pages.component.workspace.CompositionPage;
61 import org.onap.sdc.frontend.ci.tests.pages.component.workspace.InterfaceDefinitionOperationsModal;
62 import org.onap.sdc.frontend.ci.tests.pages.component.workspace.InterfaceDefinitionPage;
63 import org.onap.sdc.frontend.ci.tests.pages.component.workspace.ToscaArtifactsPage;
64 import org.onap.sdc.frontend.ci.tests.pages.home.HomePage;
65 import org.onap.sdc.frontend.ci.tests.utilities.FileHandling;
66 import org.openecomp.sdc.be.model.ComponentInstance;
67 import org.openqa.selenium.WebDriver;
68 import org.testng.annotations.BeforeClass;
69 import org.testng.annotations.Test;
70 import org.yaml.snakeyaml.Yaml;
71
72 public class ImportVfcUiTest extends SetupCDTest {
73
74     private String filePath;
75     private WebDriver webDriver;
76     private HomePage homePage;
77     private ResourceCreateData vfcCreateData;
78     private ResourceCreateData vfCreateData;
79     private ComponentInstance createdComponentInstance;
80     private final String vfcCategory = ResourceCategoryEnum.NETWORK_L4.getSubCategory();
81
82     @BeforeClass
83     public void beforeClass() {
84         filePath = FileHandling.getFilePath("VFCs/");
85         webDriver = DriverFactory.getDriver();
86         homePage = new HomePage(webDriver);
87     }
88
89     @Test
90     public void importVFC_createVF_addVFC2VF_test() throws UnzipException {
91         ComponentPage componentPage;
92         // TC - Import VFC with root namespace
93         String fileName = "org.openecomp.resource.VFC-root.yml";
94         CreateVfcFlow createVfcFlow = createVFC(fileName, ModelName.DEFAULT_MODEL_NAME.getName(), vfcCategory);
95
96         componentPage = createVfcFlow.getLandedPage().orElseThrow(() -> new UiTestFlowRuntimeException("Missing expected return ResourceCreatePage"));
97         componentPage.isLoaded();
98         componentPage.certifyComponent();
99         componentPage.isLoaded();
100
101         Map<String, Object> yamlObject = downloadToscaArtifact(componentPage);
102         checkMetadata(yamlObject, vfcCreateData);
103         checkNodeTypes(yamlObject);
104         homePage.getTopNavComponent().clickOnHome();
105
106         // TC - Import hierarchy of VFCs
107         fileName = "org.openecomp.resource.VFC-child.yml";
108         createVfcFlow = createVFC(fileName, ModelName.DEFAULT_MODEL_NAME.getName(), vfcCategory);
109         componentPage = createVfcFlow.getLandedPage().orElseThrow(() -> new UiTestFlowRuntimeException("Missing expected return ResourceCreatePage"));
110         componentPage.isLoaded();
111
112         componentPage = manageAttributes(componentPage);
113         componentPage.isLoaded();
114         componentPage.certifyComponent();
115         componentPage.isLoaded();
116
117         yamlObject = downloadToscaArtifact(componentPage);
118         checkMetadata(yamlObject, vfcCreateData);
119         checkNodeTypes(yamlObject);
120
121         componentPage = viewInterfaceDefinitionFromVFC(componentPage);
122         componentPage.isLoaded();
123
124         homePage.getTopNavComponent().clickOnHome();
125
126         // TC - Import VFC with interface inputs
127         // TC - Import VFC with attributes
128         final CreateVfFlow createVfFlow = createVF();
129         componentPage = createVfFlow.getLandedPage().orElseThrow(() -> new UiTestFlowRuntimeException("Missing expected return ResourceCreatePage"));
130         componentPage.isLoaded();
131
132         final CompositionPage compositionPage = addInterfaceOperations(componentPage);
133         componentPage = compositionPage.goToGeneral();
134         componentPage.isLoaded();
135         componentPage.certifyComponent();
136         componentPage.isLoaded();
137
138         yamlObject = downloadToscaArtifact(componentPage);
139         checkMetadata(yamlObject, vfCreateData);
140         checkTopologyTemplate(yamlObject);
141
142     }
143
144     private ComponentPage viewInterfaceDefinitionFromVFC(final ComponentPage componentPage) {
145         final InterfaceDefinitionFlow interfaceDefinitionFlow = new InterfaceDefinitionFlow(webDriver);
146         interfaceDefinitionFlow.run(componentPage);
147         final InterfaceDefinitionPage interfaceDefinitionPage = interfaceDefinitionFlow.getLandedPage()
148             .orElseThrow(() -> new UiTestFlowRuntimeException("Missing expected return InterfaceDefinitionPage"));
149         final var OPERATION_NAME = "create";
150         assertTrue(interfaceDefinitionPage.isInterfaceDefinitionOperationPresent(OPERATION_NAME));
151         final InterfaceDefinitionOperationsModal interfaceDefinitionOperationsModal = interfaceDefinitionPage.clickOnInterfaceDefinitionOperation(
152             OPERATION_NAME);
153         interfaceDefinitionOperationsModal.isLoaded();
154         ExtentTestActions
155             .takeScreenshot(Status.INFO, "clickOnOInterfaceOperation", "Interface Definition Operation Modal opened");
156         checkInterfaceDefinitionData(interfaceDefinitionOperationsModal);
157         interfaceDefinitionOperationsModal.clickOnCancel();
158         return interfaceDefinitionPage;
159     }
160
161     @Test
162     public void importVfcWithModel() {
163         final String fileName = "VFC-For-Model.yaml";
164         final CreateVfcFlow createVfcFlow = createVFC(fileName, ModelName.ETSI_SOL001_v2_5_1.getName(), vfcCategory);
165         final ComponentPage componentPage = createVfcFlow.getLandedPage()
166             .orElseThrow(() -> new UiTestFlowRuntimeException("Missing expected return ResourceCreatePage"));
167         componentPage.isLoaded();
168         componentPage.certifyComponent();
169         componentPage.isLoaded();
170     }
171
172     private void checkInterfaceDefinitionData(final InterfaceDefinitionOperationsModal interfaceDefinitionOperationsModal) {
173         assertTrue(interfaceDefinitionOperationsModal.getDescription().isEmpty());
174         assertEquals(interfaceDefinitionOperationsModal.getImplementationName(), "path/to/my/implementation.sh");
175         assertEquals(interfaceDefinitionOperationsModal.getInputName(), "first");
176         assertEquals(interfaceDefinitionOperationsModal.getInputValue(), "1234");
177     }
178
179     private ComponentPage manageAttributes(final ComponentPage componentPage) {
180         final AttributesPage attributesPage = componentPage.goToAttributes();
181         attributesPage.isLoaded();
182
183         assertTrue(attributesPage.isAttributePresent("test_1"));
184         assertTrue(attributesPage.isAttributePresent("test_2"));
185         assertTrue(attributesPage.isAttributePresent("test_3"));
186         assertTrue(attributesPage.isAttributePresent("test_4"));
187
188         attributesPage.deleteAttribute("test_2");
189         assertFalse(attributesPage.isAttributePresent("test_2"));
190         ExtentTestActions.takeScreenshot(Status.INFO, "attributesPage.deleteAttribute", "Attribute 'test_2' successfully deleted");
191         attributesPage.addAttribute(new AttributeModal.AttributeData("test_9", "Additional attribute added from UI", "string", "one More Attribute"));
192         attributesPage.isLoaded();
193         assertTrue(attributesPage.isAttributePresent("test_9"));
194         ExtentTestActions.takeScreenshot(Status.INFO, "attributesPage.addAttribute", "Additional Attribute 'test_9' successfully added");
195
196         attributesPage.editAttribute(new AttributeModal.AttributeData("test_9", "Additional attribute added from UI".toUpperCase(), "string",
197             "one More Attribute".toUpperCase()));
198         attributesPage.isLoaded();
199         assertTrue(attributesPage.isAttributePresent("test_9"));
200         ExtentTestActions.takeScreenshot(Status.INFO, "attributesPage.editAttribute", "Additional Attribute 'test_9' successfully altered");
201
202         return attributesPage.clickOnGeneralMenuItem(ResourceCreatePage.class);
203     }
204
205     private CompositionPage addInterfaceOperations(final ComponentPage componentPage) {
206         final AddNodeToCompositionFlow addNodeToCompositionFlow = addNodeToCompositionFlow(componentPage);
207         final CompositionPage compositionPage = addNodeToCompositionFlow.getLandedPage()
208             .orElseThrow(() -> new UiTestFlowRuntimeException("Missing expected return CompositionPage"));
209         final CompositionDetailSideBarComponent detailSideBar = compositionPage.getDetailSideBar();
210         detailSideBar.isLoaded();
211
212         createdComponentInstance = addNodeToCompositionFlow.getCreatedComponentInstance()
213             .orElseThrow(() -> new UiTestFlowRuntimeException("Expecting a ComponentInstance"));
214
215         compositionPage.selectNode(createdComponentInstance.getName());
216
217         CompositionInterfaceOperationsTab compositionInterfaceOperationsTab =
218             (CompositionInterfaceOperationsTab) detailSideBar.selectTab(CompositionDetailTabName.INTERFACE_OPERATIONS);
219         compositionInterfaceOperationsTab.isLoaded();
220         ExtentTestActions.takeScreenshot(Status.INFO, "compositionInterfaceOperationsTab", "Composition Interface Operations Tab opened");
221         assertTrue(compositionInterfaceOperationsTab.isOperationPresent("create"));
222         InterfaceDefinitionOperationsModal compositionInterfaceOperationsModal = compositionInterfaceOperationsTab.clickOnOperation("create");
223         compositionInterfaceOperationsModal.isLoaded();
224         ExtentTestActions
225             .takeScreenshot(Status.INFO, "compositionInterfaceOperationsTab.clickOnOperation", "Composition Interface Operations Modal opened");
226         compositionInterfaceOperationsModal.clickOnDelete();
227         ExtentTestActions.takeScreenshot(Status.INFO, "compositionInterfaceOperationsModal.clickOnDelete", "Input deleted");
228         compositionInterfaceOperationsModal.addInput();
229         ExtentTestActions.takeScreenshot(Status.INFO, "compositionInterfaceOperationsModal.addInput", "Adding Input");
230
231         final InterfaceDefinitionOperationsModal.InterfaceOperationsData interfaceOperationsData = new InterfaceDefinitionOperationsModal.InterfaceOperationsData
232             ("This is CREATE operation", "fullPath/to/my/newImplementation.sh", "second", "9876");
233         compositionInterfaceOperationsModal.updateInterfaceOperation(interfaceOperationsData);
234         compositionInterfaceOperationsTab.isLoaded();
235
236         final CompositionInformationTab compositionInformationTab =
237             (CompositionInformationTab) detailSideBar.selectTab(CompositionDetailTabName.INFORMATION);
238         compositionInformationTab.isLoaded();
239
240         compositionInterfaceOperationsTab =
241             (CompositionInterfaceOperationsTab) detailSideBar.selectTab(CompositionDetailTabName.INTERFACE_OPERATIONS);
242         compositionInterfaceOperationsTab.isLoaded();
243
244         assertTrue(compositionInterfaceOperationsTab.isOperationPresent("create"));
245         assertTrue(compositionInterfaceOperationsTab.isDescriptionPresent());
246         ExtentTestActions.takeScreenshot(Status.INFO, "isDescriptionPresent", "Description is present");
247         compositionInterfaceOperationsModal = compositionInterfaceOperationsTab.clickOnOperation("create");
248         compositionInterfaceOperationsModal.isLoaded();
249         ExtentTestActions
250             .takeScreenshot(Status.INFO, "compositionInterfaceOperationsTab.clickOnOperation", "Composition Interface Operations Modal opened");
251
252         checkCompositionInterfaceOperations(compositionInterfaceOperationsModal, interfaceOperationsData);
253         compositionInterfaceOperationsModal.clickOnCancel();
254         compositionInterfaceOperationsTab.isLoaded();
255         return compositionPage;
256     }
257
258     private AddNodeToCompositionFlow addNodeToCompositionFlow(final ComponentPage componentPage) {
259         componentPage.isLoaded();
260         final ComponentData parentComponent = new ComponentData();
261         parentComponent.setName(vfCreateData.getName());
262         parentComponent.setVersion("0.1");
263         parentComponent.setComponentType(ComponentType.RESOURCE);
264         final ComponentData componentToAdd = new ComponentData();
265         componentToAdd.setName(vfcCreateData.getName());
266         componentToAdd.setVersion("1.0");
267         componentToAdd.setComponentType(ComponentType.RESOURCE);
268         final AddNodeToCompositionFlow addNodeToCompositionFlow = new AddNodeToCompositionFlow(webDriver, parentComponent, componentToAdd);
269         addNodeToCompositionFlow.run(componentPage.goToComposition());
270         return addNodeToCompositionFlow;
271     }
272
273     private Map<String, Object> downloadToscaArtifact(final ComponentPage componentPage) throws UnzipException {
274         final DownloadCsarArtifactFlow downloadCsarArtifactFlow = downloadCsarArtifact(componentPage);
275         final ToscaArtifactsPage toscaArtifactsPage = downloadCsarArtifactFlow.getLandedPage()
276             .orElseThrow(() -> new UiTestFlowRuntimeException("Missing expected ToscaArtifactsPage"));
277
278         assertThat("No artifact download was found", toscaArtifactsPage.getDownloadedArtifactList(), not(empty()));
279         final String downloadedCsarName = toscaArtifactsPage.getDownloadedArtifactList().get(0);
280         final String downloadFolderPath = getConfig().getDownloadAutomationFolder();
281         final Map<String, byte[]> filesFromZip = FileHandling.getFilesFromZip(downloadFolderPath, downloadedCsarName);
282         final Optional<String> resourceEntryOpt = filesFromZip.keySet().stream()
283             .filter(s -> s.equals("Definitions/" + downloadedCsarName.replace("-csar.csar", "-template.yml")))
284             .findFirst();
285         if (resourceEntryOpt.isEmpty()) {
286             fail("Could not find the resource package in Definitions");
287         }
288         return loadYamlObject(filesFromZip.get(resourceEntryOpt.get()));
289     }
290
291     private Map<String, Object> getMapEntry(final Map<String, Object> yamlObj, final String entryName) {
292         try {
293             return (Map<String, Object>) yamlObj.get(entryName);
294         } catch (final Exception e) {
295             final String errorMsg = String.format("Could not get the '%s' entry.", entryName);
296             fail(errorMsg + "Error message: " + e.getMessage());
297         }
298         return null;
299     }
300
301     private Map<String, Object> loadYamlObject(final byte[] mainDefinitionFileBytes) {
302         return new Yaml().load(new String(mainDefinitionFileBytes));
303     }
304
305     private DownloadCsarArtifactFlow downloadCsarArtifact(final ComponentPage componentPage) {
306         final DownloadCsarArtifactFlow downloadCsarArtifactFlow = new DownloadCsarArtifactFlow(webDriver);
307         downloadCsarArtifactFlow.setWaitBeforeGetTheFile(5L);
308         downloadCsarArtifactFlow.run(componentPage);
309         return downloadCsarArtifactFlow;
310     }
311
312     private CreateVfcFlow createVFC(final String fileName, final String model, final String category) {
313         vfcCreateData = createVfcFormData(model, category);
314         final CreateVfcFlow createVfcFlow = new CreateVfcFlow(webDriver, vfcCreateData, filePath + fileName);
315         createVfcFlow.run(homePage);
316         return createVfcFlow;
317     }
318
319     private CreateVfFlow createVF() {
320         vfCreateData = createVfFormData();
321         final CreateVfFlow createVfFlow = new CreateVfFlow(webDriver, vfCreateData);
322         createVfFlow.run(homePage);
323         return createVfFlow;
324     }
325
326     private ResourceCreateData createVfcFormData(final String model, final String category) {
327         final ResourceCreateData vfcCreateData = new ResourceCreateData();
328         vfcCreateData.setRandomName(ElementFactory.getResourcePrefix() + "-VFC");
329         vfcCreateData.setModel(model);
330         vfcCreateData.setCategory(category);
331         vfcCreateData.setTagList(Arrays.asList(vfcCreateData.getName(), "importVFC"));
332         vfcCreateData.setDescription("aDescription");
333         vfcCreateData.setVendorName("Ericsson");
334         vfcCreateData.setVendorRelease("1.2.3");
335         vfcCreateData.setVendorModelNumber("4.5.6");
336         return vfcCreateData;
337     }
338
339     private ResourceCreateData createVfFormData() {
340         final ResourceCreateData vfCreateData = new ResourceCreateData();
341         vfCreateData.setRandomName(ElementFactory.getResourcePrefix() + "-VF");
342         vfCreateData.setCategory(ResourceCategoryEnum.NETWORK_L4.getSubCategory());
343         vfCreateData.setTagList(Arrays.asList(vfCreateData.getName(), "createVF"));
344         vfCreateData.setDescription("aDescription");
345         vfCreateData.setVendorName("Ericsson");
346         vfCreateData.setVendorRelease("6.5.4");
347         vfCreateData.setVendorModelNumber("3.2.1");
348         return vfCreateData;
349     }
350
351     private void checkMetadata(final Map<String, Object> map, final ResourceCreateData createdData) {
352         final Map<String, Object> metadata = getMapEntry(map, "metadata");
353
354         assertEquals(createdData.getName(), metadata.get("name"));
355         assertEquals(createdData.getDescription(), metadata.get("description"));
356         assertEquals("Network L4+", metadata.get("category"));
357         assertThat((String) metadata.get("type"), not(emptyString()));
358         assertEquals(createdData.getCategory(), metadata.get("subcategory"));
359         assertEquals(createdData.getVendorName(), metadata.get("resourceVendor"));
360         assertEquals(createdData.getVendorRelease(), metadata.get("resourceVendorRelease"));
361         assertEquals(createdData.getVendorModelNumber(), metadata.get("reourceVendorModelNumber"));
362     }
363
364     private void checkNodeTypes(final Map<String, Object> map) {
365         final Map<String, Object> mapEntry = getMapEntry(map, "node_types");
366         final Map<String, Object> nodeTypes = getMapEntry(mapEntry, mapEntry.keySet().iterator().next());
367
368         assertFalse(MapUtils.isEmpty(nodeTypes));
369         assertEquals("aDescription", nodeTypes.get("description"));
370
371         final Map<String, Object> properties = getMapEntry(nodeTypes, "properties");
372         assertFalse(MapUtils.isEmpty(properties));
373
374         final Map<String, Object> attributes = getMapEntry(nodeTypes, "attributes");
375         assertFalse(MapUtils.isEmpty(attributes));
376
377         final Map<String, Object> interfaces = getMapEntry(nodeTypes, "interfaces");
378         assertFalse(MapUtils.isEmpty(interfaces));
379
380     }
381
382     private void checkTopologyTemplate(final Map<String, Object> map) {
383         final Map<String, Object> topologyTemplate = getMapEntry(map, "topology_template");
384         assertNotNull(topologyTemplate);
385
386         final Map<String, Object> inputs = getMapEntry(topologyTemplate, "inputs");
387         assertFalse(MapUtils.isEmpty(inputs));
388
389         final Map<String, Object> nodeTemplates = getMapEntry(topologyTemplate, "node_templates");
390         assertFalse(MapUtils.isEmpty(nodeTemplates));
391
392         final Map<String, Object> attributes = getMapEntry((Map<String, Object>) nodeTemplates.get(createdComponentInstance.getName()), "attributes");
393         assertFalse(MapUtils.isEmpty(attributes));
394         assertEquals(4, attributes.keySet().stream()
395             .filter(s -> (s.contains("test_1") || s.contains("test_3") || s.contains("test_4") || s.contains("test_9")) && !s.contains("test_2"))
396             .count());
397
398         final Map<String, Object> substitutionMappings = getMapEntry(topologyTemplate, "substitution_mappings");
399         assertFalse(MapUtils.isEmpty(substitutionMappings));
400
401     }
402
403     private void checkCompositionInterfaceOperations(final InterfaceDefinitionOperationsModal compositionInterfaceOperationsModal,
404                                                      final InterfaceDefinitionOperationsModal.InterfaceOperationsData interfaceOperationsData) {
405         assertEquals(interfaceOperationsData.getDescription(), compositionInterfaceOperationsModal.getDescription());
406         assertEquals(interfaceOperationsData.getImplementationName(), compositionInterfaceOperationsModal.getImplementationName());
407         assertEquals(interfaceOperationsData.getInputName(), compositionInterfaceOperationsModal.getInputName());
408         assertEquals(interfaceOperationsData.getInputValue(), compositionInterfaceOperationsModal.getInputValue());
409     }
410 }