aboutsummaryrefslogtreecommitdiffstats
path: root/integration-tests/src/test/java/org/onap/sdc/frontend/ci/tests/execute/sanity/EtsiNetworkServiceUiTests.java
blob: 0854bec7856e749dbdb8caef20c8b70823007557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2021 Nordix Foundation
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.onap.sdc.frontend.ci.tests.execute.sanity;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.fail;

import com.aventstack.extentreports.Status;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import org.apache.commons.io.FilenameUtils;
import org.junit.jupiter.api.Assertions;
import org.onap.sdc.backend.ci.tests.datatypes.enums.ServiceCategoriesEnum;
import org.onap.sdc.backend.ci.tests.datatypes.enums.UserRoleEnum;
import org.onap.sdc.frontend.ci.tests.datatypes.ServiceCreateData;
import org.onap.sdc.frontend.ci.tests.exception.UnzipException;
import org.onap.sdc.frontend.ci.tests.execute.setup.DriverFactory;
import org.onap.sdc.frontend.ci.tests.execute.setup.SetupCDTest;
import org.onap.sdc.frontend.ci.tests.flow.CheckEtsiNsPropertiesFlow;
import org.onap.sdc.frontend.ci.tests.flow.CreateServiceFlow;
import org.onap.sdc.frontend.ci.tests.flow.DownloadCsarArtifactFlow;
import org.onap.sdc.frontend.ci.tests.flow.EditServicePropertiesFlow;
import org.onap.sdc.frontend.ci.tests.flow.exception.UiTestFlowRuntimeException;
import org.onap.sdc.frontend.ci.tests.pages.ServiceComponentPage;
import org.onap.sdc.frontend.ci.tests.pages.TopNavComponent;
import org.onap.sdc.frontend.ci.tests.pages.component.workspace.ToscaArtifactsPage;
import org.onap.sdc.frontend.ci.tests.pages.home.HomePage;
import org.onap.sdc.frontend.ci.tests.utilities.FileHandling;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;
import org.yaml.snakeyaml.Yaml;

public class EtsiNetworkServiceUiTests extends SetupCDTest {

    private static final Logger LOGGER = LoggerFactory.getLogger(EtsiNetworkServiceUiTests.class);

    private WebDriver webDriver;

    @Test
    public void createEtsiNetworkService() throws UnzipException {
        webDriver = DriverFactory.getDriver();

        final CreateServiceFlow createServiceFlow = createService();
        final CheckEtsiNsPropertiesFlow checkEtsiNsPropertiesFlow = checkServiceProperties();
        final ServiceComponentPage serviceComponentPage = checkEtsiNsPropertiesFlow.getLandedPage()
            .orElseThrow(() -> new UiTestFlowRuntimeException("Missing expected ServiceComponentPage"));

        final Map<String, Object> propertyMap = createPropertyToEditMap();
        editProperties(serviceComponentPage, propertyMap);

        final DownloadCsarArtifactFlow downloadCsarArtifactFlow = downloadCsarArtifact(serviceComponentPage);
        final ToscaArtifactsPage toscaArtifactsPage = downloadCsarArtifactFlow.getLandedPage()
            .orElseThrow(() -> new UiTestFlowRuntimeException("Missing expected ToscaArtifactsPage"));

        assertThat("No artifact download was found", toscaArtifactsPage.getDownloadedArtifactList(), not(empty()));

        final String downloadedCsarName = toscaArtifactsPage.getDownloadedArtifactList().get(0);
        checkEtsiNsPackage(createServiceFlow.getServiceCreateData().getName(), downloadedCsarName, propertyMap);
    }

    private CreateServiceFlow createService() {
        final ServiceCreateData serviceCreateData = createServiceFormData();
        final CreateServiceFlow createServiceFlow = new CreateServiceFlow(webDriver, serviceCreateData);
        final TopNavComponent topNavComponent = new TopNavComponent(webDriver);
        createServiceFlow.run(new HomePage(webDriver, topNavComponent));
        return createServiceFlow;
    }

    private CheckEtsiNsPropertiesFlow checkServiceProperties() {
        final CheckEtsiNsPropertiesFlow checkEtsiNsPropertiesFlow = new CheckEtsiNsPropertiesFlow(webDriver);
        checkEtsiNsPropertiesFlow.run();
        return checkEtsiNsPropertiesFlow;
    }

