You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by zh...@apache.org on 2023/06/13 07:41:02 UTC

[shenyu] branch master updated: [type:unit test]Adds SpringMvcApiMetaRegisterTest (#4728)

This is an automated email from the ASF dual-hosted git repository.

zhangzicheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 9553edf7f [type:unit test]Adds SpringMvcApiMetaRegisterTest (#4728)
9553edf7f is described below

commit 9553edf7f1dd656586cf6fd6805ccc4fe4cc50cf
Author: 愿凌飞 <ti...@foxmail.com>
AuthorDate: Tue Jun 13 15:40:54 2023 +0800

    [type:unit test]Adds SpringMvcApiMetaRegisterTest (#4728)
    
    Co-authored-by: tian-pengfei <ti...@apache.org>
    Co-authored-by: dragon-zhang <zh...@apache.org>
---
 .../register/SpringMvcApiMetaRegister.java         |   7 +-
 .../register/SpringMvcApiMetaRegisterTest.java     | 248 +++++++++++++++++++++
 2 files changed, 253 insertions(+), 2 deletions(-)

diff --git a/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiMetaRegister.java b/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiMetaRegister.java
index 29dd26957..411c4d27c 100644
--- a/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiMetaRegister.java
+++ b/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiMetaRegister.java
@@ -115,11 +115,14 @@ public class SpringMvcApiMetaRegister extends AbstractApiMetaRegistrar {
             methodPath = apiDefinition.getMethodPath();
         }
 
-        String apiPath = PathUtils.pathJoin(apiDefinition.getContextPath(), apiDefinition.getBeanPath(), methodPath);
-
         ShenyuSpringMvcClient classAnnotation = apiDefinition.getApiBean()
                 .getAnnotation(ShenyuSpringMvcClient.class);
 
+        String beanPath = Objects.isNull(classAnnotation) || StringUtils.isBlank(classAnnotation.path())
+                ? apiDefinition.getBeanPath() : classAnnotation.path();
+
+        String apiPath = PathUtils.pathJoin(apiDefinition.getContextPath(), beanPath, methodPath);
+
         String pathDesc = Objects.isNull(methodAnnotation) ? classAnnotation.desc() : methodAnnotation.desc();
 
         boolean enabled = (Objects.isNull(classAnnotation) || classAnnotation.enabled())
diff --git a/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiMetaRegisterTest.java b/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiMetaRegisterTest.java
new file mode 100644
index 000000000..0d2a08912
--- /dev/null
+++ b/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiMetaRegisterTest.java
@@ -0,0 +1,248 @@
+/*
+ * 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.client.springmvc.register;
+
+import org.apache.shenyu.client.core.disruptor.ShenyuClientRegisterEventPublisher;
+import org.apache.shenyu.client.core.register.ApiBean;
+import org.apache.shenyu.client.core.register.ClientRegisterConfig;
+import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
+import org.apache.shenyu.common.enums.RpcTypeEnum;
+import org.apache.shenyu.register.client.api.ShenyuClientRegisterRepository;
+import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
+import org.apache.shenyu.register.common.type.DataTypeParent;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.nullValue;
+
+public class SpringMvcApiMetaRegisterTest {
+
+    private TestShenyuClientRegisterEventPublisher testShenyuClientRegisterEventPublisher;
+
+    private SpringMvcApiMetaRegister springMvcApiMetaRegister;
+
+    @BeforeEach
+    public void init() {
+
+        testShenyuClientRegisterEventPublisher = new TestShenyuClientRegisterEventPublisher();
+
+        springMvcApiMetaRegister = new SpringMvcApiMetaRegister(
+                testShenyuClientRegisterEventPublisher, new TestClientRegisterConfig());
+    }
+
+    @Test
+    void testAnnotatedClass() throws Exception {
+
+        ApiBean apiBean = createSimpleApiBean(TestApiBeanAnnotatedClass.class);
+
+        springMvcApiMetaRegister.register(apiBean);
+
+        assertThat(testShenyuClientRegisterEventPublisher.metaData.getPath(),
+                equalTo("/testContext/testClass/testMethod"));
+    }
+
+    @Test
+    void testAnnotatedMethod() throws Exception {
+
+        ApiBean apiBean = createSimpleApiBean(TestApiBeanAnnotatedMethod.class);
+
+        springMvcApiMetaRegister.register(apiBean);
+
+        assertThat(testShenyuClientRegisterEventPublisher.metaData.getPath(),
+                equalTo("/testContext/testClass/testMethod"));
+    }
+
+    @Test
+    void testAnnotatedMethodAndClass() throws Exception {
+
+        ApiBean apiBean = createSimpleApiBean(TestApiBeanAnnotatedMethodAndClass.class);
+
+        springMvcApiMetaRegister.register(apiBean);
+
+        assertThat(testShenyuClientRegisterEventPublisher.metaData.getPath(),
+                equalTo("/testContext/testClass/testMethod"));
+    }
+
+    @Test
+    void testApiBeanNoAnnotated() throws Exception {
+
+        ApiBean apiBean = createSimpleApiBean(TestApiBeanNoAnnotated.class);
+
+        springMvcApiMetaRegister.register(apiBean);
+
+        assertThat(testShenyuClientRegisterEventPublisher.metaData, nullValue());
+    }
+
+    @Test
+    void testPreApiBean() throws Exception {
+
+        ApiBean apiBean = createSimpleApiBean(TestPreApiBean.class);
+
+        springMvcApiMetaRegister.register(apiBean);
+
+        assertThat(testShenyuClientRegisterEventPublisher.metaData.getPath(),
+                equalTo("/testContext/testClass/**"));
+
+    }
+
+    @Test
+    void testApiBeanDifferentPath() throws Exception {
+
+        ApiBean apiBean = createSimpleApiBean(TestApiBeanDifferentPath.class);
+
+        springMvcApiMetaRegister.register(apiBean);
+
+        assertThat(testShenyuClientRegisterEventPublisher.metaData.getPath(),
+                equalTo("/testContext/testClassPath/testMethodPath"));
+
+    }
+
+    private static ApiBean createSimpleApiBean(final Class<?> beanClass) throws Exception {
+
+        ApiBean apiBean = new ApiBean("testContext",
+                beanClass.getName(), beanClass.getDeclaredConstructor().newInstance(),
+                "/testClass", beanClass);
+
+        apiBean.addApiDefinition(beanClass.getMethod("testMethod"), "/testMethod");
+
+        return apiBean;
+    }
+
+    @ShenyuSpringMvcClient
+    @RestController
+    @RequestMapping("/testClass")
+    static class TestApiBeanAnnotatedClass {
+        @RequestMapping("/testMethod")
+        public String testMethod() {
+            return "";
+        }
+    }
+
+    @RestController
+    @RequestMapping("/testClass")
+    static class TestApiBeanAnnotatedMethod {
+
+        @RequestMapping("/testMethod")
+        @ShenyuSpringMvcClient("/testMethod")
+        public String testMethod() {
+            return "";
+        }
+    }
+
+    @ShenyuSpringMvcClient
+    @RestController
+    @RequestMapping("/testClass")
+    static class TestApiBeanAnnotatedMethodAndClass {
+
+        @RequestMapping("/testMethod")
+        @ShenyuSpringMvcClient("/testMethod")
+        public String testMethod() {
+            return "";
+        }
+    }
+
+    @RestController
+    @ShenyuSpringMvcClient("/testClass/**")
+    @RequestMapping("/testClass")
+    static class TestPreApiBean {
+
+        @RequestMapping("/testMethod")
+        @ShenyuSpringMvcClient
+        public String testMethod() {
+            return "";
+        }
+    }
+
+    @RestController
+    @RequestMapping("/testClass")
+    static class TestApiBeanNoAnnotated {
+
+        @RequestMapping("/testMethod")
+        public String testMethod() {
+            return "";
+        }
+    }
+
+    @RestController
+    @ShenyuSpringMvcClient("/testClassPath")
+    @RequestMapping("/testClass")
+    static class TestApiBeanDifferentPath {
+
+        @RequestMapping("/testMethod")
+        @ShenyuSpringMvcClient("/testMethodPath")
+        public String testMethod() {
+            return "";
+        }
+    }
+
+    static class TestShenyuClientRegisterEventPublisher extends ShenyuClientRegisterEventPublisher {
+
+        private MetaDataRegisterDTO metaData;
+
+        @Override
+        public void start(final ShenyuClientRegisterRepository shenyuClientRegisterRepository) {
+        }
+
+        @Override
+        public void publishEvent(final DataTypeParent data) {
+            this.metaData = (MetaDataRegisterDTO) data;
+        }
+    }
+
+    static class TestClientRegisterConfig implements ClientRegisterConfig {
+        @Override
+        public Integer getPort() {
+            return -1;
+        }
+
+        @Override
+        public String getHost() {
+            return "127.0.0.1";
+        }
+
+        @Override
+        public String getAppName() {
+            return "test";
+        }
+
+        @Override
+        public String getContextPath() {
+            return "testContext";
+        }
+
+        @Override
+        public String getIpAndPort() {
+            return "127.0.0.1:80";
+        }
+
+        @Override
+        public Boolean getAddPrefixed() {
+            return false;
+        }
+
+        @Override
+        public RpcTypeEnum getRpcTypeEnum() {
+            return RpcTypeEnum.HTTP;
+        }
+    }
+
+}