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
10 * * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * * SPDX-License-Identifier: Apache-2.0
19 * * ============LICENSE_END=========================================================
23 package org.openecomp.core.impl;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.Matchers.equalTo;
27 import static org.junit.Assert.fail;
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;
47 @RunWith(Parameterized.class)
48 public class ToscaSolConverterPnfTest {
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();
56 public ToscaSolConverterPnfTest(String inputFilename) {
57 this.inputFilename = inputFilename;
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());
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);
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));
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));
94 private static Path getPathFromClasspath(final String location) {
95 return Paths.get(Thread.currentThread().getContextClassLoader().getResource(location).getPath());
98 private byte[] getInputFileResource(final String inputFilename) {
99 return getFileResource(INPUT_DIR + inputFilename);
102 private InputStream getOutputFileResourceCorrespondingTo(final String inputFilename) {
103 final String outputFilename = getOutputFilenameFrom(inputFilename);
104 return getFileResourceAsInputStream(OUTPUT_DIR + outputFilename);
107 private String getOutputFilenameFrom(final String inputFilename) {
108 return inputFilename.replace("pnfDescriptor", "topologyTemplate");
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));
121 private InputStream getFileResourceAsInputStream(final String filePath) {
122 return Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);