diff options
author | Rodrigo Lima <rodrigo.lima@yoppworks.com> | 2020-05-15 15:50:10 -0400 |
---|---|---|
committer | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2020-05-17 07:29:39 +0000 |
commit | bcf6a1c5302625ea034067cab8c3489746f733bd (patch) | |
tree | 41b3d88cd95194f1bf0aca25f815425c2a738c8a /openecomp-be/lib | |
parent | a532a8c241439ee774326068137e6b5cbb8f348f (diff) |
Remove powermock from HostAddressCacheTest
- Remove powermock and easymock from HostAddressCacheTest
- Add package level constructor to HostAddressCache and pass read address function to verified in unit test
Issue-ID: SDC-3068
Signed-off-by: Rodrigo Lima <rodrigo.lima@yoppworks.com>
Change-Id: I749211393d9e0ecb3ff3b587dc0f811b58bb967c
Diffstat (limited to 'openecomp-be/lib')
2 files changed, 51 insertions, 28 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/HostAddressCache.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/HostAddressCache.java index 0930888aac..6e63728a07 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/HostAddressCache.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/HostAddressCache.java @@ -22,6 +22,7 @@ import java.net.SocketException; import java.net.UnknownHostException; import java.util.Enumeration; import java.util.Optional; +import java.util.function.Supplier; /** * Holds a reference to local host address as returned by Java runtime. A value of host address will be cached for the @@ -41,6 +42,8 @@ public class HostAddressCache { private volatile CacheEntry cachedAddress; + private final Supplier<InetAddress> readAddress; + public HostAddressCache() { this(DEFAULT_REFRESH_INTERVAL); } @@ -50,7 +53,20 @@ public class HostAddressCache { */ public HostAddressCache(long refreshInterval) { this.interval = refreshInterval; - this.cachedAddress = new CacheEntry(System.currentTimeMillis(), read()); + this.readAddress = HostAddressCache::read; + this.cachedAddress = new CacheEntry(System.currentTimeMillis(), readAddress.get()); + } + + /** + * Package level constructor used for unit test in order to avoid static mock + * + * @param readAddress + * @param refreshInterval + */ + HostAddressCache(Supplier<InetAddress> readAddress, long refreshInterval) { + this.interval = refreshInterval; + this.readAddress = readAddress; + this.cachedAddress = new CacheEntry(System.currentTimeMillis(), this.readAddress.get()); } /** @@ -65,12 +81,12 @@ public class HostAddressCache { return Optional.ofNullable(cachedAddress.address); } - InetAddress address = read(); // register the attempt even if null, i.e. failed to get a meaningful address + InetAddress address = readAddress.get(); // register the attempt even if null, i.e. failed to get a meaningful address cachedAddress = new CacheEntry(current, address); return Optional.ofNullable(address); } - private InetAddress read() { + private static InetAddress read() { try { return InetAddress.getLocalHost(); @@ -85,7 +101,7 @@ public class HostAddressCache { } } - private InetAddress getFallbackLocalHost() { + private static InetAddress getFallbackLocalHost() { try { @@ -107,7 +123,7 @@ public class HostAddressCache { } } - private InetAddress getAddress(NetworkInterface networkInterface) throws SocketException { + private static InetAddress getAddress(NetworkInterface networkInterface) throws SocketException { if (networkInterface.isLoopback() || networkInterface.isUp()) { return null; @@ -125,7 +141,7 @@ public class HostAddressCache { return null; } - private boolean isHostAddress(InetAddress address) { + private static boolean isHostAddress(InetAddress address) { return !address.isLoopbackAddress() && !address.isAnyLocalAddress() && !address.isLinkLocalAddress() && !address.isMulticastAddress(); } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressCacheTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressCacheTest.java index 5c253a1ec1..697252e941 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressCacheTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressCacheTest.java @@ -16,16 +16,14 @@ package org.openecomp.sdc.logging.context; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.net.InetAddress; import java.net.UnknownHostException; -import org.easymock.EasyMock; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.easymock.PowerMock; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import java.util.function.Supplier; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; /** * Retrieval and caching of host address. @@ -33,10 +31,24 @@ import org.powermock.modules.junit4.PowerMockRunner; * @author evitaliy * @since 28 Mar 2018 */ -@PrepareForTest(InetAddress.class) -@RunWith(PowerMockRunner.class) public class HostAddressCacheTest { + private int readAddressCalls = 0; + + private Supplier<InetAddress> readAddress = () -> { + readAddressCalls++ ; + try { + return InetAddress.getLocalHost(); + } catch (UnknownHostException e) { + return null; + } + }; + + @BeforeEach + public void setUp() { + this.readAddressCalls = 0; + } + @Test public void hostAddressIsAlwaysPopulated() { assertTrue(new HostAddressCache().get().isPresent()); @@ -44,27 +56,22 @@ public class HostAddressCacheTest { @Test public void cachedAddressRemainsTheSameWhenGotWithingRefreshInterval() throws UnknownHostException { - mockInetAddress(1); - HostAddressCache addressCache = new HostAddressCache(1000); + HostAddressCache addressCache = new HostAddressCache(readAddress, 1000); addressCache.get(); addressCache.get(); + addressCache.get(); + addressCache.get(); + + assertEquals(1, readAddressCalls); } @Test public void cachedAddressReplacedWhenGotAfterRefreshInterval() throws UnknownHostException { - mockInetAddress(2); - HostAddressCache addressCache = new HostAddressCache(-1); + HostAddressCache addressCache = new HostAddressCache(readAddress, -1); addressCache.get(); addressCache.get(); - } - private void mockInetAddress(int times) throws UnknownHostException { - InetAddress inetAddress = EasyMock.mock(InetAddress.class); - EasyMock.replay(inetAddress); - PowerMock.mockStatic(InetAddress.class); - //noinspection ResultOfMethodCallIgnored - InetAddress.getLocalHost(); - PowerMock.expectLastCall().andReturn(inetAddress).times(times); - PowerMock.replay(InetAddress.class); + // one call in the constructor and two of addressCache::get + assertEquals(3, readAddressCalls); } } |