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;
23 import static org.junit.Assert.assertNull;
24 import static org.mockito.Mockito.spy;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.util.Collection;
32 import java.util.HashMap;
34 import java.util.Optional;
35 import java.util.stream.Collectors;
36 import java.util.stream.Stream;
37 import mockit.Deencapsulation;
38 import org.apache.commons.io.IOUtils;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.junit.runners.Parameterized;
42 import org.mockito.Mockito;
43 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
44 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
45 import org.onap.sdc.tosca.services.YamlUtil;
46 import org.openecomp.core.converter.ServiceTemplateReaderService;
47 import org.openecomp.core.converter.impl.pnfd.parser.PnfdCustomNodeTypeBlockParser;
48 import org.openecomp.core.converter.pnfd.PnfdTransformationEngine;
49 import org.openecomp.core.converter.pnfd.model.ConversionDefinition;
50 import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl;
52 @RunWith(Parameterized.class)
53 public class PnfTransformationEngineTest {
55 public static final String INPUT_DIR = "pnfDescriptor/in/";
56 public static final String OUTPUT_DIR = "pnfDescriptor/out/";
57 private String inputFilename;
58 private final YamlUtil yamlUtil = new YamlUtil();
59 private final ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
61 public PnfTransformationEngineTest(final String inputFilename) {
62 this.inputFilename = inputFilename;
65 @Parameterized.Parameters(name = "{index}: {0}")
66 public static Collection<String> input() throws IOException {
67 try (final Stream<Path> files = Files.list(getPathFromClasspath(INPUT_DIR))) {
68 return files.map(path -> path.getFileName().toString())
69 .collect(Collectors.toList());
74 public void testTopologyTemplateConversions() throws IOException {
75 final byte[] descriptor = getInputFileResource(inputFilename);
76 final ServiceTemplateReaderService serviceTemplateReaderService = new ServiceTemplateReaderServiceImpl(descriptor);
77 final ServiceTemplate serviceTemplate = new ServiceTemplate();
79 PnfdTransformationEngine pnfdTransformationEngine = new PnfdNodeTypeTransformationEngine(
80 serviceTemplateReaderService, serviceTemplate);
81 pnfdTransformationEngine.transform();
82 pnfdTransformationEngine = new PnfdNodeTemplateTransformationEngine(serviceTemplateReaderService, serviceTemplate);
83 pnfdTransformationEngine.transform();
85 final String yamlContent = yamlUtil.objectToYaml(serviceTemplate);
86 final String result = yamlUtil.objectToYaml(yamlUtil.yamlToObject(yamlContent, ServiceTemplate.class));
87 final String expectedResult = getExpectedResultFor(inputFilename);
88 assertEquals(expectedResult, result);
92 public void testBuildParseBlock() {
93 final PnfdCustomNodeTypeBlockParser blockParser = spy(new PnfdCustomNodeTypeBlockParser(null));
94 final ConversionDefinition conversionDefinition = Mockito.mock(ConversionDefinition.class);
95 final Map<String, Object> stringObjectMap = new HashMap<>();
96 stringObjectMap.put("type", null);
97 stringObjectMap.put("name", null);
98 assertEquals(Optional.empty(), Deencapsulation.invoke(blockParser, "buildParsedBlock",
99 stringObjectMap, stringObjectMap, conversionDefinition));
103 public void testReadDefinition() {
104 final PnfdTransformationEngine engine = spy(
105 new PnfdNodeTemplateTransformationEngine(null, null, "test.txt"));
106 Deencapsulation.invoke(engine, "readDefinition");
107 assertNull(Deencapsulation.getField(engine, "transformationDescription"));
110 private String getExpectedResultFor(final String inputFilename) throws IOException {
111 try (final InputStream inputStream = getOutputFileResourceCorrespondingTo(inputFilename)) {
112 final ServiceTemplate serviceTemplate = toscaExtensionYamlUtil.yamlToObject(inputStream, ServiceTemplate.class);
113 return yamlUtil.objectToYaml(serviceTemplate);
117 private static Path getPathFromClasspath(final String location) {
118 return Paths.get(Thread.currentThread().getContextClassLoader().getResource(location).getPath());
121 private byte[] getInputFileResource(final String inputFilename) throws IOException {
122 return getFileResource(INPUT_DIR + inputFilename);
125 private InputStream getOutputFileResourceCorrespondingTo(final String inputFilename) {
126 final String outputFilename = getOutputFilenameFrom(inputFilename);
127 return getFileResourceAsInputStream(OUTPUT_DIR + outputFilename);
130 private String getOutputFilenameFrom(final String inputFilename) {
131 return inputFilename.replace("pnfDescriptor", "topologyTemplate");
134 private byte[] getFileResource(final String filePath) throws IOException {
135 try (final InputStream inputStream = getFileResourceAsInputStream(filePath)) {
136 return IOUtils.toByteArray(inputStream);
140 private InputStream getFileResourceAsInputStream(final String filePath) {
141 return Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);