4a8c715e5c6d92e6fd5fa5f7d9fd733a9682a017
[so.git] / so-etsi-nfvo / so-etsi-nfvo-ns-lcm / so-etsi-nfvo-ns-lcm-bpmn-flows / src / test / java / org / onap / so / etsi / nfvo / ns / lcm / bpmn / flows / nsd / parser / NetworkServiceDescriptorParserTest.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  *
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.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.nsd.parser;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import java.io.File;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Paths;
29 import java.util.Map;
30 import java.util.Optional;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.TestApplication;
34 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.nsd.NetworkServiceDescriptor;
35 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.nsd.parser.NetworkServiceDescriptorParser;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.boot.test.context.SpringBootTest;
38 import org.springframework.test.context.ActiveProfiles;
39 import org.springframework.test.context.junit4.SpringRunner;
40
41
42 /**
43  * @author Waqas Ikram (waqas.ikram@est.tech)
44  *
45  */
46 @RunWith(SpringRunner.class)
47 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
48 @ActiveProfiles("test")
49 public class NetworkServiceDescriptorParserTest {
50
51     private static final String VALID_ETSI_NSD_FILE = "src/test/resources/ns.csar";
52     private static final String INVALID_ETSI_NSD_FILE = "src/test/resources/invalid_ns.csar";
53
54     @Autowired
55     private NetworkServiceDescriptorParser objUnderTest;
56
57     @Test
58     public void testValidEtsiNsd_ableToParseIt() throws IOException {
59         final byte[] zipBytes = Files.readAllBytes(Paths.get(getAbsolutePath(VALID_ETSI_NSD_FILE)));
60         final Optional<NetworkServiceDescriptor> optional = objUnderTest.parse(zipBytes);
61         assertTrue(optional.isPresent());
62         final NetworkServiceDescriptor actualNsd = optional.get();
63         assertEquals(NetworkServiceDescriptorParser.NS_NODE_TYPE, actualNsd.getType());
64         assertFalse(actualNsd.getProperties().isEmpty());
65
66         final Map<String, Object> actualNsdProperties = actualNsd.getProperties();
67         assertEquals(5, actualNsdProperties.size());
68         assertEquals("ffdddc5d-a44b-45ae-8fc3-e6551cce350f", actualNsdProperties.get("descriptor_id"));
69         assertEquals(5, actualNsd.getVnfs().size());
70
71     }
72
73     @Test
74     public void testEmptyEtsiNsd_returnEmptyOptional() throws IOException {
75         assertFalse(objUnderTest.parse(new byte[] {}).isPresent());
76     }
77
78     @Test
79     public void testInvalidEtsiNsd_returnEmptyOptional() throws IOException {
80         final byte[] zipBytes = Files.readAllBytes(Paths.get(getAbsolutePath(INVALID_ETSI_NSD_FILE)));
81         assertFalse(objUnderTest.parse(zipBytes).isPresent());
82     }
83
84     private String getAbsolutePath(final String path) {
85         final File file = new File(path);
86         return file.getAbsolutePath();
87     }
88
89 }