Skip to content

Commit

Permalink
[type:fix] fix namespace sql error
Browse files Browse the repository at this point in the history
  • Loading branch information
moremind committed Oct 18, 2024
1 parent 0a29b90 commit 8b3eb15
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public final class ZookeeperDataChangedListenerTest {
*/
@Test
public void testOnAppAuthChangedCreate() {
AppAuthData appAuthData = AppAuthData.builder().appKey(MOCK_APP_KEY).appSecret(MOCK_APP_SECRET).build();
AppAuthData appAuthData = AppAuthData.builder().appKey(MOCK_APP_KEY).appSecret(MOCK_APP_SECRET).namespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID).build();
String appAuthPath = DefaultPathConstants.buildAppAuthPath(Constants.SYS_DEFAULT_NAMESPACE_ID, appAuthData.getAppKey());

zookeeperDataChangedListener.onAppAuthChanged(ImmutableList.of(appAuthData), DataEventTypeEnum.CREATE);
Expand All @@ -88,7 +88,7 @@ public void testOnAppAuthChangedCreate() {
*/
@Test
public void testOnAppAuthChangedUpdate() {
AppAuthData appAuthData = AppAuthData.builder().appKey(MOCK_APP_KEY).appSecret(MOCK_APP_SECRET).build();
AppAuthData appAuthData = AppAuthData.builder().appKey(MOCK_APP_KEY).appSecret(MOCK_APP_SECRET).namespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID).build();
String appAuthPath = DefaultPathConstants.buildAppAuthPath(Constants.SYS_DEFAULT_NAMESPACE_ID, appAuthData.getAppKey());

zookeeperDataChangedListener.onAppAuthChanged(ImmutableList.of(appAuthData), DataEventTypeEnum.UPDATE);
Expand All @@ -100,7 +100,7 @@ public void testOnAppAuthChangedUpdate() {
*/
@Test
public void testOnAppAuthChangedDelete() {
AppAuthData appAuthData = AppAuthData.builder().appKey(MOCK_APP_KEY).appSecret(MOCK_APP_SECRET).build();
AppAuthData appAuthData = AppAuthData.builder().appKey(MOCK_APP_KEY).appSecret(MOCK_APP_SECRET).namespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID).build();
String appAuthPath = DefaultPathConstants.buildAppAuthPath(Constants.SYS_DEFAULT_NAMESPACE_ID, appAuthData.getAppKey());

when(zkClient.isExist(appAuthPath)).thenReturn(true);
Expand All @@ -113,7 +113,7 @@ public void testOnAppAuthChangedDelete() {
*/
@Test
public void testOnMetaDataChangedCreate() throws UnsupportedEncodingException {
MetaData metaData = MetaData.builder().id(MOCK_ID).path(MOCK_PATH).appName(MOCK_APP_NAME).build();
MetaData metaData = MetaData.builder().id(MOCK_ID).path(MOCK_PATH).appName(MOCK_APP_NAME).namespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID).build();
String metaDataPath = DefaultPathConstants.buildMetaDataPath(Constants.SYS_DEFAULT_NAMESPACE_ID, URLEncoder.encode(metaData.getPath(), StandardCharsets.UTF_8));

zookeeperDataChangedListener.onMetaDataChanged(ImmutableList.of(metaData), DataEventTypeEnum.CREATE);
Expand All @@ -125,7 +125,7 @@ public void testOnMetaDataChangedCreate() throws UnsupportedEncodingException {
*/
@Test
public void testOnMetaDataChangedUpdate() throws UnsupportedEncodingException {
MetaData metaData = MetaData.builder().id(MOCK_ID).path(MOCK_PATH).appName(MOCK_APP_NAME).build();
MetaData metaData = MetaData.builder().id(MOCK_ID).path(MOCK_PATH).appName(MOCK_APP_NAME).namespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID).build();
String metaDataPath = DefaultPathConstants.buildMetaDataPath(Constants.SYS_DEFAULT_NAMESPACE_ID, URLEncoder.encode(metaData.getPath(), StandardCharsets.UTF_8));

zookeeperDataChangedListener.onMetaDataChanged(ImmutableList.of(metaData), DataEventTypeEnum.UPDATE);
Expand All @@ -137,7 +137,7 @@ public void testOnMetaDataChangedUpdate() throws UnsupportedEncodingException {
*/
@Test
public void testOnMetaDataChangedDelete() throws UnsupportedEncodingException {
MetaData metaData = MetaData.builder().id(MOCK_ID).path(MOCK_PATH).appName(MOCK_APP_NAME).build();
MetaData metaData = MetaData.builder().id(MOCK_ID).path(MOCK_PATH).appName(MOCK_APP_NAME).namespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID).build();
String metaDataPath = DefaultPathConstants.buildMetaDataPath(Constants.SYS_DEFAULT_NAMESPACE_ID, URLEncoder.encode(metaData.getPath(), StandardCharsets.UTF_8));

when(zkClient.isExist(metaDataPath)).thenReturn(true);
Expand All @@ -150,7 +150,7 @@ public void testOnMetaDataChangedDelete() throws UnsupportedEncodingException {
*/
@Test
public void testOnPluginChangedCreate() {
PluginData pluginData = PluginData.builder().id(MOCK_ID).name(MOCK_NAME).config(MOCK_CONFIG).build();
PluginData pluginData = PluginData.builder().id(MOCK_ID).name(MOCK_NAME).namespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID).config(MOCK_CONFIG).build();
String pluginPath = DefaultPathConstants.buildPluginPath(Constants.SYS_DEFAULT_NAMESPACE_ID, pluginData.getName());

zookeeperDataChangedListener.onPluginChanged(ImmutableList.of(pluginData), DataEventTypeEnum.CREATE);
Expand All @@ -162,7 +162,7 @@ public void testOnPluginChangedCreate() {
*/
@Test
public void testOnPluginChangedUpdate() {
PluginData pluginData = PluginData.builder().id(MOCK_ID).name(MOCK_NAME).config(MOCK_CONFIG).build();
PluginData pluginData = PluginData.builder().id(MOCK_ID).name(MOCK_NAME).namespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID).config(MOCK_CONFIG).build();
String pluginPath = DefaultPathConstants.buildPluginPath(Constants.SYS_DEFAULT_NAMESPACE_ID, pluginData.getName());

zookeeperDataChangedListener.onPluginChanged(ImmutableList.of(pluginData), DataEventTypeEnum.UPDATE);
Expand All @@ -174,7 +174,7 @@ public void testOnPluginChangedUpdate() {
*/
@Test
public void testOnPluginChangedDelete() {
PluginData pluginData = PluginData.builder().id(MOCK_ID).name(MOCK_NAME).config(MOCK_CONFIG).build();
PluginData pluginData = PluginData.builder().id(MOCK_ID).name(MOCK_NAME).namespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID).config(MOCK_CONFIG).build();
String pluginPath = DefaultPathConstants.buildPluginPath(Constants.SYS_DEFAULT_NAMESPACE_ID, pluginData.getName());
String selectorParentPath = DefaultPathConstants.buildSelectorParentPath(Constants.SYS_DEFAULT_NAMESPACE_ID, pluginData.getName());
String ruleParentPath = DefaultPathConstants.buildRuleParentPath(Constants.SYS_DEFAULT_NAMESPACE_ID, pluginData.getName());
Expand All @@ -194,7 +194,7 @@ public void testOnPluginChangedDelete() {
*/
@Test
public void testOnSelectorChangedCreate() {
SelectorData selectorData = SelectorData.builder().id(MOCK_ID).name(MOCK_NAME).pluginName(MOCK_PLUGIN_NAME).build();
SelectorData selectorData = SelectorData.builder().id(MOCK_ID).name(MOCK_NAME).namespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID).pluginName(MOCK_PLUGIN_NAME).build();

String selectorRealPath = DefaultPathConstants.buildSelectorRealPath(Constants.SYS_DEFAULT_NAMESPACE_ID, selectorData.getPluginName(), selectorData.getId());

Expand All @@ -207,7 +207,7 @@ public void testOnSelectorChangedCreate() {
*/
@Test
public void testOnSelectorChangedUpdate() {
SelectorData selectorData = SelectorData.builder().id(MOCK_ID).name(MOCK_NAME).pluginName(MOCK_PLUGIN_NAME).build();
SelectorData selectorData = SelectorData.builder().id(MOCK_ID).name(MOCK_NAME).namespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID).pluginName(MOCK_PLUGIN_NAME).build();

String selectorRealPath = DefaultPathConstants.buildSelectorRealPath(Constants.SYS_DEFAULT_NAMESPACE_ID, selectorData.getPluginName(), selectorData.getId());

Expand All @@ -220,7 +220,7 @@ public void testOnSelectorChangedUpdate() {
*/
@Test
public void testOnSelectorChangedRefresh() {
SelectorData selectorData = SelectorData.builder().id(MOCK_ID).name(MOCK_NAME).pluginName(MOCK_PLUGIN_NAME).build();
SelectorData selectorData = SelectorData.builder().id(MOCK_ID).name(MOCK_NAME).namespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID).pluginName(MOCK_PLUGIN_NAME).build();
String selectorParentPath = DefaultPathConstants.buildSelectorParentPath(Constants.SYS_DEFAULT_NAMESPACE_ID, selectorData.getPluginName());

when(zkClient.isExist(selectorParentPath)).thenReturn(true);
Expand All @@ -233,7 +233,7 @@ public void testOnSelectorChangedRefresh() {
*/
@Test
public void testOnSelectorChangedDelete() {
SelectorData selectorData = SelectorData.builder().id(MOCK_ID).name(MOCK_NAME).pluginName(MOCK_PLUGIN_NAME).build();
SelectorData selectorData = SelectorData.builder().id(MOCK_ID).name(MOCK_NAME).namespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID).pluginName(MOCK_PLUGIN_NAME).build();
String selectorRealPath = DefaultPathConstants.buildSelectorRealPath(Constants.SYS_DEFAULT_NAMESPACE_ID, selectorData.getPluginName(), selectorData.getId());

when(zkClient.isExist(selectorRealPath)).thenReturn(true);
Expand All @@ -251,6 +251,7 @@ public void testOnRuleChangedCreate() {
.name(MOCK_NAME)
.pluginName(MOCK_PLUGIN_NAME)
.selectorId(MOCK_SELECTOR_ID)
.namespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID)
.build();
String ruleRealPath = DefaultPathConstants.buildRulePath(Constants.SYS_DEFAULT_NAMESPACE_ID, ruleData.getPluginName(), ruleData.getSelectorId(), ruleData.getId());

Expand All @@ -268,6 +269,7 @@ public void testOnRuleChangedUpdate() {
.name(MOCK_NAME)
.pluginName(MOCK_PLUGIN_NAME)
.selectorId(MOCK_SELECTOR_ID)
.namespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID)
.build();
String ruleRealPath = DefaultPathConstants.buildRulePath(Constants.SYS_DEFAULT_NAMESPACE_ID, ruleData.getPluginName(), ruleData.getSelectorId(),
ruleData.getId());
Expand All @@ -286,6 +288,7 @@ public void testOnRuleChangedRefresh() {
.name(MOCK_NAME)
.pluginName(MOCK_PLUGIN_NAME)
.selectorId(MOCK_SELECTOR_ID)
.namespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID)
.build();
String ruleParentPath = DefaultPathConstants.buildRuleParentPath(Constants.SYS_DEFAULT_NAMESPACE_ID, ruleData.getPluginName());

Expand All @@ -304,6 +307,7 @@ public void testOnRuleChangedDelete() {
.name(MOCK_NAME)
.pluginName(MOCK_PLUGIN_NAME)
.selectorId(MOCK_SELECTOR_ID)
.namespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID)
.build();
String ruleRealPath = DefaultPathConstants.buildRulePath(Constants.SYS_DEFAULT_NAMESPACE_ID, ruleData.getPluginName(), ruleData.getSelectorId(), ruleData.getId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.constant.ConsulConstants;
import org.apache.shenyu.common.constant.DefaultPathConstants;
import static org.apache.shenyu.common.constant.DefaultPathConstants.handlePathData;
import org.apache.shenyu.sync.data.api.AuthDataSubscriber;
import org.apache.shenyu.sync.data.api.DiscoveryUpstreamDataSubscriber;
import org.apache.shenyu.sync.data.api.MetaDataSubscriber;
Expand All @@ -50,6 +49,8 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static org.apache.shenyu.common.constant.DefaultPathConstants.handlePathData;

/**
* Consul sync data service.
*/
Expand Down Expand Up @@ -81,7 +82,8 @@ public class ConsulSyncDataService extends AbstractPathDataSyncService {
* @param metaDataSubscribers the meta data subscribers
* @param authDataSubscribers the auth data subscribers
*/
public ConsulSyncDataService(final ShenyuConfig shenyuConfig, final ConsulClient consulClient,
public ConsulSyncDataService(final ShenyuConfig shenyuConfig,
final ConsulClient consulClient,
final ConsulConfig consulConfig,
final PluginDataSubscriber pluginDataSubscriber,
final List<MetaDataSubscriber> metaDataSubscribers,
Expand All @@ -99,7 +101,7 @@ public ConsulSyncDataService(final ShenyuConfig shenyuConfig, final ConsulClient
}

private void watcherData() {
String configNamespace = Constants.PATH_SEPARATOR + shenyuConfig.getNamespace();
String configNamespace = Constants.PATH_SEPARATOR + this.shenyuConfig.getNamespace();
watcherData0(handlePathData(String.join(Constants.PATH_SEPARATOR, configNamespace, DefaultPathConstants.PLUGIN_PARENT)));
watcherData0(handlePathData(String.join(Constants.PATH_SEPARATOR, configNamespace, DefaultPathConstants.SELECTOR_PARENT)));
watcherData0(handlePathData(String.join(Constants.PATH_SEPARATOR, configNamespace, DefaultPathConstants.RULE_PARENT)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.kv.model.GetValue;
import org.apache.shenyu.common.config.ShenyuConfig;
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.sync.data.consul.config.ConsulConfig;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -62,6 +64,9 @@ public final class ConsulSyncDataServiceTest {
@Mock
private ConsulConfig consulConfig;

@Mock
private ShenyuConfig shenyuConfig;

@InjectMocks
private ConsulSyncDataService consulSyncDataService;

Expand Down Expand Up @@ -91,6 +96,11 @@ public void testWatchConfigKeyValues() throws NoSuchMethodException, IllegalAcce
executorField.setAccessible(true);
executorField.set(consulSyncDataService, mock(ScheduledThreadPoolExecutor.class));

final Field shenyuConfigField = ConsulSyncDataService.class.getDeclaredField("shenyuConfig");
shenyuConfigField.setAccessible(true);
final ShenyuConfig shenyuConfig = mock(ShenyuConfig.class);
shenyuConfigField.set(consulSyncDataService, shenyuConfig);

final Response<List<GetValue>> response = mock(Response.class);

when(consulClient.getKVValues(any(), any(), any())).thenReturn(response);
Expand All @@ -101,7 +111,7 @@ public void testWatchConfigKeyValues() throws NoSuchMethodException, IllegalAcce
Consumer<String> deleteHandler = removeKey -> {

};
String watchPathRoot = "/shenyu";
String watchPathRoot = "/" + Constants.SYS_DEFAULT_NAMESPACE_ID + "/shenyu";
Assertions.assertDoesNotThrow(() -> watchConfigKeyValues.invoke(consulSyncDataService, watchPathRoot, updateHandler, deleteHandler));

List<GetValue> getValues = new ArrayList<>(1);
Expand Down

0 comments on commit 8b3eb15

Please sign in to comment.