2 * Copyright 2016-2017, Nokia Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc;
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;
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;
40 public class TestVfcPackageProvider extends TestBase {
42 private static final String CSAR_ID = "csarId";
43 private static final String CBAM_VNFD_ID = "CBAM_VNFD_ID";
45 private IpMappingProvider ipMappingProvider;
48 private VfcPackageProvider vfcPackageProvider;
52 public void initMocks() throws Exception {
53 setField(VfcPackageProvider.class, "logger", logger);
54 vfcPackageProvider = new VfcPackageProvider(vfcRestApiProvider, ipMappingProvider);
58 * query CBAM VNFD identifier from VF-C catalog
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);
70 String cbamVnfdId = vfcPackageProvider.getCbamVnfdId(CSAR_ID);
72 assertEquals(CBAM_VNFD_ID, cbamVnfdId);
76 * download ONAP VNFD from VF-C catalog
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));
91 byte[] actualContent = vfcPackageProvider.getPackage(CSAR_ID);
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());
100 * failure to query package from VF-C is propagated
103 public void unableToGetCbamVnfdFromCatalog() throws Exception {
104 RuntimeException expectedException = new RuntimeException();
105 when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenThrow(expectedException);
108 vfcPackageProvider.getCbamVnfdId(CSAR_ID);
110 } catch (Exception e) {
111 verify(logger).error("Unable to query VNF package with csarId", expectedException);
112 assertEquals(expectedException, e.getCause());
117 * failure to download package from VF-C is propagated
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);
134 vfcPackageProvider.getPackage(CSAR_ID);
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());
143 * failure to query package for download package from VF-C is propagated
146 public void unableToQueryPackageForDownloadFromCatalog() throws Exception {
147 RuntimeException expectedException = new RuntimeException();
148 when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenThrow(expectedException);
151 vfcPackageProvider.getPackage(CSAR_ID);
153 } catch (Exception e) {
154 verify(logger).error("Unable to query VNF package with csarId", expectedException);
155 assertEquals(expectedException, e.getCause());