1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
/*
* Copyright 2016-2017, Nokia Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
import org.onap.vnfmdriver.model.VimInfo;
import org.onap.vnfmdriver.model.VnfmInfo;
import org.springframework.core.env.Environment;
import org.springframework.test.util.ReflectionTestUtils;
import static java.lang.Long.valueOf;
import static junit.framework.TestCase.assertEquals;
import static org.mockito.Mockito.*;
public class TestGenericExternalSystemInfoProvider extends TestBase {
private GenericExternalSystemInfoProvider genericExternalSystemInfoProvider;
@Before
public void init() {
when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("");
ReflectionTestUtils.setField(GenericExternalSystemInfoProvider.class, "logger", logger);
genericExternalSystemInfoProvider = Mockito.spy(new TestClass(environment));
}
/**
* the VNFM info is not retrieved within the cache eviction period
*/
@Test
public void testQueryVnfmInfoWithin() throws Exception {
VnfmInfo expectedVnfmInfo = Mockito.mock(VnfmInfo.class);
when(genericExternalSystemInfoProvider.queryVnfmInfoFromSource(VNFM_ID)).thenReturn(expectedVnfmInfo);
when(environment.getProperty(GenericExternalSystemInfoProvider.VNFM_INFO_CACHE_EVICTION_IN_MS, Long.class, valueOf(GenericExternalSystemInfoProvider.DEFAULT_CACHE_EVICTION_TIMEOUT_IN_MS))).thenReturn(Long.valueOf(1234));
genericExternalSystemInfoProvider.afterPropertiesSet();
//when
VnfmInfo vnfmInfo = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID);
//verify
verify(logger).info("Quering VNFM info from source with " + VNFM_ID + " identifier");
assertEquals(expectedVnfmInfo, vnfmInfo);
//when
VnfmInfo vnfmInfo2 = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID);
//verify source system not called again
verify(logger).info("Quering VNFM info from source with " + VNFM_ID + " identifier");
verify(genericExternalSystemInfoProvider, Mockito.times(1)).queryVnfmInfoFromSource(VNFM_ID);
}
/**
* the VNFM info is retrieved without the cache eviction period
*/
@Test
public void testQueryVnfmInfoOutside() throws Exception {
VnfmInfo expectedVnfmInfo = Mockito.mock(VnfmInfo.class);
when(genericExternalSystemInfoProvider.queryVnfmInfoFromSource(VNFM_ID)).thenReturn(expectedVnfmInfo);
when(environment.getProperty(GenericExternalSystemInfoProvider.VNFM_INFO_CACHE_EVICTION_IN_MS, Long.class, valueOf(GenericExternalSystemInfoProvider.DEFAULT_CACHE_EVICTION_TIMEOUT_IN_MS))).thenReturn(Long.valueOf(1));
genericExternalSystemInfoProvider.afterPropertiesSet();
//when
VnfmInfo vnfmInfo = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID);
//verify
assertEquals(expectedVnfmInfo, vnfmInfo);
//when
//sleeping is required to make time pass (for cache to notice the change)
Thread.sleep(10);
VnfmInfo vnfmInfo2 = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID);
//verify source system called again
verify(logger, times(2)).info("Quering VNFM info from source with " + VNFM_ID + " identifier");
verify(genericExternalSystemInfoProvider, Mockito.times(2)).queryVnfmInfoFromSource(VNFM_ID);
}
class TestClass extends GenericExternalSystemInfoProvider {
TestClass(Environment environment) {
super(environment);
}
@Override
public VnfmInfo queryVnfmInfoFromSource(String vnfmId) {
return null;
}
@Override
public VimInfo getVimInfo(String vimId) {
return null;
}
}
}
|