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