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 io.reactivex.internal.operators.observable.ObservableFromCallable;
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.lang.reflect.Field;
30 import java.util.concurrent.Callable;
31 import java.util.zip.ZipEntry;
32 import java.util.zip.ZipInputStream;
33 import javax.servlet.http.HttpServletResponse;
34 import junit.framework.TestCase;
35 import okhttp3.RequestBody;
37 import org.apache.commons.lang3.ArrayUtils;
38 import org.apache.http.HttpEntity;
39 import org.apache.http.client.methods.CloseableHttpResponse;
40 import org.apache.http.client.methods.HttpUriRequest;
41 import org.apache.http.impl.client.CloseableHttpClient;
42 import org.assertj.core.util.Lists;
43 import org.junit.After;
44 import org.junit.Before;
45 import org.mockito.ArgumentCaptor;
46 import org.mockito.Mock;
47 import org.mockito.Mockito;
48 import org.mockito.MockitoAnnotations;
49 import org.onap.msb.api.ServiceResourceApi;
50 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.INotificationSender;
51 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VnfmInfoProvider;
52 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.MsbApiProvider;
53 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManagerForSo;
54 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManagerForVfc;
55 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AaiSecurityProvider;
56 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc.VfcRestApiProvider;
57 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions;
58 import org.onap.vfccatalog.api.VnfpackageApi;
59 import org.onap.vnfmdriver.api.NslcmApi;
60 import org.onap.vnfmdriver.model.VnfmInfo;
61 import org.slf4j.Logger;
62 import org.springframework.beans.factory.annotation.Value;
63 import org.springframework.core.env.Environment;
64 import org.springframework.test.util.ReflectionTestUtils;
65 import retrofit2.Call;
66 import retrofit2.Response;
68 import static junit.framework.TestCase.assertEquals;
69 import static junit.framework.TestCase.assertTrue;
70 import static org.mockito.Mockito.when;
71 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.SEPARATOR;
72 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip;
75 public class TestBase {
77 public static final String VNF_ID = "myVnfId";
78 public static final String VNFM_ID = "myVnfmId";
79 public static final String ONAP_CSAR_ID = "myOnapCsarId";
80 public static final String VIM_ID = "myCloudOwnerId_myRegionName";
81 public static final String JOB_ID = "myJobId";
82 public static final String CBAM_VNFD_ID = "cbamVnfdId";
83 public static final String HTTP_AUTH_URL = "http://authurl/";
84 public static final String HTTP_LCM_URL = "http://lcmurl/";
85 public static final String HTTP_LCN_URL = "http://lcnurl/";
86 public static final String HTTP_CATLOG_URL = "http://catlogurl/";
87 public static final String SUBCRIPTION_ID = "subcriptionId";
88 protected static VoidObservable VOID_OBSERVABLE = new VoidObservable();
90 protected CbamRestApiProvider cbamRestApiProvider;
92 protected CbamRestApiProviderForSo cbamRestApiProviderForSo;
94 protected CbamRestApiProviderForVfc cbamRestApiProviderForVfc;
96 protected VfcRestApiProvider vfcRestApiProvider;
98 protected MsbApiProvider msbApiProvider;
100 protected AaiSecurityProvider aaiSecurityProvider;
102 protected VnfmInfoProvider vnfmInfoProvider;
104 protected VnfsApi vnfApi;
106 protected OperationExecutionsApi operationExecutionApi;
108 protected SelfRegistrationManagerForVfc selfRegistrationManagerForVfc;
110 protected SelfRegistrationManagerForSo selfRegistrationManagerForSo;
112 protected Logger logger;
114 protected SubscriptionsApi lcnApi;
116 protected ServiceResourceApi msbClient;
118 protected Constants driverProperties;
120 protected NslcmApi nsLcmApi;
122 protected INotificationSender notificationSender;
124 protected SystemFunctions systemFunctions;
126 protected VnfpackageApi vfcCatalogApi;
128 protected DefaultApi cbamCatalogApi;
130 protected CloseableHttpClient httpClient;
132 protected CloseableHttpResponse response;
133 protected ArgumentCaptor<HttpUriRequest> request = ArgumentCaptor.forClass(HttpUriRequest.class);
135 protected HttpEntity entity;
137 protected HttpServletResponse httpResponse;
139 protected Environment environment;
141 protected VnfmInfo vnfmInfo = new VnfmInfo();
143 protected static <T> Call<T> buildCall(T response) {
144 Call<T> call = Mockito.mock(Call.class);
146 when(call.execute()).thenReturn(Response.success(response));
147 } catch (Exception e) {
148 throw new RuntimeException();
153 protected static <T> Observable<T> buildObservable(T response) {
154 return Observable.just(response);
158 public void genericSetup() throws Exception {
159 MockitoAnnotations.initMocks(this);
160 ReflectionTestUtils.setField(SystemFunctions.class, "singletonInstance", systemFunctions);
161 when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID)).thenReturn(vnfApi);
162 when(cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID)).thenReturn(operationExecutionApi);
163 when(cbamRestApiProvider.getCbamLcnApi(VNFM_ID)).thenReturn(lcnApi);
164 when(cbamRestApiProvider.getCbamCatalogApi(VNFM_ID)).thenReturn(cbamCatalogApi);
165 when(cbamRestApiProviderForSo.getCbamLcmApi(VNFM_ID)).thenReturn(vnfApi);
166 when(cbamRestApiProviderForSo.getCbamOperationExecutionApi(VNFM_ID)).thenReturn(operationExecutionApi);
167 when(cbamRestApiProviderForSo.getCbamLcnApi(VNFM_ID)).thenReturn(lcnApi);
168 when(cbamRestApiProviderForSo.getCbamCatalogApi(VNFM_ID)).thenReturn(cbamCatalogApi);
170 when(cbamRestApiProviderForVfc.getCbamLcmApi(VNFM_ID)).thenReturn(vnfApi);
171 when(cbamRestApiProviderForVfc.getCbamOperationExecutionApi(VNFM_ID)).thenReturn(operationExecutionApi);
172 when(cbamRestApiProviderForVfc.getCbamLcnApi(VNFM_ID)).thenReturn(lcnApi);
173 when(cbamRestApiProviderForVfc.getCbamCatalogApi(VNFM_ID)).thenReturn(cbamCatalogApi);
175 when(msbApiProvider.getMsbApi()).thenReturn(msbClient);
176 when(vfcRestApiProvider.getNsLcmApi()).thenReturn(nsLcmApi);
177 when(vfcRestApiProvider.getVfcCatalogApi()).thenReturn(vfcCatalogApi);
178 when(systemFunctions.getHttpClient()).thenReturn(httpClient);
179 when(httpClient.execute(request.capture())).thenReturn(response);
180 when(response.getEntity()).thenReturn(entity);
181 when(systemFunctions.getHttpClient()).thenReturn(httpClient);
182 when(logger.isInfoEnabled()).thenReturn(true);
183 when(logger.isDebugEnabled()).thenReturn(true);
184 when(logger.isWarnEnabled()).thenReturn(true);
185 when(logger.isErrorEnabled()).thenReturn(true);
186 when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(vnfmInfo);
187 vnfmInfo.setUrl(HTTP_AUTH_URL + SEPARATOR + HTTP_LCM_URL + SEPARATOR + HTTP_LCN_URL + SEPARATOR + HTTP_CATLOG_URL);
188 vnfmInfo.setUserName("myUsername" + SEPARATOR + "myClientId");
189 vnfmInfo.setPassword("myPassword" + SEPARATOR + "myClientSecret");
190 when(selfRegistrationManagerForSo.getVnfmId(SUBCRIPTION_ID)).thenReturn(VNFM_ID);
191 when(selfRegistrationManagerForVfc.getVnfmId(SUBCRIPTION_ID)).thenReturn(VNFM_ID);
196 public void tearGeneric() {
197 ReflectionTestUtils.setField(SystemFunctions.class, "singletonInstance", null);
200 protected void assertFileInZip(byte[] zip, String path, byte[] expectedContent) throws Exception {
201 assertTrue(Arrays.equals(expectedContent, getFileInZip(new ByteArrayInputStream(zip), path).toByteArray()));
204 protected void assertItenticalZips(byte[] expected, byte[] actual) throws Exception {
205 assertEquals(build(expected), build(actual));
208 byte[] getContent(RequestBody requestBody) {
210 Buffer buffer = new Buffer();
211 requestBody.writeTo(buffer);
212 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
213 buffer.copyTo(byteArrayOutputStream);
214 return byteArrayOutputStream.toByteArray();
215 } catch (Exception e) {
216 throw new RuntimeException(e);
220 private Map<String, List<Byte>> build(byte[] zip) throws Exception {
221 Map<String, List<Byte>> files = new HashMap<>();
222 ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zip));
224 while ((zipEntry = zipInputStream.getNextEntry()) != null) {
225 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
226 ByteStreams.copy(zipInputStream, byteArrayOutputStream);
227 files.put(zipEntry.getName(), Lists.newArrayList(ArrayUtils.toObject(byteArrayOutputStream.toByteArray())));
229 zipInputStream.close();
233 protected void setFieldWithPropertyAnnotation(Object obj, String key, Object value) {
234 for (Field field : obj.getClass().getDeclaredFields()) {
235 for (Value fieldValue : field.getAnnotationsByType(Value.class)) {
236 if (fieldValue.value().equals(key)) {
238 field.setAccessible(true);
239 field.set(obj, value);
241 } catch (IllegalAccessException e) {
242 throw new RuntimeException(e);
247 throw new NoSuchElementException("The " + obj.getClass() + " does not have a filed with " + key + " annotation");
250 public static class VoidObservable {
251 boolean called = false;
252 ObservableFromCallable<Void> s = new ObservableFromCallable(new Callable() {
254 public Object call() throws Exception {
260 public void assertCalled() {
261 TestCase.assertTrue(called);
264 public Observable<Void> value() {