Fix template generator for R2
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / vfc / TestVfcExternalSystemInfoProvider.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.vfc;
18
19 import com.google.common.collect.Sets;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
23 import org.onap.vnfmdriver.model.VimInfo;
24 import org.onap.vnfmdriver.model.VnfmInfo;
25 import org.springframework.test.util.ReflectionTestUtils;
26
27 import static junit.framework.TestCase.assertEquals;
28 import static junit.framework.TestCase.fail;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 public class TestVfcExternalSystemInfoProvider extends TestBase {
33     private VfcExternalSystemInfoProvider vfcExternalSystemInfoProvider;
34
35     @Before
36     public void init() {
37         vfcExternalSystemInfoProvider = new VfcExternalSystemInfoProvider(environment, vfcRestApiProvider);
38         ReflectionTestUtils.setField(VfcExternalSystemInfoProvider.class, "logger", logger);
39     }
40
41     /**
42      * VIM is queried using VF-C APIs
43      */
44     @Test
45     public void testVimRetrieval() throws Exception {
46         VimInfo expectedVimInfo = new VimInfo();
47         when(nsLcmApi.queryVIMInfo(VIM_ID)).thenReturn(buildObservable(expectedVimInfo));
48         //when
49         VimInfo vimInfo = vfcExternalSystemInfoProvider.getVimInfo(VIM_ID);
50         //verify
51         assertEquals(expectedVimInfo, vimInfo);
52     }
53
54     /**
55      * failure to retrieve VIM from VF-C is propagated
56      */
57     @Test
58     public void testUnableToQueryVim() throws Exception {
59         RuntimeException expectedException = new RuntimeException();
60         when(nsLcmApi.queryVIMInfo(VIM_ID)).thenThrow(expectedException);
61         //when
62         try {
63             vfcExternalSystemInfoProvider.getVimInfo(VIM_ID);
64             fail();
65         } catch (Exception e) {
66             assertEquals("Unable to query VIM from VF-C with " + VIM_ID + " identifier", e.getMessage());
67             verify(logger).error("Unable to query VIM from VF-C with " + VIM_ID + " identifier", expectedException);
68         }
69     }
70
71     /**
72      * VNFM is queried using VF-C APIs
73      */
74     @Test
75     public void testVnfmRetrieval() throws Exception {
76         VnfmInfo expectedVimInfo = new VnfmInfo();
77         when(nsLcmApi.queryVnfmInfo(VNFM_ID)).thenReturn(buildObservable(expectedVimInfo));
78         //when
79         VnfmInfo vimInfo = vfcExternalSystemInfoProvider.queryVnfmInfoFromSource(VNFM_ID);
80         //verify
81         assertEquals(expectedVimInfo, vimInfo);
82     }
83
84     /**
85      * failure to retrieve VNFM from VF-C is propagated
86      */
87     @Test
88     public void testUnableToQueryVnfm() throws Exception {
89         RuntimeException expectedException = new RuntimeException();
90         when(nsLcmApi.queryVnfmInfo(VNFM_ID)).thenThrow(expectedException);
91         //when
92         try {
93             vfcExternalSystemInfoProvider.queryVnfmInfoFromSource(VNFM_ID);
94             fail();
95         } catch (Exception e) {
96             assertEquals("Unable to query VNFM from VF-C with myVnfmId identifier", e.getMessage());
97             verify(logger).error("Unable to query VNFM from VF-C with myVnfmId identifier", expectedException);
98         }
99     }
100
101     /**
102      * The VNFM identifier is loaded from property files
103      */
104     @Test
105     public void testGetVnfms() {
106         setFieldWithPropertyAnnotation(vfcExternalSystemInfoProvider, "${vnfmId}", "myVnfmId");
107         assertEquals(Sets.newHashSet("myVnfmId"), vfcExternalSystemInfoProvider.getVnfms());
108     }
109 }