2 * Copyright 2016-2017, Nokia Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core;
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;
28 import static java.lang.Long.valueOf;
30 import static junit.framework.TestCase.assertEquals;
31 import static junit.framework.TestCase.fail;
32 import static org.mockito.Mockito.*;
34 public class TestGenericExternalSystemInfoProvider extends TestBase {
36 private GenericExternalSystemInfoProvider genericExternalSystemInfoProvider;
40 when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("");
41 ReflectionTestUtils.setField(GenericExternalSystemInfoProvider.class, "logger", logger);
42 genericExternalSystemInfoProvider = Mockito.spy(new TestClass(environment));
46 * the VNFM info is not retrieved within the cache eviction period
49 public void testQueryVnfmInfoWithin() throws Exception {
50 VnfmInfo expectedVnfmInfo = Mockito.mock(VnfmInfo.class);
51 when(genericExternalSystemInfoProvider.queryVnfmInfoFromSource(VNFM_ID)).thenReturn(expectedVnfmInfo);
52 when(environment.getProperty(GenericExternalSystemInfoProvider.VNFM_INFO_CACHE_EVICTION_IN_MS, Long.class, valueOf(GenericExternalSystemInfoProvider.DEFAULT_CACHE_EVICTION_TIMEOUT_IN_MS))).thenReturn(Long.valueOf(1234));
53 genericExternalSystemInfoProvider.afterPropertiesSet();
55 VnfmInfo vnfmInfo = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID);
57 verify(logger).info("Querying VNFM info from source with " + VNFM_ID + " identifier");
58 assertEquals(expectedVnfmInfo, vnfmInfo);
60 VnfmInfo vnfmInfo2 = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID);
61 //verify source system not called again
62 verify(logger).info("Querying VNFM info from source with " + VNFM_ID + " identifier");
63 verify(genericExternalSystemInfoProvider, Mockito.times(1)).queryVnfmInfoFromSource(VNFM_ID);
67 * the VNFM info is retrieved without the cache eviction period
70 //sleeping is required to make time pass (for cache to notice the change)
71 //cache is configured with 1 ms cache eviction without sleep it is not
72 //deterministic that at least 1 ms time will pass between calls
73 @SuppressWarnings("squid:S2925")
74 public void testQueryVnfmInfoOutside() throws Exception {
75 VnfmInfo expectedVnfmInfo = Mockito.mock(VnfmInfo.class);
76 when(genericExternalSystemInfoProvider.queryVnfmInfoFromSource(VNFM_ID)).thenReturn(expectedVnfmInfo);
77 when(environment.getProperty(GenericExternalSystemInfoProvider.VNFM_INFO_CACHE_EVICTION_IN_MS, Long.class, valueOf(GenericExternalSystemInfoProvider.DEFAULT_CACHE_EVICTION_TIMEOUT_IN_MS))).thenReturn(Long.valueOf(1));
78 genericExternalSystemInfoProvider.afterPropertiesSet();
80 VnfmInfo vnfmInfo = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID);
82 assertEquals(expectedVnfmInfo, vnfmInfo);
85 VnfmInfo vnfmInfo2 = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID);
86 //verify source system called again
87 verify(logger, times(2)).info("Querying VNFM info from source with " + VNFM_ID + " identifier");
88 verify(genericExternalSystemInfoProvider, Mockito.times(2)).queryVnfmInfoFromSource(VNFM_ID);
92 * Unable to query VNFM results is propagated
95 public void testUnableToQueryVnfmInfoProvider() throws Exception {
96 class TestClass extends GenericExternalSystemInfoProvider {
98 TestClass(Environment environment) {
103 public VnfmInfo queryVnfmInfoFromSource(String vnfmId) {
104 throw new RuntimeException();
108 public VimInfo getVimInfo(String vimId) {
113 new TestClass(null).getVnfmInfo(VNFM_ID);
115 } catch (Exception e) {
116 assertEquals("Unable to query VNFM info for myVnfmId", e.getMessage());
117 verify(logger).error(eq("Unable to query VNFM info for myVnfmId"), any(RuntimeException.class));
121 class TestClass extends GenericExternalSystemInfoProvider {
123 TestClass(Environment environment) {
128 public VnfmInfo queryVnfmInfoFromSource(String vnfmId) {
133 public VimInfo getVimInfo(String vimId) {