347d6d2b9a4b8bd3ee6e7531b7c713b83da35157
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / vnfm / TestBase.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
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm;
18
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;
29 import java.util.*;
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;
36 import okio.Buffer;
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;
67
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;
73
74
75 public class TestBase {
76
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();
89     @Mock
90     protected CbamRestApiProvider cbamRestApiProvider;
91     @Mock
92     protected CbamRestApiProviderForSo cbamRestApiProviderForSo;
93     @Mock
94     protected CbamRestApiProviderForVfc cbamRestApiProviderForVfc;
95     @Mock
96     protected VfcRestApiProvider vfcRestApiProvider;
97     @Mock
98     protected MsbApiProvider msbApiProvider;
99     @Mock
100     protected AaiSecurityProvider aaiSecurityProvider;
101     @Mock
102     protected VnfmInfoProvider vnfmInfoProvider;
103     @Mock
104     protected VnfsApi vnfApi;
105     @Mock
106     protected OperationExecutionsApi operationExecutionApi;
107     @Mock
108     protected SelfRegistrationManagerForVfc selfRegistrationManagerForVfc;
109     @Mock
110     protected SelfRegistrationManagerForSo selfRegistrationManagerForSo;
111     @Mock
112     protected Logger logger;
113     @Mock
114     protected SubscriptionsApi lcnApi;
115     @Mock
116     protected ServiceResourceApi msbClient;
117     @Mock
118     protected Constants driverProperties;
119     @Mock
120     protected NslcmApi nsLcmApi;
121     @Mock
122     protected INotificationSender notificationSender;
123     @Mock
124     protected SystemFunctions systemFunctions;
125     @Mock
126     protected VnfpackageApi vfcCatalogApi;
127     @Mock
128     protected DefaultApi cbamCatalogApi;
129     @Mock
130     protected CloseableHttpClient httpClient;
131     @Mock
132     protected CloseableHttpResponse response;
133     protected ArgumentCaptor<HttpUriRequest> request = ArgumentCaptor.forClass(HttpUriRequest.class);
134     @Mock
135     protected HttpEntity entity;
136     @Mock
137     protected HttpServletResponse httpResponse;
138     @Mock
139     protected Environment environment;
140
141     protected VnfmInfo vnfmInfo = new VnfmInfo();
142
143     protected static <T> Call<T> buildCall(T response) {
144         Call<T> call = Mockito.mock(Call.class);
145         try {
146             when(call.execute()).thenReturn(Response.success(response));
147         } catch (Exception e) {
148             throw new RuntimeException();
149         }
150         return call;
151     }
152
153     protected static <T> Observable<T> buildObservable(T response) {
154         return Observable.just(response);
155     }
156
157     @Before
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);
169
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);
174
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);
192
193     }
194
195     @After
196     public void tearGeneric() {
197         ReflectionTestUtils.setField(SystemFunctions.class, "singletonInstance", null);
198     }
199
200     protected void assertFileInZip(byte[] zip, String path, byte[] expectedContent) throws Exception {
201         assertTrue(Arrays.equals(expectedContent, getFileInZip(new ByteArrayInputStream(zip), path).toByteArray()));
202     }
203
204     protected void assertItenticalZips(byte[] expected, byte[] actual) throws Exception {
205         assertEquals(build(expected), build(actual));
206     }
207
208     byte[] getContent(RequestBody requestBody) {
209         try {
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);
217         }
218     }
219
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));
223         ZipEntry zipEntry;
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())));
228         }
229         zipInputStream.close();
230         return files;
231     }
232
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)) {
237                     try {
238                         field.setAccessible(true);
239                         field.set(obj, value);
240                         return;
241                     } catch (IllegalAccessException e) {
242                         throw new RuntimeException(e);
243                     }
244                 }
245             }
246         }
247         throw new NoSuchElementException("The " + obj.getClass() + " does not have a filed with " + key + " annotation");
248     }
249
250     public static class VoidObservable {
251         boolean called = false;
252         ObservableFromCallable<Void> s = new ObservableFromCallable(new Callable() {
253             @Override
254             public Object call() throws Exception {
255                 called = true;
256                 return "";
257             }
258         });
259
260         public void assertCalled() {
261             TestCase.assertTrue(called);
262         }
263
264         public Observable<Void> value() {
265             return s;
266         }
267     }
268 }