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.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm;
19 import com.google.common.io.ByteStreams;
20 import com.nokia.cbam.catalog.v1.api.DefaultApi;
21 import com.nokia.cbam.lcm.v32.api.OperationExecutionsApi;
22 import com.nokia.cbam.lcm.v32.api.VnfsApi;
23 import com.nokia.cbam.lcn.v32.api.SubscriptionsApi;
24 import org.apache.commons.lang3.ArrayUtils;
25 import org.apache.http.HttpEntity;
26 import org.apache.http.client.methods.CloseableHttpResponse;
27 import org.apache.http.client.methods.HttpUriRequest;
28 import org.apache.http.impl.client.CloseableHttpClient;
29 import org.assertj.core.util.Lists;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.INotificationSender;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VnfmInfoProvider;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.MsbApiProvider;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc.VfcRestApiProvider;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions;
42 import org.onap.vfccatalog.api.VnfpackageApi;
43 import org.onap.vnfmdriver.api.NslcmApi;
44 import org.slf4j.Logger;
45 import org.springframework.beans.factory.annotation.Value;
46 import org.springframework.core.env.Environment;
47 import org.springframework.test.util.ReflectionTestUtils;
49 import javax.servlet.http.HttpServletResponse;
50 import java.io.ByteArrayInputStream;
51 import java.io.ByteArrayOutputStream;
52 import java.lang.reflect.Field;
53 import java.util.Arrays;
54 import java.util.HashMap;
55 import java.util.List;
57 import java.util.zip.ZipEntry;
58 import java.util.zip.ZipInputStream;
60 import static junit.framework.TestCase.assertEquals;
61 import static junit.framework.TestCase.assertTrue;
62 import static org.mockito.Mockito.when;
63 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip;
65 public class TestBase {
66 public static final String VNF_ID = "myVnfId";
67 public static final String VNFM_ID = "myVnfmId";
68 public static final String ONAP_CSAR_ID = "myOnapCsarId";
69 public static final String VIM_ID = "myCloudOwnerId_myRegionName";
70 public static final String JOB_ID = "myJobId";
71 public static final String CBAM_VNFD_ID = "cbamVnfdId";
73 protected CbamRestApiProvider cbamRestApiProvider;
75 protected VfcRestApiProvider vfcRestApiProvider;
77 protected MsbApiProvider msbApiProvider;
79 protected VnfmInfoProvider vnfmInfoProvider;
81 protected VnfsApi vnfApi;
83 protected OperationExecutionsApi operationExecutionApi;
85 protected SelfRegistrationManager selfRegistrationManager;
87 protected Logger logger;
89 protected SubscriptionsApi lcnApi;
91 protected MSBServiceClient msbClient;
93 protected DriverProperties driverProperties;
95 protected NslcmApi nsLcmApi;
97 protected INotificationSender notificationSender;
99 protected SystemFunctions systemFunctions;
101 protected VnfpackageApi vfcCatalogApi;
103 protected DefaultApi cbamCatalogApi;
105 protected CloseableHttpClient httpClient;
107 protected CloseableHttpResponse response;
108 protected ArgumentCaptor<HttpUriRequest> request = ArgumentCaptor.forClass(HttpUriRequest.class);
110 protected HttpEntity entity;
112 protected HttpServletResponse httpResponse;
114 protected Environment environment;
117 public void genericSetup() throws Exception {
118 MockitoAnnotations.initMocks(this);
119 ReflectionTestUtils.setField(SystemFunctions.class, "INSTANCE", systemFunctions);
120 when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID)).thenReturn(vnfApi);
121 when(cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID)).thenReturn(operationExecutionApi);
122 when(cbamRestApiProvider.getCbamLcnApi(VNFM_ID)).thenReturn(lcnApi);
123 when(cbamRestApiProvider.getCbamCatalogApi(VNFM_ID)).thenReturn(cbamCatalogApi);
124 when(msbApiProvider.getMsbClient()).thenReturn(msbClient);
125 when(vfcRestApiProvider.getNsLcmApi()).thenReturn(nsLcmApi);
126 when(vfcRestApiProvider.getOnapCatalogApi()).thenReturn(vfcCatalogApi);
127 when(systemFunctions.getHttpClient()).thenReturn(httpClient);
128 when(httpClient.execute(request.capture())).thenReturn(response);
129 when(response.getEntity()).thenReturn(entity);
130 when(driverProperties.getVnfmId()).thenReturn(VNFM_ID);
131 when(systemFunctions.getHttpClient()).thenReturn(httpClient);
135 public void tearGeneric() {
136 ReflectionTestUtils.setField(SystemFunctions.class, "INSTANCE", null);
139 protected void assertFileInZip(byte[] zip, String path, byte[] expectedContent) throws Exception {
140 assertTrue(Arrays.equals(expectedContent, getFileInZip(new ByteArrayInputStream(zip), path).toByteArray()));
143 protected void assertItenticalZips(byte[] expected, byte[] actual) throws Exception {
144 assertEquals(build(expected), build(actual));
147 private Map<String, List<Byte>> build(byte[] zip) throws Exception {
148 Map<String, List<Byte>> files = new HashMap<>();
149 ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zip));
151 while ((zipEntry = zipInputStream.getNextEntry()) != null) {
152 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
153 ByteStreams.copy(zipInputStream, byteArrayOutputStream);
154 files.put(zipEntry.getName(), Lists.newArrayList(ArrayUtils.toObject(byteArrayOutputStream.toByteArray())));
156 zipInputStream.close();
160 protected void setFieldWithPropertyAnnotation(Object obj, String key, String value) {
161 for (Field field : obj.getClass().getDeclaredFields()) {
162 for (Value fieldValue : field.getAnnotationsByType(Value.class)) {
163 if (fieldValue.value().equals(key)) {
165 field.setAccessible(true);
166 field.set(obj, value);
167 } catch (IllegalAccessException e) {
168 throw new RuntimeException(e);