Reformat catalog-be-plugins
[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 /*
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
9  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
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.generate(vnfDescriptorName, artifactDefinition).orElse(null);
52         final String expectedNodeType = "com.ericsson.resource.abstract.Ericsson.AMF";
53         final String expectedVnfdFileName = "vnfd_amf.yaml";
54         assertThat("Vnf Descriptor should be present", vnfDescriptor, is(notNullValue()));
55         assertThat("Vnf Descriptor should have the expected name", vnfDescriptor.getName(), is(vnfDescriptorName));
56         assertThat("Vnf Descriptor should have the expected node type", vnfDescriptor.getNodeType(), is(expectedNodeType));
57         assertThat("Vnf Descriptor should have the expected vnfd file name", vnfDescriptor.getVnfdFileName(), is(expectedVnfdFileName));
58         assertThat("Vnf Descriptor should contain the expected definition files count", vnfDescriptor.getDefinitionFiles().size(), is(2));
59         assertThat("Vnf Descriptor should contain the expected definition entries", vnfDescriptor.getDefinitionFiles().keySet(),
60             contains("Definitions/vnfd_amf.yaml", "Definitions/etsi_nfv_sol001_vnfd_2_5_1_types.yaml"));
61     }
62
63     private byte[] getResourceAsByteArray(final String filename) throws IOException {
64         try (final InputStream inputStream = readFileAsStream(filename)) {
65             return IOUtils.toByteArray(inputStream);
66         } catch (final IOException ex) {
67             throw new IOException(String.format("Could not read the file \"%s\"", filename), ex);
68         }
69     }
70
71     private FileInputStream readFileAsStream(final String fileName) throws FileNotFoundException {
72         final Path path = Paths.get(testResourcesPath.toString(), fileName);
73         return new FileInputStream(path.toFile());
74     }
75 }