From 8650a419d25828211e3537e4ca65a803c8d11555 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Thu, 4 Jul 2024 22:01:48 +0800 Subject: [PATCH] [Improve] add IpDomainUtil & IntervaiExpressionUtil unit test Signed-off-by: yuluo-yx --- .../common/util/IntervalExpressionUtil.java | 6 +- .../hertzbeat/common/util/IpDomainUtil.java | 14 ++-- .../util/IntervalExpressionUtilTest.java | 49 ++++++++++-- .../common/util/IpDomainUtilTest.java | 76 ++++++++++++++++++- 4 files changed, 127 insertions(+), 18 deletions(-) diff --git a/common/src/main/java/org/apache/hertzbeat/common/util/IntervalExpressionUtil.java b/common/src/main/java/org/apache/hertzbeat/common/util/IntervalExpressionUtil.java index 21901f87485..4fffd211277 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/util/IntervalExpressionUtil.java +++ b/common/src/main/java/org/apache/hertzbeat/common/util/IntervalExpressionUtil.java @@ -49,7 +49,7 @@ private IntervalExpressionUtil() { * @return true-yes false-no */ public static boolean validNumberIntervalExpress(Double numberValue, String expression) { - if (expression == null || "".equals(expression)) { + if (expression == null || expression.isEmpty()) { return true; } if (numberValue == null) { @@ -64,12 +64,12 @@ public static boolean validNumberIntervalExpress(Double numberValue, String expr } Double[] doubleValues = new Double[2]; if (NEGATIVE.equals(values[0])) { - doubleValues[0] = Double.MIN_VALUE; + doubleValues[0] = Double.NEGATIVE_INFINITY; } else { doubleValues[0] = Double.parseDouble(values[0]); } if (POSITIVE.equals(values[1])) { - doubleValues[1] = Double.MAX_VALUE; + doubleValues[1] = Double.POSITIVE_INFINITY; } else { doubleValues[1] = Double.parseDouble(values[1]); } diff --git a/common/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java b/common/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java index 21d8d57dea1..886a5200e18 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java +++ b/common/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java @@ -29,7 +29,7 @@ import org.springframework.util.StringUtils; /** - * ipv4 ipv6 domain util + * ipv4 ipv6 domain util. */ @Slf4j public final class IpDomainUtil { @@ -40,7 +40,7 @@ public final class IpDomainUtil { private static final String LOCALHOST = "localhost"; /** - * HTTP header schema + * HTTP header schema. */ private static final Pattern DOMAIN_SCHEMA = Pattern.compile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://){1}[^\\s]*"); @@ -48,7 +48,7 @@ private IpDomainUtil() { } /** - * whether it is ip or domain + * whether it is ip or domain. * @param ipDomain ip domain string * @return true-yes false-no */ @@ -70,7 +70,7 @@ public static boolean validateIpDomain(String ipDomain) { } /** - * if domain or ip has http / https schema + * if domain or ip has http / https schema. * @param domainIp host * @return true or false */ @@ -82,7 +82,7 @@ public static boolean isHasSchema(String domainIp) { } /** - * get localhost IP + * get localhost IP. * @return ip */ public static String getLocalhostIp() { @@ -108,7 +108,7 @@ public static String getLocalhostIp() { } /** - * + * check IP address type. * @param ipDomain ip domain * @return IP address type */ @@ -120,7 +120,7 @@ public static String checkIpAddressType(String ipDomain){ } /** - * get current local host name + * get current local host name. * @return hostname */ public static String getCurrentHostName() { diff --git a/common/src/test/java/org/apache/hertzbeat/common/util/IntervalExpressionUtilTest.java b/common/src/test/java/org/apache/hertzbeat/common/util/IntervalExpressionUtilTest.java index c6abaf34d1c..91c8e0314a3 100644 --- a/common/src/test/java/org/apache/hertzbeat/common/util/IntervalExpressionUtilTest.java +++ b/common/src/test/java/org/apache/hertzbeat/common/util/IntervalExpressionUtilTest.java @@ -17,19 +17,54 @@ package org.apache.hertzbeat.common.util; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + /** * Test case for {@link IntervalExpressionUtil} */ class IntervalExpressionUtilTest { - @BeforeEach - void setUp() { - } + @Test + public void testValidNumberIntervalExpress() { + + assertTrue(IntervalExpressionUtil.validNumberIntervalExpress(5.0, null)); + + assertTrue(IntervalExpressionUtil.validNumberIntervalExpress(5.0, "")); + + assertFalse(IntervalExpressionUtil.validNumberIntervalExpress(null, "(3,7)")); + + assertTrue(IntervalExpressionUtil.validNumberIntervalExpress(5.0, "(3,7)")); + assertFalse(IntervalExpressionUtil.validNumberIntervalExpress(3.0, "(3,7)")); + assertFalse(IntervalExpressionUtil.validNumberIntervalExpress(7.0, "(3,7)")); + + assertTrue(IntervalExpressionUtil.validNumberIntervalExpress(3.0, "[3,7]")); + assertTrue(IntervalExpressionUtil.validNumberIntervalExpress(7.0, "[3,7]")); + assertFalse(IntervalExpressionUtil.validNumberIntervalExpress(2.0, "[3,7]")); + assertFalse(IntervalExpressionUtil.validNumberIntervalExpress(8.0, "[3,7]")); + + assertTrue(IntervalExpressionUtil.validNumberIntervalExpress(3.0, "[3,7)")); + assertTrue(IntervalExpressionUtil.validNumberIntervalExpress(6.9999, "[3,7)")); + assertFalse(IntervalExpressionUtil.validNumberIntervalExpress(7.0, "[3,7)")); + + assertTrue(IntervalExpressionUtil.validNumberIntervalExpress(4.0, "(3,7]")); + assertTrue(IntervalExpressionUtil.validNumberIntervalExpress(7.0, "(3,7]")); + assertFalse(IntervalExpressionUtil.validNumberIntervalExpress(3.0, "(3,7]")); + + assertTrue(IntervalExpressionUtil.validNumberIntervalExpress(-1000.0, "(-∞,5)")); + assertTrue(IntervalExpressionUtil.validNumberIntervalExpress(-1.0, "(-∞,5)")); + assertFalse(IntervalExpressionUtil.validNumberIntervalExpress(5.0, "(-∞,5)")); + assertFalse(IntervalExpressionUtil.validNumberIntervalExpress(10.0, "(-∞,5)")); + + assertTrue(IntervalExpressionUtil.validNumberIntervalExpress(1000.0, "(5,+∞)")); + assertTrue(IntervalExpressionUtil.validNumberIntervalExpress(10.0, "(5,+∞)")); + assertFalse(IntervalExpressionUtil.validNumberIntervalExpress(5.0, "(5,+∞)")); + assertFalse(IntervalExpressionUtil.validNumberIntervalExpress(0.0, "(5,+∞)")); + + assertFalse(IntervalExpressionUtil.validNumberIntervalExpress(5.0, "(3,7")); + assertFalse(IntervalExpressionUtil.validNumberIntervalExpress(5.0, "[3,7)3,7]")); + } - @Test - void validNumberIntervalExpress() { - } } diff --git a/common/src/test/java/org/apache/hertzbeat/common/util/IpDomainUtilTest.java b/common/src/test/java/org/apache/hertzbeat/common/util/IpDomainUtilTest.java index 4e182a91a9d..a711d6515d8 100644 --- a/common/src/test/java/org/apache/hertzbeat/common/util/IpDomainUtilTest.java +++ b/common/src/test/java/org/apache/hertzbeat/common/util/IpDomainUtilTest.java @@ -17,9 +17,24 @@ package org.apache.hertzbeat.common.util; +import java.net.Inet4Address; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Collections; +import java.util.Enumeration; + +import org.apache.hertzbeat.common.constants.CollectorConstants; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.jupiter.api.Test; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * Test case for {@link IpDomainUtil} @@ -56,4 +71,63 @@ void isHasSchema() { assertFalse(IpDomainUtil.isHasSchema("www.baidu.com")); assertFalse(IpDomainUtil.isHasSchema("https_www.baidu.com")); } + + @Test + void testGetLocalhostIp() throws SocketException { + + // Success + InetAddress mockedInetAddress = mock(Inet4Address.class); + when(mockedInetAddress.getHostAddress()).thenReturn("192.168.1.100"); + + NetworkInterface mockedNetworkInterface = mock(NetworkInterface.class); + when(mockedNetworkInterface.isLoopback()).thenReturn(false); + when(mockedNetworkInterface.isVirtual()).thenReturn(false); + when(mockedNetworkInterface.isUp()).thenReturn(true); + + Enumeration inetAddresses = Collections.enumeration(Collections.singletonList(mockedInetAddress)); + when(mockedNetworkInterface.getInetAddresses()).thenReturn(inetAddresses); + + Enumeration successNetworkInterfaces = Collections.enumeration(Collections.singletonList(mockedNetworkInterface)); + + try (MockedStatic mockedStaticNetworkInterface = Mockito.mockStatic(NetworkInterface.class)) { + mockedStaticNetworkInterface.when(NetworkInterface::getNetworkInterfaces).thenReturn(successNetworkInterfaces); + String localhostIp = IpDomainUtil.getLocalhostIp(); + + assertEquals("192.168.1.100", localhostIp); + } + + // no network interface + Enumeration noNetworkIFNetworkInterfaces = Collections.enumeration(Collections.emptyList()); + + try (MockedStatic mockedStaticNetworkInterface = Mockito.mockStatic(NetworkInterface.class)) { + mockedStaticNetworkInterface.when(NetworkInterface::getNetworkInterfaces).thenReturn(noNetworkIFNetworkInterfaces); + String localhostIp = IpDomainUtil.getLocalhostIp(); + + assertNull(localhostIp); + } + + // throw exception + try (MockedStatic mockedStaticNetworkInterface = Mockito.mockStatic(NetworkInterface.class)) { + mockedStaticNetworkInterface.when(NetworkInterface::getNetworkInterfaces).thenThrow(new RuntimeException("Test exception")); + String localhostIp = IpDomainUtil.getLocalhostIp(); + + assertNull(localhostIp); + } + + } + + @Test + void testCheckIpAddressType() { + + assertEquals(CollectorConstants.IPV4, IpDomainUtil.checkIpAddressType("192.168.1.1")); + assertEquals(CollectorConstants.IPV4, IpDomainUtil.checkIpAddressType("127.0.0.1")); + + assertEquals(CollectorConstants.IPV6, IpDomainUtil.checkIpAddressType("2001:0db8:85a3:0000:0000:8a2e:0370:7334")); + assertEquals(CollectorConstants.IPV6, IpDomainUtil.checkIpAddressType("::1")); + + assertEquals(CollectorConstants.IPV4, IpDomainUtil.checkIpAddressType("")); + assertEquals(CollectorConstants.IPV4, IpDomainUtil.checkIpAddressType(null)); + assertEquals(CollectorConstants.IPV4, IpDomainUtil.checkIpAddressType("invalid-ip")); + + } }