1eb09a8b445abcdee466269d17de32cb0db1fb6f
[sdc.git] /
1 /*
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
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.openecomp.core.converter.impl.pnfd;
21
22 import static org.junit.Assert.assertEquals;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.URISyntaxException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.Objects;
35 import java.util.stream.Collectors;
36 import java.util.stream.Stream;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.junit.runners.Parameterized;
40 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
41 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
42 import org.onap.sdc.tosca.services.YamlUtil;
43 import org.openecomp.core.converter.ServiceTemplateReaderService;
44 import org.openecomp.core.converter.pnfd.PnfdTransformationEngine;
45 import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl;
46
47 @RunWith(Parameterized.class)
48 public class PnfTransformationEngineParameterizedTest {
49
50     private static final String TEST_CASES_PATH = "transformation/pnfParseEngine";
51     private static final String TRANSFORMATION_DESCRIPTOR_FOLDER = "transformationDescriptor";
52     private static final String OUTPUT_FOLDER = "expectedOutput";
53     private static final String DEFAULT_OUTPUT_FILE_NAME = "defaultOutput.yaml";
54
55     private final String inputFileName;
56     private final Path inputFilePath;
57     private final String outputFileName;
58     private final Path outputFilePath;
59     private final String transformationDescriptorFileName;
60     private final Path transformationDescriptorFilePath;
61     private final YamlUtil yamlUtil = new YamlUtil();
62     private final ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
63
64     public PnfTransformationEngineParameterizedTest(final String inputFileName, final Path inputFilePath,
65             final String outputFileName, final Path outputFilePath,
66             final String transformationDescriptorFileName, final Path transformationDescriptorFilePath) {
67         this.inputFileName = inputFileName;
68         this.inputFilePath = inputFilePath;
69         this.outputFileName = outputFileName;
70         this.outputFilePath = outputFilePath;
71         this.transformationDescriptorFileName = transformationDescriptorFileName;
72         this.transformationDescriptorFilePath = transformationDescriptorFilePath;
73     }
74
75
76     @Parameterized.Parameters(name = "{index}: input: {0}, descriptor: {4}, output: {2}")
77     public static Collection<Object> input() throws IOException, URISyntaxException {
78         return Files.list(getPathFromClasspath(TEST_CASES_PATH)).map(path -> {
79             try {
80                 return buildTestCase(path);
81             } catch (final IOException e) {
82                 return null;
83             }
84         }).filter(Objects::nonNull).flatMap(Collection::stream).collect(Collectors.toList());
85     }
86
87     private static Collection<Object[]> buildTestCase(final Path testCasePath) throws IOException {
88         final Path inputFilePath = Files.list(testCasePath)
89             .filter(path -> path.toFile().getName().endsWith("yaml"))
90             .findAny().orElse(null);
91
92         if (inputFilePath == null) {
93             return Collections.emptyList();
94         }
95         final List<Path> transformationDescriptorList;
96         try (final Stream<Path> files = Files.walk(testCasePath.resolve(TRANSFORMATION_DESCRIPTOR_FOLDER))) {
97             transformationDescriptorList = files.filter(path -> Files.isRegularFile(path))
98                 .map(path -> Paths.get(TEST_CASES_PATH, testCasePath.getFileName().toString()
99                     , TRANSFORMATION_DESCRIPTOR_FOLDER, path.getFileName().toString()))
100                 .collect(Collectors.toList());
101         }
102
103         final List<Path> outputList;
104         try (final Stream<Path> files = Files.walk(testCasePath.resolve(OUTPUT_FOLDER))) {
105             outputList = files.filter(path -> Files.isRegularFile(path)).collect(Collectors.toList());
106         }
107         final Path defaultOutput = outputList.stream()
108             .filter(path -> path.toFile().getName().equals(DEFAULT_OUTPUT_FILE_NAME))
109             .findFirst().orElse(null);
110
111         final List<Object[]> testCaseList = new ArrayList<>();
112
113         for (final Path transformationDescriptorPath : transformationDescriptorList) {
114             final Path outputPath = outputList.stream()
115                 .filter(path -> path.toFile().getName().equals(transformationDescriptorPath.toFile().getName()))
116                 .findFirst().orElse(defaultOutput);
117             if (outputPath != null) {
118                 testCaseList.add(new Object[] {inputFilePath.toFile().getName(), inputFilePath,
119                     outputPath.toFile().getName(), outputPath,
120                     transformationDescriptorPath.toFile().getName(), transformationDescriptorPath});
121             }
122         }
123
124         return testCaseList;
125
126     }
127
128     @Test
129     public void testTopologyTemplateConversions() throws IOException {
130         final byte[] descriptor = Files.readAllBytes(inputFilePath);
131         final ServiceTemplateReaderService serviceTemplateReaderService = new ServiceTemplateReaderServiceImpl(descriptor);
132         final ServiceTemplate serviceTemplate = new ServiceTemplate();
133
134         final PnfdTransformationEngine pnfdTransformationEngine = new PnfdNodeTemplateTransformationEngine(
135             serviceTemplateReaderService, serviceTemplate, transformationDescriptorFilePath.toString());
136         pnfdTransformationEngine.transform();
137
138         final String result = yamlUtil.objectToYaml(serviceTemplate);
139         final String expectedResult = parseToYaml(outputFilePath);
140         assertEquals(expectedResult, result);
141     }
142
143     private String parseToYaml(final Path filePath) throws IOException {
144         try (final InputStream inputStream = Files.newInputStream(filePath)) {
145             ServiceTemplate serviceTemplate = toscaExtensionYamlUtil.yamlToObject(inputStream, ServiceTemplate.class);
146             return yamlUtil.objectToYaml(serviceTemplate);
147         }
148     }
149
150     private static Path getPathFromClasspath(final String location) throws URISyntaxException {
151         return Paths.get(PnfTransformationEngineParameterizedTest.class.getClassLoader().getResource(location).toURI());
152     }
153 }