Plugin to Generate Service ETSI NSD CSAR
[sdc.git] / catalog-be-plugins / etsi-nfv-nsd-csar-plugin / src / test / java / org / openecomp / sdc / be / plugins / etsi / nfv / nsd / generator / VnfDescriptorGeneratorImplTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator;
21
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
27 import java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import org.apache.commons.io.IOUtils;
34 import org.junit.jupiter.api.Test;
35 import org.openecomp.sdc.be.model.ArtifactDefinition;
36 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.exception.VnfDescriptorException;
37 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.VnfDescriptor;
38
39 class VnfDescriptorGeneratorImplTest {
40
41     private final VnfDescriptorGeneratorImpl vnfDescriptorGenerator = new VnfDescriptorGeneratorImpl();
42     private final Path testResourcesPath = Paths.get("src", "test", "resources", "vnf-onboarded-csar");
43
44     @Test
45     void testGenerate() throws IOException, VnfDescriptorException {
46         final byte[] onboardedPackage = getResourceAsByteArray("VnfPackage_AMF_v2.csar");
47         final ArtifactDefinition artifactDefinition = new ArtifactDefinition();
48         artifactDefinition.setPayload(onboardedPackage);
49         artifactDefinition.setArtifactName("vnf-onboarded-csar.csar");
50         final String vnfDescriptorName = "vnf-onboarded-csar";
51         final VnfDescriptor vnfDescriptor = vnfDescriptorGenerator
52             .generate(vnfDescriptorName, artifactDefinition).orElse(null);
53         final String expectedNodeType = "com.ericsson.resource.abstract.Ericsson.AMF";
54         final String expectedVnfdFileName = "vnfd_amf.yaml";
55         assertThat("Vnf Descriptor should be present", vnfDescriptor, is(notNullValue()));
56         assertThat("Vnf Descriptor should have the expected name", vnfDescriptor.getName(),
57             is(vnfDescriptorName));
58         assertThat("Vnf Descriptor should have the expected node type", vnfDescriptor.getNodeType(),
59             is(expectedNodeType));
60         assertThat("Vnf Descriptor should have the expected vnfd file name", vnfDescriptor.getVnfdFileName(),
61             is(expectedVnfdFileName));
62         assertThat("Vnf Descriptor should contain the expected definition files count",
63             vnfDescriptor.getDefinitionFiles().size(), is(2));
64         assertThat("Vnf Descriptor should contain the expected definition entries",
65             vnfDescriptor.getDefinitionFiles().keySet(), contains("Definitions/vnfd_amf.yaml",
66                 "Definitions/etsi_nfv_sol001_vnfd_2_5_1_types.yaml"));
67     }
68
69     private byte[] getResourceAsByteArray(final String filename) throws IOException {
70         try (final InputStream inputStream = readFileAsStream(filename)) {
71             return IOUtils.toByteArray(inputStream);
72         } catch (final IOException ex) {
73             throw new IOException(
74                 String.format("Could not read the file \"%s\"", filename), ex);
75         }
76     }
77
78     private FileInputStream readFileAsStream(final String fileName) throws FileNotFoundException {
79         final Path path = Paths.get(testResourcesPath.toString(), fileName);
80         return new FileInputStream(path.toFile());
81     }
82
83 }