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