199f72553554cd8e3338b120bb793df18b37e4a0
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / TestSdcPackageProvider.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.direct;
17
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;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.IOException;
26 import java.util.NoSuchElementException;
27
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;
38
39 public class TestSdcPackageProvider extends TestBase {
40     private SdcPackageProvider sdcPackageProvider;
41
42     @Before
43     public void init() {
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");
49     }
50
51     /**
52      * test package download from SDC
53      */
54     @Test
55     public void testPackageDownload() throws Exception {
56         when(entity.getContent()).thenReturn(new ByteArrayInputStream("test".getBytes()));
57         //when
58         byte[] result = sdcPackageProvider.getPackage("csarId");
59         //verify
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());
66     }
67
68     /**
69      * failure to download package from SDC is propagated
70      */
71     @Test
72     public void testFailedPackageDownload() throws Exception {
73         IOException expectedException = new IOException();
74         when(httpClient.execute(any())).thenThrow(expectedException);
75         try {
76             sdcPackageProvider.getPackage("csarId");
77             fail();
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);
82         }
83     }
84
85     /**
86      * get VNFD from ONAP package
87      */
88     @Test
89     public void testGetVnfd() throws Exception {
90         byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar");
91         when(entity.getContent()).thenReturn(new ByteArrayInputStream(onapPackageContent));
92         //when
93         String cbamVnfdId = sdcPackageProvider.getCbamVnfdId("csarId");
94         //verify
95         assertEquals("Nokia~SimpleDual_scalable~1.0~1.0", cbamVnfdId);
96     }
97
98     /**
99      * unable to download package from SDC during get CBAM VNFD id
100      */
101     @Test
102     public void testUnableToDownloadPackageDuringVnfdIdGet() throws Exception {
103         IOException expectedException = new IOException();
104         when(httpClient.execute(any())).thenThrow(expectedException);
105         try {
106             sdcPackageProvider.getCbamVnfdId("csarId");
107             fail();
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);
112         }
113     }
114
115     /**
116      * invalid VNF package results in error
117      */
118     @Test
119     public void testInvalidVNFDContent() throws Exception {
120         byte[] onapPackageContent = "invalidZip".getBytes();
121         when(entity.getContent()).thenReturn(new ByteArrayInputStream(onapPackageContent));
122         try {
123             sdcPackageProvider.getCbamVnfdId("csarId");
124             fail();
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));
128         }
129     }
130 }