This is an automated email from the ASF dual-hosted git repository.
liuhongyu 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 d0994fa94d feat: add shenyu-register-client-api unit test (#6192)
d0994fa94d is described below
commit d0994fa94d97c40e611e16d10a1f8a33651cb52b
Author: shown <[email protected]>
AuthorDate: Tue Sep 30 20:38:57 2025 +0800
feat: add shenyu-register-client-api unit test (#6192)
Signed-off-by: yuluo-yx <[email protected]>
Co-authored-by: moremind <[email protected]>
---
.idea/vcs.xml | 35 +--
.../shenyu-register-client-api/pom.xml | 17 ++
.../client/api/FailbackRegistryRepositoryTest.java | 274 +++++++++++++++++++++
.../client/api/retry/FailureRegistryTaskTest.java | 165 +++++++++++++
4 files changed, 460 insertions(+), 31 deletions(-)
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
index a8bded7fa4..35eb1ddfbb 100644
--- a/.idea/vcs.xml
+++ b/.idea/vcs.xml
@@ -1,33 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
-<!--
- ~ 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.
- -->
-
<project version="4">
- <component name="IssueNavigationConfiguration">
- <option name="links">
- <list>
- <IssueNavigationLink>
- <option name="issueRegexp" value="#(\d+)"/>
- <option name="linkRegexp"
value="https://github.com/apache/shenyu/pull/$1"/>
- </IssueNavigationLink>
- </list>
- </option>
- </component>
- <component name="VcsDirectoryMappings">
- <mapping directory="$PROJECT_DIR$" vcs="Git"/>
- </component>
-</project>
+ <component name="VcsDirectoryMappings">
+ <mapping directory="" vcs="Git" />
+ </component>
+</project>
\ No newline at end of file
diff --git
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/pom.xml
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/pom.xml
index 7204c99d6f..c8844b0de0 100644
---
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/pom.xml
+++
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/pom.xml
@@ -36,6 +36,23 @@
<artifactId>shenyu-spi</artifactId>
<version>${project.version}</version>
</dependency>
+
+ <!-- Test Dependencies -->
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-junit-jupiter</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/test/java/org/apache/shenyu/register/client/api/FailbackRegistryRepositoryTest.java
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/test/java/org/apache/shenyu/register/client/api/FailbackRegistryRepositoryTest.java
new file mode 100644
index 0000000000..b4761e8f9b
--- /dev/null
+++
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/test/java/org/apache/shenyu/register/client/api/FailbackRegistryRepositoryTest.java
@@ -0,0 +1,274 @@
+/*
+ * 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.register.client.api;
+
+import org.apache.shenyu.register.common.dto.ApiDocRegisterDTO;
+import org.apache.shenyu.register.common.dto.McpToolsRegisterDTO;
+import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
+import org.apache.shenyu.register.common.dto.URIRegisterDTO;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Field;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+/**
+ * Test case for {@link FailbackRegistryRepository}.
+ */
+public final class FailbackRegistryRepositoryTest {
+
+ private TestFailbackRegistryRepository repository;
+
+ @BeforeEach
+ public void setUp() {
+ repository = spy(new TestFailbackRegistryRepository());
+ }
+
+ @Test
+ public void testPersistInterfaceSuccess() {
+ MetaDataRegisterDTO metadata = createMetaDataRegisterDTO();
+
+ repository.persistInterface(metadata);
+
+ verify(repository, times(1)).doPersistInterface(metadata);
+ assertEquals(0, getFailureMapSize());
+ }
+
+ @Test
+ public void testPersistInterfaceFailure() {
+ MetaDataRegisterDTO metadata = createMetaDataRegisterDTO();
+ doThrow(new RuntimeException("Test
exception")).when(repository).doPersistInterface(any());
+
+ repository.persistInterface(metadata);
+
+ verify(repository, times(1)).doPersistInterface(metadata);
+ assertEquals(1, getFailureMapSize());
+ }
+
+ @Test
+ public void testPersistURISuccess() {
+ URIRegisterDTO registerDTO = createURIRegisterDTO();
+
+ repository.persistURI(registerDTO);
+
+ verify(repository, times(1)).doPersistURI(registerDTO);
+ assertEquals(0, getFailureMapSize());
+ }
+
+ @Test
+ public void testPersistURIFailure() {
+ URIRegisterDTO registerDTO = createURIRegisterDTO();
+ doThrow(new RuntimeException("Test
exception")).when(repository).doPersistURI(any());
+
+ repository.persistURI(registerDTO);
+
+ verify(repository, times(1)).doPersistURI(registerDTO);
+ /*
+ * The original code has a bug where it checks for ApiDocRegisterDTO
instead of URIRegisterDTO
+ * So the failure won't be added to the map
+ */
+ assertEquals(0, getFailureMapSize());
+ }
+
+ @Test
+ public void testPersistApiDocSuccess() {
+ ApiDocRegisterDTO registerDTO = createApiDocRegisterDTO();
+
+ repository.persistApiDoc(registerDTO);
+
+ verify(repository, times(1)).doPersistApiDoc(registerDTO);
+ assertEquals(0, getFailureMapSize());
+ }
+
+ @Test
+ public void testPersistApiDocFailure() {
+ ApiDocRegisterDTO registerDTO = createApiDocRegisterDTO();
+ doThrow(new RuntimeException("Test
exception")).when(repository).doPersistApiDoc(any());
+
+ repository.persistApiDoc(registerDTO);
+
+ verify(repository, times(1)).doPersistApiDoc(registerDTO);
+ assertEquals(1, getFailureMapSize());
+ }
+
+ @Test
+ public void testPersistMcpToolsSuccess() {
+ McpToolsRegisterDTO registerDTO = createMcpToolsRegisterDTO();
+
+ repository.persistMcpTools(registerDTO);
+
+ verify(repository, times(1)).doPersistMcpTools(registerDTO);
+ assertEquals(0, getFailureMapSize());
+ }
+
+ @Test
+ public void testPersistMcpToolsFailure() {
+ McpToolsRegisterDTO registerDTO = createMcpToolsRegisterDTO();
+ doThrow(new RuntimeException("Test
exception")).when(repository).doPersistMcpTools(any());
+
+ repository.persistMcpTools(registerDTO);
+
+ verify(repository, times(1)).doPersistMcpTools(registerDTO);
+ assertEquals(1, getFailureMapSize());
+ }
+
+ @Test
+ public void testRemove() {
+ // First add a failure
+ MetaDataRegisterDTO metadata = createMetaDataRegisterDTO();
+ doThrow(new RuntimeException("Test
exception")).when(repository).doPersistInterface(any());
+ repository.persistInterface(metadata);
+
+ assertEquals(1, getFailureMapSize());
+
+ /* Get the key and remove it */
+ String key = getFirstKeyFromFailureMap();
+ assertNotNull(key);
+
+ repository.remove(key);
+ assertEquals(0, getFailureMapSize());
+ }
+
+ @Test
+ public void testAcceptWithNonExistentKey() {
+ repository.accept("non-existent-key");
+ /* Should not throw any exception */
+ }
+
+ @Test
+ public void testAcceptWithUnknownType() throws Exception {
+ // Manually add an entry with unknown type
+ Map<String, Object> failureMap = getFailureMap();
+ Object holder = createHolder(new Object(), "test-path",
"unknown-type");
+ failureMap.put("test-key", holder);
+
+ repository.accept("test-key");
+ /* Should not throw any exception */
+ }
+
+ private MetaDataRegisterDTO createMetaDataRegisterDTO() {
+ return MetaDataRegisterDTO.builder()
+ .appName("testApp")
+ .path("/test")
+ .rpcType("http")
+ .host("127.0.0.1")
+ .port(8080)
+ .build();
+ }
+
+ private URIRegisterDTO createURIRegisterDTO() {
+ return URIRegisterDTO.builder()
+ .appName("testApp")
+ .host("127.0.0.1")
+ .port(8080)
+ .rpcType("http")
+ .build();
+ }
+
+ private ApiDocRegisterDTO createApiDocRegisterDTO() {
+ return ApiDocRegisterDTO.builder()
+ .contextPath("/api")
+ .apiPath("/test")
+ .httpMethod(0)
+ .rpcType("http")
+ .build();
+ }
+
+ private McpToolsRegisterDTO createMcpToolsRegisterDTO() {
+ MetaDataRegisterDTO metaData = createMetaDataRegisterDTO();
+ McpToolsRegisterDTO mcpToolsRegisterDTO = new McpToolsRegisterDTO();
+ mcpToolsRegisterDTO.setMetaDataRegisterDTO(metaData);
+
+ return mcpToolsRegisterDTO;
+ }
+
+ @SuppressWarnings("unchecked")
+ private int getFailureMapSize() {
+ try {
+ Field field =
FailbackRegistryRepository.class.getDeclaredField("concurrentHashMap");
+ field.setAccessible(true);
+ Map<String, Object> map = (Map<String, Object>)
field.get(repository);
+ return map.size();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private String getFirstKeyFromFailureMap() {
+ try {
+ Field field =
FailbackRegistryRepository.class.getDeclaredField("concurrentHashMap");
+ field.setAccessible(true);
+ Map<String, Object> map = (Map<String, Object>)
field.get(repository);
+ return map.keySet().iterator().hasNext() ?
map.keySet().iterator().next() : null;
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private Map<String, Object> getFailureMap() {
+ try {
+ Field field =
FailbackRegistryRepository.class.getDeclaredField("concurrentHashMap");
+ field.setAccessible(true);
+ return (Map<String, Object>) field.get(repository);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private Object createHolder(final Object obj, final String path, final
String type) throws Exception {
+ Class<?> holderClass =
Class.forName("org.apache.shenyu.register.client.api.FailbackRegistryRepository$Holder");
+ return holderClass.getDeclaredConstructor(Object.class, String.class,
String.class)
+ .newInstance(obj, path, type);
+ }
+
+ /**
+ * Test implementation of FailbackRegistryRepository.
+ */
+ private static class TestFailbackRegistryRepository extends
FailbackRegistryRepository {
+
+ @Override
+ protected void doPersistApiDoc(final ApiDocRegisterDTO
apiDocRegisterDTO) {
+ /* Test implementation */
+ }
+
+ @Override
+ protected void doPersistURI(final URIRegisterDTO registerDTO) {
+ /* Test implementation */
+ }
+
+ @Override
+ protected void doPersistInterface(final MetaDataRegisterDTO
registerDTO) {
+ /* Test implementation */
+ }
+
+ @Override
+ protected void doPersistMcpTools(final McpToolsRegisterDTO
registerDTO) {
+ /* Test implementation */
+ }
+ }
+}
diff --git
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/test/java/org/apache/shenyu/register/client/api/retry/FailureRegistryTaskTest.java
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/test/java/org/apache/shenyu/register/client/api/retry/FailureRegistryTaskTest.java
new file mode 100644
index 0000000000..594fcaa125
--- /dev/null
+++
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/test/java/org/apache/shenyu/register/client/api/retry/FailureRegistryTaskTest.java
@@ -0,0 +1,165 @@
+/*
+ * 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.register.client.api.retry;
+
+import org.apache.shenyu.common.timer.TimerTask;
+import org.apache.shenyu.register.client.api.FailbackRegistryRepository;
+import org.apache.shenyu.register.common.dto.ApiDocRegisterDTO;
+import org.apache.shenyu.register.common.dto.McpToolsRegisterDTO;
+import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
+import org.apache.shenyu.register.common.dto.URIRegisterDTO;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+/**
+ * Test case for {@link FailureRegistryTask}.
+ */
+public final class FailureRegistryTaskTest {
+
+ private static final String TEST_KEY = "test-key";
+
+ @Mock
+ private FailbackRegistryRepository mockRepository;
+
+ @Mock
+ private TimerTask mockTimerTask;
+
+ private FailureRegistryTask failureRegistryTask;
+
+ @BeforeEach
+ public void setUp() {
+ MockitoAnnotations.openMocks(this);
+ failureRegistryTask = new FailureRegistryTask(TEST_KEY,
mockRepository);
+ }
+
+
+ @Test
+ public void testDoRetry() {
+ doNothing().when(mockRepository).accept(anyString());
+ doNothing().when(mockRepository).remove(anyString());
+
+ failureRegistryTask.doRetry(TEST_KEY, mockTimerTask);
+
+ verify(mockRepository, times(1)).accept(TEST_KEY);
+ verify(mockRepository, times(1)).remove(TEST_KEY);
+ }
+
+ @Test
+ public void testDoRetryWithException() {
+
+ doNothing().when(mockRepository).accept(anyString());
+ doNothing().when(mockRepository).remove(anyString());
+
+ // This should not throw an exception
+ failureRegistryTask.doRetry(TEST_KEY, mockTimerTask);
+
+ verify(mockRepository, times(1)).accept(TEST_KEY);
+ verify(mockRepository, times(1)).remove(TEST_KEY);
+ }
+
+ @Test
+ public void testMultipleRetries() {
+
+ doNothing().when(mockRepository).accept(anyString());
+ doNothing().when(mockRepository).remove(anyString());
+
+ // Test multiple retry calls
+ for (int i = 0; i < 3; i++) {
+ failureRegistryTask.doRetry(TEST_KEY, mockTimerTask);
+ }
+
+ verify(mockRepository, times(3)).accept(TEST_KEY);
+ verify(mockRepository, times(3)).remove(TEST_KEY);
+ }
+
+ @Test
+ public void testDifferentKeys() {
+ final String key1 = "key1";
+ final String key2 = "key2";
+
+ doNothing().when(mockRepository).accept(anyString());
+ doNothing().when(mockRepository).remove(anyString());
+
+ failureRegistryTask.doRetry(key1, mockTimerTask);
+ failureRegistryTask.doRetry(key2, mockTimerTask);
+
+ verify(mockRepository, times(1)).accept(key1);
+ verify(mockRepository, times(1)).remove(key1);
+ verify(mockRepository, times(1)).accept(key2);
+ verify(mockRepository, times(1)).remove(key2);
+ }
+
+ @Test
+ public void testTaskWithDifferentRepository() {
+ TestFailbackRegistryRepository testRepository = new
TestFailbackRegistryRepository();
+ FailureRegistryTask task = new FailureRegistryTask("test",
testRepository);
+
+ task.doRetry("test", mockTimerTask);
+
+ assertTrue(testRepository.acceptCalled);
+ assertTrue(testRepository.removeCalled);
+ }
+
+ /**
+ * Test implementation of FailbackRegistryRepository for testing.
+ */
+ private static class TestFailbackRegistryRepository extends
FailbackRegistryRepository {
+
+ private boolean acceptCalled;
+
+ private boolean removeCalled;
+
+ @Override
+ public void accept(final String key) {
+ acceptCalled = true;
+ }
+
+ @Override
+ public void remove(final String key) {
+ removeCalled = true;
+ }
+
+ @Override
+ protected void doPersistApiDoc(final ApiDocRegisterDTO
apiDocRegisterDTO) {
+ /* Test implementation */
+ }
+
+ @Override
+ protected void doPersistURI(final URIRegisterDTO registerDTO) {
+ /* Test implementation */
+ }
+
+ @Override
+ protected void doPersistInterface(final MetaDataRegisterDTO
registerDTO) {
+ /* Test implementation */
+ }
+
+ @Override
+ protected void doPersistMcpTools(final McpToolsRegisterDTO
registerDTO) {
+ /* Test implementation */
+ }
+ }
+}