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.direct;
18 import org.apache.http.client.methods.HttpGet;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil;
22 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
24 import java.io.ByteArrayInputStream;
25 import java.io.IOException;
26 import java.util.NoSuchElementException;
28 import static junit.framework.TestCase.assertEquals;
29 import static junit.framework.TestCase.fail;
30 import static org.apache.http.HttpHeaders.ACCEPT;
31 import static org.mockito.Matchers.any;
32 import static org.mockito.Matchers.eq;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
35 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager.SERVICE_NAME;
36 import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE;
37 import static org.springframework.test.util.ReflectionTestUtils.setField;
39 public class TestSdcPackageProvider extends TestBase {
40 private SdcPackageProvider sdcPackageProvider;
44 sdcPackageProvider = new SdcPackageProvider(msbApiProvider, driverProperties);
45 setField(SdcPackageProvider.class, "logger", logger);
46 setFieldWithPropertyAnnotation(sdcPackageProvider, "${sdcUsername}", "sdcUsername");
47 setFieldWithPropertyAnnotation(sdcPackageProvider, "${sdcPassword}", "sdcPassword");
48 when(msbApiProvider.getMicroServiceUrl("sdc", "v1")).thenReturn("https://1.2.3.4:456/g");
52 * test package download from SDC
55 public void testPackageDownload() throws Exception {
56 when(entity.getContent()).thenReturn(new ByteArrayInputStream("test".getBytes()));
58 byte[] result = sdcPackageProvider.getPackage("csarId");
60 assertEquals("test", new String("test"));
61 HttpGet httpGet = (HttpGet) request.getValue();
62 assertEquals(VNFM_ID, httpGet.getFirstHeader("X-ECOMP-InstanceID").getValue());
63 assertEquals(SERVICE_NAME, httpGet.getFirstHeader("X-FromAppId").getValue());
64 assertEquals(APPLICATION_OCTET_STREAM_VALUE, httpGet.getFirstHeader(ACCEPT).getValue());
65 assertEquals("https://1.2.3.4:456/g/sdc/v1/catalog/resources/csarId/toscaModel", httpGet.getURI().toASCIIString());
69 * failure to download package from SDC is propagated
72 public void testFailedPackageDownload() throws Exception {
73 IOException expectedException = new IOException();
74 when(httpClient.execute(any())).thenThrow(expectedException);
76 sdcPackageProvider.getPackage("csarId");
78 } catch (Exception e) {
79 assertEquals("Unable to download csarId package from SDC", e.getMessage());
80 assertEquals(expectedException, e.getCause());
81 verify(logger).error("Unable to download csarId package from SDC", expectedException);
86 * get VNFD from ONAP package
89 public void testGetVnfd() throws Exception {
90 byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
91 when(entity.getContent()).thenReturn(new ByteArrayInputStream(onapPackageContent));
93 String cbamVnfdId = sdcPackageProvider.getCbamVnfdId("csarId");
95 assertEquals("Nokia~SimpleDual_scalable~1.0~1.0", cbamVnfdId);
99 * unable to download package from SDC during get CBAM VNFD id
102 public void testUnableToDownloadPackageDuringVnfdIdGet() throws Exception {
103 IOException expectedException = new IOException();
104 when(httpClient.execute(any())).thenThrow(expectedException);
106 sdcPackageProvider.getCbamVnfdId("csarId");
108 } catch (Exception e) {
109 assertEquals("Unable to download csarId package from SDC", e.getMessage());
110 assertEquals(expectedException, e.getCause());
111 verify(logger).error("Unable to download csarId package from SDC", expectedException);
116 * invalid VNF package results in error
119 public void testInvalidVNFDContent() throws Exception {
120 byte[] onapPackageContent = "invalidZip".getBytes();
121 when(entity.getContent()).thenReturn(new ByteArrayInputStream(onapPackageContent));
123 sdcPackageProvider.getCbamVnfdId("csarId");
125 } catch (Exception e) {
126 assertEquals("Unable to extract CBAM VNFD id from ONAP package", e.getMessage());
127 verify(logger).error(eq("Unable to extract CBAM VNFD id from ONAP package"), any(NoSuchElementException.class));