Onboard Package Handling
[sdc.git] / openecomp-be / lib / openecomp-tosca-converter-lib / openecomp-tosca-converter-core / src / test / java / org / openecomp / core / impl / ToscaSolConverterPnfTest.java
1 /*
2  * -
3  *  * ============LICENSE_START=======================================================
4  *  *  Copyright (C) 2019 Nordix Foundation.
5  *  * ================================================================================
6  *  * Licensed under the Apache License, Version 2.0 (the "License");
7  *  * you may not use this file except in compliance with the License.
8  *  * You may obtain a copy of the License at
9  *  *
10  *  *      http://www.apache.org/licenses/LICENSE-2.0
11  *  *
12  *  * Unless required by applicable law or agreed to in writing, software
13  *  * distributed under the License is distributed on an "AS IS" BASIS,
14  *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  * See the License for the specific language governing permissions and
16  *  * limitations under the License.
17  *  *
18  *  * SPDX-License-Identifier: Apache-2.0
19  *  * ============LICENSE_END=========================================================
20  *
21  */
22
23 package org.openecomp.core.impl;
24
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.Matchers.equalTo;
27 import static org.junit.Assert.fail;
28
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.Collection;
35 import java.util.stream.Collectors;
36 import java.util.stream.Stream;
37 import org.apache.commons.io.IOUtils;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.junit.runners.Parameterized;
41 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
42 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
43 import org.onap.sdc.tosca.services.YamlUtil;
44 import org.openecomp.core.converter.ServiceTemplateReaderService;
45 import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl;
46
47 @RunWith(Parameterized.class)
48 public class ToscaSolConverterPnfTest {
49
50     private static final String INPUT_DIR = "pnfDescriptor/in/";
51     private static final String OUTPUT_DIR = "pnfDescriptor/out/";
52     private String inputFilename;
53     private YamlUtil yamlUtil = new YamlUtil();
54     private ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
55
56     public ToscaSolConverterPnfTest(String inputFilename) {
57         this.inputFilename = inputFilename;
58     }
59
60     @Parameterized.Parameters(name = "{index}: {0}")
61     public static Collection<String> input() throws IOException {
62         try (Stream<Path> files = Files.list(getPathFromClasspath(INPUT_DIR))) {
63             return files.map(path -> path.getFileName().toString())
64                     .collect(Collectors.toList());
65         }
66     }
67
68     @Test
69     public void testTopologyTemplateConversions() {
70         final byte[] descriptor = getInputFileResource(inputFilename);
71         final ServiceTemplateReaderService serviceTemplateReaderService =
72             new ServiceTemplateReaderServiceImpl(descriptor);
73         final ServiceTemplate serviceTemplate = new ServiceTemplate();
74         final ToscaSolConverterPnf toscaSolConverter = new ToscaSolConverterPnf();
75         toscaSolConverter.convertTopologyTemplate(serviceTemplate, serviceTemplateReaderService);
76
77         final String actualYaml = yamlUtil.objectToYaml(serviceTemplate);
78         final String expectedYaml = getExpectedResultFor(inputFilename);
79         assertThat("Converted PNF descriptor should be the same as the expected topology template", actualYaml,
80             equalTo(expectedYaml));
81     }
82
83     private String getExpectedResultFor(final String inputFilename)  {
84         try (final InputStream inputStream = getOutputFileResourceCorrespondingTo(inputFilename)) {
85             final ServiceTemplate serviceTemplate = toscaExtensionYamlUtil.yamlToObject(inputStream, ServiceTemplate.class);
86             return yamlUtil.objectToYaml(serviceTemplate);
87         } catch (final IOException e) {
88             fail(String.format("Could not find file '%s'", inputFilename));
89         }
90
91         return null;
92     }
93
94     private static Path getPathFromClasspath(final String location) {
95         return Paths.get(Thread.currentThread().getContextClassLoader().getResource(location).getPath());
96     }
97
98     private byte[] getInputFileResource(final String inputFilename) {
99         return getFileResource(INPUT_DIR + inputFilename);
100     }
101
102     private InputStream getOutputFileResourceCorrespondingTo(final String inputFilename) {
103         final String outputFilename = getOutputFilenameFrom(inputFilename);
104         return getFileResourceAsInputStream(OUTPUT_DIR + outputFilename);
105     }
106
107     private String getOutputFilenameFrom(final String inputFilename) {
108         return inputFilename.replace("pnfDescriptor", "topologyTemplate");
109     }
110
111     private byte[] getFileResource(final String filePath) {
112         try (InputStream inputStream = getFileResourceAsInputStream(filePath)) {
113             return IOUtils.toByteArray(inputStream);
114         } catch (final IOException e) {
115             fail(String.format("Could not find file '%s'", filePath));
116         }
117
118         return null;
119     }
120
121     private InputStream getFileResourceAsInputStream(final String filePath) {
122         return Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
123     }
124
125 }