Removing jackson to mitigate cve-2017-4995
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / TestSdcPackageProvider.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.IOException;
20 import java.util.NoSuchElementException;
21 import org.apache.http.client.methods.HttpGet;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
26
27 import static junit.framework.TestCase.assertEquals;
28 import static junit.framework.TestCase.fail;
29 import static org.apache.http.HttpHeaders.ACCEPT;
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Matchers.eq;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
34 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager.SERVICE_NAME;
35 import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE;
36 import static org.springframework.test.util.ReflectionTestUtils.setField;
37
38 public class TestSdcPackageProvider extends TestBase {
39     private SdcPackageProvider sdcPackageProvider;
40
41     @Before
42     public void init() {
43         sdcPackageProvider = new SdcPackageProvider(msbApiProvider, driverProperties);
44         setField(SdcPackageProvider.class, "logger", logger);
45         setFieldWithPropertyAnnotation(sdcPackageProvider, "${sdcUsername}", "sdcUsername");
46         setFieldWithPropertyAnnotation(sdcPackageProvider, "${sdcPassword}", "sdcPassword");
47         when(msbApiProvider.getMicroServiceUrl("sdc", "v1")).thenReturn("https://1.2.3.4:456/g");
48     }
49
50     /**
51      * test package download from SDC
52      */
53     @Test
54     public void testPackageDownload() throws Exception {
55         when(entity.getContent()).thenReturn(new ByteArrayInputStream("test".getBytes()));
56         //when
57         byte[] result = sdcPackageProvider.getPackage("csarId");
58         //verify
59         assertEquals("test", new String("test"));
60         HttpGet httpGet = (HttpGet) request.getValue();
61         assertEquals(VNFM_ID, httpGet.getFirstHeader("X-ECOMP-InstanceID").getValue());
62         assertEquals(SERVICE_NAME, httpGet.getFirstHeader("X-FromAppId").getValue());
63         assertEquals(APPLICATION_OCTET_STREAM_VALUE, httpGet.getFirstHeader(ACCEPT).getValue());
64         assertEquals("https://1.2.3.4:456/g/sdc/v1/catalog/resources/csarId/toscaModel", httpGet.getURI().toASCIIString());
65     }
66
67     /**
68      * failure to download package from SDC is propagated
69      */
70     @Test
71     public void testFailedPackageDownload() throws Exception {
72         IOException expectedException = new IOException();
73         when(httpClient.execute(any())).thenThrow(expectedException);
74         try {
75             sdcPackageProvider.getPackage("csarId");
76             fail();
77         } catch (Exception e) {
78             assertEquals("Unable to download csarId package from SDC", e.getMessage());
79             assertEquals(expectedException, e.getCause());
80             verify(logger).error("Unable to download csarId package from SDC", expectedException);
81         }
82     }
83
84     /**
85      * get VNFD from ONAP package
86      */
87     @Test
88     public void testGetVnfd() throws Exception {
89         byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
90         when(entity.getContent()).thenReturn(new ByteArrayInputStream(onapPackageContent));
91         //when
92         String cbamVnfdId = sdcPackageProvider.getCbamVnfdId("csarId");
93         //verify
94         assertEquals("Nokia~SimpleDual_scalable~1.0~1.0", cbamVnfdId);
95     }
96
97     /**
98      * unable to download package from SDC during get CBAM VNFD id
99      */
100     @Test
101     public void testUnableToDownloadPackageDuringVnfdIdGet() throws Exception {
102         IOException expectedException = new IOException();
103         when(httpClient.execute(any())).thenThrow(expectedException);
104         try {
105             sdcPackageProvider.getCbamVnfdId("csarId");
106             fail();
107         } catch (Exception e) {
108             assertEquals("Unable to download csarId package from SDC", e.getMessage());
109             assertEquals(expectedException, e.getCause());
110             verify(logger).error("Unable to download csarId package from SDC", expectedException);
111         }
112     }
113
114     /**
115      * invalid VNF package results in error
116      */
117     @Test
118     public void testInvalidVNFDContent() throws Exception {
119         byte[] onapPackageContent = "invalidZip".getBytes();
120         when(entity.getContent()).thenReturn(new ByteArrayInputStream(onapPackageContent));
121         try {
122             sdcPackageProvider.getCbamVnfdId("csarId");
123             fail();
124         } catch (Exception e) {
125             assertEquals("Unable to extract CBAM VNFD id from ONAP package", e.getMessage());
126             verify(logger).error(eq("Unable to extract CBAM VNFD id from ONAP package"), any(NoSuchElementException.class));
127         }
128     }
129 }