2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.core.converter.impl.pnfd;
22 import static org.junit.Assert.assertEquals;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Objects;
34 import java.util.stream.Collectors;
35 import java.util.stream.Stream;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.junit.runners.Parameterized;
39 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
40 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
41 import org.onap.sdc.tosca.services.YamlUtil;
42 import org.openecomp.core.converter.ServiceTemplateReaderService;
43 import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl;
45 @RunWith(Parameterized.class)
46 public class PnfTransformationEngineParameterizedTest {
48 private static final String TEST_CASES_PATH = "transformation/pnfParseEngine";
49 private static final String TRANSFORMATION_DESCRIPTOR_FOLDER = "transformationDescriptor";
50 private static final String OUTPUT_FOLDER = "expectedOutput";
51 private static final String DEFAULT_OUTPUT_FILE_NAME = "defaultOutput.yaml";
53 private final String inputFileName;
54 private final Path inputFilePath;
55 private final String outputFileName;
56 private final Path outputFilePath;
57 private final String transformationDescriptorFileName;
58 private final Path transformationDescriptorFilePath;
59 private final YamlUtil yamlUtil = new YamlUtil();
60 private final ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
62 public PnfTransformationEngineParameterizedTest(final String inputFileName, final Path inputFilePath,
63 final String outputFileName, final Path outputFilePath,
64 final String transformationDescriptorFileName, final Path transformationDescriptorFilePath) {
65 this.inputFileName = inputFileName;
66 this.inputFilePath = inputFilePath;
67 this.outputFileName = outputFileName;
68 this.outputFilePath = outputFilePath;
69 this.transformationDescriptorFileName = transformationDescriptorFileName;
70 this.transformationDescriptorFilePath = transformationDescriptorFilePath;
74 @Parameterized.Parameters(name = "{index}: input: {0}, descriptor: {4}, output: {2}")
75 public static Collection input() throws IOException {
76 return Files.list(getPathFromClasspath(TEST_CASES_PATH)).map(path -> {
78 return buildTestCase(path);
79 } catch (final IOException e) {
82 }).filter(Objects::nonNull).flatMap(Collection::stream).collect(Collectors.toList());
85 private static Collection buildTestCase(final Path testCasePath) throws IOException {
86 final Path inputFilePath = Files.list(testCasePath)
87 .filter(path -> path.toFile().getName().endsWith("yaml"))
88 .findAny().orElse(null);
90 if (inputFilePath == null) {
91 return Collections.emptyList();
94 final List<Path> transformationDescriptorList;
95 try (final Stream<Path> files = Files.walk(testCasePath.resolve(TRANSFORMATION_DESCRIPTOR_FOLDER))) {
96 transformationDescriptorList = files.filter(path -> Files.isRegularFile(path))
97 .map(path -> Paths.get(TEST_CASES_PATH, testCasePath.getFileName().toString()
98 , TRANSFORMATION_DESCRIPTOR_FOLDER, path.getFileName().toString()))
99 .collect(Collectors.toList());
102 final List<Path> outputList;
103 try (final Stream<Path> files = Files.walk(testCasePath.resolve(OUTPUT_FOLDER))) {
104 outputList = files.filter(path -> Files.isRegularFile(path)).collect(Collectors.toList());
106 final Path defaultOutput = outputList.stream()
107 .filter(path -> path.toFile().getName().equals(DEFAULT_OUTPUT_FILE_NAME))
108 .findFirst().orElse(null);
110 final List<Object[]> testCaseList = new ArrayList<>();
112 for (final Path transformationDescriptorPath : transformationDescriptorList) {
113 final Path outputPath = outputList.stream()
114 .filter(path -> path.toFile().getName().equals(transformationDescriptorPath.toFile().getName()))
115 .findFirst().orElse(defaultOutput);
116 if (outputPath != null) {
117 testCaseList.add(new Object[] {inputFilePath.toFile().getName(), inputFilePath,
118 outputPath.toFile().getName(), outputPath,
119 transformationDescriptorPath.toFile().getName(), transformationDescriptorPath});
128 public void testTopologyTemplateConversions() throws IOException {
129 final byte[] descriptor = Files.readAllBytes(inputFilePath);
130 final ServiceTemplateReaderService serviceTemplateReaderService = new ServiceTemplateReaderServiceImpl(descriptor);
131 final ServiceTemplate serviceTemplate = new ServiceTemplate();
133 final PnfdTransformationEngine pnfdTransformationEngine = new PnfdTransformationEngine(serviceTemplateReaderService, serviceTemplate
134 , transformationDescriptorFilePath.toString());
135 pnfdTransformationEngine.transform();
137 final String result = yamlUtil.objectToYaml(serviceTemplate);
138 final String expectedResult = parseToYaml(outputFilePath);
139 assertEquals(expectedResult, result);
142 private String parseToYaml(final Path filePath) throws IOException {
143 try (final InputStream inputStream = Files.newInputStream(filePath)) {
144 ServiceTemplate serviceTemplate = toscaExtensionYamlUtil.yamlToObject(inputStream, ServiceTemplate.class);
145 return yamlUtil.objectToYaml(serviceTemplate);
149 private static Path getPathFromClasspath(final String location) {
150 return Paths.get(Thread.currentThread().getContextClassLoader().getResource(location).getPath());