summaryrefslogtreecommitdiffstats
path: root/integration-tests/src/test/java/org/onap/sdc/frontend/ci/tests/execute/sanity/CreateServiceSubstitutionFilterTest.java
blob: c49c2dc4479e633429c50a012e209af9d938b58e (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
/*
 * ============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.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.onap.sdc.backend.ci.tests.datatypes.enums.ServiceInstantiationType;
import org.onap.sdc.frontend.ci.tests.datatypes.LogicalOperator;
import org.onap.sdc.frontend.ci.tests.datatypes.ServiceCreateData;
import org.onap.sdc.frontend.ci.tests.datatypes.ServiceDependencyProperty;
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.CreateServiceFlow;
import org.onap.sdc.frontend.ci.tests.flow.CreateSubtitutionFilterFlow;
import org.onap.sdc.frontend.ci.tests.flow.DownloadToscaTemplateFlow;
import org.onap.sdc.frontend.ci.tests.pages.ResourcePropertiesAssignmentPage;
import org.onap.sdc.frontend.ci.tests.pages.ServiceComponentPage;
import org.onap.sdc.frontend.ci.tests.pages.component.workspace.CompositionPage;
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.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class CreateServiceSubstitutionFilterTest extends SetupCDTest {
    private final String serviceName = "CreateServiceSubstitutionFilterTest";
    private final String stringValue = "Test";
    private final String booleanValue = "TRUE";
    private final LogicalOperator operator = LogicalOperator.EQUALS;
    private final List<ServiceDependencyProperty> substitutionFilterProperties = new ArrayList<ServiceDependencyProperty>();
    private final ServiceCreateData serviceCreateData = loadServiceCreateData();

    private WebDriver webDriver;

    @BeforeClass
    public void classSetup() {
        webDriver = DriverFactory.getDriver();
    }

    @Test
    public void createSubsitutionFilter() throws Exception {
       new CreateServiceFlow(webDriver, serviceCreateData).run(new HomePage(webDriver));

       ServiceComponentPage serviceComponentPage = new ServiceComponentPage(webDriver);
       serviceComponentPage.isLoaded();

       loadSubstitutionFilterProperties(serviceComponentPage);
       final CompositionPage compositionPage = serviceComponentPage.goToComposition();

       substitutionFilterProperties.forEach(substitutionFilterProperty -> {
           final CreateSubtitutionFilterFlow createSubtitutionFilterFlow = new CreateSubtitutionFilterFlow(webDriver, substitutionFilterProperty);
           createSubtitutionFilterFlow.run();
       });

       serviceComponentPage = compositionPage.goToServiceGeneral();
       serviceComponentPage.isLoaded();

       verifyToscaTemplateYaml(serviceComponentPage, false);
    }

    private ServiceCreateData loadServiceCreateData() {
        ServiceCreateData serviceCreateData = new ServiceCreateData();
        serviceCreateData.setRandomName(serviceName);
        serviceCreateData.setDescription(serviceName);
        serviceCreateData.setCategory("Network Service");
        serviceCreateData.setInstantiationType(ServiceInstantiationType.A_LA_CARTE);
        return serviceCreateData;
    }

    private void loadSubstitutionFilterProperties(final ServiceComponentPage serviceComponentPage) {
        final ResourcePropertiesAssignmentPage propertiesPage = serviceComponentPage.goToPropertiesAssignment();
        propertiesPage.isLoaded();
        final Map<String, String> propertyNamesAndTypes = propertiesPage.getPropertyNamesAndTypes();
        propertyNamesAndTypes.forEach((name, type)
                -> substitutionFilterProperties.add(new ServiceDependencyProperty(name, getPropertyValueByType(type), operator)));
    }

    private void verifyToscaTemplateYaml(final ServiceComponentPage serviceComponentPage, final boolean delete) throws Exception {
        final DownloadToscaTemplateFlow downloadCsarTemplateFlow = new DownloadToscaTemplateFlow(webDriver);
        final ToscaArtifactsPage toscaArtifactsPage = (ToscaArtifactsPage) downloadCsarTemplateFlow.run(serviceComponentPage).get();
        final Map<?, ?> yaml = FileHandling.parseYamlFile(getConfig().getDownloadAutomationFolder()
                .concat(java.io.File.separator).concat(toscaArtifactsPage.getDownloadedArtifactList().get(0)));
        verifyToscaTemplateHasSubstitutionFilter(yaml);
    }

    private void verifyToscaTemplateHasSubstitutionFilter(final Map<?, ?> yaml) {
        assertNotNull(yaml, "No contents in TOSCA Template");
        final List<?> substitutionFilters = (List<?>) getSubstitutionFilterFromYaml(yaml).get("properties");
        substitutionFilterProperties.forEach(substitutionFilterProperty -> {
            final Map<?, ?> substitutionFilter = (Map<?, ?>) substitutionFilters.stream()
                    .filter(subFilter -> ((Map<?, ?>) subFilter).containsKey(substitutionFilterProperty.getName())).findAny().get();
            assertTrue(substitutionFilter.containsKey(substitutionFilterProperty.getName()), "Added substitution filter not found in TOSCA Template");

            final Map<?, ?> substitutionFilterValue = (Map<?, ?>) ((List<?>) substitutionFilter.get(substitutionFilterProperty.getName())).get(0);
            assertTrue(substitutionFilterValue.containsValue(convertValue(substitutionFilterProperty.getValue()))
                    , "Invalid value for added substitution filters found in TOSCA Template");
            assertTrue(substitutionFilterValue.containsKey(substitutionFilterProperty.getLogicalOperator().getName())
                    , "Invalid logical operator for added substitution filters found in TOSCA Template");
        });
    }

    private Map<?,?> getSubstitutionFilterFromYaml(final Map<?,?> yaml) {
        final Map<?, ?> topology = (Map<?, ?>) yaml.get("topology_template");
        final Map<?, ?> substitutionMappings = (Map<?, ?>) topology.get("substitution_mappings");
        return (Map<?, ?>) substitutionMappings.get("substitution_filter");
    }

    private Object convertValue(String value) {
        return booleanValue.equals(value) ? Boolean.parseBoolean(value) : value;
    }

    private String getPropertyValueByType(String type) {
        return "string".equals(type) ? stringValue : booleanValue;
    }
}