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 870b03605 [type:Integration Test] Add cache-plugin Integration Test (#3333) 870b03605 is described below commit 870b0360582f79776eaa20d8645efcd269d837d8 Author: erdengk <37730787+erde...@users.noreply.github.com> AuthorDate: Tue Apr 26 16:47:04 2022 +0800 [type:Integration Test] Add cache-plugin Integration Test (#3333) * add cache test * Revert "add cache test" This reverts commit 2b8f8e390d4d70938a253d0b19709a92786ffb77. * add cache-plugin test * fix checkstyle error --- .../http/controller/HttpTestController.java | 14 ++++ .../shenyu-integrated-test-http/pom.xml | 8 ++ .../test/http/combination/CachePluginTest.java | 92 ++++++++++++++++++++++ 3 files changed, 114 insertions(+) diff --git a/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/HttpTestController.java b/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/HttpTestController.java index cc8cad7a2..3ab9c1ac1 100644 --- a/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/HttpTestController.java +++ b/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/HttpTestController.java @@ -51,6 +51,7 @@ import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -393,4 +394,17 @@ public class HttpTestController { response.setMsg("fallback"); return response; } + + /** + * test pass for cache plugin. + * + * @return response. result bean + */ + @GetMapping("/cache") + public ResultBean testCache() { + ResultBean response = new ResultBean(); + response.setCode(200); + response.setMsg(new Date().toString()); + return response; + } } diff --git a/shenyu-integrated-test/shenyu-integrated-test-http/pom.xml b/shenyu-integrated-test/shenyu-integrated-test-http/pom.xml index 1b155215d..9faec073f 100644 --- a/shenyu-integrated-test/shenyu-integrated-test-http/pom.xml +++ b/shenyu-integrated-test/shenyu-integrated-test-http/pom.xml @@ -138,6 +138,14 @@ </dependency> <!-- apache shenyu hystrix plugin end--> + <!--shenyu cache plugin start--> + <dependency> + <groupId>org.apache.shenyu</groupId> + <artifactId>shenyu-spring-boot-starter-plugin-cache</artifactId> + <version>${project.version}</version> + </dependency> + <!--shenyu cache plugin end--> + <dependency> <groupId>org.apache.shenyu</groupId> <artifactId>shenyu-integrated-test-common</artifactId> diff --git a/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/CachePluginTest.java b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/CachePluginTest.java new file mode 100644 index 000000000..54713c1eb --- /dev/null +++ b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/CachePluginTest.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.integrated.test.http.combination; + +import org.apache.shenyu.common.dto.ConditionData; +import org.apache.shenyu.common.dto.convert.rule.impl.CacheRuleHandle; +import org.apache.shenyu.common.enums.OperatorEnum; +import org.apache.shenyu.common.enums.ParamTypeEnum; +import org.apache.shenyu.common.enums.PluginEnum; +import org.apache.shenyu.common.utils.JsonUtils; +import org.apache.shenyu.integratedtest.common.AbstractPluginDataInit; +import org.apache.shenyu.integratedtest.common.helper.HttpHelper; +import org.apache.shenyu.integratedtest.common.result.ResultBean; +import org.apache.shenyu.web.controller.LocalPluginController.RuleLocalData; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public final class CachePluginTest extends AbstractPluginDataInit { + + private static final String TEST_CACHE_PATH = "/http/test/cache"; + + @BeforeEach + public void setup() throws IOException { + String pluginResult = initPlugin(PluginEnum.CACHE.getName(), + "{\"cacheType\":\"memory\"}"); + assertThat(pluginResult, is("success")); + String selectorAndRulesResult = + initSelectorAndRules(PluginEnum.CACHE.getName(), "", buildSelectorConditionList(), buildRuleLocalDataList()); + assertThat(selectorAndRulesResult, is("success")); + } + + @Test + public void testPass() throws ExecutionException, InterruptedException { + Future<ResultBean> resp0 = this.getService().submit(() -> HttpHelper.INSTANCE.getFromGateway(TEST_CACHE_PATH, ResultBean.class)); + Thread.sleep(2000); + Future<ResultBean> resp1 = this.getService().submit(() -> HttpHelper.INSTANCE.getFromGateway(TEST_CACHE_PATH, ResultBean.class)); + assertEquals(resp0.get().getMsg(), resp1.get().getMsg()); + } + + private static List<ConditionData> buildSelectorConditionList() { + ConditionData conditionData = new ConditionData(); + conditionData.setParamType(ParamTypeEnum.URI.getName()); + conditionData.setOperator(OperatorEnum.EQ.getAlias()); + conditionData.setParamValue("/http/test/cache"); + return Collections.singletonList(conditionData); + } + + private static List<RuleLocalData> buildRuleLocalDataList() { + final RuleLocalData ruleLocalData = new RuleLocalData(); + CacheRuleHandle cacheRuleHandle = new CacheRuleHandle(); + cacheRuleHandle.setTimeoutSeconds(3000L); + ConditionData conditionData = new ConditionData(); + conditionData.setParamType(ParamTypeEnum.URI.getName()); + conditionData.setOperator(OperatorEnum.EQ.getAlias()); + conditionData.setParamName("testCache"); + conditionData.setParamValue("/http/test/cache"); + ruleLocalData.setConditionDataList(Collections.singletonList(conditionData)); + ruleLocalData.setRuleHandler(JsonUtils.toJson(cacheRuleHandle)); + return Collections.singletonList(ruleLocalData); + } + + @AfterEach + public void clean() throws IOException { + cleanPluginData(PluginEnum.CACHE.getName()); + } +}