6d7a636840c8dd267f6c55e8d90bfc28493c44f9
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
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.openecomp.sdc.logging.context;
18
19 import static org.testng.Assert.assertNotNull;
20
21 import java.net.InetAddress;
22 import java.net.UnknownHostException;
23 import org.easymock.EasyMock;
24 import org.powermock.api.easymock.PowerMock;
25 import org.powermock.core.classloader.annotations.PrepareForTest;
26 import org.powermock.modules.testng.PowerMockTestCase;
27 import org.testng.annotations.Test;
28
29 /**
30  * Retrieval and caching of host address.
31  *
32  * @author evitaliy
33  * @since 28 Mar 2018
34  */
35 @PrepareForTest(InetAddress.class)
36 public class HostAddressCacheTest extends PowerMockTestCase {
37
38     @Test
39     public void hostAddressIsAlwaysPopulated() {
40         assertNotNull(new HostAddressCache().get());
41     }
42
43     @Test
44     public void cachedAddressRemainsTheSameWhenGotWithingRefreshInterval() throws UnknownHostException {
45         mockInetAddress(1);
46         HostAddressCache addressCache = new HostAddressCache(1000);
47         addressCache.get();
48         addressCache.get();
49     }
50
51     @Test
52     public void cachedAddressReplacedWhenGotAfterRefreshInterval() throws UnknownHostException {
53         mockInetAddress(2);
54         HostAddressCache addressCache = new HostAddressCache(-1);
55         addressCache.get();
56         addressCache.get();
57     }
58
59     private void mockInetAddress(int times) throws UnknownHostException {
60         InetAddress inetAddress = EasyMock.mock(InetAddress.class);
61         EasyMock.replay(inetAddress);
62         PowerMock.mockStatic(InetAddress.class);
63         //noinspection ResultOfMethodCallIgnored
64         InetAddress.getLocalHost();
65         PowerMock.expectLastCall().andReturn(inetAddress).times(times);
66         PowerMock.replay(InetAddress.class);
67     }
68 }