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