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 48fedca43 [test] add test for UdpCollectImpl (#1906)
48fedca43 is described below
commit 48fedca436e8999e2d4e8e39fbd1c18359370b94
Author: crossoverJie <[email protected]>
AuthorDate: Fri May 3 16:28:58 2024 +0800
[test] add test for UdpCollectImpl (#1906)
---
.../collector/collect/udp/UdpCollectImplTest.java | 142 +++++++++++++++++++++
1 file changed, 142 insertions(+)
diff --git
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/udp/UdpCollectImplTest.java
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/udp/UdpCollectImplTest.java
new file mode 100644
index 000000000..f8ed3c0dd
--- /dev/null
+++
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/udp/UdpCollectImplTest.java
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hertzbeat.collector.collect.udp;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.PortUnreachableException;
+import java.net.SocketTimeoutException;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.hertzbeat.common.entity.job.Metrics;
+import org.apache.hertzbeat.common.entity.job.protocol.UdpProtocol;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.MockedConstruction;
+import org.mockito.Mockito;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+/**
+ * Test case for {@link UdpCollectImpl}
+ */
+@ExtendWith(MockitoExtension.class)
+class UdpCollectImplTest {
+
+ @InjectMocks
+ private UdpCollectImpl udpCollect;
+
+ @Test
+ void testPreCheck() {
+ CollectRep.MetricsData.Builder builder =
CollectRep.MetricsData.newBuilder();
+ List<String> aliasField = new ArrayList<>();
+ aliasField.add("responseTime");
+ Metrics metrics = new Metrics();
+ metrics.setAliasFields(aliasField);
+ udpCollect.collect(builder, 1L, "test", metrics);
+ assertEquals(builder.getCode(), CollectRep.Code.FAIL);
+
+ }
+
+ @Test
+ void testCollect(){
+ CollectRep.MetricsData.Builder builder =
CollectRep.MetricsData.newBuilder();
+ UdpProtocol ftpProtocol = UdpProtocol.builder()
+ .timeout("10")
+ .port("21")
+ .host("127.0.0.1")
+ .build();
+
+ MockedConstruction<DatagramSocket> socketMockedConstruction =
+ Mockito.mockConstruction(DatagramSocket.class, (socket,
context) -> {
+
Mockito.doNothing().when(socket).send(Mockito.any(DatagramPacket.class));
+
Mockito.doNothing().when(socket).receive(Mockito.any(DatagramPacket.class));
+ });
+
+
+ List<String> aliasField = new ArrayList<>();
+ aliasField.add("responseTime");
+ Metrics metrics = new Metrics();
+ metrics.setUdp(ftpProtocol);
+ metrics.setAliasFields(aliasField);
+ udpCollect.collect(builder, 1L, "test", metrics);
+ assertEquals(builder.getValuesCount(), 1);
+ for (CollectRep.ValueRow valueRow : builder.getValuesList()) {
+ assertNotNull(valueRow.getColumns(0));
+ }
+
+ socketMockedConstruction.close();
+ }
+
+ @Test
+ void testCollectWithSocketException(){
+ CollectRep.MetricsData.Builder builder =
CollectRep.MetricsData.newBuilder();
+ UdpProtocol ftpProtocol = UdpProtocol.builder()
+ .timeout("10")
+ .port("21")
+ .host("127.0.0.1")
+ .build();
+
+ MockedConstruction<DatagramSocket> socketMockedConstruction =
+ Mockito.mockConstruction(DatagramSocket.class, (socket,
context) -> {
+ Mockito.doThrow(new SocketTimeoutException("test
exception"))
+
.when(socket).send(Mockito.any(DatagramPacket.class));
+ });
+
+
+ List<String> aliasField = new ArrayList<>();
+ aliasField.add("responseTime");
+ Metrics metrics = new Metrics();
+ metrics.setUdp(ftpProtocol);
+ metrics.setAliasFields(aliasField);
+ udpCollect.collect(builder, 1L, "test", metrics);
+ assertEquals(builder.getCode(), CollectRep.Code.UN_CONNECTABLE);
+
+ socketMockedConstruction.close();
+ }
+
+ @Test
+ void testCollectWithPortUnreachableException(){
+ CollectRep.MetricsData.Builder builder =
CollectRep.MetricsData.newBuilder();
+ UdpProtocol ftpProtocol = UdpProtocol.builder()
+ .timeout("10")
+ .port("21")
+ .host("127.0.0.1")
+ .build();
+
+ MockedConstruction<DatagramSocket> socketMockedConstruction =
+ Mockito.mockConstruction(DatagramSocket.class, (socket,
context) -> {
+ Mockito.doThrow(new PortUnreachableException("test
exception"))
+
.when(socket).send(Mockito.any(DatagramPacket.class));
+ });
+
+
+ List<String> aliasField = new ArrayList<>();
+ aliasField.add("responseTime");
+ Metrics metrics = new Metrics();
+ metrics.setUdp(ftpProtocol);
+ metrics.setAliasFields(aliasField);
+ udpCollect.collect(builder, 1L, "test", metrics);
+ assertEquals(builder.getCode(), CollectRep.Code.UN_REACHABLE);
+
+ socketMockedConstruction.close();
+ }
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]