Fix NSD properties
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / frontend / ci / tests / execute / sanity / EtsiNetworkServiceUiTests.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
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         propertyMap.entrySet().removeIf(e -> e.getValue() == null);
83         checkEtsiNsPackage(createServiceFlow.getServiceCreateData().getName(), downloadedCsarName, propertyMap);
84     }
85
86     private CreateServiceFlow createService() {
87         final ServiceCreateData serviceCreateData = createServiceFormData();
88         final CreateServiceFlow createServiceFlow = new CreateServiceFlow(webDriver, serviceCreateData);
89         final TopNavComponent topNavComponent = new TopNavComponent(webDriver);
90         createServiceFlow.run(new HomePage(webDriver, topNavComponent));
91         return createServiceFlow;
92     }
93
94     private CheckEtsiNsPropertiesFlow checkServiceProperties() {
95         final CheckEtsiNsPropertiesFlow checkEtsiNsPropertiesFlow = new CheckEtsiNsPropertiesFlow(webDriver);
96         checkEtsiNsPropertiesFlow.run();
97         return checkEtsiNsPropertiesFlow;
98     }
99
100     private void editProperties(final ServiceComponentPage serviceComponentPage, final Map<String, Object> propertyMap) {
101         final EditServicePropertiesFlow editServicePropertiesFlow = new EditServicePropertiesFlow(webDriver, propertyMap);
102         editServicePropertiesFlow.run(serviceComponentPage);
103     }
104
105     private DownloadCsarArtifactFlow downloadCsarArtifact(final ServiceComponentPage serviceComponentPage) {
106         final DownloadCsarArtifactFlow downloadCsarArtifactFlow = new DownloadCsarArtifactFlow(webDriver);
107         downloadCsarArtifactFlow.run(serviceComponentPage);
108         return downloadCsarArtifactFlow;
109     }
110
111     private Map<String, Object> createPropertyToEditMap() {
112         final Map<String, Object> propertyMap = new HashMap<>();
113         propertyMap.put("designer", "designer1");
114         propertyMap.put("descriptor_id", "descriptor_id1");
115         propertyMap.put("flavour_id", "flavour_id1");
116         propertyMap.put("invariant_id", "invariant_id1");
117         propertyMap.put("name", "name1");
118         propertyMap.put("version", "version1");
119         propertyMap.put("service_availability_level", 1);
120         //does not work yet with TOSCA complex types
121         propertyMap.put("ns_profile", null);
122         return propertyMap;
123     }
124
125     private ServiceCreateData createServiceFormData() {
126         final ServiceCreateData serviceCreateData = new ServiceCreateData();
127         serviceCreateData.setRandomName("EtsiNfvNetworkService");
128         serviceCreateData.setCategory(ServiceCategoriesEnum.ETSI_NFV_NETWORK_SERVICE.getValue());
129         serviceCreateData.setEtsiVersion("2.5.1");
130         serviceCreateData.setDescription("aDescription");
131         return serviceCreateData;
132     }
133
134     private void checkEtsiNsPackage(final String serviceName, final String downloadedCsarName,
135                                     final Map<String, Object> expectedPropertyMap) throws UnzipException {
136         final String downloadFolderPath = getConfig().getDownloadAutomationFolder();
137         final Map<String, byte[]> filesFromZip = FileHandling.getFilesFromZip(downloadFolderPath, downloadedCsarName);
138         final Optional<String> etsiPackageEntryOpt =
139             filesFromZip.keySet().stream().filter(s -> s.startsWith("Artifacts/ETSI_PACKAGE")).findFirst();
140         if (etsiPackageEntryOpt.isEmpty()) {
141             Assertions.fail("Could not find the NSD package in Artifacts/ETSI_PACKAGE");
142         }
143         final String nodeType = String.format("org.openecomp.service.%s",
144             serviceName.substring(0, 1).toUpperCase() + serviceName.substring(1).toLowerCase());
145         final String etsiPackageEntry = etsiPackageEntryOpt.get();
146         final String nsdPackageBaseName = FilenameUtils.getBaseName(etsiPackageEntry);
147         final String nsdCsarFile = nsdPackageBaseName + ".csar";
148         final byte[] etsiPackageBytes = filesFromZip.get(etsiPackageEntry);
149         if (etsiPackageEntry.endsWith(".zip")) {
150             final Map<String, byte[]> nsPackageFileMap = FileHandling.getFilesFromZip(etsiPackageBytes);
151             assertThat("Expecting 3 files inside the NSD CSAR, the CSAR itself and its signature and certificate",
152                 nsPackageFileMap.size(), is(3));
153             assertThat("Expecting the NSD CSAR file " + nsdCsarFile, nsPackageFileMap, hasKey(nsdCsarFile));
154             final String nsdCsarSignature = nsdPackageBaseName + ".cms";
155             assertThat("Expecting the NSD CSAR signature " + nsdCsarSignature, nsPackageFileMap, hasKey(nsdCsarSignature));
156             final String nsdCertificate = nsdPackageBaseName + ".cert";
157             assertThat("Expecting the NSD CSAR certificate " + nsdCertificate, nsPackageFileMap, hasKey(nsdCertificate));
158             checkNsCsar(nsdPackageBaseName, nodeType, expectedPropertyMap, nsPackageFileMap.get(nsdCsarFile));
159             return;
160         }
161         if (etsiPackageEntry.endsWith(".csar")) {
162             final Map<String, byte[]> nsPackageFileMap = FileHandling.getFilesFromZip(etsiPackageBytes);
163             checkNsCsar(nsdPackageBaseName, nodeType, expectedPropertyMap, nsPackageFileMap.get(nsdCsarFile));
164             return;
165         }
166         fail(String.format("Unexpected ETSI NS PACKAGE entry '%s'. Expecting a '.csar' or '.zip'", etsiPackageEntry));
167     }
168
169     private void checkNsCsar(final String expectedServiceName, final String expectedServiceNodeType, final Map<String, Object> expectedPropertiesMap,
170                              final byte[] nsCsar) {
171         try {
172             final Map<String, byte[]> csarFileMap = FileHandling.getFilesFromZip(nsCsar);
173             final String mainDefinitionFile = String.format("Definitions/%s.yaml", expectedServiceName);
174             final byte[] mainDefinitionFileBytes = csarFileMap.get(mainDefinitionFile);
175             if (mainDefinitionFileBytes == null) {
176                 Assertions.fail(String.format("Could not find the Main Definition file in '%s'", mainDefinitionFile));
177             }
178
179             final Map<String, Object> mainDefinitionYamlMap = loadYamlObject(mainDefinitionFileBytes);
180             final Map<String, Object> topologyTemplateTosca = getMapEntry(mainDefinitionYamlMap, "topology_template");
181             assertThat(String.format("'%s' should contain a topology_template entry", mainDefinitionFile), topologyTemplateTosca, notNullValue());
182             final Map<String, Object> substitutionMappingsTosca = getMapEntry(topologyTemplateTosca, "substitution_mappings");
183             assertThat(String.format("'%s' should contain a substitution_mappings entry", mainDefinitionFile), substitutionMappingsTosca, notNullValue());
184             final String nodeType = (String) substitutionMappingsTosca.get("node_type");
185             assertThat("substitution_mappings->node_type should be as expected", nodeType, is(expectedServiceNodeType));
186
187             final Map<String, Object> nodeTemplatesTosca = getMapEntry(topologyTemplateTosca, "node_templates");
188             assertThat(String.format("'%s' should contain a node_templates entry", mainDefinitionFile), nodeTemplatesTosca, notNullValue());
189             final Map<String, Object> serviceNodeTemplate = getMapEntry(nodeTemplatesTosca, expectedServiceNodeType);
190             assertThat(String.format("'%s' should contain a '%s' entry in node_templates", mainDefinitionFile, expectedServiceNodeType),
191                 serviceNodeTemplate, notNullValue());
192             final Map<String, Object> properties = getMapEntry(serviceNodeTemplate, "properties");
193             assertThat(String.format("'%s' node template in '%s' should contain a properties entry", expectedServiceNodeType, mainDefinitionFile),
194                 properties, notNullValue());
195             assertThat(String.format("'%s' node template should contain '%s' properties", expectedServiceNodeType, expectedPropertiesMap.size()),
196                 properties.size(), is(expectedPropertiesMap.size()));
197             for (final Entry<String, Object> expectedPropertyEntry : expectedPropertiesMap.entrySet()) {
198                 final String expectedPropertyName = expectedPropertyEntry.getKey();
199                 assertThat(String.format("'%s' node template should contain the property '%s'", expectedServiceNodeType, expectedPropertyName),
200                     properties, hasKey(expectedPropertyName));
201                 final Object expectedPropertyValue = expectedPropertyEntry.getValue();
202                 if (expectedPropertyValue != null) {
203                     final Object actualPropertyValue = properties.get(expectedPropertyName);
204                     final String msg = String.format("The property '%s', in '%s' node template should have the expected value '%s'",
205                         expectedPropertyName, expectedServiceNodeType, actualPropertyValue);
206                     assertThat(msg, actualPropertyValue, is(expectedPropertyValue));
207                 }
208             }
209
210         } catch (final UnzipException e) {
211             final String errorMsg = "Could not unzip Network Service CSAR.";
212             LOGGER.info(errorMsg, e);
213             fail(String.format("%s Error: %s", errorMsg, e.getMessage()));
214         }
215     }
216
217     private Map<String, Object> getMapEntry(final Map<String, Object> yamlObj, final String entryName) {
218         try {
219             return (Map<String, Object>) yamlObj.get(entryName);
220         } catch (final Exception e) {
221             final String errorMsg = String.format("Could not get the '%s' entry.", entryName);
222             LOGGER.error(errorMsg, e);
223             fail(errorMsg + "Error message: " + e.getMessage());
224         }
225         return null;
226     }
227
228     private Map<String, Object> loadYamlObject(final byte[] mainDefinitionFileBytes) {
229         return new Yaml().load(new String(mainDefinitionFileBytes));
230     }
231
232 }
233