Fix sonar issues
[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.SelfRegistrationManager;
54 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AaiSecurityProvider;
55 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc.VfcRestApiProvider;
56 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions;
57 import org.onap.vfccatalog.api.VnfpackageApi;
58 import org.onap.vnfmdriver.api.NslcmApi;
59 import org.slf4j.Logger;
60 import org.springframework.beans.factory.annotation.Value;
61 import org.springframework.core.env.Environment;
62 import org.springframework.test.util.ReflectionTestUtils;
63 import retrofit2.Call;
64 import retrofit2.Response;
65
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;
70
71
72 public class TestBase {
73
74     public static final String VNF_ID = "myVnfId";
75     public static final String VNFM_ID = "myVnfmId";
76     public static final String ONAP_CSAR_ID = "myOnapCsarId";
77     public static final String VIM_ID = "myCloudOwnerId_myRegionName";
78     public static final String JOB_ID = "myJobId";
79     public static final String CBAM_VNFD_ID = "cbamVnfdId";
80     protected static Call<Void> VOID_CALL = buildCall(null);
81     protected static VoidObservable VOID_OBSERVABLE = new VoidObservable();
82     @Mock
83     protected CbamRestApiProvider cbamRestApiProvider;
84     @Mock
85     protected VfcRestApiProvider vfcRestApiProvider;
86     @Mock
87     protected MsbApiProvider msbApiProvider;
88     @Mock
89     protected AaiSecurityProvider aaiSecurityProvider;
90     @Mock
91     protected VnfmInfoProvider vnfmInfoProvider;
92     @Mock
93     protected VnfsApi vnfApi;
94     @Mock
95     protected OperationExecutionsApi operationExecutionApi;
96     @Mock
97     protected SelfRegistrationManager selfRegistrationManager;
98     @Mock
99     protected Logger logger;
100     @Mock
101     protected SubscriptionsApi lcnApi;
102     @Mock
103     protected ServiceResourceApi msbClient;
104     @Mock
105     protected DriverProperties driverProperties;
106     @Mock
107     protected NslcmApi nsLcmApi;
108     @Mock
109     protected INotificationSender notificationSender;
110     @Mock
111     protected SystemFunctions systemFunctions;
112     @Mock
113     protected VnfpackageApi vfcCatalogApi;
114     @Mock
115     protected DefaultApi cbamCatalogApi;
116     @Mock
117     protected CloseableHttpClient httpClient;
118     @Mock
119     protected CloseableHttpResponse response;
120     protected ArgumentCaptor<HttpUriRequest> request = ArgumentCaptor.forClass(HttpUriRequest.class);
121     @Mock
122     protected HttpEntity entity;
123     @Mock
124     protected HttpServletResponse httpResponse;
125     @Mock
126     protected Environment environment;
127
128     protected static <T> Call<T> buildCall(T response) {
129         Call<T> call = Mockito.mock(Call.class);
130         try {
131             when(call.execute()).thenReturn(Response.success(response));
132         } catch (Exception e) {
133             throw new RuntimeException();
134         }
135         return call;
136     }
137
138     protected static <T> Observable<T> buildObservable(T response) {
139         return Observable.just(response);
140     }
141
142     @Before
143     public void genericSetup() throws Exception {
144         MockitoAnnotations.initMocks(this);
145         ReflectionTestUtils.setField(SystemFunctions.class, "singletonInstance", systemFunctions);
146         when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID)).thenReturn(vnfApi);
147         when(cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID)).thenReturn(operationExecutionApi);
148         when(cbamRestApiProvider.getCbamLcnApi(VNFM_ID)).thenReturn(lcnApi);
149         when(cbamRestApiProvider.getCbamCatalogApi(VNFM_ID)).thenReturn(cbamCatalogApi);
150         when(msbApiProvider.getMsbApi()).thenReturn(msbClient);
151         when(vfcRestApiProvider.getNsLcmApi()).thenReturn(nsLcmApi);
152         when(vfcRestApiProvider.getVfcCatalogApi()).thenReturn(vfcCatalogApi);
153         when(systemFunctions.getHttpClient()).thenReturn(httpClient);
154         when(httpClient.execute(request.capture())).thenReturn(response);
155         when(response.getEntity()).thenReturn(entity);
156         when(driverProperties.getVnfmId()).thenReturn(VNFM_ID);
157         when(systemFunctions.getHttpClient()).thenReturn(httpClient);
158         when(logger.isInfoEnabled()).thenReturn(true);
159         when(logger.isDebugEnabled()).thenReturn(true);
160         when(logger.isWarnEnabled()).thenReturn(true);
161         when(logger.isErrorEnabled()).thenReturn(true);
162     }
163
164     @After
165     public void tearGeneric() {
166         ReflectionTestUtils.setField(SystemFunctions.class, "singletonInstance", null);
167     }
168
169     protected void assertFileInZip(byte[] zip, String path, byte[] expectedContent) throws Exception {
170         assertTrue(Arrays.equals(expectedContent, getFileInZip(new ByteArrayInputStream(zip), path).toByteArray()));
171     }
172
173     protected void assertItenticalZips(byte[] expected, byte[] actual) throws Exception {
174         assertEquals(build(expected), build(actual));
175     }
176
177     byte[] getContent(RequestBody requestBody) {
178         try {
179             Buffer buffer = new Buffer();
180             requestBody.writeTo(buffer);
181             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
182             buffer.copyTo(byteArrayOutputStream);
183             return byteArrayOutputStream.toByteArray();
184         } catch (Exception e) {
185             throw new RuntimeException(e);
186         }
187     }
188
189     private Map<String, List<Byte>> build(byte[] zip) throws Exception {
190         Map<String, List<Byte>> files = new HashMap<>();
191         ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zip));
192         ZipEntry zipEntry;
193         while ((zipEntry = zipInputStream.getNextEntry()) != null) {
194             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
195             ByteStreams.copy(zipInputStream, byteArrayOutputStream);
196             files.put(zipEntry.getName(), Lists.newArrayList(ArrayUtils.toObject(byteArrayOutputStream.toByteArray())));
197         }
198         zipInputStream.close();
199         return files;
200     }
201
202     protected void setFieldWithPropertyAnnotation(Object obj, String key, Object value) {
203         for (Field field : obj.getClass().getDeclaredFields()) {
204             for (Value fieldValue : field.getAnnotationsByType(Value.class)) {
205                 if (fieldValue.value().equals(key)) {
206                     try {
207                         field.setAccessible(true);
208                         field.set(obj, value);
209                         return;
210                     } catch (IllegalAccessException e) {
211                         throw new RuntimeException(e);
212                     }
213                 }
214             }
215         }
216         throw new NoSuchElementException("The " + obj.getClass() + " does not have a filed with " + key + " annotation");
217     }
218
219     protected static class VoidObservable {
220         boolean called = false;
221         ObservableFromCallable<Void> s = new ObservableFromCallable(new Callable() {
222             @Override
223             public Object call() throws Exception {
224                 called = true;
225                 return "";
226             }
227         });
228
229         public void assertCalled() {
230             TestCase.assertTrue(called);
231         }
232
233         public Observable<Void> value() {
234             return s;
235         }
236     }
237 }