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