Remove powermock from HostAddressCacheTest 59/107759/2
authorRodrigo Lima <rodrigo.lima@yoppworks.com>
Fri, 15 May 2020 19:50:10 +0000 (15:50 -0400)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Sun, 17 May 2020 07:29:39 +0000 (07:29 +0000)
- 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

openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/HostAddressCache.java
openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressCacheTest.java

index 0930888..6e63728 100644 (file)
@@ -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();
     }
index 5c253a1..697252e 100644 (file)
 
 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);
     }
 }