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 161625cd7e feat: add sync data api unit test (#6142)
161625cd7e is described below
commit 161625cd7e4f4fba15267a0d943e6420f858031e
Author: shown <[email protected]>
AuthorDate: Mon Sep 8 14:51:39 2025 +0800
feat: add sync data api unit test (#6142)
Signed-off-by: yuluo-yx <[email protected]>
Co-authored-by: aias00 <[email protected]>
---
.../data/core/AbstractNodeDataSyncServiceTest.java | 160 +++++++++++++++++++++
.../data/core/AbstractPathDataSyncServiceTest.java | 119 +++++++++++++++
2 files changed, 279 insertions(+)
diff --git
a/shenyu-sync-data-center/shenyu-sync-data-api/src/test/java/org/apache/shenyu/sync/data/core/AbstractNodeDataSyncServiceTest.java
b/shenyu-sync-data-center/shenyu-sync-data-api/src/test/java/org/apache/shenyu/sync/data/core/AbstractNodeDataSyncServiceTest.java
new file mode 100644
index 0000000000..6348c08fb9
--- /dev/null
+++
b/shenyu-sync-data-center/shenyu-sync-data-api/src/test/java/org/apache/shenyu/sync/data/core/AbstractNodeDataSyncServiceTest.java
@@ -0,0 +1,160 @@
+/*
+ * 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.sync.data.core;
+
+import org.apache.shenyu.common.config.ShenyuConfig;
+import org.apache.shenyu.common.dto.PluginData;
+import org.apache.shenyu.sync.data.api.AuthDataSubscriber;
+import org.apache.shenyu.sync.data.api.DiscoveryUpstreamDataSubscriber;
+import org.apache.shenyu.sync.data.api.MetaDataSubscriber;
+import org.apache.shenyu.sync.data.api.PluginDataSubscriber;
+import org.apache.shenyu.sync.data.api.ProxySelectorDataSubscriber;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Consumer;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.verify;
+
+@RunWith(MockitoJUnitRunner.class)
+public class AbstractNodeDataSyncServiceTest {
+
+ @Mock
+ private AbstractNodeDataSyncService.ChangeData changeData;
+
+ @Mock
+ private PluginDataSubscriber pluginDataSubscriber;
+
+ @Mock
+ private List<MetaDataSubscriber> metaDataSubscribers;
+
+ @Mock
+ private List<AuthDataSubscriber> authDataSubscribers;
+
+ @Mock
+ private List<ProxySelectorDataSubscriber> proxySelectorDataSubscribers;
+
+ @Mock
+ private List<DiscoveryUpstreamDataSubscriber>
discoveryUpstreamDataSubscribers;
+
+ @Mock
+ private ShenyuConfig shenyuConfig;
+
+ @Mock
+ private AuthDataSubscriber authDataSubscriber;
+
+ private AbstractNodeDataSyncService nodeDataSyncService;
+
+ @Before
+ public void setUp() {
+
+ MockitoAnnotations.openMocks(this);
+
+ authDataSubscribers = new ArrayList<>();
+ authDataSubscribers.add(authDataSubscriber);
+
+ nodeDataSyncService = new AbstractNodeDataSyncServiceImpl(
+ changeData,
+ pluginDataSubscriber,
+ metaDataSubscribers,
+ authDataSubscribers,
+ proxySelectorDataSubscribers,
+ discoveryUpstreamDataSubscribers,
+ shenyuConfig
+ );
+ }
+
+ @Test
+ public void testCachePluginData() {
+
+ String jsonData = "{\"name\":\"testPlugin\"}";
+
+ nodeDataSyncService.cachePluginData(jsonData);
+
+ ArgumentCaptor<PluginData> captor =
ArgumentCaptor.forClass(PluginData.class);
+ verify(pluginDataSubscriber).onSubscribe(captor.capture());
+
+ assertEquals("testPlugin", captor.getValue().getName());
+ }
+
+ @Test
+ public void testUnCachePluginData() {
+
+ String pluginName = "testPlugin";
+
+ nodeDataSyncService.unCachePluginData(pluginName);
+
+ ArgumentCaptor<PluginData> captor =
ArgumentCaptor.forClass(PluginData.class);
+ verify(pluginDataSubscriber).unSubscribe(captor.capture());
+ assertEquals(pluginName, captor.getValue().getName());
+ }
+
+ @Test
+ public void testCacheAuthData() {
+
+ String jsonData = "{\"appKey\":\"testApp\"}";
+
+ nodeDataSyncService.cacheAuthData(jsonData);
+
+ verify(authDataSubscribers.get(0)).onSubscribe(any());
+ }
+
+ // Mock implementation
+ static class AbstractNodeDataSyncServiceImpl extends
AbstractNodeDataSyncService {
+
+ AbstractNodeDataSyncServiceImpl(final ChangeData changeData,
+ final PluginDataSubscriber
pluginDataSubscriber,
+ final List<MetaDataSubscriber>
metaDataSubscribers,
+ final List<AuthDataSubscriber>
authDataSubscribers,
+ final
List<ProxySelectorDataSubscriber> proxySelectorDataSubscribers,
+ final
List<DiscoveryUpstreamDataSubscriber> discoveryUpstreamDataSubscribers,
+ final ShenyuConfig shenyuConfig
+ ) {
+
+ super(changeData, pluginDataSubscriber, metaDataSubscribers,
authDataSubscribers,
+ proxySelectorDataSubscribers,
discoveryUpstreamDataSubscribers, shenyuConfig);
+ }
+
+ @Override
+ protected String getServiceConfig(
+ final String key,
+ final Consumer<String> updateHandler,
+ final Consumer<String> deleteHandler
+ ) {
+
+ // Mocked response for testing
+ return "";
+ }
+
+ @Override
+ protected void doRemoveListener(final String removeKey) {
+
+ // No implementation needed for testing
+ }
+ }
+
+}
diff --git
a/shenyu-sync-data-center/shenyu-sync-data-api/src/test/java/org/apache/shenyu/sync/data/core/AbstractPathDataSyncServiceTest.java
b/shenyu-sync-data-center/shenyu-sync-data-api/src/test/java/org/apache/shenyu/sync/data/core/AbstractPathDataSyncServiceTest.java
new file mode 100644
index 0000000000..bfb80e0fcc
--- /dev/null
+++
b/shenyu-sync-data-center/shenyu-sync-data-api/src/test/java/org/apache/shenyu/sync/data/core/AbstractPathDataSyncServiceTest.java
@@ -0,0 +1,119 @@
+/*
+ * 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.sync.data.core;
+
+import org.apache.shenyu.common.dto.AppAuthData;
+import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.sync.data.api.AuthDataSubscriber;
+import org.apache.shenyu.sync.data.api.DiscoveryUpstreamDataSubscriber;
+import org.apache.shenyu.sync.data.api.MetaDataSubscriber;
+import org.apache.shenyu.sync.data.api.PluginDataSubscriber;
+import org.apache.shenyu.sync.data.api.ProxySelectorDataSubscriber;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.verify;
+
+@RunWith(MockitoJUnitRunner.class)
+public class AbstractPathDataSyncServiceTest {
+
+ @Mock
+ private PluginDataSubscriber pluginDataSubscriber;
+
+ @Mock
+ private MetaDataSubscriber metaDataSubscriber;
+
+ @Mock
+ private AuthDataSubscriber authDataSubscriber;
+
+ @Mock
+ private ProxySelectorDataSubscriber proxySelectorDataSubscriber;
+
+ @Mock
+ private DiscoveryUpstreamDataSubscriber discoveryUpstreamDataSubscriber;
+
+ private AbstractPathDataSyncService pathDataSyncService;
+
+ @Before
+ public void setUp() {
+
+ MockitoAnnotations.openMocks(this);
+
+ List<AuthDataSubscriber> authDataSubscribers = new ArrayList<>();
+ authDataSubscribers.add(authDataSubscriber);
+
+ pathDataSyncService = new AbstractPathDataSyncServiceImpl(
+ pluginDataSubscriber,
+ List.of(metaDataSubscriber),
+ authDataSubscribers,
+ List.of(proxySelectorDataSubscriber),
+ List.of(discoveryUpstreamDataSubscriber)
+ );
+ }
+
+ @Test
+ public void testUnCacheAuthData() {
+
+ String jsonData = "{\"appKey\":\"testApp\"}";
+
+
pathDataSyncService.cacheAuthData(GsonUtils.getInstance().fromJson(jsonData,
AppAuthData.class));
+ pathDataSyncService.unCacheAuthData("/namespace/auths/testApp");
+
+ verify(authDataSubscriber).unSubscribe(any());
+ }
+
+ // Mock implementation
+ static class AbstractPathDataSyncServiceImpl extends
AbstractPathDataSyncService {
+
+ AbstractPathDataSyncServiceImpl(final PluginDataSubscriber
pluginDataSubscriber,
+ final List<MetaDataSubscriber>
metaDataSubscribers,
+ final List<AuthDataSubscriber>
authDataSubscribers,
+ final
List<ProxySelectorDataSubscriber> proxySelectorDataSubscribers,
+ final
List<DiscoveryUpstreamDataSubscriber> discoveryUpstreamDataSubscribers) {
+
+ super(pluginDataSubscriber, metaDataSubscribers,
authDataSubscribers,
+ proxySelectorDataSubscribers,
discoveryUpstreamDataSubscribers);
+ }
+
+ @Override
+ public void event(
+ final String namespaceId,
+ final String updatePath,
+ final String updateData,
+ final String registerPath,
+ final EventType eventType
+ ) {
+
+ super.event(namespaceId, updatePath, updateData, registerPath,
eventType);
+ }
+
+ @Override
+ public void close() throws Exception {
+ // for test.
+ }
+ }
+
+}