This is an automated email from the ASF dual-hosted git repository.
shown 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 4b7178af0 test: add more unit tests for the collects (#2671)
4b7178af0 is described below
commit 4b7178af02fdffdea287258328adbadd0895d049
Author: Rick <[email protected]>
AuthorDate: Wed Sep 4 19:42:22 2024 +0800
test: add more unit tests for the collects (#2671)
Co-authored-by: rick <[email protected]>
Co-authored-by: aias00 <[email protected]>
Co-authored-by: shown <[email protected]>
---
.../collector/collect/push/PushCollectImpl.java | 3 +
.../collect/script/ScriptCollectImpl.java | 2 +-
.../collector/collect/dns/DnsCollectImplTest.java | 54 +++++++-
.../collector/collect/jmx/JmxCollectImplTest.java | 39 +++++-
.../collect/mq/RocketmqSingleCollectTest.java | 100 ++++++++++++++
.../collector/collect/mqtt/MqttCollectTest.java | 126 ++++++++++++++++++
.../nebulagraph/NebulaGraphCollectImplTest.java | 103 +++++++++++++++
.../collect/nebulagraph/NgqlCollectImplTest.java | 7 +
.../collect/nginx/NginxCollectImplTest.java | 25 ++++
.../collect/pop3/Pop3CollectImplTest.java | 99 ++++++++++++++
.../collect/push/PushCollectImplTest.java | 72 ++++++++++
.../collect/redfish/RedfishCollectImplTest.java | 21 +++
.../collect/script/ScriptCollectImplTest.java | 147 +++++++++++++++++++++
.../collect/smtp/SmtpCollectImplTest.java | 80 +++++++++++
.../collect/snmp/SnmpCollectImplTest.java | 74 ++++++++++-
.../collector/collect/ssh/SshCollectImplTest.java | 43 +++++-
.../collect/telnet/TelnetCollectImplTest.java | 28 ++++
.../collector/collect/udp/UdpCollectImplTest.java | 19 +++
18 files changed, 1023 insertions(+), 19 deletions(-)
diff --git
a/collector/src/main/java/org/apache/hertzbeat/collector/collect/push/PushCollectImpl.java
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/push/PushCollectImpl.java
index e4412a0da..2be1823c3 100644
---
a/collector/src/main/java/org/apache/hertzbeat/collector/collect/push/PushCollectImpl.java
+++
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/push/PushCollectImpl.java
@@ -68,6 +68,9 @@ public class PushCollectImpl extends AbstractCollect {
@Override
public void preCheck(Metrics metrics) throws IllegalArgumentException {
+ if (metrics == null || metrics.getPush() == null) {
+ throw new IllegalArgumentException("Push collect must has Push
params");
+ }
}
@Override
diff --git
a/collector/src/main/java/org/apache/hertzbeat/collector/collect/script/ScriptCollectImpl.java
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/script/ScriptCollectImpl.java
index 6ec696dd7..45d8fb665 100644
---
a/collector/src/main/java/org/apache/hertzbeat/collector/collect/script/ScriptCollectImpl.java
+++
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/script/ScriptCollectImpl.java
@@ -59,8 +59,8 @@ public class ScriptCollectImpl extends AbstractCollect {
@Override
public void preCheck(Metrics metrics) throws IllegalArgumentException {
- ScriptProtocol scriptProtocol = metrics.getScript();
Assert.notNull(metrics, "Script collect must has Imap params");
+ ScriptProtocol scriptProtocol = metrics.getScript();
Assert.notNull(scriptProtocol, "Script collect must has Imap params");
Assert.notNull(scriptProtocol.getCharset(), "Script charset is
required");
Assert.notNull(scriptProtocol.getParseType(), "Script parse type is
required");
diff --git
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/dns/DnsCollectImplTest.java
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/dns/DnsCollectImplTest.java
index e362c1890..51f45579a 100644
---
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/dns/DnsCollectImplTest.java
+++
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/dns/DnsCollectImplTest.java
@@ -17,16 +17,20 @@
package org.apache.hertzbeat.collector.collect.dns;
-
+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.util.Collections;
+
+import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
import org.apache.hertzbeat.common.entity.job.Metrics;
import org.apache.hertzbeat.common.entity.job.protocol.DnsProtocol;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-
/**
* Test case for {@link DnsCollectImpl}
*/
@@ -46,6 +50,33 @@ public class DnsCollectImplTest {
.build();
}
+ @Test
+ public void testPreCheck() {
+ assertThrows(IllegalArgumentException.class, () -> {
+ Metrics metrics = new Metrics();
+ metrics.setName("question");
+ metrics.setDns(dnsProtocol);
+ dnsCollect.preCheck(metrics);
+ });
+
+ //query class is blank
+ assertThrows(IllegalArgumentException.class, () -> {
+ DnsProtocol dns = DnsProtocol.builder().build();
+
+ Metrics metrics = new Metrics();
+ metrics.setDns(dns);
+ dnsCollect.preCheck(metrics);
+ });
+
+ // no exception throws
+ assertDoesNotThrow(() -> {
+ dnsProtocol.setTcp("tcp");
+ Metrics metrics = new Metrics();
+ metrics.setDns(dnsProtocol);
+ dnsCollect.preCheck(metrics);
+ });
+ }
+
@Test
public void testCollect() {
CollectRep.MetricsData.Builder builder =
CollectRep.MetricsData.newBuilder();
@@ -57,5 +88,24 @@ public class DnsCollectImplTest {
metrics.setAliasFields(Collections.singletonList("section"));
dnsCollect.collect(builder, monitorId, app, metrics);
assertNotNull(builder.getValues(0).getColumns(0));
+
+ // dns is null, no exception throws
+ assertDoesNotThrow(() -> {
+ dnsCollect.collect(builder, monitorId, app, null);
+ });
+
+ // metric name is header
+ assertDoesNotThrow(() -> {
+ Metrics metrics1 = new Metrics();
+ metrics1.setName("header");
+ metrics1.setDns(dnsProtocol);
+ metrics1.setAliasFields(Collections.singletonList("section"));
+ dnsCollect.collect(builder, monitorId, app, metrics1);
+ });
+ }
+
+ @Test
+ public void testSupportProtocol() {
+ assertEquals(DispatchConstants.PROTOCOL_DNS,
dnsCollect.supportProtocol());
}
}
diff --git
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImplTest.java
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImplTest.java
index 5d2fc93d1..9e9dabab6 100644
---
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImplTest.java
+++
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImplTest.java
@@ -17,7 +17,13 @@
package org.apache.hertzbeat.collector.collect.jmx;
-import org.junit.jupiter.api.AfterEach;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
+import org.apache.hertzbeat.common.entity.job.Metrics;
+import org.apache.hertzbeat.common.entity.job.protocol.JmxProtocol;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -25,20 +31,41 @@ import org.junit.jupiter.api.Test;
* Test case for {@link JmxCollectImpl}
*/
class JmxCollectImplTest {
+ private JmxCollectImpl jmxCollect;
@BeforeEach
- void setUp() {
+ void setUp() throws Exception {
+ jmxCollect = new JmxCollectImpl();
}
- @AfterEach
- void tearDown() {
+ @Test
+ void preCheck() throws IllegalArgumentException {
+ // metrics is null, will throw exception
+ assertThrows(IllegalArgumentException.class, () -> {
+ jmxCollect.preCheck(null);
+ });
+
+ // should not contain /stub/
+ assertThrows(IllegalArgumentException.class, () -> {
+ JmxProtocol jmx = JmxProtocol.builder().build();
+ jmx.setUrl("/stub/");
+ Metrics metrics = Metrics.builder().jmx(jmx).build();
+
+ jmxCollect.preCheck(metrics);
+ });
}
@Test
- void getInstance() {
+ void collect() {
+ // metrics is null
+ assertDoesNotThrow(() -> {
+ CollectRep.MetricsData.Builder builder =
CollectRep.MetricsData.newBuilder();
+ jmxCollect.collect(builder, 1L, "app", null);
+ });
}
@Test
- void collect() {
+ void supportProtocol() {
+ assert
DispatchConstants.PROTOCOL_JMX.equals(jmxCollect.supportProtocol());
}
}
diff --git
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/mq/RocketmqSingleCollectTest.java
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/mq/RocketmqSingleCollectTest.java
new file mode 100644
index 000000000..be10a22a9
--- /dev/null
+++
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/mq/RocketmqSingleCollectTest.java
@@ -0,0 +1,100 @@
+/*
+ * 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.mq;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
+import org.apache.hertzbeat.common.entity.job.Metrics;
+import org.apache.hertzbeat.common.entity.job.protocol.RocketmqProtocol;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test case for {@link RocketmqSingleCollectImpl}
+ */
+public class RocketmqSingleCollectTest {
+ private RocketmqSingleCollectImpl collect;
+
+ @BeforeEach
+ public void setUp() throws Exception {
+ collect = new RocketmqSingleCollectImpl();
+ }
+
+ @Test
+ void preCheck() {
+ // metrics is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ collect.preCheck(null);
+ });
+
+ // rocketmq is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ collect.preCheck(Metrics.builder().build());
+ });
+
+ // rocketmq srv host is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ RocketmqProtocol mq = new RocketmqProtocol();
+ collect.preCheck(Metrics.builder().rocketmq(mq).build());
+ });
+
+ // rocketmq srv port is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ RocketmqProtocol mq =
RocketmqProtocol.builder().namesrvHost("127.0.0.1").build();
+ collect.preCheck(Metrics.builder().rocketmq(mq).build());
+ });
+
+ // no exception throw
+ assertDoesNotThrow(() -> {
+ RocketmqProtocol mq =
RocketmqProtocol.builder().namesrvHost("127.0.0.1").namesrvPort("9876").build();
+ collect.preCheck(Metrics.builder().rocketmq(mq).build());
+ });
+ }
+
+ @Test
+ void destroy() {
+ assertDoesNotThrow(() -> {
+ collect.destroy();
+ });
+ }
+
+ @Test
+ void collect() {
+ // metrics is null
+ assertDoesNotThrow(() -> {
+ CollectRep.MetricsData.Builder builder =
CollectRep.MetricsData.newBuilder();
+ collect.collect(builder, 1L, "app", null);
+ });
+
+ assertDoesNotThrow(() -> {
+ CollectRep.MetricsData.Builder builder =
CollectRep.MetricsData.newBuilder();
+ RocketmqProtocol mq =
RocketmqProtocol.builder().namesrvHost("127.0.0.1").namesrvPort("9876").build();
+ Metrics metrics = Metrics.builder().rocketmq(mq).build();
+ collect.collect(builder, 1L, "app", metrics);
+ });
+ }
+
+ @Test
+ void supportProtocol() {
+ assertEquals(DispatchConstants.PROTOCOL_ROCKETMQ,
collect.supportProtocol());
+ }
+}
diff --git
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/mqtt/MqttCollectTest.java
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/mqtt/MqttCollectTest.java
new file mode 100644
index 000000000..c36b508e3
--- /dev/null
+++
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/mqtt/MqttCollectTest.java
@@ -0,0 +1,126 @@
+/*
+ * 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.mqtt;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.ArrayList;
+
+import org.apache.hertzbeat.collector.collect.mq.RocketmqSingleCollectImpl;
+import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
+import org.apache.hertzbeat.common.entity.job.Metrics;
+import org.apache.hertzbeat.common.entity.job.protocol.MqttProtocol;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import com.hivemq.client.mqtt.MqttVersion;
+
+/**
+ * Test case for {@link RocketmqSingleCollectImpl}
+ */
+public class MqttCollectTest {
+ private MqttCollectImpl mqttCollect;
+ private Metrics metrics;
+ private CollectRep.MetricsData.Builder builder;
+
+ @BeforeEach
+ public void setup() {
+ mqttCollect = new MqttCollectImpl();
+ MqttProtocol mqtt = MqttProtocol.builder().build();
+ metrics = Metrics.builder()
+ .mqtt(mqtt)
+ .build();
+ builder = CollectRep.MetricsData.newBuilder();
+ }
+
+ @Test
+ void preCheck() {
+ // host is empty
+ assertThrows(IllegalArgumentException.class, () -> {
+ mqttCollect.preCheck(metrics);
+ });
+
+ // port is empty
+ assertThrows(IllegalArgumentException.class, () -> {
+ MqttProtocol mqtt = MqttProtocol.builder().build();
+ mqtt.setHost("example.com");
+ metrics.setMqtt(mqtt);
+ mqttCollect.preCheck(metrics);
+ });
+
+ // protocol version is empty
+ assertThrows(IllegalArgumentException.class, () -> {
+ MqttProtocol mqtt = MqttProtocol.builder().build();
+ mqtt.setHost("example.com");
+ mqtt.setPort("1883");
+ metrics.setMqtt(mqtt);
+ mqttCollect.preCheck(metrics);
+ });
+
+ // everything is ok
+ assertDoesNotThrow(() -> {
+ MqttProtocol mqtt = MqttProtocol.builder().build();
+ mqtt.setHost("example.com");
+ mqtt.setPort("1883");
+ metrics.setMqtt(mqtt);
+ mqtt.setProtocolVersion("3.1.1");
+ mqttCollect.preCheck(metrics);
+ });
+ }
+
+ @Test
+ void supportProtocol() {
+ assertEquals(DispatchConstants.PROTOCOL_MQTT,
mqttCollect.supportProtocol());
+ }
+
+ @Test
+ void collect() {
+ // with version 3.1.1
+ assertDoesNotThrow(() -> {
+ MqttProtocol mqtt = MqttProtocol.builder().build();
+ mqtt.setHost("example.com");
+ mqtt.setPort("1883");
+ mqtt.setClientId("clientid");
+ mqtt.setTimeout("1");
+ mqtt.setProtocolVersion(MqttVersion.MQTT_3_1_1.name());
+
+ metrics.setMqtt(mqtt);
+ metrics.setAliasFields(new ArrayList<>());
+
+ mqttCollect.collect(builder, 1L, "app", metrics);
+ });
+
+
+ assertDoesNotThrow(() -> {
+ MqttProtocol mqtt = MqttProtocol.builder().build();
+ mqtt.setHost("example.com");
+ mqtt.setPort("1883");
+ mqtt.setClientId("clientid");
+ mqtt.setTimeout("1");
+ mqtt.setProtocolVersion(MqttVersion.MQTT_5_0.name());
+
+ metrics.setMqtt(mqtt);
+ metrics.setAliasFields(new ArrayList<>());
+
+ mqttCollect.collect(builder, 1L, "app", metrics);
+ });
+ }
+}
diff --git
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/nebulagraph/NebulaGraphCollectImplTest.java
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/nebulagraph/NebulaGraphCollectImplTest.java
new file mode 100644
index 000000000..72600503c
--- /dev/null
+++
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/nebulagraph/NebulaGraphCollectImplTest.java
@@ -0,0 +1,103 @@
+/*
+ * 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.nebulagraph;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
+import org.apache.hertzbeat.common.entity.job.Metrics;
+import org.apache.hertzbeat.common.entity.job.protocol.NebulaGraphProtocol;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test case for {@link NebulaGraphCollectImpl}
+ */
+public class NebulaGraphCollectImplTest {
+ private NebulaGraphCollectImpl nebulaGraphCollect;
+ private CollectRep.MetricsData.Builder builder;
+
+ @BeforeEach
+ void setUp() {
+ nebulaGraphCollect = new NebulaGraphCollectImpl();
+ builder = CollectRep.MetricsData.newBuilder();
+ }
+
+ @Test
+ void preCheck() {
+ // metrics is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ nebulaGraphCollect.preCheck(null);
+ });
+
+ // nebulaGraph is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ Metrics metrics = Metrics.builder().build();
+ nebulaGraphCollect.preCheck(metrics);
+ });
+
+ // everything is ok
+ assertDoesNotThrow(() -> {
+ Metrics metrics =
Metrics.builder().nebulaGraph(NebulaGraphProtocol.builder().build()).build();
+ nebulaGraphCollect.preCheck(metrics);
+ });
+ }
+
+ @Test
+ void collect() {
+ String[] validTimePeriods = new String[]{"5", "60", "600", "3600"};
+
+ // valid time period without host
+ for (String validTimePeriod : validTimePeriods) {
+ builder = CollectRep.MetricsData.newBuilder();
+ Metrics metrics =
Metrics.builder().nebulaGraph(NebulaGraphProtocol.builder().timePeriod(validTimePeriod).build()).build();
+ nebulaGraphCollect.collect(builder, 0, null, metrics);
+ assertEquals(CollectRep.Code.FAIL, builder.getCode());
+ }
+
+ // invalid time period
+ assertDoesNotThrow(() -> {
+ builder = CollectRep.MetricsData.newBuilder();
+ Metrics metrics =
Metrics.builder().nebulaGraph(NebulaGraphProtocol.builder().timePeriod("invalid").build()).build();
+ nebulaGraphCollect.collect(builder, 0, null, metrics);
+ assertEquals(CollectRep.Code.FAIL, builder.getCode());
+ });
+
+ assertDoesNotThrow(() -> {
+ // valid time period and host
+ builder = CollectRep.MetricsData.newBuilder();
+ Metrics metrics =
Metrics.builder().nebulaGraph(NebulaGraphProtocol.builder()
+ .timePeriod("5")
+ .host("localhost")
+ .port("9090")
+ .url("example.com")
+ .timeout("1")
+ .build()).build();
+ nebulaGraphCollect.collect(builder, 0, null, metrics);
+ assertEquals(CollectRep.Code.FAIL, builder.getCode());
+ });
+ }
+
+ @Test
+ void supportProtocol() {
+ assertEquals(DispatchConstants.PROTOCOL_NEBULAGRAPH,
nebulaGraphCollect.supportProtocol());
+ }
+}
diff --git
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/nebulagraph/NgqlCollectImplTest.java
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/nebulagraph/NgqlCollectImplTest.java
index 98c2c978e..57754b80e 100644
---
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/nebulagraph/NgqlCollectImplTest.java
+++
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/nebulagraph/NgqlCollectImplTest.java
@@ -18,6 +18,7 @@
package org.apache.hertzbeat.collector.collect.nebulagraph;
import static org.junit.jupiter.api.Assertions.assertEquals;
+
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -25,6 +26,8 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+
+import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
import org.apache.hertzbeat.common.entity.job.Metrics;
import org.apache.hertzbeat.common.entity.job.protocol.NgqlProtocol;
import org.apache.hertzbeat.common.entity.message.CollectRep;
@@ -197,4 +200,8 @@ class NgqlCollectImplTest {
mocked.close();
}
+ @Test
+ void supportProtocol() {
+ assertEquals(DispatchConstants.PROTOCOL_NGQL,
ngqlCollect.supportProtocol());
+ }
}
diff --git
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImplTest.java
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImplTest.java
index fb0501123..472af7109 100644
---
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImplTest.java
+++
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImplTest.java
@@ -19,6 +19,8 @@ package org.apache.hertzbeat.collector.collect.nginx;
import static
org.apache.hertzbeat.common.constants.CommonConstants.TYPE_STRING;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -28,6 +30,7 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.hertzbeat.collector.collect.common.http.CommonHttpClient;
+import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
import org.apache.hertzbeat.common.entity.job.Metrics;
import org.apache.hertzbeat.common.entity.job.protocol.NginxProtocol;
import org.apache.hertzbeat.common.entity.message.CollectRep;
@@ -70,6 +73,23 @@ public class NginxCollectImplTest {
public void setup() {
}
+ @Test
+ void preCheck() {
+ // metrics is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ nginxCollect.preCheck(null);
+ });
+
+ // nginx protocol is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ nginxCollect.preCheck(Metrics.builder().build());
+ });
+
+ // nginx protocol is invalid
+ assertThrows(IllegalArgumentException.class, () -> {
+
nginxCollect.preCheck(Metrics.builder().nginx(NginxProtocol.builder().build()).build());
+ });
+ }
@Test
public void testNginxCollectFail() throws IOException {
@@ -240,6 +260,11 @@ public class NginxCollectImplTest {
}
}
+ @Test
+ void supportProtocol() {
+ assertEquals(DispatchConstants.PROTOCOL_NGINX,
nginxCollect.supportProtocol());
+ }
+
static class CustomHttpEntity implements HttpEntity {
private final String content;
diff --git
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/pop3/Pop3CollectImplTest.java
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/pop3/Pop3CollectImplTest.java
new file mode 100644
index 000000000..6aed91c63
--- /dev/null
+++
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/pop3/Pop3CollectImplTest.java
@@ -0,0 +1,99 @@
+/*
+ * 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.pop3;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
+import org.apache.hertzbeat.common.entity.job.Metrics;
+import org.apache.hertzbeat.common.entity.job.protocol.Pop3Protocol;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test case for {@link Pop3CollectImpl}
+ */
+public class Pop3CollectImplTest {
+ private Pop3CollectImpl pop3Collect;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ pop3Collect = new Pop3CollectImpl();
+ }
+
+ @Test
+ void preCheck() throws Exception {
+ // metrics is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ pop3Collect.preCheck(null);
+ });
+
+ // protocol is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ Metrics metrics = Metrics.builder().build();
+ pop3Collect.preCheck(metrics);
+ });
+
+ // protocol is invalid
+ assertThrows(IllegalArgumentException.class, () -> {
+ Pop3Protocol pop3 = Pop3Protocol.builder().build();
+ Metrics metrics = Metrics.builder().pop3(pop3).build();
+ pop3Collect.preCheck(metrics);
+ });
+
+ // everything is ok
+ assertDoesNotThrow(() -> {
+ Pop3Protocol pop3 = Pop3Protocol.builder()
+ .host("localhost")
+ .port("110")
+ .timeout("1")
+ .ssl("true")
+ .email("[email protected]")
+ .authorize("auth")
+ .build();
+ Metrics metrics = Metrics.builder().pop3(pop3).build();
+ pop3Collect.preCheck(metrics);
+ });
+ }
+
+ @Test
+ void collect() throws Exception {
+ Pop3Protocol pop3 = Pop3Protocol.builder()
+ .host("localhost")
+ .port("110")
+ .timeout("1")
+ .ssl("true")
+ .email("[email protected]")
+ .authorize("auth")
+ .build();
+ Metrics metrics = Metrics.builder().pop3(pop3).build();
+
+ assertDoesNotThrow(() -> {
+ CollectRep.MetricsData.Builder builder =
CollectRep.MetricsData.newBuilder();
+ pop3Collect.collect(builder, 1, "app", metrics);
+ });
+ }
+
+ @Test
+ void supportProtocol() {
+ assertEquals(DispatchConstants.PROTOCOL_POP3,
pop3Collect.supportProtocol());
+ }
+}
diff --git
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/push/PushCollectImplTest.java
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/push/PushCollectImplTest.java
new file mode 100644
index 000000000..e37c574e9
--- /dev/null
+++
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/push/PushCollectImplTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.push;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
+import org.apache.hertzbeat.common.entity.job.Metrics;
+import org.apache.hertzbeat.common.entity.job.protocol.PushProtocol;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test case for {@link PushCollectImpl}
+ */
+public class PushCollectImplTest {
+ private PushCollectImpl pushCollect;
+ private PushProtocol push;
+ private CollectRep.MetricsData.Builder builder;
+
+ @BeforeEach
+ public void setup() {
+ pushCollect = new PushCollectImpl();
+ push =
PushProtocol.builder().uri("/metrics").host("example.com").port("60").build();
+ builder = CollectRep.MetricsData.newBuilder();
+ }
+
+ @Test
+ void preCheck() throws Exception {
+ // metrics is null
+ assertThrows(IllegalArgumentException.class, () ->
pushCollect.preCheck(null));
+
+ // protocol is null
+ assertThrows(IllegalArgumentException.class, () ->
pushCollect.preCheck(new Metrics()));
+
+ // everyting is ok
+ assertDoesNotThrow(() -> {
+ pushCollect.preCheck(Metrics.builder().push(push).build());
+ });
+ }
+
+ @Test
+ void collect() throws Exception {
+ assertDoesNotThrow(() -> {
+ pushCollect.collect(builder, 1L, "app",
Metrics.builder().push(push).build());
+ assertEquals(CollectRep.Code.FAIL, builder.getCode());
+ });
+ }
+
+ @Test
+ void supportProtocol() {
+ assertEquals(DispatchConstants.PROTOCOL_PUSH,
pushCollect.supportProtocol());
+ }
+}
diff --git
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/redfish/RedfishCollectImplTest.java
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/redfish/RedfishCollectImplTest.java
index 3fa01f78b..3096be857 100644
---
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/redfish/RedfishCollectImplTest.java
+++
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/redfish/RedfishCollectImplTest.java
@@ -18,8 +18,12 @@
package org.apache.hertzbeat.collector.collect.redfish;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
import java.util.ArrayList;
import java.util.List;
+
+import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
import org.apache.hertzbeat.common.entity.job.Metrics;
import org.apache.hertzbeat.common.entity.job.protocol.RedfishProtocol;
import org.apache.hertzbeat.common.entity.message.CollectRep;
@@ -171,4 +175,21 @@ public class RedfishCollectImplTest {
assertEquals("/redfish/v1/Chassis/2U/PowerSubsystem/PowerSupplies/Bay1",
builder.getValues(2).getColumns(0));
assertEquals("/redfish/v1/Chassis/2U/PowerSubsystem/PowerSupplies/Bay2",
builder.getValues(3).getColumns(0));
}
+
+ @Test
+ void preCheck() throws Exception {
+ // metrics is null
+ assertThrows(IllegalArgumentException.class, () ->
redfishCollect.preCheck(null));
+
+ // protocol is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ Metrics metrics = Metrics.builder().build();
+ redfishCollect.preCheck(metrics);
+ });
+ }
+
+ @Test
+ void supportProtocol() {
+ assertEquals(DispatchConstants.PROTOCOL_REDFISH,
redfishCollect.supportProtocol());
+ }
}
diff --git
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/script/ScriptCollectImplTest.java
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/script/ScriptCollectImplTest.java
new file mode 100644
index 000000000..46f9a4316
--- /dev/null
+++
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/script/ScriptCollectImplTest.java
@@ -0,0 +1,147 @@
+/*
+ * 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.script;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
+import org.apache.hertzbeat.common.entity.job.Metrics;
+import org.apache.hertzbeat.common.entity.job.protocol.ScriptProtocol;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test case for {@link ScriptCollectImpl}
+ */
+public class ScriptCollectImplTest {
+ private ScriptCollectImpl scriptCollect;
+ private CollectRep.MetricsData.Builder builder;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ scriptCollect = new ScriptCollectImpl();
+ builder = CollectRep.MetricsData.newBuilder();
+ }
+
+ @Test
+ void preCheck() {
+ // metrics is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ scriptCollect.preCheck(null);
+ });
+
+ // protocol is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ Metrics metrics = new Metrics();
+ scriptCollect.preCheck(metrics);
+ });
+
+ // charset is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ ScriptProtocol scriptProtocol = ScriptProtocol.builder().build();
+ Metrics metrics = new Metrics();
+ metrics.setScript(scriptProtocol);
+ scriptCollect.preCheck(metrics);
+ });
+
+ // parse type is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ ScriptProtocol scriptProtocol =
ScriptProtocol.builder().charset("utf-8").build();
+ Metrics metrics = new Metrics();
+ metrics.setScript(scriptProtocol);
+ scriptCollect.preCheck(metrics);
+ });
+
+ // script tool is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ ScriptProtocol scriptProtocol =
ScriptProtocol.builder().charset("utf-8").parseType("json").build();
+ Metrics metrics = new Metrics();
+ metrics.setScript(scriptProtocol);
+ scriptCollect.preCheck(metrics);
+ });
+
+ // script command is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ ScriptProtocol scriptProtocol =
ScriptProtocol.builder().charset("utf-8").parseType("json").scriptTool("sh").build();
+ Metrics metrics = new Metrics();
+ metrics.setScript(scriptProtocol);
+ scriptCollect.preCheck(metrics);
+ });
+
+ // script path is null
+ assertDoesNotThrow(() -> {
+ ScriptProtocol scriptProtocol =
ScriptProtocol.builder().charset("utf-8").parseType("json").scriptTool("sh").scriptCommand("ls").build();
+ Metrics metrics = new Metrics();
+ metrics.setScript(scriptProtocol);
+ scriptCollect.preCheck(metrics);
+ });
+
+ // everything is ok
+ assertDoesNotThrow(() -> {
+ ScriptProtocol scriptProtocol =
ScriptProtocol.builder().charset("utf-8").parseType("json").scriptTool("sh").scriptPath("/tmp").build();
+ Metrics metrics = new Metrics();
+ metrics.setScript(scriptProtocol);
+ scriptCollect.preCheck(metrics);
+ });
+ }
+
+ @Test
+ void collect() {
+ // not support script tool with command
+ assertDoesNotThrow(() -> {
+ ScriptProtocol scriptProtocol =
ScriptProtocol.builder().charset("utf-8").parseType("json").scriptTool("sh").scriptCommand("cmd").build();
+ Metrics metrics = new Metrics();
+ metrics.setScript(scriptProtocol);
+
+ builder = CollectRep.MetricsData.newBuilder();
+ scriptCollect.collect(builder, 0, "app", metrics);
+ assertEquals(CollectRep.Code.FAIL, builder.getCode());
+ });
+
+
+ // not support script tool with scriptpath
+ assertDoesNotThrow(() -> {
+ ScriptProtocol scriptProtocol =
ScriptProtocol.builder().charset("utf-8").parseType("json").scriptTool("sh").scriptPath("cmd").build();
+ Metrics metrics = new Metrics();
+ metrics.setScript(scriptProtocol);
+
+ builder = CollectRep.MetricsData.newBuilder();
+ scriptCollect.collect(builder, 0, "app", metrics);
+ assertEquals(CollectRep.Code.FAIL, builder.getCode());
+ });
+
+ // without script command and path
+ assertDoesNotThrow(() -> {
+ ScriptProtocol scriptProtocol =
ScriptProtocol.builder().charset("utf-8").parseType("json").build();
+ Metrics metrics = new Metrics();
+ metrics.setScript(scriptProtocol);
+
+ builder = CollectRep.MetricsData.newBuilder();
+ scriptCollect.collect(builder, 0, "app", metrics);
+ assertEquals(CollectRep.Code.FAIL, builder.getCode());
+ });
+ }
+
+ @Test
+ void supportProtocol() {
+ assertEquals(DispatchConstants.PROTOCOL_SCRIPT,
scriptCollect.supportProtocol());
+ }
+}
diff --git
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/smtp/SmtpCollectImplTest.java
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/smtp/SmtpCollectImplTest.java
new file mode 100644
index 000000000..b249578cb
--- /dev/null
+++
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/smtp/SmtpCollectImplTest.java
@@ -0,0 +1,80 @@
+/*
+ * 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.smtp;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
+import org.apache.hertzbeat.common.entity.job.Metrics;
+import org.apache.hertzbeat.common.entity.job.protocol.SmtpProtocol;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test case for {@link SmtpCollectImpl}
+ */
+public class SmtpCollectImplTest {
+ private SmtpCollectImpl smtpCollect;
+ private CollectRep.MetricsData.Builder builder;
+
+ @BeforeEach
+ void setup() {
+ smtpCollect = new SmtpCollectImpl();
+ builder = CollectRep.MetricsData.newBuilder();
+ }
+
+ @Test
+ void preCheck() {
+ // metrics is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ smtpCollect.preCheck(null);
+ });
+
+ // stmp protocol is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ smtpCollect.preCheck(new Metrics());
+ });
+
+ // everthing is ok
+ assertDoesNotThrow(() -> {
+ Metrics metrics = Metrics.builder()
+ .smtp(new SmtpProtocol())
+ .build();
+ smtpCollect.preCheck(metrics);
+ });
+ }
+
+ @Test
+ void collect() {
+ assertDoesNotThrow(() -> {
+ Metrics metrics = Metrics.builder()
+ .smtp(new SmtpProtocol())
+ .build();
+ smtpCollect.collect(builder, 0, null, metrics);
+ assertEquals(CollectRep.Code.FAIL, builder.getCode());
+ });
+ }
+
+ @Test
+ void supportProtocol() {
+ assertEquals(DispatchConstants.PROTOCOL_SMTP,
smtpCollect.supportProtocol());
+ }
+}
diff --git
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/snmp/SnmpCollectImplTest.java
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/snmp/SnmpCollectImplTest.java
index faa29a64b..3ae3c0f1b 100644
---
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/snmp/SnmpCollectImplTest.java
+++
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/snmp/SnmpCollectImplTest.java
@@ -17,7 +17,14 @@
package org.apache.hertzbeat.collector.collect.snmp;
-import org.junit.jupiter.api.AfterEach;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
+import org.apache.hertzbeat.common.entity.job.Metrics;
+import org.apache.hertzbeat.common.entity.job.protocol.SnmpProtocol;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -25,20 +32,77 @@ import org.junit.jupiter.api.Test;
* Test case for {@link SnmpCollectImpl}
*/
class SnmpCollectImplTest {
+ private SnmpCollectImpl snmpCollect;
+ private Metrics metrics;
+ private CollectRep.MetricsData.Builder builder;
@BeforeEach
void setUp() {
+ snmpCollect = new SnmpCollectImpl();
+ metrics = new Metrics();
+ SnmpProtocol snmap = new SnmpProtocol();
+ snmap.setHost("127.0.0.1");
+ snmap.setPort("161");
+ snmap.setVersion("2c");
+ metrics.setSnmp(snmap);
+ builder = CollectRep.MetricsData.newBuilder();
}
- @AfterEach
- void tearDown() {
+ @Test
+ void preCheck() {
+ // metrics is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ snmpCollect.preCheck(null);
+ });
+
+ // snmp protocol is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ Metrics metrics = new Metrics();
+ snmpCollect.preCheck(metrics);
+ });
+
+ // snmp host is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ Metrics metrics = new Metrics();
+ metrics.setSnmp(new SnmpProtocol());
+ snmpCollect.preCheck(metrics);
+ });
+
+ // snmp port is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ SnmpProtocol snmap = new SnmpProtocol();
+ snmap.setHost("127.0.0.1");
+ Metrics metrics = new Metrics();
+ metrics.setSnmp(snmap);
+ snmpCollect.preCheck(metrics);
+ });
+
+ // snmp version is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ SnmpProtocol snmap = new SnmpProtocol();
+ snmap.setHost("127.0.0.1");
+ snmap.setPort("161");
+ Metrics metrics = new Metrics();
+ metrics.setSnmp(snmap);
+ snmpCollect.preCheck(metrics);
+ });
+
+ // everything is ok
+ assertDoesNotThrow(() -> {
+ snmpCollect.preCheck(metrics);
+ });
}
@Test
- void getInstance() {
+ void collect() {
+ assertDoesNotThrow(() -> {
+ snmpCollect.collect(builder, 0, null, metrics);
+ assertEquals(CollectRep.Code.FAIL, builder.getCode());
+ });
}
@Test
- void collect() {
+ void supportProtocol() {
+ assertEquals(DispatchConstants.PROTOCOL_SNMP,
snmpCollect.supportProtocol());
}
}
diff --git
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/ssh/SshCollectImplTest.java
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/ssh/SshCollectImplTest.java
index 4d34555d4..165bcae08 100644
---
a/collector/src/test/java/org/apache/hertzbeat/collector/collect/ssh/SshCollectImplTest.java
+++
b/collector/src/test/java/org/apache/hertzbeat/collector/collect/ssh/SshCollectImplTest.java
@@ -17,7 +17,14 @@
package org.apache.hertzbeat.collector.collect.ssh;
-import org.junit.jupiter.api.AfterEach;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
+import org.apache.hertzbeat.common.entity.job.Metrics;
+import org.apache.hertzbeat.common.entity.job.protocol.SshProtocol;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -25,20 +32,46 @@ import org.junit.jupiter.api.Test;
* Test case for {@link SshCollectImpl}
*/
class SshCollectImplTest {
+ private SshCollectImpl sshCollect;
+ private CollectRep.MetricsData.Builder builder;
@BeforeEach
void setUp() {
+ sshCollect = new SshCollectImpl();
+ builder = CollectRep.MetricsData.newBuilder();
}
- @AfterEach
- void tearDown() {
+ @Test
+ void preCheck() {
+ // metrics is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ sshCollect.preCheck(null);
+ });
+
+ // ssh protocol is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ Metrics metrics = Metrics.builder().build();
+ sshCollect.preCheck(metrics);
+ });
+
+ // everything is ok
+ assertDoesNotThrow(() -> {
+ Metrics metrics = Metrics.builder().ssh(new SshProtocol()).build();
+ sshCollect.preCheck(metrics);
+ });
}
@Test
- void getInstance() {
+ void collect() {
+ assertDoesNotThrow(() -> {
+ Metrics metrics = Metrics.builder().ssh(new SshProtocol()).build();
+ sshCollect.collect(builder, 1L, "app", metrics);
+ assertEquals(CollectRep.Code.FAIL, builder.getCode());
+ });
}
@Test
- void collect() {
+ void supportProtocol() {
+ assertEquals(DispatchConstants.PROTOCOL_SSH,
sshCollect.supportProtocol());
}
}
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 1b4f88541..f09681792 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
@@ -17,8 +17,11 @@
package org.apache.hertzbeat.collector.collect.telnet;
+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.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
@@ -26,6 +29,7 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.net.telnet.TelnetClient;
+import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
import org.apache.hertzbeat.common.entity.job.Metrics;
import org.apache.hertzbeat.common.entity.job.protocol.TelnetProtocol;
import org.apache.hertzbeat.common.entity.message.CollectRep;
@@ -150,4 +154,28 @@ class TelnetCollectImplTest {
}
mocked.close();
}
+
+ @Test
+ void preCheck() throws IllegalArgumentException {
+ // metrics is null
+ assertThrows(IllegalArgumentException.class, () ->
telnetCollect.preCheck(null));
+
+ // protocol is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ Metrics metrics = new Metrics();
+ telnetCollect.preCheck(metrics);
+ });
+
+ // everyting is ok
+ assertDoesNotThrow(() -> {
+ Metrics metrics = new Metrics();
+ metrics.setTelnet(TelnetProtocol.builder().build());
+ telnetCollect.preCheck(metrics);
+ });
+ }
+
+ @Test
+ void supportProtocol() {
+ assertEquals(DispatchConstants.PROTOCOL_TELNET,
telnetCollect.supportProtocol());
+ }
}
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
index 652318b4e..4fd4b64b6 100644
---
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
@@ -17,6 +17,7 @@
package org.apache.hertzbeat.collector.collect.udp;
+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;
@@ -26,6 +27,8 @@ import java.net.PortUnreachableException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
+
+import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
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;
@@ -52,6 +55,17 @@ class UdpCollectImplTest {
Metrics metrics = new Metrics();
metrics.setAliasFields(aliasField);
assertThrows(IllegalArgumentException.class, () ->
udpCollect.preCheck(metrics));
+
+ // metrics is null
+ assertThrows(IllegalArgumentException.class, () -> {
+ udpCollect.preCheck(null);
+ });
+
+ // everything is ok
+ assertDoesNotThrow(() -> {
+ UdpProtocol udpProtocol =
UdpProtocol.builder().timeout("10").port("21").host("127.0.0.1").build();
+ udpCollect.preCheck(Metrics.builder().udp(udpProtocol).build());
+ });
}
@Test
@@ -136,4 +150,9 @@ class UdpCollectImplTest {
socketMockedConstruction.close();
}
+
+ @Test
+ void supportProtocol() {
+ assertEquals(DispatchConstants.PROTOCOL_UDP,
udpCollect.supportProtocol());
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]