Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
lixiaobaivv committed Sep 5, 2024
2 parents 9270888 + b8a2e86 commit 5a25656
Show file tree
Hide file tree
Showing 21 changed files with 1,025 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand All @@ -46,6 +50,33 @@ public void setup() {
.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();
Expand All @@ -57,5 +88,24 @@ public void testCollect() {
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,55 @@

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;

/**
* 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());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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);
});
}
}
Loading

0 comments on commit 5a25656

Please sign in to comment.