This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new ac3ecf14c [type: test]:Increase the test coverage of package: plugin
logging rocketmq to more than 80% (#3256)
ac3ecf14c is described below
commit ac3ecf14c8fda775ec2672d79a4a0c2ec1f86dfb
Author: qinghai777 <[email protected]>
AuthorDate: Sun Apr 17 12:13:10 2022 +0800
[type: test]:Increase the test coverage of package: plugin logging rocketmq
to more than 80% (#3256)
* test about logging-rocketmq-utils
* test about logging-rocketmq
---
.../plugin/logging/DefaultLogCollectorTest.java | 65 ++++++++
.../shenyu/plugin/logging/body/BodyWriterTest.java | 71 +++++++++
.../body/LoggingServerHttpResponseTest.java | 164 +++++++++++++++++++++
.../logging/config/LogCollectConfigTest.java | 92 ++++++++++++
.../plugin/logging/entity/LZ4CompressDataTest.java | 54 +++++++
.../logging/entity/ShenyuRequestLogTest.java | 86 +++++++++++
.../LoggingRocketMQPluginDataHandlerTest.java | 119 +++++++++++++++
.../rocketmq/RocketMQLogCollectClientTest.java | 90 +++++++++++
.../plugin/logging/sampler/CountSamplerTest.java | 92 ++++++++++++
9 files changed, 833 insertions(+)
diff --git
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/DefaultLogCollectorTest.java
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/DefaultLogCollectorTest.java
new file mode 100644
index 000000000..8f26aaf7a
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/DefaultLogCollectorTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.shenyu.plugin.logging;
+
+import org.apache.shenyu.plugin.logging.entity.ShenyuRequestLog;
+import org.apache.shenyu.plugin.logging.rocketmq.RocketMQLogCollectClient;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Field;
+import java.util.concurrent.BlockingQueue;
+
+/**
+ * The Test Case For DefaultLogCollector.
+ */
+public class DefaultLogCollectorTest {
+
+ private ShenyuRequestLog shenyuRequestLog = new ShenyuRequestLog();
+
+ @BeforeEach
+ public void setUp() {
+ shenyuRequestLog.setClientIp("0.0.0.0");
+ shenyuRequestLog.setPath("org/apache/shenyu/plugin/logging");
+ }
+
+ @Test
+ public void testAbstractLogCollector() throws Exception {
+ DefaultLogCollector.getInstance().start();
+ Field field1 = AbstractLogCollector.class.getDeclaredField("started");
+ field1.setAccessible(true);
+
Assertions.assertEquals(field1.get(DefaultLogCollector.getInstance()).toString(),
"true");
+ DefaultLogCollector.getInstance().collect(shenyuRequestLog);
+ Field field2 =
AbstractLogCollector.class.getDeclaredField("bufferQueue");
+ field2.setAccessible(true);
+ BlockingQueue<ShenyuRequestLog> bufferQueue =
(BlockingQueue<ShenyuRequestLog>) field2.get(DefaultLogCollector.getInstance());
+ Assertions.assertEquals(bufferQueue.size(), 1);
+ DefaultLogCollector.getInstance().start();
+ DefaultLogCollector.getInstance().close();
+ Field field3 = AbstractLogCollector.class.getDeclaredField("started");
+ field3.setAccessible(true);
+
Assertions.assertEquals(field3.get(DefaultLogCollector.getInstance()).toString(),
"false");
+ }
+
+ @Test
+ public void testGetLogConsumeClient() {
+ LogConsumeClient logConsumeClient = new
DefaultLogCollector().getLogConsumeClient();
+ Assertions.assertEquals(RocketMQLogCollectClient.class,
logConsumeClient.getClass());
+ }
+}
diff --git
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/body/BodyWriterTest.java
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/body/BodyWriterTest.java
new file mode 100644
index 000000000..6691e77fa
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/body/BodyWriterTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.shenyu.plugin.logging.body;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.UnsupportedEncodingException;
+import java.nio.ByteBuffer;
+
+/**
+ * The Test Case For BodyWriter.
+ */
+public class BodyWriterTest {
+
+ private BodyWriter writer;
+
+ private String sendString;
+
+ private ByteBuffer byteBuffer;
+
+ @BeforeEach
+ public void setUp() throws UnsupportedEncodingException {
+ this.writer = new BodyWriter();
+ this.sendString = "hello, shenyu";
+ byteBuffer = ByteBuffer.wrap(sendString.getBytes("UTF-8"));
+ }
+
+ @Test
+ public void testWrite() throws UnsupportedEncodingException {
+ ByteBuffer byteBuffer = ByteBuffer.wrap(sendString.getBytes("UTF-8"));
+ writer.write(byteBuffer.asReadOnlyBuffer());
+ String res = writer.output();
+ Assertions.assertEquals(res, "hello, shenyu");
+ }
+
+ @Test
+ public void testIsEmpty() {
+ Assertions.assertEquals(writer.isEmpty(), true);
+ }
+
+ @Test
+ public void testSize() {
+ writer.write(byteBuffer.asReadOnlyBuffer());
+ int size = writer.size();
+ Assertions.assertEquals(size, 13);
+ }
+
+ @Test
+ public void testOutput() {
+ writer.write(byteBuffer.asReadOnlyBuffer());
+ String res = writer.output();
+ Assertions.assertEquals(res, "hello, shenyu");
+ }
+}
diff --git
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/body/LoggingServerHttpResponseTest.java
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/body/LoggingServerHttpResponseTest.java
new file mode 100644
index 000000000..64b8e18eb
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/body/LoggingServerHttpResponseTest.java
@@ -0,0 +1,164 @@
+/*
+ * 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.shenyu.plugin.logging.body;
+
+import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.plugin.api.RemoteAddressResolver;
+import org.apache.shenyu.plugin.api.context.ShenyuContext;
+import org.apache.shenyu.plugin.api.result.ShenyuResult;
+import org.apache.shenyu.plugin.api.utils.SpringBeanUtils;
+import org.apache.shenyu.plugin.base.utils.HostAddressUtils;
+import org.apache.shenyu.plugin.logging.DefaultLogCollector;
+import org.apache.shenyu.plugin.logging.entity.ShenyuRequestLog;
+import org.apache.shenyu.plugin.logging.utils.LogCollectUtils;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.http.server.reactive.ServerHttpRequest;
+import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
+import org.springframework.mock.web.server.MockServerWebExchange;
+import org.springframework.web.server.ServerWebExchange;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.time.LocalDateTime;
+
+import static
org.apache.shenyu.plugin.logging.constant.LoggingConstant.SHENYU_AGENT_TRACE_ID;
+
+/**
+ * The Test Case For LoggingServerHttpResponse.
+ */
+public class LoggingServerHttpResponseTest {
+
+ private ShenyuRequestLog requestInfo = new ShenyuRequestLog();
+
+ private ServerWebExchange exchange;
+
+ private LoggingServerHttpResponse loggingServerHttpResponse;
+
+ private ServerHttpRequest serverHttpRequest;
+
+ private String userAgent = "User-Agent";
+
+ private String host = "Host";
+
+ private LocalDateTime startDateTime = LocalDateTime.now();
+
+ @BeforeEach
+ public void setUp() {
+ MockServerHttpRequest request = MockServerHttpRequest
+ .post("localhost")
+ .remoteAddress(new InetSocketAddress(8090))
+ .header("X-source", "mock test")
+ .queryParam("queryParam", "Hello,World")
+ .body("hello");
+ ConfigurableApplicationContext context =
Mockito.mock(ConfigurableApplicationContext.class);
+ SpringBeanUtils.getInstance().setApplicationContext(context);
+ RemoteAddressResolver remoteAddressResolver = new
RemoteAddressResolver() {
+ };
+ ShenyuResult shenyuResult = new ShenyuResult() {
+ };
+ this.exchange = Mockito.spy(MockServerWebExchange.from(request));
+
Mockito.lenient().when(context.getBean(RemoteAddressResolver.class)).thenReturn(remoteAddressResolver);
+
Mockito.lenient().when(context.getBean(ShenyuResult.class)).thenReturn(shenyuResult);
+ ShenyuContext shenyuContext = new ShenyuContext();
+ shenyuContext.setStartDateTime(startDateTime);
+ exchange.getAttributes().put(Constants.CONTEXT, shenyuContext);
+ exchange.getAttributes().put(SHENYU_AGENT_TRACE_ID,
"shenyu-agent-trace-id");
+ exchange.getAttributes().put(Constants.HTTP_DOMAIN,
"http://localhost:9195/http/order/path/123/name");
+ this.serverHttpRequest = exchange.getRequest();
+ requestInfo.setRequestUri(serverHttpRequest.getURI().toString());
+ requestInfo.setMethod(serverHttpRequest.getMethodValue());
+
requestInfo.setRequestHeader(LogCollectUtils.getHeaders(serverHttpRequest.getHeaders()));
+ requestInfo.setQueryParams(serverHttpRequest.getURI().getQuery());
+ requestInfo.setClientIp(HostAddressUtils.acquireIp(exchange));
+
requestInfo.setUserAgent(serverHttpRequest.getHeaders().getFirst(userAgent));
+ requestInfo.setHost(serverHttpRequest.getHeaders().getFirst(host));
+ requestInfo.setPath(serverHttpRequest.getURI().getPath());
+ this.loggingServerHttpResponse = new
LoggingServerHttpResponse(exchange.getResponse(), requestInfo,
DefaultLogCollector.getInstance());
+ }
+
+ @Test
+ public void testSetExchange() throws NoSuchFieldException,
IllegalAccessException {
+ loggingServerHttpResponse.setExchange(exchange);
+ Field field =
loggingServerHttpResponse.getClass().getDeclaredField("exchange");
+ field.setAccessible(true);
+ Assertions.assertEquals(field.get(loggingServerHttpResponse),
exchange);
+ }
+
+ @Test
+ public void testGetTraceId() throws Exception {
+ loggingServerHttpResponse.setExchange(exchange);
+ exchange.getResponse().getHeaders();
+ Method method =
loggingServerHttpResponse.getClass().getDeclaredMethod("getTraceId");
+ method.setAccessible(true);
+ String traceId = (String) method.invoke(loggingServerHttpResponse);
+ Assertions.assertEquals(traceId, "shenyu-agent-trace-id");
+ }
+
+ @Test
+ public void testGetUpstreamIp() throws Exception {
+ loggingServerHttpResponse.setExchange(exchange);
+ Method method =
loggingServerHttpResponse.getClass().getDeclaredMethod("getUpstreamIp");
+ method.setAccessible(true);
+ String upstreamIp = (String) method.invoke(loggingServerHttpResponse);
+ Assertions.assertEquals(upstreamIp, "localhost");
+ }
+
+ @Test
+ public void testGetUpstreamIpFromHttpDomain() throws Exception {
+ loggingServerHttpResponse.setExchange(exchange);
+ Method method =
loggingServerHttpResponse.getClass().getDeclaredMethod("getUpstreamIpFromHttpDomain");
+ method.setAccessible(true);
+ String upstreamIpFromHttpDomain = (String)
method.invoke(loggingServerHttpResponse);
+ Assertions.assertEquals(upstreamIpFromHttpDomain, "localhost");
+ }
+
+ @Test
+ public void testLogError() throws NoSuchFieldException,
IllegalAccessException {
+ loggingServerHttpResponse.setExchange(exchange);
+ Throwable throwable = new Throwable("error");
+ DefaultLogCollector.getInstance().start();
+ loggingServerHttpResponse.logError(throwable);
+ Field field =
loggingServerHttpResponse.getClass().getDeclaredField("logInfo");
+ field.setAccessible(true);
+ ShenyuRequestLog shenyuRequestLog = (ShenyuRequestLog)
field.get(loggingServerHttpResponse);
+ Assertions.assertEquals(shenyuRequestLog.getStatus(), 500);
+ }
+
+ @Test
+ public void testLogResponse() throws Exception {
+ DefaultLogCollector.getInstance().start();
+ loggingServerHttpResponse.setExchange(exchange);
+ BodyWriter writer = new BodyWriter();
+ String sendString = "hello, shenyu";
+ ByteBuffer byteBuffer = ByteBuffer.wrap(sendString.getBytes("UTF-8"));
+ writer.write(byteBuffer.asReadOnlyBuffer());
+ Method method =
loggingServerHttpResponse.getClass().getDeclaredMethod("logResponse",
ShenyuContext.class, BodyWriter.class);
+ method.setAccessible(true);
+ method.invoke(loggingServerHttpResponse,
exchange.getAttribute(Constants.CONTEXT), writer);
+ Field field =
loggingServerHttpResponse.getClass().getDeclaredField("logInfo");
+ field.setAccessible(true);
+ ShenyuRequestLog log = (ShenyuRequestLog)
field.get(loggingServerHttpResponse);
+ Assertions.assertEquals(log.getResponseBody(), "hello, shenyu");
+ }
+}
diff --git
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/config/LogCollectConfigTest.java
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/config/LogCollectConfigTest.java
new file mode 100644
index 000000000..ec7311b97
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/config/LogCollectConfigTest.java
@@ -0,0 +1,92 @@
+/*
+ * 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.shenyu.plugin.logging.config;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * The Test Case For LogCollectConfig.
+ */
+public class LogCollectConfigTest {
+
+ private LogCollectConfig logCollectConfig = new LogCollectConfig();
+
+ @Test
+ public void testSetLogApiConfigTopic() {
+ LogCollectConfig.LogApiConfig logApiConfig = new
LogCollectConfig.LogApiConfig();
+ logApiConfig.setTopic("test");
+ Assertions.assertEquals(logApiConfig.getTopic(), "test");
+ }
+
+ @Test
+ public void testSetLogApiConfigSampleRate() {
+ LogCollectConfig.LogApiConfig logApiConfig = new
LogCollectConfig.LogApiConfig();
+ logApiConfig.setSampleRate("1");
+ Assertions.assertEquals(logApiConfig.getSampleRate(), "1");
+ }
+
+ @Test
+ public void testGetGlobalLogConfigSampleRate() {
+ LogCollectConfig.GlobalLogConfig globalLogConfig = new
LogCollectConfig.GlobalLogConfig();
+ globalLogConfig.setSampleRate("1");
+ Assertions.assertEquals(globalLogConfig.getSampleRate(), "1");
+ }
+
+ @Test
+ public void testSetGlobalLogConfigTopic() {
+ LogCollectConfig.GlobalLogConfig globalLogConfig = new
LogCollectConfig.GlobalLogConfig();
+ globalLogConfig.setTopic("test");
+ Assertions.assertEquals(globalLogConfig.getTopic(), "test");
+ }
+
+ @Test
+ public void testSetGlobalLogConfigMaxResponseBody() {
+ LogCollectConfig.GlobalLogConfig globalLogConfig = new
LogCollectConfig.GlobalLogConfig();
+ globalLogConfig.setMaxResponseBody(5);
+ Assertions.assertEquals(globalLogConfig.getMaxResponseBody(), 5);
+ }
+
+ @Test
+ public void testSetGlobalLogConfigMaxRequestBody() {
+ LogCollectConfig.GlobalLogConfig globalLogConfig = new
LogCollectConfig.GlobalLogConfig();
+ globalLogConfig.setMaxRequestBody(5);
+ Assertions.assertEquals(globalLogConfig.getMaxRequestBody(), 5);
+ }
+
+ @Test
+ public void testSetGlobalLogConfigNamesrvAddr() {
+ LogCollectConfig.GlobalLogConfig globalLogConfig = new
LogCollectConfig.GlobalLogConfig();
+ globalLogConfig.setNamesrvAddr("test");
+ Assertions.assertEquals(globalLogConfig.getNamesrvAddr(), "test");
+ }
+
+ @Test
+ public void testSetGlobalLogConfigProducerGroup() {
+ LogCollectConfig.GlobalLogConfig globalLogConfig = new
LogCollectConfig.GlobalLogConfig();
+ globalLogConfig.setProducerGroup("test");
+ Assertions.assertEquals(globalLogConfig.getProducerGroup(), "test");
+ }
+
+ @Test
+ public void testGetGlobalLogConfig() {
+ LogCollectConfig.GlobalLogConfig globalLogConfig = new
LogCollectConfig.GlobalLogConfig();
+ logCollectConfig.setGlobalLogConfig(globalLogConfig);
+ Assertions.assertEquals(logCollectConfig.getGlobalLogConfig(),
globalLogConfig);
+ }
+}
diff --git
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/entity/LZ4CompressDataTest.java
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/entity/LZ4CompressDataTest.java
new file mode 100644
index 000000000..456c8bd1b
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/entity/LZ4CompressDataTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.shenyu.plugin.logging.entity;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * The Test Case For LZ4CompressData.
+ */
+public class LZ4CompressDataTest {
+
+ private int length;
+
+ private byte[] compressedData;
+
+ private LZ4CompressData lz4CompressData;
+
+ @BeforeEach
+ public void setUp() {
+ this.length = 5;
+ this.compressedData = new byte[]{'h', 'e', 'l', 'l', 'o'};
+ this.lz4CompressData = new LZ4CompressData(length, compressedData);
+ }
+
+ @Test
+ public void testGetLength() {
+ lz4CompressData.setLength(6);
+ Assertions.assertEquals(lz4CompressData.getLength(), 6);
+ }
+
+ @Test
+ public void testGetCompressedData() {
+ byte[] bytes = new byte[]{'h', 'e', 'l', 'l', 'o', '!'};
+ lz4CompressData.setCompressedData(bytes);
+ Assertions.assertEquals(lz4CompressData.getCompressedData(), bytes);
+ }
+}
diff --git
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/entity/ShenyuRequestLogTest.java
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/entity/ShenyuRequestLogTest.java
new file mode 100644
index 000000000..e6150c2da
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/entity/ShenyuRequestLogTest.java
@@ -0,0 +1,86 @@
+/*
+ * 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.shenyu.plugin.logging.entity;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.time.LocalDateTime;
+
+/**
+ * The Test Case For ShenyuRequestLog.
+ */
+public class ShenyuRequestLogTest {
+
+ private ShenyuRequestLog shenyuRequestLog = new ShenyuRequestLog();
+
+ @Test
+ public void testGetModule() {
+ shenyuRequestLog.setModule("test");
+ Assertions.assertEquals(shenyuRequestLog.getModule(), "test");
+ }
+
+ @Test
+ public void testResponseContentLength() {
+ shenyuRequestLog.setResponseContentLength(5);
+ Assertions.assertEquals(shenyuRequestLog.getResponseContentLength(),
5);
+ }
+
+ @Test
+ public void testGetUserAgent() {
+ shenyuRequestLog.setUserAgent("test");
+ Assertions.assertEquals(shenyuRequestLog.getUserAgent(), "test");
+ }
+
+ @Test
+ public void testGetHost() {
+ shenyuRequestLog.setHost("test");
+ Assertions.assertEquals(shenyuRequestLog.getHost(), "test");
+ }
+
+ @Test
+ public void testGetClientIp() {
+ shenyuRequestLog.setClientIp("0.0.0.0");
+ Assertions.assertEquals(shenyuRequestLog.getClientIp(), "0.0.0.0");
+ }
+
+ @Test
+ public void testGetTimeLocal() {
+ LocalDateTime timeLocal = LocalDateTime.now();
+ shenyuRequestLog.setTimeLocal(timeLocal.toString());
+ Assertions.assertEquals(shenyuRequestLog.getTimeLocal(),
timeLocal.toString());
+ }
+
+ @Test
+ public void testGetMethod() {
+ shenyuRequestLog.setMethod("test");
+ Assertions.assertEquals(shenyuRequestLog.getMethod(), "test");
+ }
+
+ @Test
+ public void testGetRequestBody() {
+ shenyuRequestLog.setRequestBody("hello");
+ Assertions.assertEquals(shenyuRequestLog.getRequestBody(), "hello");
+ }
+
+ @Test
+ public void testGetUpstreamIp() {
+ shenyuRequestLog.setUpstreamIp("0.0.0.0");
+ Assertions.assertEquals(shenyuRequestLog.getUpstreamIp(), "0.0.0.0");
+ }
+}
diff --git
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/handler/LoggingRocketMQPluginDataHandlerTest.java
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/handler/LoggingRocketMQPluginDataHandlerTest.java
new file mode 100644
index 000000000..276a876a5
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/handler/LoggingRocketMQPluginDataHandlerTest.java
@@ -0,0 +1,119 @@
+/*
+ * 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.shenyu.plugin.logging.handler;
+
+import org.apache.shenyu.common.dto.ConditionData;
+import org.apache.shenyu.common.dto.PluginData;
+import org.apache.shenyu.common.dto.SelectorData;
+import org.apache.shenyu.plugin.logging.rocketmq.RocketMQLogCollectClient;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * The Test Case For LoggingRocketMQPluginDataHandler.
+ */
+public class LoggingRocketMQPluginDataHandlerTest {
+
+ private LoggingRocketMQPluginDataHandler loggingRocketMQPluginDataHandler;
+
+ private SelectorData selectorData = new SelectorData();
+
+ private ConditionData conditionData = new ConditionData();
+
+ private PluginData pluginData = new PluginData();
+
+ @BeforeEach
+ public void setUp() {
+ this.loggingRocketMQPluginDataHandler = new
LoggingRocketMQPluginDataHandler();
+ selectorData.setId("1");
+ selectorData.setType(1);
+ selectorData.setHandle("{\"topic\":\"test\", \"sampleRate\":\"1\"}");
+ conditionData.setParamName("id");
+ conditionData.setParamType("uri");
+ conditionData.setParamValue("11");
+ conditionData.setOperator("=");
+ List<ConditionData> list = new ArrayList<>();
+ list.add(conditionData);
+ selectorData.setConditionList(list);
+ pluginData.setEnabled(true);
+ pluginData.setConfig("{\"topic\":\"test\", \"namesrvAddr\":\"test\",
\"producerGroup\":\"test\"}");
+ }
+
+ @Test
+ public void testHandlerPlugin() throws NoSuchFieldException,
IllegalAccessException {
+ loggingRocketMQPluginDataHandler.handlerPlugin(pluginData);
+ Field field =
loggingRocketMQPluginDataHandler.getClass().getDeclaredField("ROCKET_MQ_LOG_COLLECT_CLIENT");
+ field.setAccessible(true);
+
Assertions.assertEquals(field.get(loggingRocketMQPluginDataHandler).getClass(),
RocketMQLogCollectClient.class);
+ }
+
+ @Test
+ public void testHandlerSelector() throws NoSuchFieldException,
IllegalAccessException {
+ loggingRocketMQPluginDataHandler.handlerSelector(selectorData);
+ Field field1 =
loggingRocketMQPluginDataHandler.getClass().getDeclaredField("SELECT_ID_URI_LIST_MAP");
+ field1.setAccessible(true);
+ Field field2 =
loggingRocketMQPluginDataHandler.getClass().getDeclaredField("SELECT_API_CONFIG_MAP");
+ field2.setAccessible(true);
+ Assertions.assertEquals(field1.get("1").toString(), "{1=[11]}");
+ Assertions.assertNotEquals(field2.get("1").toString(), "{}");
+ }
+
+ @Test
+ public void testRemoveSelector() throws NoSuchFieldException,
IllegalAccessException {
+ loggingRocketMQPluginDataHandler.handlerSelector(selectorData);
+ Field field1 =
loggingRocketMQPluginDataHandler.getClass().getDeclaredField("SELECT_ID_URI_LIST_MAP");
+ field1.setAccessible(true);
+ Field field2 =
loggingRocketMQPluginDataHandler.getClass().getDeclaredField("SELECT_API_CONFIG_MAP");
+ field2.setAccessible(true);
+ Assertions.assertEquals(field1.get("1").toString(), "{1=[11]}");
+ Assertions.assertNotEquals(field2.get("1").toString(), "{}");
+ loggingRocketMQPluginDataHandler.removeSelector(selectorData);
+ Field field3 =
loggingRocketMQPluginDataHandler.getClass().getDeclaredField("SELECT_ID_URI_LIST_MAP");
+ field3.setAccessible(true);
+ Field field4 =
loggingRocketMQPluginDataHandler.getClass().getDeclaredField("SELECT_API_CONFIG_MAP");
+ field4.setAccessible(true);
+ Assertions.assertEquals(field3.get("1").toString(), "{}");
+ Assertions.assertEquals(field4.get("1").toString(), "{}");
+ }
+
+ @Test
+ public void testPluginNamed() {
+
Assertions.assertEquals(loggingRocketMQPluginDataHandler.pluginNamed(),
"loggingRocketMQ");
+ }
+
+ @Test
+ public void testGetRocketMqLogCollectClient() {
+
Assertions.assertEquals(LoggingRocketMQPluginDataHandler.getRocketMqLogCollectClient().getClass(),
RocketMQLogCollectClient.class);
+ }
+
+ @Test
+ public void testGetSelectIdUriListMap() {
+
Assertions.assertEquals(LoggingRocketMQPluginDataHandler.getSelectIdUriListMap().getClass(),
ConcurrentHashMap.class);
+ }
+
+ @Test
+ public void testGetSelectApiConfigMap() {
+
Assertions.assertEquals(LoggingRocketMQPluginDataHandler.getSelectApiConfigMap().getClass(),
ConcurrentHashMap.class);
+ }
+}
diff --git
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/rocketmq/RocketMQLogCollectClientTest.java
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/rocketmq/RocketMQLogCollectClientTest.java
new file mode 100644
index 000000000..23c3972e6
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/rocketmq/RocketMQLogCollectClientTest.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.shenyu.plugin.logging.rocketmq;
+
+import org.apache.shenyu.common.dto.PluginData;
+import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.plugin.logging.config.LogCollectConfig;
+import org.apache.shenyu.plugin.logging.constant.LoggingConstant;
+import org.apache.shenyu.plugin.logging.entity.ShenyuRequestLog;
+import org.apache.shenyu.plugin.logging.utils.LogCollectConfigUtils;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+/**
+ * The Test Case For RocketMQLogCollectClient.
+ */
+public class RocketMQLogCollectClientTest {
+
+ private RocketMQLogCollectClient rocketMQLogCollectClient;
+
+ private Properties props = new Properties();
+
+ private PluginData pluginData = new PluginData();
+
+ private LogCollectConfig.GlobalLogConfig globalLogConfig;
+
+ private List<ShenyuRequestLog> logs = new ArrayList<>();
+
+ private ShenyuRequestLog shenyuRequestLog = new ShenyuRequestLog();
+
+ @BeforeEach
+ public void setUp() {
+ this.rocketMQLogCollectClient = new RocketMQLogCollectClient();
+ pluginData.setEnabled(true);
+ pluginData.setConfig("{\"topic\":\"shenyu-access-logging\",
\"namesrvAddr\":\"localhost:9876\",
\"producerGroup\":\"shenyu-plugin-logging-rocketmq\"}");
+ globalLogConfig =
GsonUtils.getInstance().fromJson(pluginData.getConfig(),
+ LogCollectConfig.GlobalLogConfig.class);
+ globalLogConfig.setCompressAlg("LZ4");
+ props.setProperty(LoggingConstant.TOPIC, globalLogConfig.getTopic());
+ props.setProperty(LoggingConstant.NAMESERVER_ADDRESS,
globalLogConfig.getNamesrvAddr());
+ props.setProperty(LoggingConstant.PRODUCER_GROUP,
globalLogConfig.getProducerGroup());
+ shenyuRequestLog.setClientIp("0.0.0.0");
+ shenyuRequestLog.setPath("org/apache/shenyu/plugin/logging");
+ logs.add(shenyuRequestLog);
+ }
+
+ @Test
+ public void testInitProducer() throws NoSuchFieldException,
IllegalAccessException {
+ rocketMQLogCollectClient.initProducer(props);
+ Field field =
rocketMQLogCollectClient.getClass().getDeclaredField("topic");
+ field.setAccessible(true);
+ Assertions.assertEquals(field.get(rocketMQLogCollectClient),
"shenyu-access-logging");
+ rocketMQLogCollectClient.close();
+ }
+
+ @Test
+ public void testConsume() {
+ String msg = "";
+ LogCollectConfigUtils.setGlobalConfig(globalLogConfig);
+ rocketMQLogCollectClient.initProducer(props);
+ try {
+ rocketMQLogCollectClient.consume(logs);
+ } catch (Exception e) {
+ msg = "false";
+ }
+ Assertions.assertEquals(msg, "");
+ rocketMQLogCollectClient.close();
+ }
+}
diff --git
a/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/sampler/CountSamplerTest.java
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/sampler/CountSamplerTest.java
new file mode 100644
index 000000000..92554a36e
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-logging-rocketmq/src/test/java/org/apache/shenyu/plugin/logging/sampler/CountSamplerTest.java
@@ -0,0 +1,92 @@
+/*
+ * 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.shenyu.plugin.logging.sampler;
+
+import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.plugin.api.context.ShenyuContext;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+import org.springframework.http.server.reactive.ServerHttpRequest;
+import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
+import org.springframework.mock.web.server.MockServerWebExchange;
+import org.springframework.web.server.ServerWebExchange;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.InetSocketAddress;
+import java.util.BitSet;
+
+/**
+ * The Test Case For CountSampler.
+ */
+public class CountSamplerTest {
+
+ private CountSampler countSampler;
+
+ private ServerHttpRequest request;
+
+ private ServerWebExchange exchange;
+
+ @BeforeEach
+ public void setUp() {
+ this.countSampler = new CountSampler(1);
+ MockServerHttpRequest request = MockServerHttpRequest
+ .get("localhost")
+ .remoteAddress(new InetSocketAddress(8090))
+ .header("X-source", "mock test")
+ .queryParam("queryParam", "Hello,World")
+ .build();
+ this.exchange = Mockito.spy(MockServerWebExchange.from(request));
+ ShenyuContext shenyuContext = Mockito.mock(ShenyuContext.class);
+ exchange.getAttributes().put(Constants.CONTEXT, shenyuContext);
+ this.request = exchange.getRequest();
+ }
+
+ @Test
+ public void testIsSampled() {
+ Assertions.assertEquals(countSampler.isSampled(request), true);
+ }
+
+ @Test
+ public void testMod() throws NoSuchMethodException,
InvocationTargetException, IllegalAccessException {
+ Method method = countSampler.getClass().getDeclaredMethod("mod",
int.class);
+ method.setAccessible(true);
+ int res = (int) method.invoke(countSampler, 1);
+ Assertions.assertEquals(res, 1);
+ }
+
+ @Test
+ public void testGenRandomBitSet() throws NoSuchMethodException,
InvocationTargetException, IllegalAccessException {
+ Method method =
countSampler.getClass().getDeclaredMethod("genRandomBitSet", int.class,
int.class);
+ method.setAccessible(true);
+ BitSet bitSet = (BitSet) method.invoke(countSampler, 1, 1);
+ BitSet res = new BitSet(1);
+ res.set(0);
+ Assertions.assertEquals(bitSet, res);
+ }
+
+ @Test
+ public void testCreate() {
+ Assertions.assertEquals(CountSampler.create(""),
Sampler.ALWAYS_SAMPLE);
+ Assertions.assertEquals(CountSampler.create("0"),
Sampler.NEVER_SAMPLE);
+ Assertions.assertEquals(CountSampler.create("1"),
Sampler.ALWAYS_SAMPLE);
+ Assertions.assertEquals(CountSampler.create("0.5").getClass(),
CountSampler.class);
+ }
+}