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;
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;
39 public class TestVfcPackageProvider extends TestBase {
41 private static final String CSAR_ID = "csarId";
42 private static final String CBAM_VNFD_ID = "CBAM_VNFD_ID";
44 private IpMappingProvider ipMappingProvider;
47 private VfcPackageProvider vfcPackageProvider;
51 public void initMocks() throws Exception {
52 setField(VfcPackageProvider.class, "logger", logger);
53 vfcPackageProvider = new VfcPackageProvider(vfcRestApiProvider, ipMappingProvider);
57 * query CBAM VNFD identifier from VF-C catalog
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));
68 String cbamVnfdId = vfcPackageProvider.getCbamVnfdId(CSAR_ID);
70 assertEquals(CBAM_VNFD_ID, cbamVnfdId);
74 * download ONAP VNFD from VF-C catalog
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));
88 byte[] actualContent = vfcPackageProvider.getPackage(CSAR_ID);
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());
97 * failure to query package from VF-C is propagated
100 public void unableToGetCbamVnfdFromCatalog() throws Exception {
101 RuntimeException expectedException = new RuntimeException();
102 when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenThrow(expectedException);
105 vfcPackageProvider.getCbamVnfdId(CSAR_ID);
107 } catch (Exception e) {
108 verify(logger).error("Unable to query VNF package with csarId", expectedException);
109 assertEquals(expectedException, e.getCause());
114 * failure to download package from VF-C is propagated
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);
130 vfcPackageProvider.getPackage(CSAR_ID);
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());
139 * failure to query package for download package from VF-C is propagated
142 public void unableToQueryPackageForDownloadFromCatalog() throws Exception {
143 RuntimeException expectedException = new RuntimeException();
144 when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenThrow(expectedException);
147 vfcPackageProvider.getPackage(CSAR_ID);
149 } catch (Exception e) {
150 verify(logger).error("Unable to query VNF package with csarId", expectedException);
151 assertEquals(expectedException, e.getCause());