    private void editProperties(final ServiceComponentPage serviceComponentPage, final Map<String, Object> propertyMap) {
        final EditServicePropertiesFlow editServicePropertiesFlow = new EditServicePropertiesFlow(webDriver, propertyMap);
        editServicePropertiesFlow.run(serviceComponentPage);
    }

    private DownloadCsarArtifactFlow downloadCsarArtifact(final ServiceComponentPage serviceComponentPage) {
        final DownloadCsarArtifactFlow downloadCsarArtifactFlow = new DownloadCsarArtifactFlow(webDriver);
        downloadCsarArtifactFlow.run(serviceComponentPage);
        return downloadCsarArtifactFlow;
    }

    private Map<String, Object> createPropertyToEditMap() {
        final Map<String, Object> propertyMap = new HashMap<>();
        propertyMap.put("designer", "designer1");
        propertyMap.put("descriptor_id", "descriptor_id1");
        propertyMap.put("flavour_id", "flavour_id1");
        propertyMap.put("invariant_id", "invariant_id1");
        propertyMap.put("name", "name1");
        propertyMap.put("version", "version1");
        propertyMap.put("service_availability_level", 1);
        //does not work yet with TOSCA complex types
        propertyMap.put("ns_profile", null);
        return propertyMap;
    }

    private ServiceCreateData createServiceFormData() {
        final ServiceCreateData serviceCreateData = new ServiceCreateData();
        serviceCreateData.setRandomName("EtsiNfvNetworkService");
        serviceCreateData.setCategory(ServiceCategoriesEnum.ETSI_NFV_NETWORK_SERVICE.getValue());
        serviceCreateData.setEtsiVersion("2.5.1");
        serviceCreateData.setDescription("aDescription");
        return serviceCreateData;
    }

    private void checkEtsiNsPackage(final String serviceName, final String downloadedCsarName,
                                    final Map<String, Object> expectedPropertyMap) throws UnzipException {
        final String downloadFolderPath = getConfig().getDownloadAutomationFolder();
        final Map<String, byte[]> filesFromZip = FileHandling.getFilesFromZip(downloadFolderPath, downloadedCsarName);
        final Optional<String> etsiPackageEntryOpt =
            filesFromZip.keySet().stream().filter(s -> s.startsWith("Artifacts/ETSI_PACKAGE")).findFirst();
        if (etsiPackageEntryOpt.isEmpty()) {
            Assertions.fail("Could not find the NSD package in Artifacts/ETSI_PACKAGE");
        }
        final String nodeType = String.format("org.openecomp.service.%s",
            serviceName.substring(0, 1).toUpperCase() + serviceName.substring(1).toLowerCase());
        final String etsiPackageEntry = etsiPackageEntryOpt.get();
        final String nsdPackageBaseName = FilenameUtils.getBaseName(etsiPackageEntry);
        final String nsdCsarFile = nsdPackageBaseName + ".csar";
        final byte[] etsiPackageBytes = filesFromZip.get(etsiPackageEntry);
        if (etsiPackageEntry.endsWith(".zip")) {
            final Map<String, byte[]> nsPackageFileMap = FileHandling.getFilesFromZip(etsiPackageBytes);
            assertThat("Expecting 3 files inside the NSD CSAR, the CSAR itself and its signature and certificate",
                nsPackageFileMap.size(), is(3));
            assertThat("Expecting the NSD CSAR file " + nsdCsarFile, nsPackageFileMap, hasKey(nsdCsarFile));
            final String nsdCsarSignature = nsdPackageBaseName + ".cms";
            assertThat("Expecting the NSD CSAR signature " + nsdCsarSignature, nsPackageFileMap, hasKey(nsdCsarSignature));
            final String nsdCertificate = nsdPackageBaseName + ".cert";
            assertThat("Expecting the NSD CSAR certificate " + nsdCertificate, nsPackageFileMap, hasKey(nsdCertificate));
            checkNsCsar(nsdPackageBaseName, nodeType, expectedPropertyMap, nsPackageFileMap.get(nsdCsarFile));
            return;
        }
        if (etsiPackageEntry.endsWith(".csar")) {
            final Map<String, byte[]> nsPackageFileMap = FileHandling.getFilesFromZip(etsiPackageBytes);
            checkNsCsar(nsdPackageBaseName, nodeType, expectedPropertyMap, nsPackageFileMap.get(nsdCsarFile));
            return;
        }
        fail(String.format("Unexpected ETSI NS PACKAGE entry '%s'. Expecting a '.csar' or '.zip'", etsiPackageEntry));
    }

