0854bec7856e749dbdb8caef20c8b70823007557
[sdc.git] /
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
20 package org.onap.sdc.frontend.ci.tests.execute.sanity;
21
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.empty;
24 import static org.hamcrest.Matchers.hasKey;
25 import static org.hamcrest.Matchers.is;
26 import static org.hamcrest.Matchers.not;
27 import static org.hamcrest.Matchers.notNullValue;
28 import static org.junit.Assert.fail;
29
30 import com.aventstack.extentreports.Status;
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 import java.util.Optional;
35 import org.apache.commons.io.FilenameUtils;
36 import org.junit.jupiter.api.Assertions;
37 import org.onap.sdc.backend.ci.tests.datatypes.enums.ServiceCategoriesEnum;
38 import org.onap.sdc.backend.ci.tests.datatypes.enums.UserRoleEnum;
39 import org.onap.sdc.frontend.ci.tests.datatypes.ServiceCreateData;
40 import org.onap.sdc.frontend.ci.tests.exception.UnzipException;
41 import org.onap.sdc.frontend.ci.tests.execute.setup.DriverFactory;
42 import org.onap.sdc.frontend.ci.tests.execute.setup.SetupCDTest;
43 import org.onap.sdc.frontend.ci.tests.flow.CheckEtsiNsPropertiesFlow;
44 import org.onap.sdc.frontend.ci.tests.flow.CreateServiceFlow;
45 import org.onap.sdc.frontend.ci.tests.flow.DownloadCsarArtifactFlow;
46 import org.onap.sdc.frontend.ci.tests.flow.EditServicePropertiesFlow;
47 import org.onap.sdc.frontend.ci.tests.flow.exception.UiTestFlowRuntimeException;
48 import org.onap.sdc.frontend.ci.tests.pages.ServiceComponentPage;
49 import org.onap.sdc.frontend.ci.tests.pages.TopNavComponent;
50 import org.onap.sdc.frontend.ci.tests.pages.component.workspace.ToscaArtifactsPage;
51 import org.onap.sdc.frontend.ci.tests.pages.home.HomePage;
52 import org.onap.sdc.frontend.ci.tests.utilities.FileHandling;
53 import org.openqa.selenium.WebDriver;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.testng.annotations.Test;
57 import org.yaml.snakeyaml.Yaml;
58
59 public class EtsiNetworkServiceUiTests extends SetupCDTest {
60
61     private static final Logger LOGGER = LoggerFactory.getLogger(EtsiNetworkServiceUiTests.class);
62
63     private WebDriver webDriver;
64
65     @Test
66     public void createEtsiNetworkService() throws UnzipException {
67         webDriver = DriverFactory.getDriver();
68
69         final CreateServiceFlow createServiceFlow = createService();
70         final CheckEtsiNsPropertiesFlow checkEtsiNsPropertiesFlow = checkServiceProperties();
71         final ServiceComponentPage serviceComponentPage = checkEtsiNsPropertiesFlow.getLandedPage()
72             .orElseThrow(() -> new UiTestFlowRuntimeException("Missing expected ServiceComponentPage"));
73
74         final Map<String, Object> propertyMap = createPropertyToEditMap();
75         editProperties(serviceComponentPage, propertyMap);
76
77         final DownloadCsarArtifactFlow downloadCsarArtifactFlow = downloadCsarArtifact(serviceComponentPage);
78         final ToscaArtifactsPage toscaArtifactsPage = downloadCsarArtifactFlow.getLandedPage()
79             .orElseThrow(() -> new UiTestFlowRuntimeException("Missing expected ToscaArtifactsPage"));
80
81         assertThat("No artifact download was found", toscaArtifactsPage.getDownloadedArtifactList(), not(empty()));
82
83         final String downloadedCsarName = toscaArtifactsPage.getDownloadedArtifactList().get(0);
84         checkEtsiNsPackage(createServiceFlow.getServiceCreateData().getName(), downloadedCsarName, propertyMap);
85     }
86
87     private CreateServiceFlow createService() {
88         final ServiceCreateData serviceCreateData = createServiceFormData();
89         final CreateServiceFlow createServiceFlow = new CreateServiceFlow(webDriver, serviceCreateData);
90         final TopNavComponent topNavComponent = new TopNavComponent(webDriver);
91         createServiceFlow.run(new HomePage(webDriver, topNavComponent));
92         return createServiceFlow;
93     }
94
95     private CheckEtsiNsPropertiesFlow checkServiceProperties() {
96         final CheckEtsiNsPropertiesFlow checkEtsiNsPropertiesFlow = new CheckEtsiNsPropertiesFlow(webDriver);
97         checkEtsiNsPropertiesFlow.run();
98         return checkEtsiNsPropertiesFlow;
99     }
100
101     private void editProperties(final ServiceComponentPage serviceComponentPage, final Map<String, Object> propertyMap) {
102         final EditServicePropertiesFlow editServicePropertiesFlow = new EditServicePropertiesFlow(webDriver, propertyMap);
103         editServicePropertiesFlow.run(serviceComponentPage);
104     }
105
106     private DownloadCsarArtifactFlow downloadCsarArtifact(final ServiceComponentPage serviceComponentPage) {
107         final DownloadCsarArtifactFlow downloadCsarArtifactFlow = new DownloadCsarArtifactFlow(webDriver);
108         downloadCsarArtifactFlow.run(serviceComponentPage);
109         return downloadCsarArtifactFlow;
110     }
111
112     private Map<String, Object> createPropertyToEditMap() {
113         final Map<String, Object> propertyMap = new HashMap<>();
114         propertyMap.put("designer", "designer1");
115         propertyMap.put("descriptor_id", "descriptor_id1");
116         propertyMap.put("flavour_id", "flavour_id1");
117         propertyMap.put("invariant_id", "invariant_id1");
118         propertyMap.put("name", "name1");
119         propertyMap.put("version", "version1");
120         propertyMap.put("service_availability_level", 1);
121         //does not work yet with TOSCA complex types
122         propertyMap.put("ns_profile", null);
123         return propertyMap;
124     }
125
126     private ServiceCreateData createServiceFormData() {
127         final ServiceCreateData serviceCreateData = new ServiceCreateData();
128         serviceCreateData.setRandomName("EtsiNfvNetworkService");
129         serviceCreateData.setCategory(ServiceCategoriesEnum.ETSI_NFV_NETWORK_SERVICE.getValue());
130         serviceCreateData.setEtsiVersion("2.5.1");
131         serviceCreateData.setDescription("aDescription");
132         return serviceCreateData;
133     }
134
135     private void checkEtsiNsPackage(final String serviceName, final String downloadedCsarName,
136                                     final Map<String, Object> expectedPropertyMap) throws UnzipException {
137         final String downloadFolderPath = getConfig().getDownloadAutomationFolder();
138         final Map<String, byte[]> filesFromZip = FileHandling.getFilesFromZip(downloadFolderPath, downloadedCsarName);
139         final Optional<String> etsiPackageEntryOpt =
140             filesFromZip.keySet().stream().filter(s -> s.startsWith("Artifacts/ETSI_PACKAGE")).findFirst();
141         if (etsiPackageEntryOpt.isEmpty()) {
142             Assertions.fail("Could not find the NSD package in Artifacts/ETSI_PACKAGE");
143         }
144         final String nodeType = String.format("org.openecomp.service.%s",
145             serviceName.substring(0, 1).toUpperCase() + serviceName.substring(1).toLowerCase());
146         final String etsiPackageEntry = etsiPackageEntryOpt.get();
147         final String nsdPackageBaseName = FilenameUtils.getBaseName(etsiPackageEntry);
148         final String nsdCsarFile = nsdPackageBaseName + ".csar";
149         final byte[] etsiPackageBytes = filesFromZip.get(etsiPackageEntry);
150         if (etsiPackageEntry.endsWith(".zip")) {
151             final Map<String, byte[]> nsPackageFileMap = FileHandling.getFilesFromZip(etsiPackageBytes);
152             assertThat("Expecting 3 files inside the NSD CSAR, the CSAR itself and its signature and certificate",
153                 nsPackageFileMap.size(), is(3));
154             assertThat("Expecting the NSD CSAR file " + nsdCsarFile, nsPackageFileMap, hasKey(nsdCsarFile));
155             final String nsdCsarSignature = nsdPackageBaseName + ".cms";
156             assertThat("Expecting the NSD CSAR signature " + nsdCsarSignature, nsPackageFileMap, hasKey(nsdCsarSignature));
157             final String nsdCertificate = nsdPackageBaseName + ".cert";
158             assertThat("Expecting the NSD CSAR certificate " + nsdCertificate, nsPackageFileMap, hasKey(nsdCertificate));
159             checkNsCsar(nsdPackageBaseName, nodeType, expectedPropertyMap, nsPackageFileMap.get(nsdCsarFile));
160             return;
161         }
162         if (etsiPackageEntry.endsWith(".csar")) {
163             final Map<String, byte[]> nsPackageFileMap = FileHandling.getFilesFromZip(etsiPackageBytes);
164             checkNsCsar(nsdPackageBaseName, nodeType, expectedPropertyMap, nsPackageFileMap.get(nsdCsarFile));
165             return;
166         }
167         fail(String.format("Unexpected ETSI NS PACKAGE entry '%s'. Expecting a '.csar' or '.zip'", etsiPackageEntry));
168     }
169
170     private void checkNsCsar(final String expectedServiceName, final String expectedServiceNodeType, final Map<String, Object> expectedPropertiesMap,
171                              final byte[] nsCsar) {
172         try {
173             final Map<String, byte[]> csarFileMap = FileHandling.getFilesFromZip(nsCsar);
174             final String mainDefinitionFile = String.format("Definitions/%s.yaml", expectedServiceName);
175             final byte[] mainDefinitionFileBytes = csarFileMap.get(mainDefinitionFile);
176             if (mainDefinitionFileBytes == null) {
177                 Assertions.fail(String.format("Could not find the Main Definition file in '%s'", mainDefinitionFile));
178             }
179
180             final Map<String, Object> mainDefinitionYamlMap = loadYamlObject(mainDefinitionFileBytes);
181             final Map<String, Object> topologyTemplateTosca = getMapEntry(mainDefinitionYamlMap, "topology_template");
182             assertThat(String.format("'%s' should contain a topology_template entry", mainDefinitionFile), topologyTemplateTosca, notNullValue());
183             final Map<String, Object> substitutionMappingsTosca = getMapEntry(topologyTemplateTosca, "substitution_mappings");
184             assertThat(String.format("'%s' should contain a substitution_mappings entry", mainDefinitionFile), substitutionMappingsTosca, notNullValue());
185             final String nodeType = (String) substitutionMappingsTosca.get("node_type");
186             assertThat("substitution_mappings->node_type should be as expected", nodeType, is(expectedServiceNodeType));
187
188             final Map<String, Object> nodeTemplatesTosca = getMapEntry(topologyTemplateTosca, "node_templates");
189             assertThat(String.format("'%s' should contain a node_templates entry", mainDefinitionFile), nodeTemplatesTosca, notNullValue());
190             final Map<String, Object> serviceNodeTemplate = getMapEntry(nodeTemplatesTosca, expectedServiceNodeType);
191             assertThat(String.format("'%s' should contain a '%s' entry in node_templates", mainDefinitionFile, expectedServiceNodeType),
192                 serviceNodeTemplate, notNullValue());
193             final Map<String, Object> properties = getMapEntry(serviceNodeTemplate, "properties");
194             assertThat(String.format("'%s' node template in '%s' should contain a properties entry", expectedServiceNodeType, mainDefinitionFile),
195                 properties, notNullValue());
196             assertThat(String.format("'%s' node template should contain '%s' properties", expectedServiceNodeType, expectedPropertiesMap.size()),
197                 properties.size(), is(expectedPropertiesMap.size()));
198             for (final Entry<String, Object> expectedPropertyEntry : expectedPropertiesMap.entrySet()) {
199                 final String expectedPropertyName = expectedPropertyEntry.getKey();
200                 assertThat(String.format("'%s' node template should contain the property '%s'", expectedServiceNodeType, expectedPropertyName),
201                     properties, hasKey(expectedPropertyName));
202                 final Object expectedPropertyValue = expectedPropertyEntry.getValue();
203                 if (expectedPropertyValue != null) {
204                     final Object actualPropertyValue = properties.get(expectedPropertyName);
205                     final String msg = String.format("The property '%s', in '%s' node template should have the expected value '%s'",
206                         expectedPropertyName, expectedServiceNodeType, actualPropertyValue);
207                     assertThat(msg, actualPropertyValue, is(expectedPropertyValue));
208                 }
209             }
210
211         } catch (final UnzipException e) {
212             final String errorMsg = "Could not unzip Network Service CSAR.";
213             LOGGER.info(errorMsg, e);
214             fail(String.format("%s Error: %s", errorMsg, e.getMessage()));
215         }
216     }
217
218     private Map<String, Object> getMapEntry(final Map<String, Object> yamlObj, final String entryName) {
219         try {
220             return (Map<String, Object>) yamlObj.get(entryName);
221         } catch (final Exception e) {
222             final String errorMsg = String.format("Could not get the '%s' entry.", entryName);
223             LOGGER.error(errorMsg, e);
224             fail(errorMsg + "Error message: " + e.getMessage());
225         }
226         return null;
227     }
228
229     private Map<String, Object> loadYamlObject(final byte[] mainDefinitionFileBytes) {
230         return new Yaml().load(new String(mainDefinitionFileBytes));
231     }
232
233 }