cb5412785f13feff3e677f592e7f3a3e24d143f3
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / vnfm / TestCbamRestApiProvider.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.nokia.cbam.catalog.v1.api.DefaultApi;
20 import com.nokia.cbam.lcm.v32.ApiClient;
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 okhttp3.Interceptor;
25 import okhttp3.OakExtractor;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.invocation.InvocationOnMock;
31 import org.mockito.stubbing.Answer;
32 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
33 import org.onap.msb.sdk.discovery.entity.NodeInfo;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.GenericExternalSystemInfoProvider;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.IpMappingProvider;
36 import org.onap.vnfmdriver.model.VnfmInfo;
37 import org.springframework.core.env.Environment;
38
39 import javax.net.ssl.HostnameVerifier;
40 import javax.net.ssl.SSLSocketFactory;
41 import java.util.HashSet;
42 import java.util.Map;
43 import java.util.Set;
44
45 import static junit.framework.TestCase.assertEquals;
46 import static junit.framework.TestCase.assertNotNull;
47 import static org.mockito.Mockito.*;
48 import static org.springframework.test.util.ReflectionTestUtils.setField;
49
50 class ResultCaptor<T> implements Answer {
51     private T result = null;
52
53     public T getResult() {
54         return result;
55     }
56
57     @Override
58     public T answer(InvocationOnMock invocationOnMock) throws Throwable {
59         result = (T) invocationOnMock.callRealMethod();
60         return result;
61     }
62 }
63
64 public class TestCbamRestApiProvider extends TestBase {
65     @Mock
66     private Environment environment;
67     @Mock
68     private CbamTokenProvider cbamTokenProvider;
69     @Mock
70     private MicroServiceFullInfo microServiceInfo = new MicroServiceFullInfo();
71     @Mock
72     private Interceptor interceptor;
73     @Mock
74     private HostnameVerifier hostnameVerifier;
75     private Set<NodeInfo> nodes = new HashSet<>();
76
77     private CbamRestApiProvider cbamRestApiProvider;
78     private CbamSecurityProvider cbamSecurityProvider = spy(new CbamSecurityProvider());
79
80     @Before
81     public void init() {
82         microServiceInfo.setNodes(nodes);
83         setField(cbamSecurityProvider, "skipCertificateVerification", true);
84         setField(cbamSecurityProvider, "skipHostnameVerification", true);
85         cbamRestApiProvider = new CbamRestApiProvider(driverProperties, cbamTokenProvider, vnfmInfoProvider, cbamSecurityProvider);
86         when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("");
87         when(environment.getProperty(GenericExternalSystemInfoProvider.VNFM_INFO_CACHE_EVICTION_IN_MS, Long.class, Long.valueOf(10 * 60 * 1000))).thenReturn(10 * 60 * 1000L);
88     }
89
90     /**
91      * test CBAM LCM API
92      */
93     @Test
94     public void testCbamLcmApi() throws Exception {
95         VnfmInfo expectedVnfmInfo = new VnfmInfo();
96         when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
97         expectedVnfmInfo.setUrl("https://cbamurl:123/d/");
98         ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
99         doAnswer(sslSocketFactoryResultCaptor).when(cbamSecurityProvider).buildSSLSocketFactory();
100         when(cbamSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
101         when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn(interceptor);
102         //when
103         ApiClient cbamLcmApi = cbamRestApiProvider.buildLcmApiClient(VNFM_ID);
104         //verify
105         String actual = cbamLcmApi.getAdapterBuilder().build().baseUrl().toString();
106         assertEquals("https://cbamurl:123/d/", actual);
107         assertEquals(sslSocketFactoryResultCaptor.getResult(), cbamLcmApi.getOkBuilder().build().sslSocketFactory());
108         Map<String, Interceptor> apiAuthorizations = cbamLcmApi.getApiAuthorizations();
109         assertEquals(1, apiAuthorizations.size());
110         assertEquals(interceptor, apiAuthorizations.values().iterator().next());
111         assertEquals(hostnameVerifier, OakExtractor.extract(cbamLcmApi.getOkBuilder()));
112     }
113
114     /**
115      * test CBAM catalog API
116      */
117     @Test
118     public void testCbamCatalogApi() throws Exception {
119         ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
120         doAnswer(sslSocketFactoryResultCaptor).when(cbamSecurityProvider).buildSSLSocketFactory();
121         when(cbamSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
122         when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn(interceptor);
123         when(driverProperties.getCbamCatalogUrl()).thenReturn("https://cbamurl:123/d/");
124         //when
125         com.nokia.cbam.catalog.v1.ApiClient cbamLcmApi = cbamRestApiProvider.buildCatalogApiClient(VNFM_ID);
126         //verify
127         String actual = cbamLcmApi.getAdapterBuilder().build().baseUrl().toString();
128         assertEquals("https://cbamurl:123/d/", actual);
129         assertEquals(sslSocketFactoryResultCaptor.getResult(), cbamLcmApi.getOkBuilder().build().sslSocketFactory());
130         Map<String, Interceptor> apiAuthorizations = cbamLcmApi.getApiAuthorizations();
131         assertEquals(1, apiAuthorizations.size());
132         assertEquals(interceptor, apiAuthorizations.values().iterator().next());
133         assertEquals(hostnameVerifier, OakExtractor.extract(cbamLcmApi.getOkBuilder()));
134     }
135
136     /**
137      * test CBAM LCN API
138      */
139     @Test
140     public void testCbamLcnApi() throws Exception {
141         ResultCaptor<SSLSocketFactory> sslSocketFactoryResultCaptor = new ResultCaptor<>();
142         doAnswer(sslSocketFactoryResultCaptor).when(cbamSecurityProvider).buildSSLSocketFactory();
143         when(cbamSecurityProvider.buildHostnameVerifier()).thenReturn(hostnameVerifier);
144         when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn(interceptor);
145         when(driverProperties.getCbamLcnUrl()).thenReturn("https://cbamurl:123/d/");
146         //when
147         com.nokia.cbam.lcn.v32.ApiClient cbamLcmApi = cbamRestApiProvider.buildLcnApiClient(VNFM_ID);
148         //verify
149         String actual = cbamLcmApi.getAdapterBuilder().build().baseUrl().toString();
150         assertEquals("https://cbamurl:123/d/", actual);
151         assertEquals(sslSocketFactoryResultCaptor.getResult(), cbamLcmApi.getOkBuilder().build().sslSocketFactory());
152         Map<String, Interceptor> apiAuthorizations = cbamLcmApi.getApiAuthorizations();
153         assertEquals(1, apiAuthorizations.size());
154         assertEquals(interceptor, apiAuthorizations.values().iterator().next());
155         assertEquals(hostnameVerifier, OakExtractor.extract(cbamLcmApi.getOkBuilder()));
156     }
157
158     /**
159      * Test API wrapping for Catalog
160      * (questionable benefit [ this is more less ensured by Java type safety) ]
161      */
162     @Test
163     public void testCatalogAPiWrapping() {
164         com.nokia.cbam.catalog.v1.ApiClient c = Mockito.mock(com.nokia.cbam.catalog.v1.ApiClient.class);
165         class TestClasss extends CbamRestApiProvider {
166             public TestClasss() {
167                 super(driverProperties, cbamTokenProvider, vnfmInfoProvider, cbamSecurityProvider);
168             }
169
170             @Override
171             com.nokia.cbam.catalog.v1.ApiClient buildCatalogApiClient(String vnfmId) {
172                 return c;
173             }
174         }
175         DefaultApi defaultApi = Mockito.mock(DefaultApi.class);
176         when(c.createService(DefaultApi.class)).thenReturn(defaultApi);
177         //verify
178         TestClasss testInstnace = new TestClasss();
179         assertNotNull(testInstnace.getCbamCatalogApi(VNFM_ID));
180         assertEquals(defaultApi, testInstnace.getCbamCatalogApi(VNFM_ID));
181     }
182
183     /**
184      * Test API wrapping for LCN
185      * (questionable benefit [ this is more less ensured by Java type safety) ]
186      */
187     @Test
188     public void testLcmAPiWrapping() {
189         com.nokia.cbam.lcn.v32.ApiClient c = Mockito.mock(com.nokia.cbam.lcn.v32.ApiClient.class);
190         class TestClasss extends CbamRestApiProvider {
191             public TestClasss() {
192                 super(driverProperties, cbamTokenProvider, vnfmInfoProvider, cbamSecurityProvider);
193             }
194
195             @Override
196             com.nokia.cbam.lcn.v32.ApiClient buildLcnApiClient(String vnfmId) {
197                 return c;
198             }
199         }
200         SubscriptionsApi defaultApi = Mockito.mock(SubscriptionsApi.class);
201         when(c.createService(SubscriptionsApi.class)).thenReturn(defaultApi);
202         //verify
203         TestClasss testInstnace = new TestClasss();
204         assertNotNull(testInstnace.getCbamLcnApi(VNFM_ID));
205         assertEquals(defaultApi, testInstnace.getCbamLcnApi(VNFM_ID));
206     }
207
208     /**
209      * Test API wrapping for LCM
210      * (questionable benefit [ this is more less ensured by Java type safety) ]
211      */
212     @Test
213     public void testLcnAPiWrapping() {
214         com.nokia.cbam.lcm.v32.ApiClient c = Mockito.mock(com.nokia.cbam.lcm.v32.ApiClient.class);
215         class TestClasss extends CbamRestApiProvider {
216             public TestClasss() {
217                 super(driverProperties, cbamTokenProvider, vnfmInfoProvider, cbamSecurityProvider);
218             }
219
220             @Override
221             ApiClient buildLcmApiClient(String vnfmId) {
222                 return c;
223             }
224         }
225         VnfsApi defaultApi = Mockito.mock(VnfsApi.class);
226         when(c.createService(VnfsApi.class)).thenReturn(defaultApi);
227         //verify
228         TestClasss testInstnace = new TestClasss();
229         assertNotNull(testInstnace.getCbamLcmApi(VNFM_ID));
230         assertEquals(defaultApi, testInstnace.getCbamLcmApi(VNFM_ID));
231     }
232
233     /**
234      * Test API wrapping for LCM
235      * (questionable benefit [ this is more less ensured by Java type safety) ]
236      */
237     @Test
238     public void testOperationExecutionsApiAPiWrapping() {
239         com.nokia.cbam.lcm.v32.ApiClient c = Mockito.mock(com.nokia.cbam.lcm.v32.ApiClient.class);
240         class TestClasss extends CbamRestApiProvider {
241             public TestClasss() {
242                 super(driverProperties, cbamTokenProvider, vnfmInfoProvider, cbamSecurityProvider);
243             }
244
245             @Override
246             ApiClient buildLcmApiClient(String vnfmId) {
247                 return c;
248             }
249         }
250         OperationExecutionsApi defaultApi = Mockito.mock(OperationExecutionsApi.class);
251         when(c.createService(OperationExecutionsApi.class)).thenReturn(defaultApi);
252         //verify
253         TestClasss testInstnace = new TestClasss();
254         assertNotNull(testInstnace.getCbamOperationExecutionApi(VNFM_ID));
255         assertEquals(defaultApi, testInstnace.getCbamOperationExecutionApi(VNFM_ID));
256     }
257 }