You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2022/06/20 07:57:52 UTC
[incubator-shenyu] branch master updated: add es unit test (#3591)
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 324e34880 add es unit test (#3591)
324e34880 is described below
commit 324e34880295aaa795cdd7f80cbd7022710eee07
Author: qinghai777 <80...@users.noreply.github.com>
AuthorDate: Mon Jun 20 15:57:45 2022 +0800
add es unit test (#3591)
---
.../elasticsearch/DefaultLogCollectorTest.java | 59 ++++++++
.../LoggingElasticSearchPluginTest.java | 98 +++++++++++++
.../logging/elasticsearch/body/BodyWriterTest.java | 71 +++++++++
...LoggingElasticSearchServerHttpResponseTest.java | 163 +++++++++++++++++++++
.../elasticsearch/config/LogCollectConfigTest.java | 78 ++++++++++
.../elasticsearch/entity/LZ4CompressDataTest.java | 54 +++++++
.../elasticsearch/entity/ShenyuRequestLogTest.java | 86 +++++++++++
.../LoggingElasticSearchPluginDataHandlerTest.java | 119 +++++++++++++++
.../elasticsearch/sampler/CountSamplerTest.java | 92 ++++++++++++
.../utils/LogCollectConfigUtilsTest.java | 117 +++++++++++++++
.../elasticsearch/utils/LogCollectUtilsTest.java | 66 +++++++++
11 files changed, 1003 insertions(+)
diff --git a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/DefaultLogCollectorTest.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/DefaultLogCollectorTest.java
new file mode 100644
index 000000000..4e268e641
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/DefaultLogCollectorTest.java
@@ -0,0 +1,59 @@
+/*
+ * 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.elasticsearch;
+
+import org.apache.shenyu.plugin.logging.elasticsearch.elasticsearch.ElasticSearchLogCollectClient;
+import org.apache.shenyu.plugin.logging.elasticsearch.entity.ShenyuRequestLog;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Field;
+
+/**
+ * The Test Case For DefaultLogCollector.
+ */
+public final 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);
+ DefaultLogCollector.getInstance().close();
+ Field field2 = AbstractLogCollector.class.getDeclaredField("started");
+ field2.setAccessible(true);
+ Assertions.assertEquals(field2.get(DefaultLogCollector.getInstance()).toString(), "false");
+ }
+
+ @Test
+ public void testGetLogConsumeClient() {
+ LogConsumeClient logConsumeClient = new DefaultLogCollector().getLogConsumeClient();
+ Assertions.assertEquals(ElasticSearchLogCollectClient.class, logConsumeClient.getClass());
+ }
+}
diff --git a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/LoggingElasticSearchPluginTest.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/LoggingElasticSearchPluginTest.java
new file mode 100644
index 000000000..b7d028653
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/LoggingElasticSearchPluginTest.java
@@ -0,0 +1,98 @@
+/*
+ * 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.elasticsearch;
+
+import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.common.dto.RuleData;
+import org.apache.shenyu.common.dto.SelectorData;
+import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.plugin.api.RemoteAddressResolver;
+import org.apache.shenyu.plugin.api.ShenyuPluginChain;
+import org.apache.shenyu.plugin.api.context.ShenyuContext;
+import org.apache.shenyu.plugin.api.utils.SpringBeanUtils;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.ArgumentMatchers;
+import org.mockito.Mockito;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
+import org.springframework.mock.web.server.MockServerWebExchange;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Mono;
+import reactor.test.StepVerifier;
+
+import java.net.InetSocketAddress;
+
+/**
+ * The Test Case For LoggingElasticSearchPlugin.
+ */
+@ExtendWith(MockitoExtension.class)
+public final class LoggingElasticSearchPluginTest {
+
+ private LoggingElasticSearchPlugin loggingElasticSearchPlugin;
+
+ private ServerWebExchange exchange;
+
+ private RuleData ruleData;
+
+ private ShenyuPluginChain chain;
+
+ private SelectorData selectorData;
+
+ @BeforeEach
+ public void setUp() {
+ this.loggingElasticSearchPlugin = new LoggingElasticSearchPlugin();
+ this.ruleData = Mockito.mock(RuleData.class);
+ this.chain = Mockito.mock(ShenyuPluginChain.class);
+ this.selectorData = Mockito.mock(SelectorData.class);
+ MockServerHttpRequest request = MockServerHttpRequest
+ .get("localhost")
+ .remoteAddress(new InetSocketAddress(8090))
+ .header("X-source", "mock test")
+ .queryParam("queryParam", "Hello,World")
+ .build();
+ ConfigurableApplicationContext context = Mockito.mock(ConfigurableApplicationContext.class);
+ SpringBeanUtils.getInstance().setApplicationContext(context);
+ RemoteAddressResolver remoteAddressResolver = new RemoteAddressResolver() {
+ };
+ Mockito.lenient().when(context.getBean(RemoteAddressResolver.class)).thenReturn(remoteAddressResolver);
+ this.exchange = Mockito.spy(MockServerWebExchange.from(request));
+ ShenyuContext shenyuContext = Mockito.mock(ShenyuContext.class);
+ exchange.getAttributes().put(Constants.CONTEXT, shenyuContext);
+ }
+
+ @Test
+ public void testDoExecute() {
+ Mockito.when(chain.execute(ArgumentMatchers.any())).thenReturn(Mono.empty());
+ Mono<Void> result = loggingElasticSearchPlugin.doExecute(exchange, chain, selectorData, ruleData);
+ StepVerifier.create(result).expectSubscription().verifyComplete();
+ }
+
+ @Test
+ public void testGetOrder() {
+ Assertions.assertEquals(loggingElasticSearchPlugin.getOrder(), PluginEnum.LOGGING_ElasticSearch.getCode());
+ }
+
+ @Test
+ public void testNamed() {
+ Assertions.assertEquals(loggingElasticSearchPlugin.named(), PluginEnum.LOGGING_ElasticSearch.getName());
+ }
+}
diff --git a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/body/BodyWriterTest.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/body/BodyWriterTest.java
new file mode 100644
index 000000000..2277e34f9
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/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.elasticsearch.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 final 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/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/body/LoggingElasticSearchServerHttpResponseTest.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/body/LoggingElasticSearchServerHttpResponseTest.java
new file mode 100644
index 000000000..2402edec8
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/body/LoggingElasticSearchServerHttpResponseTest.java
@@ -0,0 +1,163 @@
+/*
+ * 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.elasticsearch.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.elasticsearch.DefaultLogCollector;
+import org.apache.shenyu.plugin.logging.elasticsearch.constant.LoggingConstant;
+import org.apache.shenyu.plugin.logging.elasticsearch.entity.ShenyuRequestLog;
+import org.apache.shenyu.plugin.logging.elasticsearch.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;
+
+/**
+ * The Test Case For LoggingServerHttpResponse.
+ */
+public final class LoggingElasticSearchServerHttpResponseTest {
+
+ private ShenyuRequestLog requestInfo = new ShenyuRequestLog();
+
+ private ServerWebExchange exchange;
+
+ private LoggingElasticSearchServerResponse loggingElasticSearchServerResponse;
+
+ 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(LoggingConstant.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.loggingElasticSearchServerResponse = new LoggingElasticSearchServerResponse(exchange.getResponse(), requestInfo, DefaultLogCollector.getInstance());
+ }
+
+ @Test
+ public void testSetExchange() throws NoSuchFieldException, IllegalAccessException {
+ loggingElasticSearchServerResponse.setExchange(exchange);
+ Field field = loggingElasticSearchServerResponse.getClass().getDeclaredField("exchange");
+ field.setAccessible(true);
+ Assertions.assertEquals(field.get(loggingElasticSearchServerResponse), exchange);
+ }
+
+ @Test
+ public void testGetTraceId() throws Exception {
+ loggingElasticSearchServerResponse.setExchange(exchange);
+ exchange.getResponse().getHeaders();
+ Method method = loggingElasticSearchServerResponse.getClass().getDeclaredMethod("getTraceId");
+ method.setAccessible(true);
+ String traceId = (String) method.invoke(loggingElasticSearchServerResponse);
+ Assertions.assertEquals(traceId, "shenyu-agent-trace-id");
+ }
+
+ @Test
+ public void testGetUpstreamIp() throws Exception {
+ loggingElasticSearchServerResponse.setExchange(exchange);
+ Method method = loggingElasticSearchServerResponse.getClass().getDeclaredMethod("getUpstreamIp");
+ method.setAccessible(true);
+ String upstreamIp = (String) method.invoke(loggingElasticSearchServerResponse);
+ Assertions.assertEquals(upstreamIp, "localhost");
+ }
+
+ @Test
+ public void testGetUpstreamIpFromHttpDomain() throws Exception {
+ loggingElasticSearchServerResponse.setExchange(exchange);
+ Method method = loggingElasticSearchServerResponse.getClass().getDeclaredMethod("getUpstreamIpFromHttpDomain");
+ method.setAccessible(true);
+ String upstreamIpFromHttpDomain = (String) method.invoke(loggingElasticSearchServerResponse);
+ Assertions.assertEquals(upstreamIpFromHttpDomain, "localhost");
+ }
+
+ @Test
+ public void testLogError() throws NoSuchFieldException, IllegalAccessException {
+ loggingElasticSearchServerResponse.setExchange(exchange);
+ Throwable throwable = new Throwable("error");
+ DefaultLogCollector.getInstance().start();
+ loggingElasticSearchServerResponse.logError(throwable);
+ Field field = loggingElasticSearchServerResponse.getClass().getDeclaredField("logInfo");
+ field.setAccessible(true);
+ ShenyuRequestLog shenyuRequestLog = (ShenyuRequestLog) field.get(loggingElasticSearchServerResponse);
+ Assertions.assertEquals(shenyuRequestLog.getStatus(), 500);
+ }
+
+ @Test
+ public void testLogResponse() throws Exception {
+ DefaultLogCollector.getInstance().start();
+ loggingElasticSearchServerResponse.setExchange(exchange);
+ BodyWriter writer = new BodyWriter();
+ String sendString = "hello, shenyu";
+ ByteBuffer byteBuffer = ByteBuffer.wrap(sendString.getBytes("UTF-8"));
+ writer.write(byteBuffer.asReadOnlyBuffer());
+ Method method = loggingElasticSearchServerResponse.getClass().getDeclaredMethod("logResponse", ShenyuContext.class, BodyWriter.class);
+ method.setAccessible(true);
+ method.invoke(loggingElasticSearchServerResponse, exchange.getAttribute(Constants.CONTEXT), writer);
+ Field field = loggingElasticSearchServerResponse.getClass().getDeclaredField("logInfo");
+ field.setAccessible(true);
+ ShenyuRequestLog log = (ShenyuRequestLog) field.get(loggingElasticSearchServerResponse);
+ Assertions.assertEquals(log.getResponseBody(), "hello, shenyu");
+ }
+}
diff --git a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/config/LogCollectConfigTest.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/config/LogCollectConfigTest.java
new file mode 100644
index 000000000..20bc4f442
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/config/LogCollectConfigTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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.elasticsearch.config;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * The Test Case For LogCollectConfig.
+ */
+public final class LogCollectConfigTest {
+
+ private LogCollectConfig logCollectConfig = new LogCollectConfig();
+
+ @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 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 testSetGlobalLogConfigHost() {
+ LogCollectConfig.GlobalLogConfig globalLogConfig = new LogCollectConfig.GlobalLogConfig();
+ globalLogConfig.setHost("localhost");
+ Assertions.assertEquals(globalLogConfig.getHost(), "localhost");
+ }
+
+ @Test
+ public void testSetGlobalLogConfigPort() {
+ LogCollectConfig.GlobalLogConfig globalLogConfig = new LogCollectConfig.GlobalLogConfig();
+ globalLogConfig.setPort("9200");
+ Assertions.assertEquals(globalLogConfig.getPort(), "9200");
+ }
+
+ @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/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/entity/LZ4CompressDataTest.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/entity/LZ4CompressDataTest.java
new file mode 100644
index 000000000..8324cf79a
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/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.elasticsearch.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 final 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/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/entity/ShenyuRequestLogTest.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/entity/ShenyuRequestLogTest.java
new file mode 100644
index 000000000..8f5cd327b
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/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.elasticsearch.entity;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.time.LocalDateTime;
+
+/**
+ * The Test Case For ShenyuRequestLog.
+ */
+public final 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/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/handler/LoggingElasticSearchPluginDataHandlerTest.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/handler/LoggingElasticSearchPluginDataHandlerTest.java
new file mode 100644
index 000000000..af4faff85
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/handler/LoggingElasticSearchPluginDataHandlerTest.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.elasticsearch.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.elasticsearch.elasticsearch.ElasticSearchLogCollectClient;
+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 LoggingElasticSearchPluginDataHandler.
+ */
+public final class LoggingElasticSearchPluginDataHandlerTest {
+
+ private LoggingElasticSearchPluginDataHandler loggingElasticSearchPluginDataHandler;
+
+ private SelectorData selectorData = new SelectorData();
+
+ private ConditionData conditionData = new ConditionData();
+
+ private PluginData pluginData = new PluginData();
+
+ @BeforeEach
+ public void setUp() {
+ this.loggingElasticSearchPluginDataHandler = new LoggingElasticSearchPluginDataHandler();
+ selectorData.setId("1");
+ selectorData.setType(1);
+ selectorData.setHandle("{\"index\":\"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("{\"host\":\"localhost\", \"port\":\"9200\"}");
+ }
+
+ @Test
+ public void testHandlerPlugin() throws NoSuchFieldException, IllegalAccessException {
+ loggingElasticSearchPluginDataHandler.handlerPlugin(pluginData);
+ Field field = loggingElasticSearchPluginDataHandler.getClass().getDeclaredField("ELASTICSEARCH_LOG_COLLECT_CLIENT");
+ field.setAccessible(true);
+ Assertions.assertEquals(field.get(loggingElasticSearchPluginDataHandler).getClass(), ElasticSearchLogCollectClient.class);
+ }
+
+ @Test
+ public void testHandlerSelector() throws NoSuchFieldException, IllegalAccessException {
+ loggingElasticSearchPluginDataHandler.handlerSelector(selectorData);
+ Field field1 = loggingElasticSearchPluginDataHandler.getClass().getDeclaredField("SELECT_ID_URI_LIST_MAP");
+ field1.setAccessible(true);
+ Field field2 = loggingElasticSearchPluginDataHandler.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 {
+ loggingElasticSearchPluginDataHandler.handlerSelector(selectorData);
+ Field field1 = loggingElasticSearchPluginDataHandler.getClass().getDeclaredField("SELECT_ID_URI_LIST_MAP");
+ field1.setAccessible(true);
+ Field field2 = loggingElasticSearchPluginDataHandler.getClass().getDeclaredField("SELECT_API_CONFIG_MAP");
+ field2.setAccessible(true);
+ Assertions.assertEquals(field1.get("1").toString(), "{1=[11]}");
+ Assertions.assertNotEquals(field2.get("1").toString(), "{}");
+ loggingElasticSearchPluginDataHandler.removeSelector(selectorData);
+ Field field3 = loggingElasticSearchPluginDataHandler.getClass().getDeclaredField("SELECT_ID_URI_LIST_MAP");
+ field3.setAccessible(true);
+ Field field4 = loggingElasticSearchPluginDataHandler.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(loggingElasticSearchPluginDataHandler.pluginNamed(), "loggingElasticSearch");
+ }
+
+ @Test
+ public void testGetRocketMqLogCollectClient() {
+ Assertions.assertEquals(LoggingElasticSearchPluginDataHandler.getElasticSearchLogCollectClient().getClass(), ElasticSearchLogCollectClient.class);
+ }
+
+ @Test
+ public void testGetSelectIdUriListMap() {
+ Assertions.assertEquals(LoggingElasticSearchPluginDataHandler.getSelectIdUriListMap().getClass(), ConcurrentHashMap.class);
+ }
+
+ @Test
+ public void testGetSelectApiConfigMap() {
+ Assertions.assertEquals(LoggingElasticSearchPluginDataHandler.getSelectApiConfigMap().getClass(), ConcurrentHashMap.class);
+ }
+}
diff --git a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/sampler/CountSamplerTest.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/sampler/CountSamplerTest.java
new file mode 100644
index 000000000..d9a671ba2
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/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.elasticsearch.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 final 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);
+ }
+}
diff --git a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/utils/LogCollectConfigUtilsTest.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/utils/LogCollectConfigUtilsTest.java
new file mode 100644
index 000000000..5f2c0bdc7
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/utils/LogCollectConfigUtilsTest.java
@@ -0,0 +1,117 @@
+/*
+ * 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.elasticsearch.utils;
+
+import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.plugin.api.context.ShenyuContext;
+import org.apache.shenyu.plugin.logging.elasticsearch.config.LogCollectConfig.GlobalLogConfig;
+import org.apache.shenyu.plugin.logging.elasticsearch.sampler.Sampler;
+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.Field;
+import java.net.InetSocketAddress;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * The Test Case For LogCollectConfigUtils.
+ */
+
+public final class LogCollectConfigUtilsTest {
+
+ private GlobalLogConfig config = new GlobalLogConfig();
+
+ private ServerWebExchange exchange;
+
+ private ServerHttpRequest request;
+
+ private Map<String, String> uriSampleMap = new HashMap<>();
+
+ private Map<String, String> apiTopicMap = new HashMap<>();
+
+ @BeforeEach
+ public void setUp() {
+ config.setBufferQueueSize(5000);
+ LogCollectConfigUtils.setGlobalConfig(config);
+ uriSampleMap.put("const", "1");
+ apiTopicMap.put("topic", "shenyu-access-logging");
+ 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 testGetGlobalConfig() {
+ GlobalLogConfig globalLogConfig = LogCollectConfigUtils.getGlobalLogConfig();
+ assertEquals(globalLogConfig.getClass(), GlobalLogConfig.class);
+ }
+
+ @Test
+ public void testSetGlobalConfig() {
+ assertEquals(LogCollectConfigUtils.getGlobalLogConfig().getBufferQueueSize(), 5000);
+ }
+
+ @Test
+ public void testSetSampler() throws IllegalAccessException, NoSuchFieldException {
+ LogCollectConfigUtils.setSampler(uriSampleMap);
+ Field field = LogCollectConfigUtils.class.getDeclaredField("apiSamplerMap");
+ field.setAccessible(true);
+ Assertions.assertEquals(field.get("const").toString(), "{const=" + Sampler.ALWAYS_SAMPLE + "}");
+ }
+
+ @Test
+ public void testIsSampled() {
+ assertEquals(LogCollectConfigUtils.isSampled(request), false);
+ }
+
+ @Test
+ public void testIsRequestBodyTooLarge() {
+ assertEquals(LogCollectConfigUtils.isRequestBodyTooLarge(524289), true);
+ assertEquals(LogCollectConfigUtils.isRequestBodyTooLarge(524288), false);
+ }
+
+ @Test
+ public void testIsResponseBodyTooLarge() {
+ assertEquals(LogCollectConfigUtils.isResponseBodyTooLarge(524289), true);
+ assertEquals(LogCollectConfigUtils.isResponseBodyTooLarge(524288), false);
+ }
+
+ @Test
+ public void testSetGlobalSampler() throws NoSuchFieldException, IllegalAccessException {
+ LogCollectConfigUtils.setGlobalSampler("0");
+ Field field = LogCollectConfigUtils.class.getDeclaredField("globalSampler");
+ field.setAccessible(true);
+ assertEquals(field.get("const"), Sampler.NEVER_SAMPLE);
+ }
+}
diff --git a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/utils/LogCollectUtilsTest.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/utils/LogCollectUtilsTest.java
new file mode 100644
index 000000000..d6e721dc8
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/utils/LogCollectUtilsTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.elasticsearch.utils;
+
+import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.plugin.api.context.ShenyuContext;
+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.net.InetSocketAddress;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * The Test Case For LogCollectUtils.
+ */
+public final class LogCollectUtilsTest {
+
+ private ServerWebExchange exchange;
+
+ private ServerHttpRequest request;
+
+ @BeforeEach
+ public void setUp() {
+ 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 testIsNotBinaryType() {
+ assertEquals(LogCollectUtils.isNotBinaryType(request.getHeaders()), true);
+ }
+
+ @Test
+ public void testGetHeaders() {
+ assertEquals(LogCollectUtils.getHeaders(request.getHeaders()), "{X-source:mock test}");
+ }
+}