summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/converter/impl/pnfd/PnfTransformationEngineParameterizedTest.java
blob: 1eb09a8b445abcdee466269d17de32cb0db1fb6f (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
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 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.openecomp.core.converter.impl.pnfd;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
import org.onap.sdc.tosca.services.YamlUtil;
import org.openecomp.core.converter.ServiceTemplateReaderService;
import org.openecomp.core.converter.pnfd.PnfdTransformationEngine;
import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl;

@RunWith(Parameterized.class)
public class PnfTransformationEngineParameterizedTest {

    private static final String TEST_CASES_PATH = "transformation/pnfParseEngine";
    private static final String TRANSFORMATION_DESCRIPTOR_FOLDER = "transformationDescriptor";
    private static final String OUTPUT_FOLDER = "expectedOutput";
    private static final String DEFAULT_OUTPUT_FILE_NAME = "defaultOutput.yaml";

    private final String inputFileName;
    private final Path inputFilePath;
    private final String outputFileName;
    private final Path outputFilePath;
    private final String transformationDescriptorFileName;
    private final Path transformationDescriptorFilePath;
    private final YamlUtil yamlUtil = new YamlUtil();
    private final ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();

    public PnfTransformationEngineParameterizedTest(final String inputFileName, final Path inputFilePath,
            final String outputFileName, final Path outputFilePath,
            final String transformationDescriptorFileName, final Path transformationDescriptorFilePath) {
        this.inputFileName = inputFileName;
        this.inputFilePath = inputFilePath;
        this.outputFileName = outputFileName;
        this.outputFilePath = outputFilePath;
        this.transformationDescriptorFileName = transformationDescriptorFileName;
        this.transformationDescriptorFilePath = transformationDescriptorFilePath;
    }


    @Parameterized.Parameters(name = "{index}: input: {0}, descriptor: {4}, output: {2}")
    public static Collection<Object> input() throws IOException, URISyntaxException {
        return Files.list(getPathFromClasspath(TEST_CASES_PATH)).map(path -> {
            try {
                return buildTestCase(path);
            } catch (final IOException e) {
                return null;
            }
        }).filter(Objects::nonNull).flatMap(Collection::stream).collect(Collectors.toList());
    }

    private static Collection<Object[]> buildTestCase(final Path testCasePath) throws IOException {
        final Path inputFilePath = Files.list(testCasePath)
            .filter(path -> path.toFile().getName().endsWith("yaml"))
            .findAny().orElse(null);

        if (inputFilePath == null) {
            return Collections.emptyList();
        }
        final List<Path> transformationDescriptorList;
        try (final Stream<Path> files = Files.walk(testCasePath.resolve(TRANSFORMATION_DESCRIPTOR_FOLDER))) {
            transformationDescriptorList = files.filter(path -> Files.isRegularFile(path))
                .map(path -> Paths.get(TEST_CASES_PATH, testCasePath.getFileName().toString()
                    , TRANSFORMATION_DESCRIPTOR_FOLDER, path.getFileName().toString()))
                .collect(Collectors.toList());
        }

        final List<Path> outputList;
        try (final Stream<Path> files = Files.walk(testCasePath.resolve(OUTPUT_FOLDER))) {
            outputList = files.filter(path -> Files.isRegularFile(path)).collect(Collectors.toList());
        }
        final Path defaultOutput = outputList.stream()
            .filter(path -> path.toFile().getName().equals(DEFAULT_OUTPUT_FILE_NAME))
            .findFirst().orElse(null);

        final List<Object[]> testCaseList = new ArrayList<>();

        for (final Path transformationDescriptorPath : transformationDescriptorList) {
            final Path outputPath = outputList.stream()
                .filter(path -> path.toFile().getName().equals(transformationDescriptorPath.toFile().getName()))
                .findFirst().orElse(defaultOutput);
            if (outputPath != null) {
                testCaseList.add(new Object[] {inputFilePath.toFile().getName(), inputFilePath,
                    outputPath.toFile().getName(), outputPath,
                    transformationDescriptorPath.toFile().getName(), transformationDescriptorPath});
            }
        }

        return testCaseList;

    }

    @Test
    public void testTopologyTemplateConversions() throws IOException {
        final byte[] descriptor = Files.readAllBytes(inputFilePath);
        final ServiceTemplateReaderService serviceTemplateReaderService = new ServiceTemplateReaderServiceImpl(descriptor);
        final ServiceTemplate serviceTemplate = new ServiceTemplate();

        final PnfdTransformationEngine pnfdTransformationEngine = new PnfdNodeTemplateTransformationEngine(
            serviceTemplateReaderService, serviceTemplate, transformationDescriptorFilePath.toString());
        pnfdTransformationEngine.transform();

        final String result = yamlUtil.objectToYaml(serviceTemplate);
        final String expectedResult = parseToYaml(outputFilePath);
        assertEquals(expectedResult, result);
    }

    private String parseToYaml(final Path filePath) throws IOException {
        try (final InputStream inputStream = Files.newInputStream(filePath)) {
            ServiceTemplate serviceTemplate = toscaExtensionYamlUtil.yamlToObject(inputStream, ServiceTemplate.class);
            return yamlUtil.objectToYaml(serviceTemplate);
        }
    }

    private static Path getPathFromClasspath(final String location) throws URISyntaxException {
        return Paths.get(PnfTransformationEngineParameterizedTest.class.getClassLoader().getResource(location).toURI());
    }
}