Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve] fix code style #2034

Merged
merged 2 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

package org.apache.hertzbeat.collector.collect.common.cache;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@

package org.apache.hertzbeat.collector.collect.http.promethus.exporter;

import org.apache.hertzbeat.common.util.JsonUtil;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Map;
import org.junit.jupiter.api.Test;

/**
* Test case for {@link ExporterParser}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.hertzbeat.common.entity.job.Metrics;
import org.apache.hertzbeat.common.entity.job.protocol.MongodbProtocol;
import org.apache.hertzbeat.common.entity.message.CollectRep;
Expand Down Expand Up @@ -72,7 +72,7 @@ void setUp() {
}

@Test
void MockTest() {
void mockTest() {
CollectRep.MetricsData.Builder builder = CollectRep.MetricsData.newBuilder();
mongodbProtocol.setCommand("hostInfo.os");
Metrics metrics = new Metrics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void testPreCheck() {
@Test
void testCollect() {
CollectRep.MetricsData.Builder builder = CollectRep.MetricsData.newBuilder();
UdpProtocol ftpProtocol = UdpProtocol.builder()
UdpProtocol udpProtocol = UdpProtocol.builder()
.timeout("10")
.port("21")
.host("127.0.0.1")
Expand All @@ -73,7 +73,7 @@ void testCollect() {
List<String> aliasField = new ArrayList<>();
aliasField.add("responseTime");
Metrics metrics = new Metrics();
metrics.setUdp(ftpProtocol);
metrics.setUdp(udpProtocol);
metrics.setAliasFields(aliasField);
udpCollect.preCheck(metrics);
udpCollect.collect(builder, 1L, "test", metrics);
Expand All @@ -88,7 +88,7 @@ void testCollect() {
@Test
void testCollectWithSocketException() {
CollectRep.MetricsData.Builder builder = CollectRep.MetricsData.newBuilder();
UdpProtocol ftpProtocol = UdpProtocol.builder()
UdpProtocol udpProtocol = UdpProtocol.builder()
.timeout("10")
.port("21")
.host("127.0.0.1")
Expand All @@ -104,7 +104,7 @@ void testCollectWithSocketException() {
List<String> aliasField = new ArrayList<>();
aliasField.add("responseTime");
Metrics metrics = new Metrics();
metrics.setUdp(ftpProtocol);
metrics.setUdp(udpProtocol);
metrics.setAliasFields(aliasField);
udpCollect.preCheck(metrics);
udpCollect.collect(builder, 1L, "test", metrics);
Expand All @@ -116,7 +116,7 @@ void testCollectWithSocketException() {
@Test
void testCollectWithPortUnreachableException() {
CollectRep.MetricsData.Builder builder = CollectRep.MetricsData.newBuilder();
UdpProtocol ftpProtocol = UdpProtocol.builder()
UdpProtocol udpProtocol = UdpProtocol.builder()
.timeout("10")
.port("21")
.host("127.0.0.1")
Expand All @@ -132,7 +132,7 @@ void testCollectWithPortUnreachableException() {
List<String> aliasField = new ArrayList<>();
aliasField.add("responseTime");
Metrics metrics = new Metrics();
metrics.setUdp(ftpProtocol);
metrics.setUdp(udpProtocol);
metrics.setAliasFields(aliasField);
udpCollect.preCheck(metrics);
udpCollect.collect(builder, 1L, "test", metrics);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import java.util.Map;
import org.apache.hertzbeat.common.entity.job.Configmap;
import org.apache.hertzbeat.common.entity.job.Metrics;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@

package org.apache.hertzbeat.collector.util;

import com.jayway.jsonpath.TypeRef;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

/**
* Test case for {@link JsonPathParser}
Expand Down Expand Up @@ -80,62 +77,26 @@ class JsonPathParserTest {
@Test
void parseContentWithJsonPath() {
// process array
List<Object> tom = JsonPathParser.parseContentWithJsonPath(JSON_ARRAY,"$[0].name");
List<Object> tom = JsonPathParser.parseContentWithJsonPath(JSON_ARRAY, "$[0].name");
assertNotNull(tom);
assertEquals("tom",tom.get(0));
assertEquals("tom", tom.get(0));
// get json array map
List<Object> entry = JsonPathParser.parseContentWithJsonPath(JSON_ARRAY,"$[1]");
List<Object> entry = JsonPathParser.parseContentWithJsonPath(JSON_ARRAY, "$[1]");
assertNotNull(entry);
entry.forEach(e -> {
assertInstanceOf(Map.class, e);
assertEquals("543",((Map)e).get("speed"));
assertEquals("543", ((Map) e).get("speed"));
});
// process object
List<Object> author = JsonPathParser.parseContentWithJsonPath(JSON_OBJECT,"$.store.book[0].author");
List<Object> author = JsonPathParser.parseContentWithJsonPath(JSON_OBJECT, "$.store.book[0].author");
assertNotNull(author);
assertEquals("Nigel Rees",author.get(0));
assertEquals("Nigel Rees", author.get(0));
// get json object map
List<Object> book = JsonPathParser.parseContentWithJsonPath(JSON_OBJECT,"$.store.book[1]");
List<Object> book = JsonPathParser.parseContentWithJsonPath(JSON_OBJECT, "$.store.book[1]");
assertNotNull(book);
book.forEach(e -> {
assertInstanceOf(Map.class, e);
assertEquals("Sword of Honour",((Map)e).get("title"));
});
}

/**
* @throws java.lang.UnsupportedOperationException: Json-smart provider does not support TypeRef! Use a Jackson or Gson based provider
* need provid an provider to support TypeRef,like this:
* final Configuration configuration = Configuration.builder()//
* .jsonProvider(new JacksonJsonProvider(Json.mapper()))//
* .mappingProvider(new JacksonMappingProvider(Json.mapper()))//
* .build();
*/
// @Test
void parseContentWithJsonPath2() {
TypeRef<List<String>> typeStringRef = new TypeRef<List<String>>() {};
// process array
List<String> tom = JsonPathParser.parseContentWithJsonPath(JSON_ARRAY,"$[0].name",typeStringRef);
assertNotNull(tom);
assertEquals("tom",tom.get(0));
TypeRef<List<Map>> typeMapRef = new TypeRef<List<Map>>() {};
// get json array map
List<Map> entry = JsonPathParser.parseContentWithJsonPath(JSON_ARRAY,"$[1]",typeMapRef);
assertNotNull(entry);
entry.forEach(e -> {
assertEquals("543",e.get("speed"));
});
TypeRef<List<String>> typeStrRef = new TypeRef<List<String>>() {};
// process object
List<String> author = JsonPathParser.parseContentWithJsonPath(JSON_OBJECT,"$.store.book[0].author",typeStrRef);
assertNotNull(author);
assertEquals("Nigel Rees",author.get(0));
TypeRef<List<Map>> typeObjMapRef = new TypeRef<List<Map>>() {};
// get json object map
List<Map> book = JsonPathParser.parseContentWithJsonPath(JSON_OBJECT,"$.store.book[1]",typeObjMapRef);
assertNotNull(book);
book.forEach(e -> {
assertEquals("Sword of Honour",e.get("title"));
assertEquals("Sword of Honour", ((Map) e).get("title"));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.time.LocalDateTime;
import org.junit.jupiter.api.Test;

Expand Down
Loading