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