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