17cf8258841caeb4b256dfe40a117b108b96e91e
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / core / TestGenericExternalSystemInfoProvider.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 org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mockito;
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.core.env.Environment;
26 import org.springframework.test.util.ReflectionTestUtils;
27
28 import static java.lang.Long.valueOf;
29 import static junit.framework.TestCase.assertEquals;
30 import static junit.framework.TestCase.fail;
31 import static org.mockito.Mockito.*;
32
33 public class TestGenericExternalSystemInfoProvider extends TestBase {
34
35     private GenericExternalSystemInfoProvider genericExternalSystemInfoProvider;
36
37     @Before
38     public void init() {
39         when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("");
40         ReflectionTestUtils.setField(GenericExternalSystemInfoProvider.class, "logger", logger);
41         genericExternalSystemInfoProvider = Mockito.spy(new TestClass(environment));
42     }
43
44     /**
45      * the VNFM info is not retrieved within the cache eviction period
46      */
47     @Test
48     public void testQueryVnfmInfoWithin() throws Exception {
49         VnfmInfo expectedVnfmInfo = Mockito.mock(VnfmInfo.class);
50         when(genericExternalSystemInfoProvider.queryVnfmInfoFromSource(VNFM_ID)).thenReturn(expectedVnfmInfo);
51         when(environment.getProperty(GenericExternalSystemInfoProvider.VNFM_INFO_CACHE_EVICTION_IN_MS, Long.class, valueOf(GenericExternalSystemInfoProvider.DEFAULT_CACHE_EVICTION_TIMEOUT_IN_MS))).thenReturn(Long.valueOf(1234));
52         genericExternalSystemInfoProvider.afterPropertiesSet();
53         //when
54         VnfmInfo vnfmInfo = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID);
55         //verify
56         verify(logger).info("Querying VNFM info from source with " + VNFM_ID + " identifier");
57         assertEquals(expectedVnfmInfo, vnfmInfo);
58         //when
59         VnfmInfo vnfmInfo2 = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID);
60         //verify source system not called again
61         verify(logger).info("Querying VNFM info from source with " + VNFM_ID + " identifier");
62         verify(genericExternalSystemInfoProvider, Mockito.times(1)).queryVnfmInfoFromSource(VNFM_ID);
63     }
64
65     /**
66      * the VNFM info is retrieved without the cache eviction period
67      */
68     @Test
69     //sleeping is required to make time pass (for cache to notice the change)
70     //cache is configured with 1 ms cache eviction without sleep it is not
71     //deterministic that at least 1 ms time will pass between calls
72     @SuppressWarnings("squid:S2925")
73     public void testQueryVnfmInfoOutside() throws Exception {
74         VnfmInfo expectedVnfmInfo = Mockito.mock(VnfmInfo.class);
75         when(genericExternalSystemInfoProvider.queryVnfmInfoFromSource(VNFM_ID)).thenReturn(expectedVnfmInfo);
76         when(environment.getProperty(GenericExternalSystemInfoProvider.VNFM_INFO_CACHE_EVICTION_IN_MS, Long.class, valueOf(GenericExternalSystemInfoProvider.DEFAULT_CACHE_EVICTION_TIMEOUT_IN_MS))).thenReturn(Long.valueOf(1));
77         genericExternalSystemInfoProvider.afterPropertiesSet();
78         //when
79         VnfmInfo vnfmInfo = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID);
80         //verify
81         assertEquals(expectedVnfmInfo, vnfmInfo);
82         //when
83         Thread.sleep(10);
84         VnfmInfo vnfmInfo2 = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID);
85         //verify source system called again
86         verify(logger, times(2)).info("Querying VNFM info from source with " + VNFM_ID + " identifier");
87         verify(genericExternalSystemInfoProvider, Mockito.times(2)).queryVnfmInfoFromSource(VNFM_ID);
88     }
89
90     class TestClass extends GenericExternalSystemInfoProvider {
91
92         TestClass(Environment environment) {
93             super(environment);
94         }
95
96         @Override
97         public VnfmInfo queryVnfmInfoFromSource(String vnfmId) {
98             return null;
99         }
100
101         @Override
102         public VimInfo getVimInfo(String vimId) {
103             return null;
104         }
105     }
106
107
108     /**
109      * Unable to query VNFM results is propagated
110      */
111     @Test
112     public void testUnableToQueryVnfmInfoProvider() throws Exception{
113         class TestClass extends GenericExternalSystemInfoProvider {
114
115             TestClass(Environment environment) {
116                 super(environment);
117             }
118
119             @Override
120             public VnfmInfo queryVnfmInfoFromSource(String vnfmId) {
121                 throw new RuntimeException();
122             }
123
124             @Override
125             public VimInfo getVimInfo(String vimId) {
126                 return null;
127             }
128         }
129         try {
130             new TestClass(null).getVnfmInfo(VNFM_ID);
131             fail();
132         }
133         catch (Exception e){
134             assertEquals("Unable to query VNFM info for myVnfmId", e.getMessage());
135             verify(logger).error(eq("Unable to query VNFM info for myVnfmId"), any(RuntimeException.class));
136         }
137     }
138 }