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