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