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 27013fd63 [test] Add test for NtpCollectImpl (#1940)
27013fd63 is described below

commit 27013fd635c895fd4ecfeeb54b0b54f081166c60
Author: crossoverJie <[email protected]>
AuthorDate: Thu May 9 08:44:26 2024 +0800

    [test] Add test for NtpCollectImpl (#1940)
    
    Co-authored-by: tomsun28 <[email protected]>
---
 .../collector/collect/ntp/NtpCollectImpl.java      |  4 +-
 .../collector/collect/ntp/NtpCollectImplTest.java  | 90 ++++++++++++++++++++++
 .../collect/telnet/TelnetCollectImplTest.java      |  3 -
 3 files changed, 92 insertions(+), 5 deletions(-)

diff --git 
a/collector/src/main/java/org/apache/hertzbeat/collector/collect/ntp/NtpCollectImpl.java
 
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/ntp/NtpCollectImpl.java
index 63fd4bfb9..1161e0fba 100644
--- 
a/collector/src/main/java/org/apache/hertzbeat/collector/collect/ntp/NtpCollectImpl.java
+++ 
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/ntp/NtpCollectImpl.java
@@ -120,10 +120,10 @@ public class NtpCollectImpl extends AbstractCollect {
     private Map<String, String> getNtpInfo(TimeInfo timeInfo) {
         Map<String, String> valueMap = new HashMap<>(16);
 
-        TimeStamp timeStamp = timeInfo.getMessage().getTransmitTimeStamp();
+        NtpV3Packet message = timeInfo.getMessage();
+        TimeStamp timeStamp = message.getTransmitTimeStamp();
         Date date = timeStamp.getDate();
 
-        NtpV3Packet message = timeInfo.getMessage();
         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd 
HH:mm:ss");
         valueMap.put("time", Long.toString(timeStamp.getTime()));
         valueMap.put("date", simpleDateFormat.format(date));
diff --git 
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/ntp/NtpCollectImplTest.java
 
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/ntp/NtpCollectImplTest.java
new file mode 100644
index 000000000..56aeb8e1e
--- /dev/null
+++ 
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/ntp/NtpCollectImplTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.ntp;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import java.net.InetAddress;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.commons.net.ntp.NTPUDPClient;
+import org.apache.commons.net.ntp.NtpV3Impl;
+import org.apache.commons.net.ntp.NtpV3Packet;
+import org.apache.commons.net.ntp.TimeInfo;
+import org.apache.commons.net.ntp.TimeStamp;
+import org.apache.hertzbeat.common.entity.job.Metrics;
+import org.apache.hertzbeat.common.entity.job.protocol.NtpProtocol;
+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 NtpCollectImpl}
+ */
+@ExtendWith(MockitoExtension.class)
+class NtpCollectImplTest {
+
+    @InjectMocks
+    private NtpCollectImpl ntpCollect;
+
+    @Test
+    void testCollect(){
+        CollectRep.MetricsData.Builder builder = 
CollectRep.MetricsData.newBuilder();
+        NtpProtocol telnetProtocol = NtpProtocol.builder()
+                .host("192.168.77.100")
+                .port("1234")
+                .timeout("10")
+                .build();
+
+        NtpV3Packet packet = new NtpV3Impl();
+        int version = 1;
+        packet.setVersion(version);
+        packet.setOriginateTimeStamp(new TimeStamp(3000));
+        packet.setReceiveTimeStamp(new TimeStamp(2000));
+        packet.setTransmitTime(new TimeStamp(1000));
+        TimeInfo timeInfo = new TimeInfo(packet, 1000, false);;
+
+        MockedConstruction<NTPUDPClient> mocked =
+                Mockito.mockConstruction(NTPUDPClient.class, (client, context) 
-> {
+                    Mockito.doNothing().when(client).open();
+                    
Mockito.when(client.getTime(InetAddress.getByName(telnetProtocol.getHost()))).thenReturn(timeInfo);
+
+                });
+
+        List<String> aliasField = new ArrayList<>();
+        aliasField.add("responseTime");
+        aliasField.add("version");
+        Metrics metrics = new Metrics();
+        metrics.setNtp(telnetProtocol);
+        metrics.setAliasFields(aliasField);
+        ntpCollect.collect(builder, 1L, "test", metrics);
+        assertEquals(builder.getValuesCount(), 1);
+        for (CollectRep.ValueRow valueRow : builder.getValuesList()) {
+            assertNotNull(valueRow.getColumns(0));
+            assertEquals(valueRow.getColumns(1), String.valueOf(version));
+        }
+
+        mocked.close();
+    }
+
+
+}
\ No newline at end of file
diff --git 
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/telnet/TelnetCollectImplTest.java
 
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/telnet/TelnetCollectImplTest.java
index db08dfc53..35e3327b0 100644
--- 
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/telnet/TelnetCollectImplTest.java
+++ 
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/telnet/TelnetCollectImplTest.java
@@ -19,12 +19,9 @@ package org.apache.hertzbeat.collector.collect.telnet;
 
 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.ByteArrayInputStream;
-import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.net.ConnectException;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to