Implement PNFD Model driven conversion
[sdc.git] / openecomp-be / lib / openecomp-tosca-converter-lib / openecomp-tosca-converter-core / src / test / java / org / openecomp / core / converter / impl / pnfd / PnfTransformationEngineTest.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 static org.junit.Assert.assertEquals;
23
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.Collection;
30 import java.util.stream.Collectors;
31 import java.util.stream.Stream;
32 import org.apache.commons.io.IOUtils;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.junit.runners.Parameterized;
36 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
37 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
38 import org.onap.sdc.tosca.services.YamlUtil;
39 import org.openecomp.core.converter.ServiceTemplateReaderService;
40 import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl;
41
42 @RunWith(Parameterized.class)
43 public class PnfTransformationEngineTest {
44
45     public static final String INPUT_DIR = "pnfDescriptor/in/";
46     public static final String OUTPUT_DIR = "pnfDescriptor/out/";
47     private String inputFilename;
48     private final YamlUtil yamlUtil = new YamlUtil();
49     private final ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
50
51     public PnfTransformationEngineTest(final String inputFilename) {
52         this.inputFilename = inputFilename;
53     }
54
55     @Parameterized.Parameters(name = "{index}: {0}")
56     public static Collection<String> input() throws IOException {
57         try (final Stream<Path> files = Files.list(getPathFromClasspath(INPUT_DIR))) {
58             return files.map(path -> path.getFileName().toString())
59                     .collect(Collectors.toList());
60         }
61     }
62
63     @Test
64     public void testTopologyTemplateConversions() throws IOException {
65         final byte[] descriptor = getInputFileResource(inputFilename);
66         final ServiceTemplateReaderService serviceTemplateReaderService = new ServiceTemplateReaderServiceImpl(descriptor);
67         final ServiceTemplate serviceTemplate = new ServiceTemplate();
68
69         final PnfdTransformationEngine pnfdTransformationEngine = new PnfdTransformationEngine(
70             serviceTemplateReaderService, serviceTemplate);
71         pnfdTransformationEngine.transform();
72
73         final String result = yamlUtil.objectToYaml(serviceTemplate);
74         final String expectedResult = getExpectedResultFor(inputFilename);
75         assertEquals(expectedResult, result);
76     }
77
78     private String getExpectedResultFor(final String inputFilename) throws IOException {
79         try (final InputStream inputStream = getOutputFileResourceCorrespondingTo(inputFilename)) {
80             final ServiceTemplate serviceTemplate = toscaExtensionYamlUtil.yamlToObject(inputStream, ServiceTemplate.class);
81             return yamlUtil.objectToYaml(serviceTemplate);
82         }
83     }
84
85     private static Path getPathFromClasspath(final String location) {
86         return Paths.get(Thread.currentThread().getContextClassLoader().getResource(location).getPath());
87     }
88
89     private byte[] getInputFileResource(final String inputFilename) throws IOException {
90         return getFileResource(INPUT_DIR + inputFilename);
91     }
92
93     private InputStream getOutputFileResourceCorrespondingTo(final String inputFilename) {
94         final String outputFilename = getOutputFilenameFrom(inputFilename);
95         return getFileResourceAsInputStream(OUTPUT_DIR + outputFilename);
96     }
97
98     private String getOutputFilenameFrom(final String inputFilename) {
99         return inputFilename.replace("pnfDescriptor", "topologyTemplate");
100     }
101
102     private byte[] getFileResource(final String filePath) throws IOException {
103         try (final InputStream inputStream = getFileResourceAsInputStream(filePath)) {
104             return IOUtils.toByteArray(inputStream);
105         }
106     }
107
108     private InputStream getFileResourceAsInputStream(final String filePath) {
109         return Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
110     }
111
112 }