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 io.reactivex.Observable;
25 import okhttp3.RequestBody;
27 import org.apache.commons.lang3.ArrayUtils;
28 import org.apache.http.HttpEntity;
29 import org.apache.http.client.methods.CloseableHttpResponse;
30 import org.apache.http.client.methods.HttpUriRequest;
31 import org.apache.http.impl.client.CloseableHttpClient;
32 import org.assertj.core.util.Lists;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.mockito.MockitoAnnotations;
39 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.INotificationSender;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VnfmInfoProvider;
42 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.MsbApiProvider;
43 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
44 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc.VfcRestApiProvider;
45 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions;
46 import org.onap.vfccatalog.api.VnfpackageApi;
47 import org.onap.vnfmdriver.api.NslcmApi;
48 import org.slf4j.Logger;
49 import org.springframework.beans.factory.annotation.Value;
50 import org.springframework.core.env.Environment;
51 import org.springframework.test.util.ReflectionTestUtils;
52 import retrofit2.Call;
53 import retrofit2.Response;
55 import javax.servlet.http.HttpServletResponse;
56 import java.io.ByteArrayInputStream;
57 import java.io.ByteArrayOutputStream;
58 import java.lang.reflect.Field;
59 import java.util.Arrays;
60 import java.util.HashMap;
61 import java.util.List;
63 import java.util.zip.ZipEntry;
64 import java.util.zip.ZipInputStream;
66 import static junit.framework.TestCase.assertEquals;
67 import static junit.framework.TestCase.assertTrue;
68 import static org.mockito.Mockito.when;
69 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip;
71 public class TestBase {
72 public static final String VNF_ID = "myVnfId";
73 public static final String VNFM_ID = "myVnfmId";
74 public static final String ONAP_CSAR_ID = "myOnapCsarId";
75 public static final String VIM_ID = "myCloudOwnerId_myRegionName";
76 public static final String JOB_ID = "myJobId";
77 public static final String CBAM_VNFD_ID = "cbamVnfdId";
78 protected static Call<Void> VOID_CALL = buildCall(null);
80 protected CbamRestApiProvider cbamRestApiProvider;
82 protected VfcRestApiProvider vfcRestApiProvider;
84 protected MsbApiProvider msbApiProvider;
86 protected VnfmInfoProvider vnfmInfoProvider;
88 protected VnfsApi vnfApi;
90 protected OperationExecutionsApi operationExecutionApi;
92 protected SelfRegistrationManager selfRegistrationManager;
94 protected Logger logger;
96 protected SubscriptionsApi lcnApi;
98 protected MSBServiceClient msbClient;
100 protected DriverProperties driverProperties;
102 protected NslcmApi nsLcmApi;
104 protected INotificationSender notificationSender;
106 protected SystemFunctions systemFunctions;
108 protected VnfpackageApi vfcCatalogApi;
110 protected DefaultApi cbamCatalogApi;
112 protected CloseableHttpClient httpClient;
114 protected CloseableHttpResponse response;
115 protected ArgumentCaptor<HttpUriRequest> request = ArgumentCaptor.forClass(HttpUriRequest.class);
117 protected HttpEntity entity;
119 protected HttpServletResponse httpResponse;
121 protected Environment environment;
123 protected static <T> Call<T> buildCall(T response) {
124 Call<T> call = Mockito.mock(Call.class);
126 when(call.execute()).thenReturn(Response.success(response));
127 } catch (Exception e) {
128 throw new RuntimeException();
133 protected static <T> Observable<T> buildObservable(T response) {
134 return Observable.just(response);
138 public void genericSetup() throws Exception {
139 MockitoAnnotations.initMocks(this);
140 ReflectionTestUtils.setField(SystemFunctions.class, "singletonInstance", systemFunctions);
141 when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID)).thenReturn(vnfApi);
142 when(cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID)).thenReturn(operationExecutionApi);
143 when(cbamRestApiProvider.getCbamLcnApi(VNFM_ID)).thenReturn(lcnApi);
144 when(cbamRestApiProvider.getCbamCatalogApi(VNFM_ID)).thenReturn(cbamCatalogApi);
145 when(msbApiProvider.getMsbClient()).thenReturn(msbClient);
146 when(vfcRestApiProvider.getNsLcmApi()).thenReturn(nsLcmApi);
147 when(vfcRestApiProvider.getVfcCatalogApi()).thenReturn(vfcCatalogApi);
148 when(systemFunctions.getHttpClient()).thenReturn(httpClient);
149 when(httpClient.execute(request.capture())).thenReturn(response);
150 when(response.getEntity()).thenReturn(entity);
151 when(driverProperties.getVnfmId()).thenReturn(VNFM_ID);
152 when(systemFunctions.getHttpClient()).thenReturn(httpClient);
153 when(logger.isInfoEnabled()).thenReturn(true);
154 when(logger.isDebugEnabled()).thenReturn(true);
155 when(logger.isWarnEnabled()).thenReturn(true);
156 when(logger.isErrorEnabled()).thenReturn(true);
160 public void tearGeneric() {
161 ReflectionTestUtils.setField(SystemFunctions.class, "singletonInstance", null);
164 protected void assertFileInZip(byte[] zip, String path, byte[] expectedContent) throws Exception {
165 assertTrue(Arrays.equals(expectedContent, getFileInZip(new ByteArrayInputStream(zip), path).toByteArray()));
168 protected void assertItenticalZips(byte[] expected, byte[] actual) throws Exception {
169 assertEquals(build(expected), build(actual));
172 byte[] getContent(RequestBody requestBody) {
174 Buffer buffer = new Buffer();
175 requestBody.writeTo(buffer);
176 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
177 buffer.copyTo(byteArrayOutputStream);
178 return byteArrayOutputStream.toByteArray();
179 } catch (Exception e) {
180 throw new RuntimeException(e);
184 private Map<String, List<Byte>> build(byte[] zip) throws Exception {
185 Map<String, List<Byte>> files = new HashMap<>();
186 ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zip));
188 while ((zipEntry = zipInputStream.getNextEntry()) != null) {
189 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
190 ByteStreams.copy(zipInputStream, byteArrayOutputStream);
191 files.put(zipEntry.getName(), Lists.newArrayList(ArrayUtils.toObject(byteArrayOutputStream.toByteArray())));
193 zipInputStream.close();
197 protected void setFieldWithPropertyAnnotation(Object obj, String key, String value) {
198 for (Field field : obj.getClass().getDeclaredFields()) {
199 for (Value fieldValue : field.getAnnotationsByType(Value.class)) {
200 if (fieldValue.value().equals(key)) {
202 field.setAccessible(true);
203 field.set(obj, value);
204 } catch (IllegalAccessException e) {
205 throw new RuntimeException(e);