aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java
diff options
context:
space:
mode:
authorRodrigo Lima <rodrigo.lima@yoppworks.com>2020-05-15 15:50:10 -0400
committerOfir Sonsino <ofir.sonsino@intl.att.com>2020-05-17 07:29:39 +0000
commitbcf6a1c5302625ea034067cab8c3489746f733bd (patch)
tree41b3d88cd95194f1bf0aca25f815425c2a738c8a /openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java
parenta532a8c241439ee774326068137e6b5cbb8f348f (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/main/java')
-rw-r--r--openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/HostAddressCache.java28
1 files changed, 22 insertions, 6 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();
}