Fix template generator for R2
[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.Autowired;
63 import org.springframework.beans.factory.annotation.Value;
64 import org.springframework.core.env.Environment;
65 import org.springframework.stereotype.Component;
66 import org.springframework.test.util.ReflectionTestUtils;
67 import retrofit2.Call;
68 import retrofit2.Response;
69
70 import static junit.framework.TestCase.*;
71 import static org.mockito.Mockito.when;
72 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.SEPARATOR;
73 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip;
74
75
76 public class TestBase {
77
78     public static final String VNF_ID = "myVnfId";
79     public static final String VNFM_ID = "myVnfmId";
80     public static final String ONAP_CSAR_ID = "myOnapCsarId";
81     public static final String VIM_ID = "myCloudOwnerId_myRegionName";
82     public static final String JOB_ID = "myJobId";
83     public static final String CBAM_VNFD_ID = "cbamVnfdId";
84     public static final String HTTP_AUTH_URL = "http://authurl/";
85     public static final String HTTP_LCM_URL = "http://lcmurl/";
86     public static final String HTTP_LCN_URL = "http://lcnurl/";
87     public static final String HTTP_CATLOG_URL = "http://catlogurl/";
88     public static final String SUBCRIPTION_ID = "subcriptionId";
89     protected static VoidObservable VOID_OBSERVABLE = new VoidObservable();
90     @Mock
91     protected CbamRestApiProvider cbamRestApiProvider;
92     @Mock
93     protected CbamRestApiProviderForSo cbamRestApiProviderForSo;
94     @Mock
95     protected CbamRestApiProviderForVfc cbamRestApiProviderForVfc;
96     @Mock
97     protected VfcRestApiProvider vfcRestApiProvider;
98     @Mock
99     protected MsbApiProvider msbApiProvider;
100     @Mock
101     protected AaiSecurityProvider aaiSecurityProvider;
102     @Mock
103     protected VnfmInfoProvider vnfmInfoProvider;
104     @Mock
105     protected VnfsApi vnfApi;
106     @Mock
107     protected OperationExecutionsApi operationExecutionApi;
108     @Mock
109     protected SelfRegistrationManagerForVfc selfRegistrationManagerForVfc;
110     @Mock
111     protected SelfRegistrationManagerForSo selfRegistrationManagerForSo;
112     @Mock
113     protected Logger logger;
114     @Mock
115     protected SubscriptionsApi lcnApi;
116     @Mock
117     protected ServiceResourceApi msbClient;
118     @Mock
119     protected Constants driverProperties;
120     @Mock
121     protected NslcmApi nsLcmApi;
122     @Mock
123     protected INotificationSender notificationSender;
124     @Mock
125     protected SystemFunctions systemFunctions;
126     @Mock
127     protected VnfpackageApi vfcCatalogApi;
128     @Mock
129     protected DefaultApi cbamCatalogApi;
130     @Mock
131     protected CloseableHttpClient httpClient;
132     @Mock
133     protected CloseableHttpResponse response;
134     protected ArgumentCaptor<HttpUriRequest> request = ArgumentCaptor.forClass(HttpUriRequest.class);
135     @Mock
136     protected HttpEntity entity;
137     @Mock
138     protected HttpServletResponse httpResponse;
139     @Mock
140     protected Environment environment;
141
142     protected VnfmInfo vnfmInfo = new VnfmInfo();
143
144     protected static <T> Call<T> buildCall(T response) {
145         Call<T> call = Mockito.mock(Call.class);
146         try {
147             when(call.execute()).thenReturn(Response.success(response));
148         } catch (Exception e) {
149             throw new RuntimeException();
150         }
151         return call;
152     }
153
154     protected static <T> Observable<T> buildObservable(T response) {
155         return Observable.just(response);
156     }
157
158     @Before
159     public void genericSetup() throws Exception {
160         MockitoAnnotations.initMocks(this);
161         ReflectionTestUtils.setField(SystemFunctions.class, "singletonInstance", systemFunctions);
162         when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID)).thenReturn(vnfApi);
163         when(cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID)).thenReturn(operationExecutionApi);
164         when(cbamRestApiProvider.getCbamLcnApi(VNFM_ID)).thenReturn(lcnApi);
165         when(cbamRestApiProvider.getCbamCatalogApi(VNFM_ID)).thenReturn(cbamCatalogApi);
166         when(cbamRestApiProviderForSo.getCbamLcmApi(VNFM_ID)).thenReturn(vnfApi);
167         when(cbamRestApiProviderForSo.getCbamOperationExecutionApi(VNFM_ID)).thenReturn(operationExecutionApi);
168         when(cbamRestApiProviderForSo.getCbamLcnApi(VNFM_ID)).thenReturn(lcnApi);
169         when(cbamRestApiProviderForSo.getCbamCatalogApi(VNFM_ID)).thenReturn(cbamCatalogApi);
170
171         when(cbamRestApiProviderForVfc.getCbamLcmApi(VNFM_ID)).thenReturn(vnfApi);
172         when(cbamRestApiProviderForVfc.getCbamOperationExecutionApi(VNFM_ID)).thenReturn(operationExecutionApi);
173         when(cbamRestApiProviderForVfc.getCbamLcnApi(VNFM_ID)).thenReturn(lcnApi);
174         when(cbamRestApiProviderForVfc.getCbamCatalogApi(VNFM_ID)).thenReturn(cbamCatalogApi);
175
176         when(msbApiProvider.getMsbApi()).thenReturn(msbClient);
177         when(vfcRestApiProvider.getNsLcmApi()).thenReturn(nsLcmApi);
178         when(vfcRestApiProvider.getVfcCatalogApi()).thenReturn(vfcCatalogApi);
179         when(systemFunctions.getHttpClient()).thenReturn(httpClient);
180         when(httpClient.execute(request.capture())).thenReturn(response);
181         when(response.getEntity()).thenReturn(entity);
182         when(systemFunctions.getHttpClient()).thenReturn(httpClient);
183         when(logger.isInfoEnabled()).thenReturn(true);
184         when(logger.isDebugEnabled()).thenReturn(true);
185         when(logger.isWarnEnabled()).thenReturn(true);
186         when(logger.isErrorEnabled()).thenReturn(true);
187         when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(vnfmInfo);
188         vnfmInfo.setUrl(HTTP_AUTH_URL + SEPARATOR + HTTP_LCM_URL + SEPARATOR + HTTP_LCN_URL + SEPARATOR + HTTP_CATLOG_URL);
189         vnfmInfo.setUserName("myUsername" + SEPARATOR + "myClientId");
190         vnfmInfo.setPassword("myPassword" + SEPARATOR + "myClientSecret");
191         when(selfRegistrationManagerForSo.getVnfmId(SUBCRIPTION_ID)).thenReturn(VNFM_ID);
192         when(selfRegistrationManagerForVfc.getVnfmId(SUBCRIPTION_ID)).thenReturn(VNFM_ID);
193
194     }
195
196     @After
197     public void tearGeneric() {
198         ReflectionTestUtils.setField(SystemFunctions.class, "singletonInstance", null);
199     }
200
201     protected void assertFileInZip(byte[] zip, String path, byte[] expectedContent) throws Exception {
202         assertTrue(Arrays.equals(expectedContent, getFileInZip(new ByteArrayInputStream(zip), path).toByteArray()));
203     }
204
205     protected void assertItenticalZips(byte[] expected, byte[] actual) throws Exception {
206         assertEquals(build(expected), build(actual));
207     }
208
209     byte[] getContent(RequestBody requestBody) {
210         try {
211             Buffer buffer = new Buffer();
212             requestBody.writeTo(buffer);
213             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
214             buffer.copyTo(byteArrayOutputStream);
215             return byteArrayOutputStream.toByteArray();
216         } catch (Exception e) {
217             throw new RuntimeException(e);
218         }
219     }
220
221     private Map<String, List<Byte>> build(byte[] zip) throws Exception {
222         Map<String, List<Byte>> files = new HashMap<>();
223         ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zip));
224         ZipEntry zipEntry;
225         while ((zipEntry = zipInputStream.getNextEntry()) != null) {
226             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
227             ByteStreams.copy(zipInputStream, byteArrayOutputStream);
228             files.put(zipEntry.getName(), Lists.newArrayList(ArrayUtils.toObject(byteArrayOutputStream.toByteArray())));
229         }
230         zipInputStream.close();
231         return files;
232     }
233
234     protected void setFieldWithPropertyAnnotation(Object obj, String key, Object value) {
235         for (Field field : obj.getClass().getDeclaredFields()) {
236             for (Value fieldValue : field.getAnnotationsByType(Value.class)) {
237                 if (fieldValue.value().equals(key)) {
238                     try {
239                         field.setAccessible(true);
240                         field.set(obj, value);
241                         return;
242                     } catch (IllegalAccessException e) {
243                         throw new RuntimeException(e);
244                     }
245                 }
246             }
247         }
248         throw new NoSuchElementException("The " + obj.getClass() + " does not have a filed with " + key + " annotation");
249     }
250
251     protected void assertBean(Class<?> clazz) {
252         assertEquals(1, clazz.getDeclaredConstructors().length);
253         Autowired annotation = clazz.getDeclaredConstructors()[0].getAnnotation(Autowired.class);
254         assertNotNull(annotation);
255         assertNotNull(clazz.getAnnotation(Component.class));
256     }
257
258     public static class VoidObservable {
259         boolean called = false;
260         ObservableFromCallable<Void> s = new ObservableFromCallable(new Callable() {
261             @Override
262             public Object call() throws Exception {
263                 called = true;
264                 return "";
265             }
266         });
267
268         public void assertCalled() {
269             TestCase.assertTrue(called);
270         }
271
272         public Observable<Void> value() {
273             return s;
274         }
275     }
276 }