Removing jackson to mitigate cve-2017-4995
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / TestAAIExternalSystemInfoProvider.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 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct;
17
18 import java.util.ArrayList;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.onap.aai.api.CloudInfrastructureApi;
23 import org.onap.aai.api.ExternalSystemApi;
24 import org.onap.aai.model.CloudRegion;
25 import org.onap.aai.model.EsrSystemInfo;
26 import org.onap.aai.model.EsrVnfm;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
28 import org.onap.vnfmdriver.model.VimInfo;
29 import org.onap.vnfmdriver.model.VnfmInfo;
30
31 import static junit.framework.TestCase.assertEquals;
32 import static junit.framework.TestCase.fail;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
35 import static org.springframework.test.util.ReflectionTestUtils.setField;
36
37 public class TestAAIExternalSystemInfoProvider extends TestBase {
38     private AAIExternalSystemInfoProvider aaiExternalSystemInfoProvider;
39     @Mock
40     private AAIRestApiProvider aaiRestApiProvider;
41     @Mock
42     private ExternalSystemApi externalSystemApi;
43     @Mock
44     private CloudInfrastructureApi cloudInfrastructureApi;
45
46     @Before
47     public void init() {
48         setField(AAIExternalSystemInfoProvider.class, "logger", logger);
49         aaiExternalSystemInfoProvider = new AAIExternalSystemInfoProvider(environment, aaiRestApiProvider);
50         when(aaiRestApiProvider.getExternalSystemApi()).thenReturn(externalSystemApi);
51         when(aaiRestApiProvider.getCloudInfrastructureApi()).thenReturn(cloudInfrastructureApi);
52     }
53
54     /**
55      * test query VIM success scenario
56      */
57     @Test
58     public void testVim() throws Exception {
59         CloudRegion cloudRegion = new CloudRegion();
60         cloudRegion.setEsrSystemInfoList(new ArrayList<>());
61         EsrSystemInfo vim = new EsrSystemInfo();
62         cloudRegion.getEsrSystemInfoList().add(vim);
63         vim.setPassword("myPassword");
64         vim.setUserName("myUsername");
65         vim.setServiceUrl("http://1.2.3.4:1234/a");
66         vim.setVersion("v123");
67         vim.setSystemStatus("active");
68         vim.setSystemName("name");
69         vim.setType("type");
70         vim.setSslInsecure(true);
71         vim.setVendor("vendor");
72         when(cloudInfrastructureApi.getCloudInfrastructureCloudRegionsCloudRegion("myCloudOwnerId", "myRegionName", null, null)).thenReturn(buildObservable(cloudRegion));
73         //when
74         VimInfo vimInfo = aaiExternalSystemInfoProvider.getVimInfo(VIM_ID);
75         assertEquals("myPassword", vimInfo.getPassword());
76         assertEquals("true", vimInfo.getSslInsecure());
77         assertEquals(null, vimInfo.getSslCacert());
78         assertEquals("myUsername", vimInfo.getUserName());
79         assertEquals("name", vimInfo.getDescription());
80         assertEquals("name", vimInfo.getName());
81         assertEquals("http://1.2.3.4:1234/a", vimInfo.getUrl());
82         assertEquals("active", vimInfo.getStatus());
83         assertEquals("type", vimInfo.getType());
84         assertEquals("v123", vimInfo.getVersion());
85         assertEquals(VIM_ID, vimInfo.getVimId());
86         assertEquals(null, vimInfo.getCreateTime());
87
88     }
89
90     /**
91      * test query VIM success scenario for SSL
92      */
93     @Test
94     public void testVimSsl() throws Exception {
95         CloudRegion cloudRegion = new CloudRegion();
96         cloudRegion.setEsrSystemInfoList(new ArrayList<>());
97         EsrSystemInfo vim = new EsrSystemInfo();
98         cloudRegion.getEsrSystemInfoList().add(vim);
99         vim.setPassword("myPassword");
100         vim.setUserName("myUsername");
101         vim.setServiceUrl("https://1.2.3.4:1234/a");
102         vim.setVersion("v123");
103         vim.setSystemStatus("active");
104         vim.setSystemName("name");
105         vim.setType("type");
106         vim.setSslInsecure(false);
107         vim.setSslCacert("cert");
108         vim.setVendor("vendor");
109         when(cloudInfrastructureApi.getCloudInfrastructureCloudRegionsCloudRegion("myCloudOwnerId", "myRegionName", null, null)).thenReturn(buildObservable(cloudRegion));
110         //when
111         VimInfo vimInfo = aaiExternalSystemInfoProvider.getVimInfo(VIM_ID);
112         assertEquals("myPassword", vimInfo.getPassword());
113         assertEquals("false", vimInfo.getSslInsecure());
114         assertEquals("cert", vimInfo.getSslCacert());
115         assertEquals("myUsername", vimInfo.getUserName());
116         assertEquals("name", vimInfo.getDescription());
117         assertEquals("name", vimInfo.getName());
118         assertEquals("https://1.2.3.4:1234/a", vimInfo.getUrl());
119         assertEquals("active", vimInfo.getStatus());
120         assertEquals("type", vimInfo.getType());
121         assertEquals("v123", vimInfo.getVersion());
122         assertEquals(VIM_ID, vimInfo.getVimId());
123         assertEquals(null, vimInfo.getCreateTime());
124     }
125
126     /**
127      * unable to query VIM from AAI results in error
128      */
129     @Test
130     public void testVimUnableToQuery() throws Exception {
131         RuntimeException expectedException = new RuntimeException();
132         when(cloudInfrastructureApi.getCloudInfrastructureCloudRegionsCloudRegion("myCloudOwnerId", "myRegionName", null, null)).thenThrow(expectedException);
133         //when
134         try {
135             aaiExternalSystemInfoProvider.getVimInfo(VIM_ID);
136             fail();
137         } catch (Exception e) {
138             verify(logger).error("Unable to query VIM with myCloudOwnerId_myRegionName identifier from AAI", expectedException);
139             assertEquals(expectedException, e.getCause());
140         }
141     }
142
143     /**
144      * test VNFM query success scenario
145      */
146     @Test
147     public void testVnfmQuery() throws Exception {
148         EsrVnfm vnfm = new EsrVnfm();
149         vnfm.setVimId(VIM_ID);
150         vnfm.setEsrSystemInfoList(new ArrayList<>());
151         EsrSystemInfo esrInfo = new EsrSystemInfo();
152         vnfm.getEsrSystemInfoList().add(esrInfo);
153         esrInfo.setPassword("myPassword");
154         esrInfo.setUserName("myUsername");
155         esrInfo.setServiceUrl("https://1.2.3.4:1234/a");
156         esrInfo.setVersion("v123");
157         esrInfo.setSystemStatus("active");
158         esrInfo.setSystemName("name");
159         esrInfo.setType("type");
160         esrInfo.setSslInsecure(false);
161         esrInfo.setSslCacert("cert");
162         esrInfo.setVendor("vendor");
163         vnfm.setVnfmId(VNFM_ID);
164         when(externalSystemApi.getExternalSystemEsrVnfmListEsrVnfm(VNFM_ID)).thenReturn(buildObservable(vnfm));
165
166         //when
167         VnfmInfo actualVnfmInfo = aaiExternalSystemInfoProvider.queryVnfmInfoFromSource(VNFM_ID);
168         //verify
169         assertEquals("myPassword", actualVnfmInfo.getPassword());
170         assertEquals("https://1.2.3.4:1234/a", actualVnfmInfo.getUrl());
171         assertEquals("myUsername", actualVnfmInfo.getUserName());
172         assertEquals(null, actualVnfmInfo.getCreateTime());
173         assertEquals(null, actualVnfmInfo.getDescription());
174         assertEquals("name", actualVnfmInfo.getName());
175         assertEquals("type", actualVnfmInfo.getType());
176         assertEquals("vendor", actualVnfmInfo.getVendor());
177         assertEquals("v123", actualVnfmInfo.getVersion());
178         assertEquals(VIM_ID, actualVnfmInfo.getVimId());
179         assertEquals(VNFM_ID, actualVnfmInfo.getVnfmId());
180     }
181
182     /**
183      * unable to query VNFM from AAI results in error
184      */
185     @Test
186     public void testVnfmUnableToQuery() throws Exception {
187         RuntimeException expectedException = new RuntimeException();
188         when(externalSystemApi.getExternalSystemEsrVnfmListEsrVnfm(VNFM_ID)).thenThrow(expectedException);
189         //when
190         try {
191             aaiExternalSystemInfoProvider.queryVnfmInfoFromSource(VNFM_ID);
192             fail();
193         } catch (Exception e) {
194             verify(logger).error("Unable to query VNFM with " + VNFM_ID + " identifier from AAI", expectedException);
195             assertEquals(expectedException, e.getCause());
196         }
197     }
198 }