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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.core.converter.impl.pnfd;
22 import static org.junit.Assert.assertEquals;
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.converter.pnfd.PnfdTransformationEngine;
41 import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl;
43 @RunWith(Parameterized.class)
44 public class PnfTransformationEngineTest {
46 public static final String INPUT_DIR = "pnfDescriptor/in/";
47 public static final String OUTPUT_DIR = "pnfDescriptor/out/";
48 private String inputFilename;
49 private final YamlUtil yamlUtil = new YamlUtil();
50 private final ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
52 public PnfTransformationEngineTest(final String inputFilename) {
53 this.inputFilename = inputFilename;
56 @Parameterized.Parameters(name = "{index}: {0}")
57 public static Collection<String> input() throws IOException {
58 try (final Stream<Path> files = Files.list(getPathFromClasspath(INPUT_DIR))) {
59 return files.map(path -> path.getFileName().toString())
60 .collect(Collectors.toList());
65 public void testTopologyTemplateConversions() throws IOException {
66 final byte[] descriptor = getInputFileResource(inputFilename);
67 final ServiceTemplateReaderService serviceTemplateReaderService = new ServiceTemplateReaderServiceImpl(descriptor);
68 final ServiceTemplate serviceTemplate = new ServiceTemplate();
70 PnfdTransformationEngine pnfdTransformationEngine = new PnfdNodeTypeTransformationEngine(
71 serviceTemplateReaderService, serviceTemplate);
72 pnfdTransformationEngine.transform();
73 pnfdTransformationEngine = new PnfdNodeTemplateTransformationEngine(serviceTemplateReaderService, serviceTemplate);
74 pnfdTransformationEngine.transform();
76 final String yamlContent = yamlUtil.objectToYaml(serviceTemplate);
77 final String result = yamlUtil.objectToYaml(yamlUtil.yamlToObject(yamlContent, ServiceTemplate.class));
78 final String expectedResult = getExpectedResultFor(inputFilename);
79 assertEquals(expectedResult, result);
82 private String getExpectedResultFor(final String inputFilename) throws IOException {
83 try (final InputStream inputStream = getOutputFileResourceCorrespondingTo(inputFilename)) {
84 final ServiceTemplate serviceTemplate = toscaExtensionYamlUtil.yamlToObject(inputStream, ServiceTemplate.class);
85 return yamlUtil.objectToYaml(serviceTemplate);
89 private static Path getPathFromClasspath(final String location) {
90 return Paths.get(Thread.currentThread().getContextClassLoader().getResource(location).getPath());
93 private byte[] getInputFileResource(final String inputFilename) throws IOException {
94 return getFileResource(INPUT_DIR + inputFilename);
97 private InputStream getOutputFileResourceCorrespondingTo(final String inputFilename) {
98 final String outputFilename = getOutputFilenameFrom(inputFilename);
99 return getFileResourceAsInputStream(OUTPUT_DIR + outputFilename);
102 private String getOutputFilenameFrom(final String inputFilename) {
103 return inputFilename.replace("pnfDescriptor", "topologyTemplate");
106 private byte[] getFileResource(final String filePath) throws IOException {
107 try (final InputStream inputStream = getFileResourceAsInputStream(filePath)) {
108 return IOUtils.toByteArray(inputStream);
112 private InputStream getFileResourceAsInputStream(final String filePath) {
113 return Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);