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 / vfc / TestVfcPackageProvider.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.vfc;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.IOException;
20 import org.apache.http.HttpHeaders;
21 import org.apache.http.client.methods.HttpGet;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.IpMappingProvider;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
30 import org.onap.vfccatalog.model.VnfPkgDetailInfo;
31 import org.onap.vfccatalog.model.VnfPkgInfo;
32 import retrofit2.Call;
33
34 import static junit.framework.TestCase.assertEquals;
35 import static junit.framework.TestCase.fail;
36 import static org.mockito.Mockito.verify;
37 import static org.mockito.Mockito.when;
38 import static org.springframework.test.util.ReflectionTestUtils.setField;
39
40 public class TestVfcPackageProvider extends TestBase {
41
42     private static final String CSAR_ID = "csarId";
43     private static final String CBAM_VNFD_ID = "CBAM_VNFD_ID";
44     @Mock
45     private IpMappingProvider ipMappingProvider;
46
47     @Mock
48     private VfcPackageProvider vfcPackageProvider;
49
50
51     @Before
52     public void initMocks() throws Exception {
53         setField(VfcPackageProvider.class, "logger", logger);
54         vfcPackageProvider = new VfcPackageProvider(vfcRestApiProvider, ipMappingProvider);
55     }
56
57     /**
58      * query CBAM VNFD identifier from VF-C catalog
59      */
60     @Test
61     public void testGetCbamVnfd() throws Exception {
62         VnfPkgDetailInfo vnfPackageDetails = new VnfPkgDetailInfo();
63         vnfPackageDetails.setCsarId(CSAR_ID);
64         vnfPackageDetails.setPackageInfo(new VnfPkgInfo());
65         vnfPackageDetails.getPackageInfo().setVnfdModel("{ \"metadata\" : { \"resourceVendorModelNumber\" : \"" + CBAM_VNFD_ID + "\" }}");
66         vnfPackageDetails.getPackageInfo().setDownloadUrl("http://127.0.0.1/a.csar");
67         Call<VnfPkgDetailInfo> vnfPkgDetailInfoCall = buildCall(vnfPackageDetails);
68         when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenReturn(vnfPkgDetailInfoCall);
69         //when
70         String cbamVnfdId = vfcPackageProvider.getCbamVnfdId(CSAR_ID);
71         //verify
72         assertEquals(CBAM_VNFD_ID, cbamVnfdId);
73     }
74
75     /**
76      * download ONAP VNFD from VF-C catalog
77      */
78     @Test
79     public void testDownload() throws Exception {
80         VnfPkgDetailInfo vnfPackageDetails = new VnfPkgDetailInfo();
81         vnfPackageDetails.setCsarId(CSAR_ID);
82         vnfPackageDetails.setPackageInfo(new VnfPkgInfo());
83         vnfPackageDetails.getPackageInfo().setVnfdModel("{ \"metadata\" : { \"resourceVendorModelNumber\" : \"" + CBAM_VNFD_ID + "\" }}");
84         vnfPackageDetails.getPackageInfo().setDownloadUrl("http://127.0.0.1/a.csar");
85         Call<VnfPkgDetailInfo> vnfPkgDetailInfoCall = buildCall(vnfPackageDetails);
86         when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenReturn(vnfPkgDetailInfoCall);
87         byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
88         when(ipMappingProvider.mapPrivateIpToPublicIp("127.0.0.1")).thenReturn("1.2.3.4");
89         when(entity.getContent()).thenReturn(new ByteArrayInputStream(onapPackageContent));
90         //when
91         byte[] actualContent = vfcPackageProvider.getPackage(CSAR_ID);
92         //verify
93         Assert.assertArrayEquals(onapPackageContent, actualContent);
94         assertEquals(HttpGet.class, request.getValue().getClass());
95         assertEquals("http://1.2.3.4/a.csar", request.getValue().getURI().toString());
96         assertEquals("application/octet-stream", request.getValue().getFirstHeader(HttpHeaders.ACCEPT).getValue());
97     }
98
99     /**
100      * failure to query package from VF-C is propagated
101      */
102     @Test
103     public void unableToGetCbamVnfdFromCatalog() throws Exception {
104         RuntimeException expectedException = new RuntimeException();
105         when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenThrow(expectedException);
106         //when
107         try {
108             vfcPackageProvider.getCbamVnfdId(CSAR_ID);
109             fail();
110         } catch (Exception e) {
111             verify(logger).error("Unable to query VNF package with csarId", expectedException);
112             assertEquals(expectedException, e.getCause());
113         }
114     }
115
116     /**
117      * failure to download package from VF-C is propagated
118      */
119     @Test
120     public void unableToDownloadFromCatalog() throws Exception {
121         VnfPkgDetailInfo vnfPackageDetails = new VnfPkgDetailInfo();
122         vnfPackageDetails.setCsarId(CSAR_ID);
123         vnfPackageDetails.setPackageInfo(new VnfPkgInfo());
124         vnfPackageDetails.getPackageInfo().setVnfdModel("{ \"metadata\" : { \"resourceVendorModelNumber\" : \"" + CBAM_VNFD_ID + "\" }}");
125         vnfPackageDetails.getPackageInfo().setDownloadUrl("http://127.0.0.1/a.csar");
126         Call<VnfPkgDetailInfo> vnfPkgDetailInfoCall = buildCall(vnfPackageDetails);
127         when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenReturn(vnfPkgDetailInfoCall);
128         byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
129         when(ipMappingProvider.mapPrivateIpToPublicIp("127.0.0.1")).thenReturn("1.2.3.4");
130         IOException expectedException = new IOException();
131         when(httpClient.execute(Mockito.any())).thenThrow(expectedException);
132         //when
133         try {
134             vfcPackageProvider.getPackage(CSAR_ID);
135             fail();
136         } catch (Exception e) {
137             verify(logger).error("Unable to download package from http://1.2.3.4/a.csar", expectedException);
138             assertEquals(expectedException, e.getCause());
139         }
140     }
141
142     /**
143      * failure to query package for download package from VF-C is propagated
144      */
145     @Test
146     public void unableToQueryPackageForDownloadFromCatalog() throws Exception {
147         RuntimeException expectedException = new RuntimeException();
148         when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenThrow(expectedException);
149         //when
150         try {
151             vfcPackageProvider.getPackage(CSAR_ID);
152             fail();
153         } catch (Exception e) {
154             verify(logger).error("Unable to query VNF package with csarId", expectedException);
155             assertEquals(expectedException, e.getCause());
156         }
157     }
158 }