3 * ============LICENSE_START=======================================================
4 * Copyright (C) 2020 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
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.contains;
24 import static org.hamcrest.core.Is.is;
25 import static org.hamcrest.core.IsNull.notNullValue;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.nio.charset.StandardCharsets;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import org.apache.commons.io.IOUtils;
37 import org.junit.jupiter.api.Test;
38 import org.openecomp.sdc.be.model.ArtifactDefinition;
39 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.exception.VnfDescriptorException;
40 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.VnfDescriptor;
42 class VnfDescriptorGeneratorImplTest {
44 private final VnfDescriptorGeneratorImpl vnfDescriptorGenerator = new VnfDescriptorGeneratorImpl();
45 private final Path testResourcesPath = Paths.get("src", "test", "resources", "vnf-onboarded-csar");
48 void testGenerate() throws IOException, VnfDescriptorException {
49 final byte[] onboardedPackage = getResourceAsByteArray("TestVnf.csar");
50 final ArtifactDefinition artifactDefinition = new ArtifactDefinition();
51 artifactDefinition.setPayload(onboardedPackage);
52 artifactDefinition.setArtifactName("vnf-onboarded-csar.csar");
53 final String vnfDescriptorName = "vnf-onboarded-csar";
54 final VnfDescriptor vnfDescriptor = vnfDescriptorGenerator.generate(vnfDescriptorName, artifactDefinition).orElse(null);
55 final String expectedNodeType = "org.onap.resource.testVnf";
56 final String expectedVnfdFileName = "test_vnfd.yaml";
57 assertThat("Vnf Descriptor should be present", vnfDescriptor, is(notNullValue()));
58 assertThat("Vnf Descriptor should have the expected name", vnfDescriptor.getName(), is(vnfDescriptorName));
59 assertThat("Vnf Descriptor should have the expected node type", vnfDescriptor.getNodeType(), is(expectedNodeType));
60 assertThat("Vnf Descriptor should have the expected vnfd file name", vnfDescriptor.getVnfdFileName(), is(expectedVnfdFileName));
61 assertThat("Vnf Descriptor should contain the expected definition files count", vnfDescriptor.getDefinitionFiles().size(), is(2));
62 assertTrue("Vnf Descriptor should contain the expected definition entries", vnfDescriptor.getDefinitionFiles().keySet().contains("Definitions/test_vnfd.yaml"));
63 assertTrue("Vnf Descriptor should contain the expected definition entries", vnfDescriptor.getDefinitionFiles().keySet().contains("Definitions/etsi_nfv_sol001_vnfd_2_5_1_types.yaml"));
65 final String vnfdContents = new String(vnfDescriptor.getDefinitionFiles().get("Definitions/test_vnfd.yaml"), StandardCharsets.UTF_8);
66 assertFalse(vnfdContents.contains("interfaces:"));
70 private byte[] getResourceAsByteArray(final String filename) throws IOException {
71 try (final InputStream inputStream = readFileAsStream(filename)) {
72 return IOUtils.toByteArray(inputStream);
73 } catch (final IOException ex) {
74 throw new IOException(String.format("Could not read the file \"%s\"", filename), ex);
78 private FileInputStream readFileAsStream(final String fileName) throws FileNotFoundException {
79 final Path path = Paths.get(testResourcesPath.toString(), fileName);
80 return new FileInputStream(path.toFile());