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