Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improve] add IpDomainUtil & IntervaiExpressionUtil unit test #2204

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.springframework.util.StringUtils;

/**
* ipv4 ipv6 domain util
* ipv4 ipv6 domain util.
*/
@Slf4j
public final class IpDomainUtil {
Expand All @@ -40,15 +40,15 @@ 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]*");

private IpDomainUtil() {
}

/**
* whether it is ip or domain
* whether it is ip or domain.
* @param ipDomain ip domain string
* @return true-yes false-no
*/
Expand All @@ -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
*/
Expand All @@ -82,7 +82,7 @@ public static boolean isHasSchema(String domainIp) {
}

/**
* get localhost IP
* get localhost IP.
* @return ip
*/
public static String getLocalhostIp() {
Expand All @@ -108,7 +108,7 @@ public static String getLocalhostIp() {
}

/**
*
* check IP address type.
* @param ipDomain ip domain
* @return IP address type
*/
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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<InetAddress> inetAddresses = Collections.enumeration(Collections.singletonList(mockedInetAddress));
when(mockedNetworkInterface.getInetAddresses()).thenReturn(inetAddresses);

Enumeration<NetworkInterface> successNetworkInterfaces = Collections.enumeration(Collections.singletonList(mockedNetworkInterface));

try (MockedStatic<NetworkInterface> 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<NetworkInterface> noNetworkIFNetworkInterfaces = Collections.enumeration(Collections.emptyList());

try (MockedStatic<NetworkInterface> mockedStaticNetworkInterface = Mockito.mockStatic(NetworkInterface.class)) {
mockedStaticNetworkInterface.when(NetworkInterface::getNetworkInterfaces).thenReturn(noNetworkIFNetworkInterfaces);
String localhostIp = IpDomainUtil.getLocalhostIp();

assertNull(localhostIp);
}

// throw exception
try (MockedStatic<NetworkInterface> 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"));

}
}
Loading