summaryrefslogtreecommitdiffstats
path: root/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/core/TestGenericExternalSystemInfoProvider.java
blob: efa452341d8a626c9cb499d4304d3b16118d9905 (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * 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 junit.framework.TestCase.fail;
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("Querying 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("Querying 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
    //sleeping is required to make time pass (for cache to notice the change)
    //cache is configured with 1 ms cache eviction without sleep it is not
    //deterministic that at least 1 ms time will pass between calls
    @SuppressWarnings("squid:S2925")
    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
        Thread.sleep(10);
        VnfmInfo vnfmInfo2 = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID);
        //verify source system called again
        verify(logger, times(2)).info("Querying VNFM info from source with " + VNFM_ID + " identifier");
        verify(genericExternalSystemInfoProvider, Mockito.times(2)).queryVnfmInfoFromSource(VNFM_ID);
    }

    /**
     * Unable to query VNFM results is propagated
     */
    @Test
    public void testUnableToQueryVnfmInfoProvider() throws Exception {
        class TestClass extends GenericExternalSystemInfoProvider {

            TestClass(Environment environment) {
                super(environment);
            }

            @Override
            public VnfmInfo queryVnfmInfoFromSource(String vnfmId) {
                throw new RuntimeException();
            }

            @Override
            public VimInfo getVimInfo(String vimId) {
                return null;
            }
        }
        try {
            new TestClass(null).getVnfmInfo(VNFM_ID);
            fail();
        } catch (Exception e) {
            assertEquals("Unable to query VNFM info for myVnfmId", e.getMessage());
            verify(logger).error(eq("Unable to query VNFM info for myVnfmId"), any(RuntimeException.class));
        }
    }

    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;
        }
    }
}