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
This commit is contained in:
Mark4z 2020-11-25 15:01:18 +08:00 committed by GitHub
parent 70a26959e3
commit 6ad8da4f68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 1 deletions

View File

@ -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.
*
* <p>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)}
*
* <p>etc:
* <p>serviceName | groupName | result</p>
* <p>serviceA | groupA | groupA@@serviceA</p>
* <p>nil | groupA | threw IllegalArgumentException</p>
*
* @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'
* <p>This method works similar with {@link com.alibaba.nacos.api.naming.utils.NamingUtils#getGroupedName} But not verify any parameters.
*
* </p> etc:
* <p>serviceName | groupName | result</p>
* <p>serviceA | groupA | groupA@@serviceA</p>
* <p>nil | groupA | groupA@@</p>
* <p>nil | nil | @@</p>
*
* @return 'groupName@@serviceName'
*/
public static String getGroupedNameOptional(final String serviceName, final String groupName) {
return groupName + Constants.SERVICE_INFO_SPLITER + serviceName;
}
}

View File

@ -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");
}
}

View File

@ -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<ServiceDetailInfo> serviceDetailInfoList = new ArrayList<>();