add direct information source
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / core / 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.onap.core;
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 org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mock;
27 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
28 import org.onap.msb.sdk.discovery.entity.NodeInfo;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamTokenProvider;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
33 import org.onap.vnfmdriver.model.VnfmInfo;
34 import org.springframework.core.env.Environment;
35
36 import java.util.Base64;
37 import java.util.HashSet;
38 import java.util.Set;
39
40 import static junit.framework.TestCase.*;
41 import static org.mockito.Mockito.spy;
42 import static org.mockito.Mockito.when;
43 import static org.springframework.test.util.ReflectionTestUtils.setField;
44
45 public class TestCbamRestApiProvider extends TestBase {
46     @Mock
47     private Environment environment;
48     @Mock
49     private CbamTokenProvider cbamTokenProvider;
50     private MicroServiceFullInfo microServiceInfo = new MicroServiceFullInfo();
51     private Set<NodeInfo> nodes = new HashSet<>();
52
53     private CbamRestApiProvider cbamRestApiProvider;
54
55     @Before
56     public void init() {
57         microServiceInfo.setNodes(nodes);
58         CbamRestApiProvider real = new CbamRestApiProvider(driverProperties, cbamTokenProvider, vnfmInfoProvider);
59         setField(real, "trustedCertificates", "mytrustedCertificates");
60         setField(real, "skipCertificateVerification", true);
61         cbamRestApiProvider = spy(real);
62         when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("");
63         when(environment.getProperty(GenericExternalSystemInfoProvider.VNFM_INFO_CACHE_EVICTION_IN_MS, Long.class, Long.valueOf(10 * 60 * 1000))).thenReturn(10 * 60 * 1000L);
64     }
65
66     /**
67      * test CBAM LCM API retrieval without SSL verification
68      */
69     @Test
70     public void testCbamLcmApi() throws Exception {
71         VnfmInfo expectedVnfmInfo = new VnfmInfo();
72         when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
73         expectedVnfmInfo.setUrl("https://cbamUrl:123/d");
74         when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
75         //when
76         VnfsApi cbamLcmApi = cbamRestApiProvider.getCbamLcmApi(VNFM_ID);
77         //verify
78         ApiClient apiClient = cbamLcmApi.getApiClient();
79         assertEquals("https://cbamUrl:123/d", apiClient.getBasePath());
80         assertNull(apiClient.getSslCaCert());
81         assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
82         assertEquals(2, cbamLcmApi.getApiClient().getAuthentications().size());
83         assertTrue(!cbamLcmApi.getApiClient().isVerifyingSsl());
84     }
85
86     /**
87      * test CBAM LCM API retrieval with SSL verification
88      */
89     @Test
90     public void testCbamLcmApiWithSslVerfy() throws Exception {
91         VnfmInfo expectedVnfmInfo = new VnfmInfo();
92         when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
93         expectedVnfmInfo.setUrl("https://cbamUrl:123/d");
94         when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
95         setField(cbamRestApiProvider, "skipCertificateVerification", false);
96         setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem")));
97         //when
98         VnfsApi cbamLcmApi = cbamRestApiProvider.getCbamLcmApi(VNFM_ID);
99         //verify
100         ApiClient apiClient = cbamLcmApi.getApiClient();
101         assertEquals("https://cbamUrl:123/d", apiClient.getBasePath());
102         assertNotNull(apiClient.getSslCaCert());
103         assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
104         assertEquals(2, cbamLcmApi.getApiClient().getAuthentications().size());
105         assertTrue(cbamLcmApi.getApiClient().isVerifyingSsl());
106     }
107
108     /**
109      * test CBAM Catalog API retrieval without SSL verification
110      */
111     @Test
112     public void testCbamCatalogApi() throws Exception {
113         VnfmInfo expectedVnfmInfo = new VnfmInfo();
114         when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
115         when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
116         when(driverProperties.getCbamCatalogUrl()).thenReturn("https://1.2.3.4/path");
117         //when
118         DefaultApi cbamCatalogApi = cbamRestApiProvider.getCbamCatalogApi(VNFM_ID);
119         //verify
120         com.nokia.cbam.catalog.v1.ApiClient apiClient = cbamCatalogApi.getApiClient();
121         assertEquals("https://1.2.3.4/path", apiClient.getBasePath());
122         assertNull(apiClient.getSslCaCert());
123         assertEquals("myToken", ((com.nokia.cbam.catalog.v1.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
124         assertEquals(2, cbamCatalogApi.getApiClient().getAuthentications().size());
125         assertTrue(!cbamCatalogApi.getApiClient().isVerifyingSsl());
126     }
127
128     /**
129      * test CBAM Catalog API retrieval with SSL verification
130      */
131     @Test
132     public void testCbamCatalogApiWithSslVerfy() throws Exception {
133         VnfmInfo expectedVnfmInfo = new VnfmInfo();
134         when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
135         when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
136         when(driverProperties.getCbamCatalogUrl()).thenReturn("https://1.2.3.4/path");
137         setField(cbamRestApiProvider, "skipCertificateVerification", false);
138         setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem")));
139         //when
140         DefaultApi cbamLcmApi = cbamRestApiProvider.getCbamCatalogApi(VNFM_ID);
141         //verify
142         com.nokia.cbam.catalog.v1.ApiClient apiClient = cbamLcmApi.getApiClient();
143         assertEquals("https://1.2.3.4/path", apiClient.getBasePath());
144         assertNotNull(apiClient.getSslCaCert());
145         assertEquals("myToken", ((com.nokia.cbam.catalog.v1.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
146         assertEquals(2, cbamLcmApi.getApiClient().getAuthentications().size());
147         assertTrue(cbamLcmApi.getApiClient().isVerifyingSsl());
148     }
149
150     /**
151      * test CBAM Lcn API retrieval without SSL verification
152      */
153     @Test
154     public void testCbamLcnApi() throws Exception {
155         VnfmInfo expectedVnfmInfo = new VnfmInfo();
156         when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
157         when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
158         when(driverProperties.getCbamLcnUrl()).thenReturn("https://1.2.3.4/path");
159         //when
160         SubscriptionsApi cbamLcnApi = cbamRestApiProvider.getCbamLcnApi(VNFM_ID);
161         //verify
162         com.nokia.cbam.lcn.v32.ApiClient apiClient = cbamLcnApi.getApiClient();
163         assertEquals("https://1.2.3.4/path", apiClient.getBasePath());
164         assertNull(apiClient.getSslCaCert());
165         assertEquals("myToken", ((com.nokia.cbam.lcn.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
166         assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size());
167         assertTrue(!cbamLcnApi.getApiClient().isVerifyingSsl());
168     }
169
170     /**
171      * test CBAM Lcn API retrieval with SSL verification
172      */
173     @Test
174     public void testCbamLcnApiWithSslVerfy() throws Exception {
175         VnfmInfo expectedVnfmInfo = new VnfmInfo();
176         when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
177         when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
178         when(driverProperties.getCbamLcnUrl()).thenReturn("https://1.2.3.4/path");
179         setField(cbamRestApiProvider, "skipCertificateVerification", false);
180         setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem")));
181         //when
182         SubscriptionsApi cbamLcnApi = cbamRestApiProvider.getCbamLcnApi(VNFM_ID);
183         //verify
184         com.nokia.cbam.lcn.v32.ApiClient apiClient = cbamLcnApi.getApiClient();
185         assertEquals("https://1.2.3.4/path", apiClient.getBasePath());
186         assertNotNull(apiClient.getSslCaCert());
187         assertEquals("myToken", ((com.nokia.cbam.lcn.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
188         assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size());
189         assertTrue(cbamLcnApi.getApiClient().isVerifyingSsl());
190     }
191
192     /**
193      * test CBAM operation exeution API retrieval without SSL verification
194      */
195     @Test
196     public void testCbamOpexApi() throws Exception {
197         VnfmInfo expectedVnfmInfo = new VnfmInfo();
198         when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
199         when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
200         when(nsLcmApi.queryVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
201         expectedVnfmInfo.setUrl("https://cbamUrl:123/d");
202         //when
203         OperationExecutionsApi cbamLcnApi = cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID);
204         //verify
205         ApiClient apiClient = cbamLcnApi.getApiClient();
206         assertEquals("https://cbamUrl:123/d", apiClient.getBasePath());
207         assertNull(apiClient.getSslCaCert());
208         assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
209         assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size());
210         assertTrue(!cbamLcnApi.getApiClient().isVerifyingSsl());
211     }
212
213     /**
214      * test CBAM operation execution API retrieval with SSL verification
215      */
216     @Test
217     public void testCbamOpexApiWithSslVerfy() throws Exception {
218         when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken");
219         setField(cbamRestApiProvider, "skipCertificateVerification", false);
220         setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem")));
221         VnfmInfo expectedVnfmInfo = new VnfmInfo();
222         when(nsLcmApi.queryVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
223         expectedVnfmInfo.setUrl("https://cbamUrl:123/d");
224         when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo);
225         //when
226         OperationExecutionsApi cbamLcnApi = cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID);
227         //verify
228         ApiClient apiClient = cbamLcnApi.getApiClient();
229         assertEquals("https://cbamUrl:123/d", apiClient.getBasePath());
230         assertNotNull(apiClient.getSslCaCert());
231         assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken());
232         assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size());
233         assertTrue(cbamLcnApi.getApiClient().isVerifyingSsl());
234     }
235 }