Removing jackson to mitigate cve-2017-4995
[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 java.io.ByteArrayInputStream;
26 import java.io.ByteArrayOutputStream;
27 import java.lang.reflect.Field;
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.zip.ZipEntry;
33 import java.util.zip.ZipInputStream;
34 import javax.servlet.http.HttpServletResponse;
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 public class TestBase {
72     public static final String VNF_ID = "myVnfId";
73     public static final String VNFM_ID = "myVnfmId";
74     public static final String ONAP_CSAR_ID = "myOnapCsarId";
75     public static final String VIM_ID = "myCloudOwnerId_myRegionName";
76     public static final String JOB_ID = "myJobId";
77     public static final String CBAM_VNFD_ID = "cbamVnfdId";
78     protected static Call<Void> VOID_CALL = buildCall(null);
79     @Mock
80     protected CbamRestApiProvider cbamRestApiProvider;
81     @Mock
82     protected VfcRestApiProvider vfcRestApiProvider;
83     @Mock
84     protected MsbApiProvider msbApiProvider;
85     @Mock
86     protected AaiSecurityProvider aaiSecurityProvider;
87     @Mock
88     protected VnfmInfoProvider vnfmInfoProvider;
89     @Mock
90     protected VnfsApi vnfApi;
91     @Mock
92     protected OperationExecutionsApi operationExecutionApi;
93     @Mock
94     protected SelfRegistrationManager selfRegistrationManager;
95     @Mock
96     protected Logger logger;
97     @Mock
98     protected SubscriptionsApi lcnApi;
99     @Mock
100     protected ServiceResourceApi msbClient;
101     @Mock
102     protected DriverProperties driverProperties;
103     @Mock
104     protected NslcmApi nsLcmApi;
105     @Mock
106     protected INotificationSender notificationSender;
107     @Mock
108     protected SystemFunctions systemFunctions;
109     @Mock
110     protected VnfpackageApi vfcCatalogApi;
111     @Mock
112     protected DefaultApi cbamCatalogApi;
113     @Mock
114     protected CloseableHttpClient httpClient;
115     @Mock
116     protected CloseableHttpResponse response;
117     protected ArgumentCaptor<HttpUriRequest> request = ArgumentCaptor.forClass(HttpUriRequest.class);
118     @Mock
119     protected HttpEntity entity;
120     @Mock
121     protected HttpServletResponse httpResponse;
122     @Mock
123     protected Environment environment;
124
125     protected static <T> Call<T> buildCall(T response) {
126         Call<T> call = Mockito.mock(Call.class);
127         try {
128             when(call.execute()).thenReturn(Response.success(response));
129         } catch (Exception e) {
130             throw new RuntimeException();
131         }
132         return call;
133     }
134
135     protected static <T> Observable<T> buildObservable(T response) {
136         return Observable.just(response);
137     }
138
139     @Before
140     public void genericSetup() throws Exception {
141         MockitoAnnotations.initMocks(this);
142         ReflectionTestUtils.setField(SystemFunctions.class, "singletonInstance", systemFunctions);
143         when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID)).thenReturn(vnfApi);
144         when(cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID)).thenReturn(operationExecutionApi);
145         when(cbamRestApiProvider.getCbamLcnApi(VNFM_ID)).thenReturn(lcnApi);
146         when(cbamRestApiProvider.getCbamCatalogApi(VNFM_ID)).thenReturn(cbamCatalogApi);
147         when(msbApiProvider.getMsbApi()).thenReturn(msbClient);
148         when(vfcRestApiProvider.getNsLcmApi()).thenReturn(nsLcmApi);
149         when(vfcRestApiProvider.getVfcCatalogApi()).thenReturn(vfcCatalogApi);
150         when(systemFunctions.getHttpClient()).thenReturn(httpClient);
151         when(httpClient.execute(request.capture())).thenReturn(response);
152         when(response.getEntity()).thenReturn(entity);
153         when(driverProperties.getVnfmId()).thenReturn(VNFM_ID);
154         when(systemFunctions.getHttpClient()).thenReturn(httpClient);
155         when(logger.isInfoEnabled()).thenReturn(true);
156         when(logger.isDebugEnabled()).thenReturn(true);
157         when(logger.isWarnEnabled()).thenReturn(true);
158         when(logger.isErrorEnabled()).thenReturn(true);
159     }
160
161     @After
162     public void tearGeneric() {
163         ReflectionTestUtils.setField(SystemFunctions.class, "singletonInstance", null);
164     }
165
166     protected void assertFileInZip(byte[] zip, String path, byte[] expectedContent) throws Exception {
167         assertTrue(Arrays.equals(expectedContent, getFileInZip(new ByteArrayInputStream(zip), path).toByteArray()));
168     }
169
170     protected void assertItenticalZips(byte[] expected, byte[] actual) throws Exception {
171         assertEquals(build(expected), build(actual));
172     }
173
174     byte[] getContent(RequestBody requestBody) {
175         try {
176             Buffer buffer = new Buffer();
177             requestBody.writeTo(buffer);
178             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
179             buffer.copyTo(byteArrayOutputStream);
180             return byteArrayOutputStream.toByteArray();
181         } catch (Exception e) {
182             throw new RuntimeException(e);
183         }
184     }
185
186     private Map<String, List<Byte>> build(byte[] zip) throws Exception {
187         Map<String, List<Byte>> files = new HashMap<>();
188         ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zip));
189         ZipEntry zipEntry;
190         while ((zipEntry = zipInputStream.getNextEntry()) != null) {
191             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
192             ByteStreams.copy(zipInputStream, byteArrayOutputStream);
193             files.put(zipEntry.getName(), Lists.newArrayList(ArrayUtils.toObject(byteArrayOutputStream.toByteArray())));
194         }
195         zipInputStream.close();
196         return files;
197     }
198
199     protected void setFieldWithPropertyAnnotation(Object obj, String key, Object value) {
200         for (Field field : obj.getClass().getDeclaredFields()) {
201             for (Value fieldValue : field.getAnnotationsByType(Value.class)) {
202                 if (fieldValue.value().equals(key)) {
203                     try {
204                         field.setAccessible(true);
205                         field.set(obj, value);
206                     } catch (IllegalAccessException e) {
207                         throw new RuntimeException(e);
208                     }
209                 }
210             }
211         }
212     }
213 }