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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
19 package org.onap.sdc.frontend.ci.tests.execute.sanity;
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.hamcrest.Matchers.anEmptyMap;
23 import static org.hamcrest.Matchers.empty;
24 import static org.hamcrest.Matchers.emptyString;
25 import static org.hamcrest.Matchers.not;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.fail;
30 import java.util.Arrays;
32 import java.util.Optional;
33 import org.onap.sdc.backend.ci.tests.datatypes.enums.ResourceCategoryEnum;
34 import org.onap.sdc.backend.ci.tests.utils.general.ElementFactory;
35 import org.onap.sdc.frontend.ci.tests.datatypes.ResourceCreateData;
36 import org.onap.sdc.frontend.ci.tests.exception.UnzipException;
37 import org.onap.sdc.frontend.ci.tests.execute.setup.DriverFactory;
38 import org.onap.sdc.frontend.ci.tests.execute.setup.SetupCDTest;
39 import org.onap.sdc.frontend.ci.tests.flow.AddNodeToCompositionFlow;
40 import org.onap.sdc.frontend.ci.tests.flow.CreateVfFlow;
41 import org.onap.sdc.frontend.ci.tests.flow.CreateVfcFlow;
42 import org.onap.sdc.frontend.ci.tests.flow.DownloadCsarArtifactFlow;
43 import org.onap.sdc.frontend.ci.tests.flow.exception.UiTestFlowRuntimeException;
44 import org.onap.sdc.frontend.ci.tests.pages.ResourceCreatePage;
45 import org.onap.sdc.frontend.ci.tests.pages.component.workspace.ToscaArtifactsPage;
46 import org.onap.sdc.frontend.ci.tests.pages.home.HomePage;
47 import org.onap.sdc.frontend.ci.tests.utilities.FileHandling;
48 import org.openqa.selenium.WebDriver;
49 import org.testng.annotations.BeforeClass;
50 import org.testng.annotations.Test;
51 import org.yaml.snakeyaml.Yaml;
53 public class ImportVfcUiTest extends SetupCDTest {
55 private String filePath;
56 private WebDriver webDriver;
57 private HomePage homePage;
58 private ResourceCreateData vfcCreateData;
59 private ResourceCreateData vfCreateData;
62 public void beforeClass() {
63 filePath = FileHandling.getFilePath("importVfc/");
67 public void importVFC_createVF_addVFC2VF_test() throws UnzipException {
68 webDriver = DriverFactory.getDriver();
69 homePage = new HomePage(webDriver);
70 // TC - Import VFC with root namespace
71 String fileName = "org.openecomp.resource.VFC-root.yml";
72 CreateVfcFlow createVfcFlow = createVFC(fileName);
74 createVfcFlow.getLandedPage().get().clickOnCertify();
76 Map<String, Object> yamlObject = downloadToscaArtifact(createVfcFlow.getLandedPage().get());
77 checkMetadata(yamlObject, vfcCreateData);
78 checkNodeTypes(yamlObject);
79 homePage.getTopNavComponent().clickOnHome();
81 // TC - Import hierarchy of VFCs
82 fileName = "org.openecomp.resource.VFC-child.yml";
83 createVfcFlow = createVFC(fileName);
84 createVfcFlow.getLandedPage().get().clickOnCertify();
86 yamlObject = downloadToscaArtifact(createVfcFlow.getLandedPage().get());
87 checkMetadata(yamlObject, vfcCreateData);
88 checkNodeTypes(yamlObject);
89 homePage.getTopNavComponent().clickOnHome();
91 // TC - Import VFC with interface inputs
92 // TC - Import VFC with attributes
93 final CreateVfFlow createVfFlow = createVF();
95 final AddNodeToCompositionFlow addNodeToCompositionFlow = addNodeToCompositionFlow(createVfFlow);
97 createVfFlow.getLandedPage().get().clickOnCertify();
98 yamlObject = downloadToscaArtifact(createVfFlow.getLandedPage().get());
99 checkMetadata(yamlObject, vfCreateData);
100 checkTopologyTemplate(yamlObject);
104 private AddNodeToCompositionFlow addNodeToCompositionFlow(final CreateVfFlow createVfFlow) {
105 final AddNodeToCompositionFlow addNodeToCompositionFlow = new AddNodeToCompositionFlow(webDriver, vfCreateData, vfcCreateData);
106 addNodeToCompositionFlow.run(createVfFlow.getLandedPage().get());
107 return addNodeToCompositionFlow;
110 private Map<String, Object> downloadToscaArtifact(final ResourceCreatePage resourceCreatePage) throws UnzipException {
111 final DownloadCsarArtifactFlow downloadCsarArtifactFlow = downloadCsarArtifact(resourceCreatePage);
112 final ToscaArtifactsPage toscaArtifactsPage = downloadCsarArtifactFlow.getLandedPage()
113 .orElseThrow(() -> new UiTestFlowRuntimeException("Missing expected ToscaArtifactsPage"));
115 assertThat("No artifact download was found", toscaArtifactsPage.getDownloadedArtifactList(), not(empty()));
116 final String downloadedCsarName = toscaArtifactsPage.getDownloadedArtifactList().get(0);
117 final String downloadFolderPath = getConfig().getDownloadAutomationFolder();
118 final Map<String, byte[]> filesFromZip = FileHandling.getFilesFromZip(downloadFolderPath, downloadedCsarName);
119 final Optional<String> resourceEntryOpt = filesFromZip.keySet().stream()
120 .filter(s -> s.equals("Definitions/" + downloadedCsarName.replace("-csar.csar", "-template.yml")))
122 if (resourceEntryOpt.isEmpty()) {
123 fail("Could not find the resource package in Definitions");
125 return loadYamlObject(filesFromZip.get(resourceEntryOpt.get()));
128 private Map<String, Object> getMapEntry(final Map<String, Object> yamlObj, final String entryName) {
130 return (Map<String, Object>) yamlObj.get(entryName);
131 } catch (final Exception e) {
132 final String errorMsg = String.format("Could not get the '%s' entry.", entryName);
133 fail(errorMsg + "Error message: " + e.getMessage());
138 private Map<String, Object> loadYamlObject(final byte[] mainDefinitionFileBytes) {
139 return new Yaml().load(new String(mainDefinitionFileBytes));
142 private DownloadCsarArtifactFlow downloadCsarArtifact(final ResourceCreatePage resourceCreatePage) {
143 final DownloadCsarArtifactFlow downloadCsarArtifactFlow = new DownloadCsarArtifactFlow(webDriver);
144 downloadCsarArtifactFlow.setWaitBeforeGetTheFile(5L);
145 downloadCsarArtifactFlow.run(resourceCreatePage);
146 return downloadCsarArtifactFlow;
149 private CreateVfcFlow createVFC(final String fileName) {
150 vfcCreateData = createVfcFormData();
151 final CreateVfcFlow createVfcFlow = new CreateVfcFlow(webDriver, vfcCreateData, filePath + fileName);
152 createVfcFlow.run(homePage);
153 return createVfcFlow;
156 private CreateVfFlow createVF() {
157 vfCreateData = createVfFormData();
158 final CreateVfFlow createVfFlow = new CreateVfFlow(webDriver, vfCreateData);
159 createVfFlow.run(homePage);
163 private ResourceCreateData createVfcFormData() {
164 final ResourceCreateData vfcCreateData = new ResourceCreateData();
165 vfcCreateData.setRandomName(ElementFactory.getResourcePrefix() + "-VFC");
166 vfcCreateData.setCategory(ResourceCategoryEnum.NETWORK_L4.getSubCategory());
167 vfcCreateData.setTagList(Arrays.asList(vfcCreateData.getName(), "importVFC"));
168 vfcCreateData.setDescription("aDescription");
169 vfcCreateData.setVendorName("Ericsson");
170 vfcCreateData.setVendorRelease("1.2.3");
171 vfcCreateData.setVendorModelNumber("4.5.6");
172 return vfcCreateData;
175 private ResourceCreateData createVfFormData() {
176 final ResourceCreateData vfCreateData = new ResourceCreateData();
177 vfCreateData.setRandomName(ElementFactory.getResourcePrefix() + "-VF");
178 vfCreateData.setCategory(ResourceCategoryEnum.NETWORK_L4.getSubCategory());
179 vfCreateData.setTagList(Arrays.asList(vfCreateData.getName(), "createVF"));
180 vfCreateData.setDescription("aDescription");
181 vfCreateData.setVendorName("Ericsson");
182 vfCreateData.setVendorRelease("6.5.4");
183 vfCreateData.setVendorModelNumber("3.2.1");
187 private void checkMetadata(final Map<String, Object> map, final ResourceCreateData createdData) {
188 final Map<String, Object> metadata = getMapEntry(map, "metadata");
190 assertEquals(createdData.getName(), metadata.get("name"));
191 assertEquals(createdData.getDescription(), metadata.get("description"));
192 assertEquals("Network L4+", metadata.get("category"));
193 assertThat((String) metadata.get("type"), not(emptyString()));
194 assertEquals(createdData.getCategory(), metadata.get("subcategory"));
195 assertEquals(createdData.getVendorName(), metadata.get("resourceVendor"));
196 assertEquals(createdData.getVendorRelease(), metadata.get("resourceVendorRelease"));
197 assertEquals(createdData.getVendorModelNumber(), metadata.get("reourceVendorModelNumber"));
200 private void checkNodeTypes(final Map<String, Object> map) {
201 final Map<String, Object> mapEntry = getMapEntry(map, "node_types");
202 final Map<String, Object> nodeTypes = getMapEntry(mapEntry, mapEntry.keySet().iterator().next());
204 assertNotNull(nodeTypes);
205 assertEquals("aDescription", nodeTypes.get("description"));
207 final Map<String, Object> properties = getMapEntry(nodeTypes, "properties");
208 assertThat(properties, not(anEmptyMap()));
210 final Map<String, Object> attributes = getMapEntry(nodeTypes, "attributes");
211 assertThat(attributes, not(anEmptyMap()));
213 final Map<String, Object> interfaces = getMapEntry(nodeTypes, "interfaces");
214 assertThat(interfaces, not(anEmptyMap()));
218 private void checkTopologyTemplate(final Map<String, Object> map) {
219 final Map<String, Object> mapEntry = getMapEntry(map, "topology_template");
220 assertNotNull(mapEntry);
222 final Map<String, Object> properties = getMapEntry(mapEntry, "inputs");
223 assertThat(properties, not(anEmptyMap()));
225 final Map<String, Object> attributes = getMapEntry(mapEntry, "node_templates");
226 assertThat(attributes, not(anEmptyMap()));
228 final Map<String, Object> interfaces = getMapEntry(mapEntry, "substitution_mappings");
229 assertThat(interfaces, not(anEmptyMap()));