    private void checkNsCsar(final String expectedServiceName, final String expectedServiceNodeType, final Map<String, Object> expectedPropertiesMap,
                             final byte[] nsCsar) {
        try {
            final Map<String, byte[]> csarFileMap = FileHandling.getFilesFromZip(nsCsar);
            final String mainDefinitionFile = String.format("Definitions/%s.yaml", expectedServiceName);
            final byte[] mainDefinitionFileBytes = csarFileMap.get(mainDefinitionFile);
            if (mainDefinitionFileBytes == null) {
                Assertions.fail(String.format("Could not find the Main Definition file in '%s'", mainDefinitionFile));
            }

            final Map<String, Object> mainDefinitionYamlMap = loadYamlObject(mainDefinitionFileBytes);
            final Map<String, Object> topologyTemplateTosca = getMapEntry(mainDefinitionYamlMap, "topology_template");
            assertThat(String.format("'%s' should contain a topology_template entry", mainDefinitionFile), topologyTemplateTosca, notNullValue());
            final Map<String, Object> substitutionMappingsTosca = getMapEntry(topologyTemplateTosca, "substitution_mappings");
            assertThat(String.format("'%s' should contain a substitution_mappings entry", mainDefinitionFile), substitutionMappingsTosca, notNullValue());
            final String nodeType = (String) substitutionMappingsTosca.get("node_type");
            assertThat("substitution_mappings->node_type should be as expected", nodeType, is(expectedServiceNodeType));

            final Map<String, Object> nodeTemplatesTosca = getMapEntry(topologyTemplateTosca, "node_templates");
            assertThat(String.format("'%s' should contain a node_templates entry", mainDefinitionFile), nodeTemplatesTosca, notNullValue());
            final Map<String, Object> serviceNodeTemplate = getMapEntry(nodeTemplatesTosca, expectedServiceNodeType);
            assertThat(String.format("'%s' should contain a '%s' entry in node_templates", mainDefinitionFile, expectedServiceNodeType),
                serviceNodeTemplate, notNullValue());
            final Map<String, Object> properties = getMapEntry(serviceNodeTemplate, "properties");
            assertThat(String.format("'%s' node template in '%s' should contain a properties entry", expectedServiceNodeType, mainDefinitionFile),
                properties, notNullValue());
            assertThat(String.format("'%s' node template should contain '%s' properties", expectedServiceNodeType, expectedPropertiesMap.size()),
                properties.size(), is(expectedPropertiesMap.size()));
            for (final Entry<String, Object> expectedPropertyEntry : expectedPropertiesMap.entrySet()) {
                final String expectedPropertyName = expectedPropertyEntry.getKey();
                assertThat(String.format("'%s' node template should contain the property '%s'", expectedServiceNodeType, expectedPropertyName),
                    properties, hasKey(expectedPropertyName));
                final Object expectedPropertyValue = expectedPropertyEntry.getValue();
                if (expectedPropertyValue != null) {
                    final Object actualPropertyValue = properties.get(expectedPropertyName);
                    final String msg = String.format("The property '%s', in '%s' node template should have the expected value '%s'",
                        expectedPropertyName, expectedServiceNodeType, actualPropertyValue);
                    assertThat(msg, actualPropertyValue, is(expectedPropertyValue));
                }
            }

        } catch (final UnzipException e) {
            final String errorMsg = "Could not unzip Network Service CSAR.";
            LOGGER.info(errorMsg, e);
            fail(String.format("%s Error: %s", errorMsg, e.getMessage()));
        }
    }

    private Map<String, Object> getMapEntry(final Map<String, Object> yamlObj, final String entryName) {
        try {
            return (Map<String, Object>) yamlObj.get(entryName);
        } catch (final Exception e) {
            final String errorMsg = String.format("Could not get the '%s' entry.", entryName);
            LOGGER.error(errorMsg, e);
            fail(errorMsg + "Error message: " + e.getMessage());
        }
        return null;
    }

    private Map<String, Object> loadYamlObject(final byte[] mainDefinitionFileBytes) {
        return new Yaml().load(new String(mainDefinitionFileBytes));
    }

}