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