Merge "Updating Nokia driver"
[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 org.apache.commons.lang3.ArrayUtils;
25 import org.apache.http.HttpEntity;
26 import org.apache.http.client.methods.CloseableHttpResponse;
27 import org.apache.http.client.methods.HttpUriRequest;
28 import org.apache.http.impl.client.CloseableHttpClient;
29 import org.assertj.core.util.Lists;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.INotificationSender;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VnfmInfoProvider;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.MsbApiProvider;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc.VfcRestApiProvider;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions;
42 import org.onap.vfccatalog.api.VnfpackageApi;
43 import org.onap.vnfmdriver.api.NslcmApi;
44 import org.slf4j.Logger;
45 import org.springframework.beans.factory.annotation.Value;
46 import org.springframework.core.env.Environment;
47 import org.springframework.test.util.ReflectionTestUtils;
48
49 import javax.servlet.http.HttpServletResponse;
50 import java.io.ByteArrayInputStream;
51 import java.io.ByteArrayOutputStream;
52 import java.lang.reflect.Field;
53 import java.util.Arrays;
54 import java.util.HashMap;
55 import java.util.List;
56 import java.util.Map;
57 import java.util.zip.ZipEntry;
58 import java.util.zip.ZipInputStream;
59
60 import static junit.framework.TestCase.assertEquals;
61 import static junit.framework.TestCase.assertTrue;
62 import static org.mockito.Mockito.when;
63 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip;
64
65 public class TestBase {
66     public static final String VNF_ID = "myVnfId";
67     public static final String VNFM_ID = "myVnfmId";
68     public static final String ONAP_CSAR_ID = "myOnapCsarId";
69     public static final String VIM_ID = "myVimId";
70     public static final String JOB_ID = "myJobId";
71     public static final String CBAM_VNFD_ID = "cbamVnfdId";
72     @Mock
73     protected CbamRestApiProvider cbamRestApiProvider;
74     @Mock
75     protected VfcRestApiProvider vfcRestApiProvider;
76     @Mock
77     protected MsbApiProvider msbApiProvider;
78     @Mock
79     protected VnfmInfoProvider vnfmInfoProvider;
80     @Mock
81     protected VnfsApi vnfApi;
82     @Mock
83     protected OperationExecutionsApi operationExecutionApi;
84     @Mock
85     protected SelfRegistrationManager selfRegistrationManager;
86     @Mock
87     protected Logger logger;
88     @Mock
89     protected SubscriptionsApi lcnApi;
90     @Mock
91     protected MSBServiceClient msbClient;
92     @Mock
93     protected DriverProperties driverProperties;
94     @Mock
95     protected NslcmApi nsLcmApi;
96     @Mock
97     protected INotificationSender notificationSender;
98     @Mock
99     protected SystemFunctions systemFunctions;
100     @Mock
101     protected VnfpackageApi vfcCatalogApi;
102     @Mock
103     protected DefaultApi cbamCatalogApi;
104     @Mock
105     protected CloseableHttpClient httpClient;
106     @Mock
107     protected CloseableHttpResponse response;
108     protected ArgumentCaptor<HttpUriRequest> request = ArgumentCaptor.forClass(HttpUriRequest.class);
109     @Mock
110     protected HttpEntity entity;
111     @Mock
112     protected HttpServletResponse httpResponse;
113     @Mock
114     protected Environment environment;
115
116     @Before
117     public void genericSetup() throws Exception {
118         MockitoAnnotations.initMocks(this);
119         ReflectionTestUtils.setField(SystemFunctions.class, "INSTANCE", systemFunctions);
120         when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID)).thenReturn(vnfApi);
121         when(cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID)).thenReturn(operationExecutionApi);
122         when(cbamRestApiProvider.getCbamLcnApi(VNFM_ID)).thenReturn(lcnApi);
123         when(cbamRestApiProvider.getCbamCatalogApi(VNFM_ID)).thenReturn(cbamCatalogApi);
124         when(msbApiProvider.getMsbClient()).thenReturn(msbClient);
125         when(vfcRestApiProvider.getNsLcmApi()).thenReturn(nsLcmApi);
126         when(vfcRestApiProvider.getOnapCatalogApi()).thenReturn(vfcCatalogApi);
127         when(systemFunctions.getHttpClient()).thenReturn(httpClient);
128         when(httpClient.execute(request.capture())).thenReturn(response);
129         when(response.getEntity()).thenReturn(entity);
130         when(driverProperties.getVnfmId()).thenReturn(VNFM_ID);
131         when(systemFunctions.getHttpClient()).thenReturn(httpClient);
132     }
133
134     @After
135     public void tearGeneric() {
136         ReflectionTestUtils.setField(SystemFunctions.class, "INSTANCE", null);
137     }
138
139     protected void assertFileInZip(byte[] zip, String path, byte[] expectedContent) throws Exception {
140         assertTrue(Arrays.equals(expectedContent, getFileInZip(new ByteArrayInputStream(zip), path).toByteArray()));
141     }
142
143     protected void assertItenticalZips(byte[] expected, byte[] actual) throws Exception {
144         assertEquals(build(expected), build(actual));
145     }
146
147     private Map<String, List<Byte>> build(byte[] zip) throws Exception {
148         Map<String, List<Byte>> files = new HashMap<>();
149         ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zip));
150         ZipEntry zipEntry;
151         while ((zipEntry = zipInputStream.getNextEntry()) != null) {
152             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
153             ByteStreams.copy(zipInputStream, byteArrayOutputStream);
154             files.put(zipEntry.getName(), Lists.newArrayList(ArrayUtils.toObject(byteArrayOutputStream.toByteArray())));
155         }
156         zipInputStream.close();
157         return files;
158     }
159
160     protected void setFieldWithPropertyAnnotation(Object obj, String key, String value) {
161         for (Field field : obj.getClass().getDeclaredFields()) {
162             for (Value fieldValue : field.getAnnotationsByType(Value.class)) {
163                 if (fieldValue.value().equals(key)) {
164                     try {
165                         field.setAccessible(true);
166                         field.set(obj, value);
167                     } catch (IllegalAccessException e) {
168                         throw new RuntimeException(e);
169                     }
170                 }
171             }
172         }
173     }
174 }