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/27 16:27:41 UTC

[incubator-shenyu] branch master updated: add logging-elasticsearch starter test (#3626)

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 01c0853a6 add logging-elasticsearch starter test (#3626)
01c0853a6 is described below

commit 01c0853a6008a7830f1589507a5d88cb79983f72
Author: qinghai777 <80...@users.noreply.github.com>
AuthorDate: Tue Jun 28 00:27:32 2022 +0800

    add logging-elasticsearch starter test (#3626)
---
 .../ElasticSearchLogCollectClient.java             |  9 +--
 ...oggingElasticSearchPluginConfigurationTest.java | 64 ++++++++++++++++++++++
 2 files changed, 69 insertions(+), 4 deletions(-)

diff --git a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/main/java/org/apache/shenyu/plugin/logging/elasticsearch/elasticsearch/ElasticSearchLogCollectClient.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/main/java/org/apache/shenyu/plugin/logging/elasticsearch/elasticsearch/ElasticSearchLogCollectClient.java
index 357097e7a..baa62dd27 100644
--- a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/main/java/org/apache/shenyu/plugin/logging/elasticsearch/elasticsearch/ElasticSearchLogCollectClient.java
+++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/main/java/org/apache/shenyu/plugin/logging/elasticsearch/elasticsearch/ElasticSearchLogCollectClient.java
@@ -67,15 +67,16 @@ public class ElasticSearchLogCollectClient implements LogConsumeClient {
         transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
         client = new ElasticsearchClient(transport);
         LOG.info("init ElasticSearchLogCollectClient success");
+        if (!existsIndex(LoggingConstant.INDEX)) {
+            createIndex(LoggingConstant.INDEX);
+            LOG.info("create index success");
+        }
         isStarted.set(true);
         Runtime.getRuntime().addShutdownHook(new Thread(this::close));
     }
 
     @Override
     public void consume(final List<ShenyuRequestLog> logs) throws Exception {
-        if (!existsIndex(LoggingConstant.INDEX)) {
-            createIndex(LoggingConstant.INDEX);
-        }
         if (CollectionUtils.isEmpty(logs) || !isStarted.get()) {
             return;
         }
@@ -107,7 +108,7 @@ public class ElasticSearchLogCollectClient implements LogConsumeClient {
     }
 
     /**
-     * create elasticsearch client.
+     * create elasticsearch index.
      *
      * @param indexName index name
      * @return true or false
diff --git a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/springboot/starter/plugin/logging/elasticsearch/LoggingElasticSearchPluginConfigurationTest.java b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/springboot/starter/plugin/logging/elasticsearch/LoggingElasticSearchPluginConfigura [...]
new file mode 100644
index 000000000..dcee1ffbd
--- /dev/null
+++ b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/springboot/starter/plugin/logging/elasticsearch/LoggingElasticSearchPluginConfigurationTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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.springboot.starter.plugin.logging.elasticsearch;
+
+import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.plugin.api.ShenyuPlugin;
+import org.apache.shenyu.plugin.base.handler.PluginDataHandler;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.autoconfigure.AutoConfigurations;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.boot.test.context.runner.ApplicationContextRunner;
+import org.springframework.context.annotation.Configuration;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+/**
+ * logging elasticsearch plugin config test.
+ */
+@Configuration
+@EnableConfigurationProperties
+public class LoggingElasticSearchPluginConfigurationTest {
+
+    private ApplicationContextRunner applicationContextRunner;
+
+    @BeforeEach
+    public void before() {
+        applicationContextRunner = new ApplicationContextRunner()
+                .withConfiguration(AutoConfigurations.of(LoggingElasticSearchPluginConfiguration.class))
+                .withBean(LoggingElasticSearchPluginConfigurationTest.class);
+    }
+
+    @Test
+    public void testLoggingElasticSearchPlugin() {
+        applicationContextRunner
+                .withPropertyValues(
+                        "debug=true",
+                        "shenyu.logging.elasticsearch.enabled=true"
+                )
+                .run(context -> {
+                    PluginDataHandler pluginDataHandler = context.getBean("loggingElasticSearchPluginDataHandler", PluginDataHandler.class);
+                    assertNotNull(pluginDataHandler);
+                    ShenyuPlugin plugin = context.getBean("loggingElasticSearchPlugin", ShenyuPlugin.class);
+                    assertNotNull(plugin);
+                    assertThat(plugin.named()).isEqualTo(PluginEnum.LOGGING_ElasticSearch.getName());
+                });
+    }
+}