From 6ad8da4f683ca024fac2879a70ee0ad29bbb575e Mon Sep 17 00:00:00 2001 From: Mark4z <36187602+mark4z@users.noreply.github.com> Date: Wed, 25 Nov 2020 15:01:18 +0800 Subject: [PATCH] fix service list can not search by groupName only (#4308) * fix service list can not search by groupName only * fix service list can not search by groupName only * fix checkStyle * add ut for NamingUtils --- .../nacos/api/naming/utils/NamingUtils.java | 29 +++++++++++++++++++ .../api/naming/utils/NamingUtilsTest.java | 25 ++++++++++++++++ .../naming/controllers/CatalogController.java | 2 +- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 api/src/test/java/com/alibaba/nacos/api/naming/utils/NamingUtilsTest.java diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java b/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java index 6259d0a87..0ab3bd615 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java @@ -27,6 +27,19 @@ import com.alibaba.nacos.api.utils.StringUtils; */ public class NamingUtils { + /** + * Returns a combined string with serviceName and groupName. serviceName can not be nil. + * + *
In most cases, serviceName can not be nil. In other cases, for search or anything, See {@link + * com.alibaba.nacos.api.naming.utils.NamingUtils#getGroupedNameOptional(String, String)} + * + *
etc: + *
serviceName | groupName | result
+ *serviceA | groupA | groupA@@serviceA
+ *nil | groupA | threw IllegalArgumentException
+ * + * @return 'groupName@@serviceName' + */ public static String getGroupedName(final String serviceName, final String groupName) { if (StringUtils.isBlank(serviceName)) { throw new IllegalArgumentException("Param 'serviceName' is illegal, serviceName is blank"); @@ -73,4 +86,20 @@ public class NamingUtils { "Param 'serviceName' is illegal, it should be format as 'groupName@@serviceName'"); } } + + /** + * Returns a combined string with serviceName and groupName. Such as 'groupName@@serviceName' + *This method works similar with {@link com.alibaba.nacos.api.naming.utils.NamingUtils#getGroupedName} But not verify any parameters. + * + *
etc: + *serviceName | groupName | result
+ *serviceA | groupA | groupA@@serviceA
+ *nil | groupA | groupA@@
+ *nil | nil | @@
+ * + * @return 'groupName@@serviceName' + */ + public static String getGroupedNameOptional(final String serviceName, final String groupName) { + return groupName + Constants.SERVICE_INFO_SPLITER + serviceName; + } } diff --git a/api/src/test/java/com/alibaba/nacos/api/naming/utils/NamingUtilsTest.java b/api/src/test/java/com/alibaba/nacos/api/naming/utils/NamingUtilsTest.java new file mode 100644 index 000000000..8c129f263 --- /dev/null +++ b/api/src/test/java/com/alibaba/nacos/api/naming/utils/NamingUtilsTest.java @@ -0,0 +1,25 @@ +package com.alibaba.nacos.api.naming.utils; + +import com.alibaba.nacos.api.utils.StringUtils; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * @author mark4z + * @date 2020/11/25 13:35 + */ +public class NamingUtilsTest{ + + @Test + public void testGetGroupedNameOptional() { + String onlyGroupName = NamingUtils.getGroupedNameOptional(StringUtils.EMPTY, "groupA"); + assertEquals(onlyGroupName, "groupA@@"); + + String onlyServiceName = NamingUtils.getGroupedNameOptional("serviceA" ,StringUtils.EMPTY); + assertEquals(onlyServiceName, "@@serviceA"); + + String GroupNameAndServiceName = NamingUtils.getGroupedNameOptional("serviceA" ,"groupA"); + assertEquals(GroupNameAndServiceName, "groupA@@serviceA"); + } +} \ No newline at end of file diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/CatalogController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/CatalogController.java index ce0992e8f..2e532f17b 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/CatalogController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/CatalogController.java @@ -187,7 +187,7 @@ public class CatalogController { @RequestParam(required = false) boolean hasIpCount) { String param = StringUtils.isBlank(serviceName) && StringUtils.isBlank(groupName) ? StringUtils.EMPTY - : NamingUtils.getGroupedName(serviceName, groupName); + : NamingUtils.getGroupedNameOptional(serviceName, groupName); if (withInstances) { List