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 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.ApiException;
29 import org.onap.vfccatalog.model.VnfPkgDetailInfo;
30 import org.onap.vfccatalog.model.VnfPkgInfo;
32 import java.io.ByteArrayInputStream;
33 import java.io.IOException;
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;
41 public class TestVfcPackageProvider extends TestBase {
43 private static final String CSAR_ID = "csarId";
44 private static final String CBAM_VNFD_ID = "CBAM_VNFD_ID";
46 private IpMappingProvider ipMappingProvider;
49 private VfcPackageProvider vfcPackageProvider;
53 public void initMocks() throws Exception {
54 setField(VfcPackageProvider.class, "logger", logger);
55 vfcPackageProvider = new VfcPackageProvider(vfcRestApiProvider, ipMappingProvider);
59 * query CBAM VNFD identifier from VF-C catalog
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 when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenReturn(vnfPackageDetails);
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 when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenReturn(vnfPackageDetails);
86 byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
87 when(ipMappingProvider.mapPrivateIpToPublicIp("127.0.0.1")).thenReturn("1.2.3.4");
88 when(entity.getContent()).thenReturn(new ByteArrayInputStream(onapPackageContent));
90 byte[] actualContent = vfcPackageProvider.getPackage(CSAR_ID);
92 Assert.assertArrayEquals(onapPackageContent, actualContent);
93 assertEquals(HttpGet.class, request.getValue().getClass());
94 assertEquals("http://1.2.3.4/a.csar", request.getValue().getURI().toString());
95 assertEquals("application/octet-stream", request.getValue().getFirstHeader(HttpHeaders.ACCEPT).getValue());
99 * failure to query package from VF-C is propagated
102 public void unableToGetCbamVnfdFromCatalog() throws Exception {
103 ApiException expectedException = new ApiException();
104 when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenThrow(expectedException);
107 vfcPackageProvider.getCbamVnfdId(CSAR_ID);
109 } catch (Exception e) {
110 verify(logger).error("Unable to query VNF package with csarId from VF-C", expectedException);
111 assertEquals(expectedException, e.getCause());
116 * failure to download package from VF-C is propagated
119 public void unableToDownloadFromCatalog() throws Exception {
120 VnfPkgDetailInfo vnfPackageDetails = new VnfPkgDetailInfo();
121 vnfPackageDetails.setCsarId(CSAR_ID);
122 vnfPackageDetails.setPackageInfo(new VnfPkgInfo());
123 vnfPackageDetails.getPackageInfo().setVnfdModel("{ \"metadata\" : { \"resourceVendorModelNumber\" : \"" + CBAM_VNFD_ID + "\" }}");
124 vnfPackageDetails.getPackageInfo().setDownloadUrl("http://127.0.0.1/a.csar");
125 when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenReturn(vnfPackageDetails);
126 byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
127 when(ipMappingProvider.mapPrivateIpToPublicIp("127.0.0.1")).thenReturn("1.2.3.4");
128 IOException expectedException = new IOException();
129 when(httpClient.execute(Mockito.any())).thenThrow(expectedException);
132 vfcPackageProvider.getPackage(CSAR_ID);
134 } catch (Exception e) {
135 verify(logger).error("Unable to download package from http://1.2.3.4/a.csar from VF-C", expectedException);
136 assertEquals(expectedException, e.getCause());
141 * failure to query package for download package from VF-C is propagated
144 public void unableToQueryPackageForDownloadFromCatalog() throws Exception {
145 ApiException expectedException = new ApiException();
146 when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenThrow(expectedException);
149 vfcPackageProvider.getPackage(CSAR_ID);
151 } catch (Exception e) {
152 verify(logger).error("Unable to query VNF package with csarId from VF-C", expectedException);
153 assertEquals(expectedException, e.getCause());