diff options
author | 2020-05-15 15:50:10 -0400 | |
---|---|---|
committer | 2020-05-17 07:29:39 +0000 | |
commit | bcf6a1c5302625ea034067cab8c3489746f733bd (patch) | |
tree | 41b3d88cd95194f1bf0aca25f815425c2a738c8a /openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org | |
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/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org')
-rw-r--r-- | openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressCacheTest.java | 51 |
1 files changed, 29 insertions, 22 deletions
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); } } |