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