Skip to content

Commit 9b2158b

Browse files
authored
test: Enhance NacosRegistryServiceImplTest with additional mocks for service name group and cluster (#8003)
1 parent a1bb731 commit 9b2158b

File tree

7 files changed

+36
-16
lines changed

7 files changed

+36
-16
lines changed

.mvn/wrapper/maven-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17-
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
17+
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.9.5/apache-maven-3.9.5-bin.zip
1818
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar

changes/en-us/2.x.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ Add changes here for all PR submitted to the 2.x branch.
4949
### security:
5050

5151
### test:
52+
5253
- [[#7962](https://github.com/apache/incubator-seata/pull/7962)] add unit tests for NacosRegistryProvider and NacosRegistryServiceImpl
54+
- [[#8003](https://github.com/apache/incubator-seata/pull/8003)] Enhance NacosRegistryServiceImplTest with additional mocks for service name group and cluster
5355

5456
### refactor:
5557

changes/zh-cn/2.x.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@
5454

5555

5656
### test:
57+
5758
- [[#7962](https://github.com/apache/incubator-seata/pull/7962)] 为 NacosRegistryProvider 和 NacosRegistryServiceImpl 添加单元测试用例
59+
- [[#8003](https://github.com/apache/incubator-seata/pull/8003)] 为 NacosRegistryServiceImplTest 增加服务名称、分组和集群的额外模拟
60+
5861

5962
### refactor:
6063

discovery/seata-discovery-nacos/src/main/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImpl.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ public class NacosRegistryServiceImpl implements RegistryService<EventListener>
7272
private static final String PUBLIC_NAMING_ADDRESS_PREFIX = "public_";
7373
private static final String PUBLIC_NAMING_SERVICE_META_IP_KEY = "publicIp";
7474
private static final String PUBLIC_NAMING_SERVICE_META_PORT_KEY = "publicPort";
75-
private static final Configuration FILE_CONFIG = ConfigurationFactory.CURRENT_FILE_INSTANCE;
75+
// Use a method to retrieve file config dynamically to support test mocking on Java 17+
76+
private static Configuration getFileConfig() {
77+
return ConfigurationFactory.CURRENT_FILE_INSTANCE;
78+
}
79+
7680
private static volatile NamingService naming;
7781
private static final ConcurrentMap<String, List<EventListener>> LISTENER_SERVICE_MAP = new ConcurrentHashMap<>();
7882
private static final ConcurrentMap<String, List<InetSocketAddress>> CLUSTER_ADDRESS_MAP = new ConcurrentHashMap<>();
@@ -86,7 +90,7 @@ public class NacosRegistryServiceImpl implements RegistryService<EventListener>
8690
private String transactionServiceGroup;
8791

8892
private NacosRegistryServiceImpl() {
89-
String configForNacosSLB = FILE_CONFIG.getConfig(getNacosUrlPatternOfSLB());
93+
String configForNacosSLB = getFileConfig().getConfig(getNacosUrlPatternOfSLB());
9094
Pattern patternOfNacosRegistryForSLB = StringUtils.isBlank(configForNacosSLB)
9195
? DEFAULT_SLB_REGISTRY_PATTERN
9296
: Pattern.compile(configForNacosSLB);
@@ -278,15 +282,15 @@ private static Properties getNamingProperties() {
278282
if (System.getProperty(PRO_SERVER_ADDR_KEY) != null) {
279283
properties.setProperty(PRO_SERVER_ADDR_KEY, System.getProperty(PRO_SERVER_ADDR_KEY));
280284
} else {
281-
String address = FILE_CONFIG.getConfig(getNacosAddrFileKey());
285+
String address = getFileConfig().getConfig(getNacosAddrFileKey());
282286
if (address != null) {
283287
properties.setProperty(PRO_SERVER_ADDR_KEY, address);
284288
}
285289
}
286290
if (System.getProperty(PRO_NAMESPACE_KEY) != null) {
287291
properties.setProperty(PRO_NAMESPACE_KEY, System.getProperty(PRO_NAMESPACE_KEY));
288292
} else {
289-
String namespace = FILE_CONFIG.getConfig(getNacosNameSpaceFileKey());
293+
String namespace = getFileConfig().getConfig(getNacosNameSpaceFileKey());
290294
if (namespace == null) {
291295
namespace = DEFAULT_NAMESPACE;
292296
}
@@ -297,7 +301,7 @@ private static Properties getNamingProperties() {
297301
}
298302
String contextPath = StringUtils.isNotBlank(System.getProperty(CONTEXT_PATH))
299303
? System.getProperty(CONTEXT_PATH)
300-
: FILE_CONFIG.getConfig(getNacosContextPathKey());
304+
: getFileConfig().getConfig(getNacosContextPathKey());
301305
if (StringUtils.isNotBlank(contextPath)) {
302306
properties.setProperty(CONTEXT_PATH, contextPath);
303307
}
@@ -315,11 +319,11 @@ private static Properties getNamingProperties() {
315319
private static boolean initNacosAuthProperties(Properties sourceProperties) {
316320
String userName = StringUtils.isNotBlank(System.getProperty(USER_NAME))
317321
? System.getProperty(USER_NAME)
318-
: FILE_CONFIG.getConfig(getNacosUserName());
322+
: getFileConfig().getConfig(getNacosUserName());
319323
if (StringUtils.isNotBlank(userName)) {
320324
String password = StringUtils.isNotBlank(System.getProperty(PASSWORD))
321325
? System.getProperty(PASSWORD)
322-
: FILE_CONFIG.getConfig(getNacosPassword());
326+
: getFileConfig().getConfig(getNacosPassword());
323327
if (StringUtils.isNotBlank(password)) {
324328
sourceProperties.setProperty(USER_NAME, userName);
325329
sourceProperties.setProperty(PASSWORD, password);
@@ -329,14 +333,14 @@ private static boolean initNacosAuthProperties(Properties sourceProperties) {
329333
} else {
330334
String accessKey = StringUtils.isNotBlank(System.getProperty(ACCESS_KEY))
331335
? System.getProperty(ACCESS_KEY)
332-
: FILE_CONFIG.getConfig(getNacosAccessKey());
336+
: getFileConfig().getConfig(getNacosAccessKey());
333337
String ramRoleName = StringUtils.isNotBlank(System.getProperty(RAM_ROLE_NAME_KEY))
334338
? System.getProperty(RAM_ROLE_NAME_KEY)
335-
: FILE_CONFIG.getConfig(getNacosRamRoleNameKey());
339+
: getFileConfig().getConfig(getNacosRamRoleNameKey());
336340
if (StringUtils.isNotBlank(accessKey)) {
337341
String secretKey = StringUtils.isNotBlank(System.getProperty(SECRET_KEY))
338342
? System.getProperty(SECRET_KEY)
339-
: FILE_CONFIG.getConfig(getNacosSecretKey());
343+
: getFileConfig().getConfig(getNacosSecretKey());
340344
if (StringUtils.isNotBlank(secretKey)) {
341345
sourceProperties.put(ACCESS_KEY, accessKey);
342346
sourceProperties.put(SECRET_KEY, secretKey);
@@ -353,15 +357,15 @@ private static boolean initNacosAuthProperties(Properties sourceProperties) {
353357
}
354358

355359
private static String getClusterName() {
356-
return FILE_CONFIG.getConfig(getNacosClusterFileKey(), DEFAULT_CLUSTER);
360+
return getFileConfig().getConfig(getNacosClusterFileKey(), DEFAULT_CLUSTER);
357361
}
358362

359363
private static String getServiceName() {
360-
return FILE_CONFIG.getConfig(getNacosApplicationFileKey(), DEFAULT_APPLICATION);
364+
return getFileConfig().getConfig(getNacosApplicationFileKey(), DEFAULT_APPLICATION);
361365
}
362366

363367
private static String getServiceGroup() {
364-
return FILE_CONFIG.getConfig(getNacosApplicationGroupKey(), DEFAULT_GROUP);
368+
return getFileConfig().getConfig(getNacosApplicationGroupKey(), DEFAULT_GROUP);
365369
}
366370

367371
private static String getNacosAddrFileKey() {

discovery/seata-discovery-nacos/src/test/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImplTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,20 @@ public void beforeEach() throws Exception {
8888
when(mockedCurrentNacosConfiguration.getConfig(SLB_PATTERN_KEY)).thenReturn("");
8989
when(mockedCurrentNacosConfiguration.getConfig(SERVER_ADDR_KEY)).thenReturn("127.0.0.1");
9090

91+
// Mock for getServiceName() -> registry.nacos.application
92+
when(mockedCurrentNacosConfiguration.getConfig(APPLICATION_KEY)).thenReturn(NACOS_MOCKED_APPLICATION);
9193
when(mockedCurrentNacosConfiguration.getConfig(APPLICATION_KEY, "seata-server"))
9294
.thenReturn(NACOS_MOCKED_APPLICATION);
95+
96+
// Mock for getServiceGroup() -> registry.nacos.group
97+
when(mockedCurrentNacosConfiguration.getConfig(GROUP_KEY)).thenReturn(NACOS_MOCKED_GROUP);
9398
when(mockedCurrentNacosConfiguration.getConfig(GROUP_KEY, "DEFAULT_GROUP"))
9499
.thenReturn(NACOS_MOCKED_GROUP);
100+
101+
// Mock for getClusterName() -> registry.nacos.cluster
102+
when(mockedCurrentNacosConfiguration.getConfig(CLUSTER_KEY)).thenReturn(NACOS_MOCKED_CLUSTER);
95103
when(mockedCurrentNacosConfiguration.getConfig(CLUSTER_KEY, "default")).thenReturn(NACOS_MOCKED_CLUSTER);
104+
96105
when(mockedCurrentNacosConfiguration.getConfig(CONTEXT_PATH_KEY)).thenReturn("/foo");
97106

98107
nacosRegistryService = NacosRegistryServiceImpl.getInstance();

server/src/test/java/org/apache/seata/server/coordinator/AbstractCoreTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.junit.jupiter.api.Assertions;
3333
import org.junit.jupiter.api.BeforeAll;
3434
import org.junit.jupiter.api.Test;
35+
import org.springframework.context.ApplicationContext;
3536

3637
import java.util.Collection;
3738

@@ -53,7 +54,7 @@ public class AbstractCoreTest extends BaseSpringBootTest {
5354
private static final String applicationData = "{\"data\":\"test\"}";
5455

5556
@BeforeAll
56-
public static void initSessionManager() throws Exception {
57+
public static void initSessionManager(ApplicationContext context) throws Exception {
5758
SessionHolder.init(SessionMode.FILE);
5859
remotingServer = new DefaultCoordinatorTest.MockServerMessageSender();
5960
abstractCore = new TestableAbstractCore(remotingServer);

server/src/test/java/org/apache/seata/server/coordinator/RaftCoordinatorTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.junit.jupiter.api.Test;
2929
import org.mockito.MockedStatic;
3030
import org.mockito.Mockito;
31+
import org.springframework.context.ApplicationContext;
3132

3233
/**
3334
* The type Raft coordinator test.
@@ -41,7 +42,7 @@ public class RaftCoordinatorTest extends BaseSpringBootTest {
4142
private static final String ANOTHER_GROUP = "another_group";
4243

4344
@BeforeAll
44-
public static void setup() throws Exception {
45+
public static void setup(ApplicationContext context) throws Exception {
4546
SessionHolder.init(SessionMode.FILE);
4647
remotingServer = new DefaultCoordinatorTest.MockServerMessageSender();
4748
raftCoordinator = new RaftCoordinator(remotingServer);

0 commit comments

Comments
 (0)