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