This is an automated email from the ASF dual-hosted git repository.
gongchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
The following commit(s) were added to refs/heads/master by this push:
new 49ebdfbea [improve] add IcmpCollectImplTest (#2033)
49ebdfbea is described below
commit 49ebdfbeaedcfd42fabb4c8f3c89b46869ab071f
Author: Zhang Yuxuan <[email protected]>
AuthorDate: Sun May 26 17:04:44 2024 +0800
[improve] add IcmpCollectImplTest (#2033)
---
.../collect/icmp/IcmpCollectImplTest.java | 116 ++++++++++++++++++++-
1 file changed, 114 insertions(+), 2 deletions(-)
diff --git
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/icmp/IcmpCollectImplTest.java
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/icmp/IcmpCollectImplTest.java
index 69c9ec715..a786d887a 100644
---
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/icmp/IcmpCollectImplTest.java
+++
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/icmp/IcmpCollectImplTest.java
@@ -17,23 +17,135 @@
package org.apache.hertzbeat.collector.collect.icmp;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.hertzbeat.common.entity.job.Metrics;
+import org.apache.hertzbeat.common.entity.job.protocol.IcmpProtocol;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+import org.mockito.junit.jupiter.MockitoExtension;
/**
* Test case for {@link IcmpCollectImpl}
*/
+@ExtendWith(MockitoExtension.class)
class IcmpCollectImplTest {
+ @Mock
+ IcmpProtocol icmpProtocol;
+
+ @Mock
+ Metrics metrics;
+
+ @Mock
+ InetAddress inetAddress;
+
+ @Mock
+ CollectRep.MetricsData.Builder builder;
+
+ @InjectMocks
+ private IcmpCollectImpl icmpCollect;
+
@BeforeEach
void setUp() {
+ icmpProtocol = IcmpProtocol.builder()
+ .host("127.0.0.1")
+ .timeout("3000")
+ .build();
+ List<String> aliasField = new ArrayList<>();
+ aliasField.add("responseTime");
+ metrics = new Metrics();
+ metrics.setName("test");
+ metrics.setIcmp(icmpProtocol);
+ metrics.setAliasFields(aliasField);
+ builder = CollectRep.MetricsData.newBuilder();
+ }
+
+ @Test
+ void testPreCheck() {
+ assertDoesNotThrow(() -> {
+ icmpCollect.preCheck(metrics);
+ });
+ assertThrows(IllegalArgumentException.class, () -> {
+ icmpCollect.preCheck(null);
+ });
+ metrics.setIcmp(null);
+ assertThrows(IllegalArgumentException.class, () -> {
+ icmpCollect.preCheck(null);
+ });
+ }
+
+ @Test
+ void testCollect() throws Exception {
+ try (MockedStatic<InetAddress> mockedInetAddress =
Mockito.mockStatic(InetAddress.class)) {
+ mockedInetAddress.when(() ->
InetAddress.getByName(Mockito.anyString())).thenReturn(inetAddress);
+
Mockito.when(inetAddress.isReachable(Mockito.anyInt())).thenReturn(true);
+ assertDoesNotThrow(() -> icmpCollect.collect(builder, 1L, "app",
metrics));
+ assertEquals(1, builder.getValuesCount());
+ assertNotNull(builder.getValues(0).getColumns(0));
+
+ }
+ }
+
+ @Test
+ void testUnreachable() throws Exception {
+ try (MockedStatic<InetAddress> mockedInetAddress =
Mockito.mockStatic(InetAddress.class)) {
+ mockedInetAddress.when(() ->
InetAddress.getByName(Mockito.anyString())).thenReturn(inetAddress);
+
Mockito.when(inetAddress.isReachable(Mockito.anyInt())).thenReturn(false);
+ assertDoesNotThrow(() -> icmpCollect.collect(builder, 1L, "app",
metrics));
+ assertEquals(CollectRep.Code.UN_REACHABLE, builder.getCode());
+ assertNotNull(builder.getMsg());
+ }
+ }
+
+ @Test
+ void testUnknownHostException() {
+ try (MockedStatic<InetAddress> mockedInetAddress =
Mockito.mockStatic(InetAddress.class)) {
+ mockedInetAddress.when(() ->
InetAddress.getByName(Mockito.anyString())).thenThrow(new
UnknownHostException("Mocked exception"));
+ assertDoesNotThrow(() -> icmpCollect.collect(builder, 1L, "app",
metrics));
+ assertEquals(CollectRep.Code.UN_REACHABLE, builder.getCode());
+ assertNotNull(builder.getMsg());
+ }
+ }
+
+ @Test
+ void testIoException() throws Exception {
+ try (MockedStatic<InetAddress> mockedInetAddress =
Mockito.mockStatic(InetAddress.class)) {
+ mockedInetAddress.when(() ->
InetAddress.getByName(Mockito.anyString())).thenReturn(inetAddress);
+
Mockito.when(inetAddress.isReachable(Mockito.anyInt())).thenThrow(new
IOException("Mocked exception"));
+ assertDoesNotThrow(() -> icmpCollect.collect(builder, 1L, "app",
metrics));
+ assertEquals(CollectRep.Code.UN_REACHABLE, builder.getCode());
+ assertNotNull(builder.getMsg());
+ }
}
@Test
- void getInstance() {
+ void testException() throws Exception {
+ try (MockedStatic<InetAddress> mockedInetAddress =
Mockito.mockStatic(InetAddress.class)) {
+ mockedInetAddress.when(() ->
InetAddress.getByName(Mockito.anyString())).thenReturn(inetAddress);
+
Mockito.when(inetAddress.isReachable(Mockito.anyInt())).thenThrow(new
RuntimeException("Mocked exception"));
+ assertDoesNotThrow(() -> icmpCollect.collect(builder, 1L, "app",
metrics));
+ assertEquals(CollectRep.Code.FAIL, builder.getCode());
+ assertNotNull(builder.getMsg());
+ }
}
@Test
- void collect() {
+ void testSupportProtocol() {
+ assertEquals("icmp", icmpCollect.supportProtocol());
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]