ea1afe5f83b95e04e9466d1e183b3edfa5ba5632
[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 org.mockito.Mockito.*;
31
32 public class TestGenericExternalSystemInfoProvider extends TestBase {
33
34     private GenericExternalSystemInfoProvider genericExternalSystemInfoProvider;
35
36     @Before
37     public void init() {
38         when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("");
39         ReflectionTestUtils.setField(GenericExternalSystemInfoProvider.class, "logger", logger);
40         genericExternalSystemInfoProvider = Mockito.spy(new TestClass(environment));
41     }
42
43     /**
44      * the VNFM info is not retrieved within the cache eviction period
45      */
46     @Test
47     public void testQueryVnfmInfoWithin() throws Exception {
48         VnfmInfo expectedVnfmInfo = Mockito.mock(VnfmInfo.class);
49         when(genericExternalSystemInfoProvider.queryVnfmInfoFromSource(VNFM_ID)).thenReturn(expectedVnfmInfo);
50         when(environment.getProperty(GenericExternalSystemInfoProvider.VNFM_INFO_CACHE_EVICTION_IN_MS, Long.class, valueOf(GenericExternalSystemInfoProvider.DEFAULT_CACHE_EVICTION_TIMEOUT_IN_MS))).thenReturn(Long.valueOf(1234));
51         genericExternalSystemInfoProvider.afterPropertiesSet();
52         //when
53         VnfmInfo vnfmInfo = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID);
54         //verify
55         verify(logger).info("Quering VNFM info from source with " + VNFM_ID + " identifier");
56         assertEquals(expectedVnfmInfo, vnfmInfo);
57         //when
58         VnfmInfo vnfmInfo2 = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID);
59         //verify source system not called again
60         verify(logger).info("Quering VNFM info from source with " + VNFM_ID + " identifier");
61         verify(genericExternalSystemInfoProvider, Mockito.times(1)).queryVnfmInfoFromSource(VNFM_ID);
62     }
63
64     /**
65      * the VNFM info is retrieved without the cache eviction period
66      */
67     @Test
68     public void testQueryVnfmInfoOutside() throws Exception {
69         VnfmInfo expectedVnfmInfo = Mockito.mock(VnfmInfo.class);
70         when(genericExternalSystemInfoProvider.queryVnfmInfoFromSource(VNFM_ID)).thenReturn(expectedVnfmInfo);
71         when(environment.getProperty(GenericExternalSystemInfoProvider.VNFM_INFO_CACHE_EVICTION_IN_MS, Long.class, valueOf(GenericExternalSystemInfoProvider.DEFAULT_CACHE_EVICTION_TIMEOUT_IN_MS))).thenReturn(Long.valueOf(1));
72         genericExternalSystemInfoProvider.afterPropertiesSet();
73
74         //when
75         VnfmInfo vnfmInfo = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID);
76         //verify
77         assertEquals(expectedVnfmInfo, vnfmInfo);
78         //when
79         //sleeping is required to make time pass (for cache to notice the change)
80         //cache is configured with 1 ms cache eviction without sleep it is not
81         //deterministic that at least 1 ms time will pass between calls
82         Thread.sleep(10);  //NO SONAR
83         VnfmInfo vnfmInfo2 = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID);
84         //verify source system called again
85         verify(logger, times(2)).info("Quering VNFM info from source with " + VNFM_ID + " identifier");
86         verify(genericExternalSystemInfoProvider, Mockito.times(2)).queryVnfmInfoFromSource(VNFM_ID);
87     }
88
89     class TestClass extends GenericExternalSystemInfoProvider {
90
91         TestClass(Environment environment) {
92             super(environment);
93         }
94
95         @Override
96         public VnfmInfo queryVnfmInfoFromSource(String vnfmId) {
97             return null;
98         }
99
100         @Override
101         public VimInfo getVimInfo(String vimId) {
102             return null;
103         }
104     }
105
106 }