This is an automated email from the ASF dual-hosted git repository. sunnianjun pushed a commit to branch 5.4.1.1-release in repository https://gitbox.apache.org/repos/asf/shardingsphere-plugin.git
commit b7e56729cdef936d0292b8b3e4a236e6825acae7 Author: 孙念君 Nianjun Sun <[email protected]> AuthorDate: Fri Dec 15 15:26:17 2023 +0800 Fix : remove compile issue tests --- infra/data-source-pool/c3p0/pom.xml | 13 - .../creator/C3P0DataSourcePoolCreatorTest.java | 61 ---- infra/data-source-pool/dbcp/pom.xml | 13 - .../creator/DBCPDataSourcePoolCreatorTest.java | 66 ---- jdbc/driver/apollo/pom.xml | 7 - mode/cluster/repository/nacos/pom.xml | 7 - .../cluster/nacos/NacosRepositoryTest.java | 377 --------------------- .../cluster/nacos/props/NacosPropertiesTest.java | 62 ---- 8 files changed, 606 deletions(-) diff --git a/infra/data-source-pool/c3p0/pom.xml b/infra/data-source-pool/c3p0/pom.xml index c03651d..8d2447f 100644 --- a/infra/data-source-pool/c3p0/pom.xml +++ b/infra/data-source-pool/c3p0/pom.xml @@ -38,19 +38,6 @@ <artifactId>c3p0</artifactId> <version>${c3p0.version}</version> </dependency> - - <dependency> - <groupId>org.apache.shardingsphere</groupId> - <artifactId>shardingsphere-test-fixture-database</artifactId> - <version>${shardingsphere.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.shardingsphere</groupId> - <artifactId>shardingsphere-test-util</artifactId> - <version>${shardingsphere.version}</version> - <scope>test</scope> - </dependency> </dependencies> <build> diff --git a/infra/data-source-pool/c3p0/src/test/java/org/apache/shardingsphere/infra/datasource/pool/c3p0/creator/C3P0DataSourcePoolCreatorTest.java b/infra/data-source-pool/c3p0/src/test/java/org/apache/shardingsphere/infra/datasource/pool/c3p0/creator/C3P0DataSourcePoolCreatorTest.java deleted file mode 100644 index 7e4e2f5..0000000 --- a/infra/data-source-pool/c3p0/src/test/java/org/apache/shardingsphere/infra/datasource/pool/c3p0/creator/C3P0DataSourcePoolCreatorTest.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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.shardingsphere.infra.datasource.pool.c3p0.creator; - -import com.mchange.v2.c3p0.ComboPooledDataSource; -import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator; -import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; -import org.junit.jupiter.api.Test; - -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; - -class C3P0DataSourcePoolCreatorTest { - - @Test - void assertCreateDataSource() { - ComboPooledDataSource actual = (ComboPooledDataSource) DataSourcePoolCreator.create(new DataSourcePoolProperties(ComboPooledDataSource.class.getName(), createDataSourcePoolProperties())); - assertThat(actual.getJdbcUrl(), is("jdbc:mock://127.0.0.1/foo_ds")); - assertThat(actual.getUser(), is("root")); - assertThat(actual.getPassword(), is("root")); - Properties props = new Properties(); - props.put("foo", "foo_value"); - props.put("bar", "bar_value"); - props.put("password", "root"); - props.put("user", "root"); - assertThat(actual.getProperties(), is(props)); - } - - private Map<String, Object> createDataSourcePoolProperties() { - Map<String, Object> result = new HashMap<>(); - result.put("url", "jdbc:mock://127.0.0.1/foo_ds"); - result.put("driverClassName", MockedDataSource.class.getName()); - result.put("username", "root"); - result.put("password", "root"); - Properties props = new Properties(); - props.put("foo", "foo_value"); - props.put("bar", "bar_value"); - result.put("properties", props); - return result; - } -} diff --git a/infra/data-source-pool/dbcp/pom.xml b/infra/data-source-pool/dbcp/pom.xml index 5d3fde0..c1bc29e 100644 --- a/infra/data-source-pool/dbcp/pom.xml +++ b/infra/data-source-pool/dbcp/pom.xml @@ -38,19 +38,6 @@ <artifactId>commons-dbcp2</artifactId> <version>${commons-dbcp2.version}</version> </dependency> - - <dependency> - <groupId>org.apache.shardingsphere</groupId> - <artifactId>shardingsphere-test-fixture-database</artifactId> - <version>${shardingsphere.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.shardingsphere</groupId> - <artifactId>shardingsphere-test-util</artifactId> - <version>${shardingsphere.version}</version> - <scope>test</scope> - </dependency> </dependencies> <build> diff --git a/infra/data-source-pool/dbcp/src/test/java/org/apache/shardingsphere/infra/datasource/pool/dbcp/creator/DBCPDataSourcePoolCreatorTest.java b/infra/data-source-pool/dbcp/src/test/java/org/apache/shardingsphere/infra/datasource/pool/dbcp/creator/DBCPDataSourcePoolCreatorTest.java deleted file mode 100644 index cc637a9..0000000 --- a/infra/data-source-pool/dbcp/src/test/java/org/apache/shardingsphere/infra/datasource/pool/dbcp/creator/DBCPDataSourcePoolCreatorTest.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 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.shardingsphere.infra.datasource.pool.dbcp.creator; - -import lombok.SneakyThrows; -import org.apache.commons.dbcp2.BasicDataSource; -import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator; -import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; -import org.junit.jupiter.api.Test; -import org.mockito.internal.configuration.plugins.Plugins; - -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; - -class DBCPDataSourcePoolCreatorTest { - - @Test - void assertCreateDataSource() { - BasicDataSource actual = (BasicDataSource) DataSourcePoolCreator.create(new DataSourcePoolProperties(BasicDataSource.class.getName(), createDataSourcePoolProperties())); - assertThat(actual.getUrl(), is("jdbc:mock://127.0.0.1/foo_ds")); - assertThat(actual.getUsername(), is("root")); - assertThat(actual.getPassword(), is("root")); - Properties props = new Properties(); - props.put("foo", "foo_value"); - props.put("bar", "bar_value"); - assertThat(getConnectionProperties(actual), is(props)); - } - - private Map<String, Object> createDataSourcePoolProperties() { - Map<String, Object> result = new HashMap<>(); - result.put("url", "jdbc:mock://127.0.0.1/foo_ds"); - result.put("driverClassName", MockedDataSource.class.getName()); - result.put("username", "root"); - result.put("password", "root"); - Properties props = new Properties(); - props.put("foo", "foo_value"); - props.put("bar", "bar_value"); - result.put("connectionProperties", props); - return result; - } - - @SneakyThrows(ReflectiveOperationException.class) - private Properties getConnectionProperties(final BasicDataSource actual) { - return (Properties) Plugins.getMemberAccessor().get(BasicDataSource.class.getDeclaredField("connectionProperties"), actual); - } -} diff --git a/jdbc/driver/apollo/pom.xml b/jdbc/driver/apollo/pom.xml index ed4c006..ef31181 100644 --- a/jdbc/driver/apollo/pom.xml +++ b/jdbc/driver/apollo/pom.xml @@ -37,13 +37,6 @@ <version>${shardingsphere.version}</version> </dependency> - <dependency> - <groupId>org.apache.shardingsphere</groupId> - <artifactId>shardingsphere-test-util</artifactId> - <version>${shardingsphere.version}</version> - <scope>test</scope> - </dependency> - <dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> diff --git a/mode/cluster/repository/nacos/pom.xml b/mode/cluster/repository/nacos/pom.xml index 0c00aa1..d7d59d8 100644 --- a/mode/cluster/repository/nacos/pom.xml +++ b/mode/cluster/repository/nacos/pom.xml @@ -37,13 +37,6 @@ <version>${shardingsphere.version}</version> </dependency> - <dependency> - <groupId>org.apache.shardingsphere</groupId> - <artifactId>shardingsphere-test-util</artifactId> - <version>${shardingsphere.version}</version> - <scope>test</scope> - </dependency> - <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> diff --git a/mode/cluster/repository/nacos/src/test/java/org/apache/shardingsphere/mode/repository/cluster/nacos/NacosRepositoryTest.java b/mode/cluster/repository/nacos/src/test/java/org/apache/shardingsphere/mode/repository/cluster/nacos/NacosRepositoryTest.java deleted file mode 100644 index efccc41..0000000 --- a/mode/cluster/repository/nacos/src/test/java/org/apache/shardingsphere/mode/repository/cluster/nacos/NacosRepositoryTest.java +++ /dev/null @@ -1,377 +0,0 @@ -/* - * 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.shardingsphere.mode.repository.cluster.nacos; - -import com.alibaba.nacos.api.exception.NacosException; -import com.alibaba.nacos.api.naming.NamingService; -import com.alibaba.nacos.api.naming.PreservedMetadataKeys; -import com.alibaba.nacos.api.naming.listener.Event; -import com.alibaba.nacos.api.naming.listener.EventListener; -import com.alibaba.nacos.api.naming.listener.NamingEvent; -import com.alibaba.nacos.api.naming.pojo.Instance; -import com.google.common.util.concurrent.SettableFuture; -import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.repository.cluster.exception.ClusterPersistRepositoryException; -import org.apache.shardingsphere.mode.repository.cluster.nacos.entity.ServiceController; -import org.apache.shardingsphere.mode.repository.cluster.nacos.entity.ServiceMetaData; -import org.apache.shardingsphere.mode.repository.cluster.nacos.props.NacosProperties; -import org.apache.shardingsphere.mode.repository.cluster.nacos.props.NacosPropertyKey; -import org.apache.shardingsphere.mode.repository.cluster.nacos.util.NacosMetaDataUtils; -import org.apache.shardingsphere.mode.spi.PersistRepository; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.AdditionalAnswers; -import org.mockito.ArgumentCaptor; -import org.mockito.Mock; -import org.mockito.internal.configuration.plugins.Plugins; -import org.mockito.junit.jupiter.MockitoExtension; -import org.mockito.plugins.MemberAccessor; -import org.mockito.stubbing.VoidAnswer2; - -import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Properties; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.atomic.AtomicInteger; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -class NacosRepositoryTest { - - private static final NacosRepository REPOSITORY = new NacosRepository(); - - @Mock - private NamingService client; - - private ServiceController serviceController; - - @BeforeEach - void initClient() throws ReflectiveOperationException { - MemberAccessor accessor = Plugins.getMemberAccessor(); - accessor.set(REPOSITORY.getClass().getDeclaredField("nacosProps"), REPOSITORY, new NacosProperties(new Properties())); - accessor.set(REPOSITORY.getClass().getDeclaredField("client"), REPOSITORY, client); - accessor.invoke(REPOSITORY.getClass().getDeclaredMethod("initServiceMetaData"), REPOSITORY); - serviceController = (ServiceController) accessor.get(REPOSITORY.getClass().getDeclaredField("serviceController"), REPOSITORY); - } - - @Test - void assertGetLatestKey() throws NacosException { - int total = 2; - String key = "/test/children/keys/persistent/1"; - List<Instance> instances = new LinkedList<>(); - for (int count = 1; count <= total; count++) { - Instance instance = new Instance(); - Map<String, String> metaDataMap = new HashMap<>(2, 1F); - metaDataMap.put(key, "value" + count); - metaDataMap.put(NacosMetaDataUtils.UTC_ZONE_OFFSET.toString(), String.valueOf(count)); - instance.setMetadata(metaDataMap); - instances.add(instance); - } - ServiceMetaData persistentService = serviceController.getPersistentService(); - when(client.getAllInstances(persistentService.getServiceName(), false)).thenReturn(instances); - String value = REPOSITORY.getDirectly(key); - assertThat(value, is("value2")); - } - - @Test - void assertGetChildrenKeys() throws NacosException { - Instance instance = new Instance(); - String key = "/test/children/keys/persistent/0"; - instance.setMetadata(Collections.singletonMap(key, "value0")); - ServiceMetaData persistentService = serviceController.getPersistentService(); - when(client.getAllInstances(persistentService.getServiceName(), false)).thenReturn(Collections.singletonList(instance)); - instance = new Instance(); - key = "/test/children/keys/ephemeral/0"; - instance.setMetadata(Collections.singletonMap(key, "value0")); - ServiceMetaData ephemeralService = serviceController.getEphemeralService(); - when(client.getAllInstances(ephemeralService.getServiceName(), false)).thenReturn(Collections.singletonList(instance)); - List<String> childrenKeys = REPOSITORY.getChildrenKeys("/test/children/keys"); - assertThat(childrenKeys.size(), is(2)); - assertThat(childrenKeys.get(0), is("persistent")); - assertThat(childrenKeys.get(1), is("ephemeral")); - } - - @Test - void assertPersistNotExistKey() throws NacosException { - String key = "/test/children/keys/persistent/1"; - doAnswer(AdditionalAnswers.answerVoid(getRegisterInstanceAnswer())).when(client).registerInstance(anyString(), any(Instance.class)); - REPOSITORY.persist(key, "value4"); - ArgumentCaptor<Instance> instanceArgumentCaptor = ArgumentCaptor.forClass(Instance.class); - ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class); - verify(client, times(5)).registerInstance(stringArgumentCaptor.capture(), instanceArgumentCaptor.capture()); - Instance registerInstance = instanceArgumentCaptor.getValue(); - String registerType = stringArgumentCaptor.getValue(); - ServiceMetaData persistentService = serviceController.getPersistentService(); - assertThat(registerType, is(persistentService.getServiceName())); - assertThat(registerInstance.isEphemeral(), is(false)); - assertThat(NacosMetaDataUtils.getValue(registerInstance), is("value4")); - } - - @Test - void assertPersistExistKey() throws NacosException { - String ip = "127.0.0.1"; - Instance instance = new Instance(); - instance.setIp(ip); - instance.setEphemeral(false); - String key = "/test/children/keys/persistent/0"; - instance.setMetadata(new HashMap<>(Collections.singletonMap(key, "value0"))); - List<Instance> instances = new LinkedList<>(); - buildParentPath(key, instances); - instances.add(instance); - ServiceMetaData persistentService = serviceController.getPersistentService(); - when(client.getAllInstances(persistentService.getServiceName(), false)).thenReturn(instances); - doAnswer(AdditionalAnswers.answerVoid(getRegisterInstanceAnswer())).when(client).registerInstance(anyString(), any(Instance.class)); - REPOSITORY.persist(key, "value4"); - ArgumentCaptor<Instance> instanceArgumentCaptor = ArgumentCaptor.forClass(Instance.class); - ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class); - verify(client).registerInstance(stringArgumentCaptor.capture(), instanceArgumentCaptor.capture()); - Instance registerInstance = instanceArgumentCaptor.getValue(); - String registerType = stringArgumentCaptor.getValue(); - assertThat(registerType, is(persistentService.getServiceName())); - assertThat(registerInstance.getIp(), is(ip)); - assertThat(registerInstance.isEphemeral(), is(false)); - assertThat(NacosMetaDataUtils.getValue(registerInstance), is("value4")); - } - - @Test - void assertPersistEphemeralExistKey() throws NacosException { - final String key = "/test/children/keys/ephemeral/1"; - final Instance instance = new Instance(); - instance.setEphemeral(true); - Map<String, String> metaDataMap = new HashMap<>(4, 1F); - metaDataMap.put(PreservedMetadataKeys.HEART_BEAT_INTERVAL, String.valueOf(2000)); - metaDataMap.put(PreservedMetadataKeys.HEART_BEAT_TIMEOUT, String.valueOf(4000)); - metaDataMap.put(PreservedMetadataKeys.IP_DELETE_TIMEOUT, String.valueOf(6000)); - metaDataMap.put(key, "value0"); - instance.setMetadata(metaDataMap); - List<Instance> instances = new LinkedList<>(); - buildParentPath(key, instances); - ServiceMetaData persistentService = serviceController.getPersistentService(); - when(client.getAllInstances(persistentService.getServiceName(), false)).thenReturn(instances); - instances = new LinkedList<>(); - instances.add(instance); - ServiceMetaData ephemeralService = serviceController.getEphemeralService(); - when(client.getAllInstances(ephemeralService.getServiceName(), false)).thenReturn(instances); - doAnswer(AdditionalAnswers.answerVoid(getDeregisterInstanceAnswer())).when(client).deregisterInstance(anyString(), any(Instance.class)); - doAnswer(AdditionalAnswers.answerVoid(getRegisterInstanceAnswer())).when(client).registerInstance(anyString(), any(Instance.class)); - REPOSITORY.persistEphemeral(key, "value4"); - ArgumentCaptor<Instance> instanceArgumentCaptor = ArgumentCaptor.forClass(Instance.class); - ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class); - verify(client).deregisterInstance(anyString(), any(Instance.class)); - verify(client).registerInstance(stringArgumentCaptor.capture(), instanceArgumentCaptor.capture()); - Instance registerInstance = instanceArgumentCaptor.getValue(); - String registerType = stringArgumentCaptor.getValue(); - assertThat(registerType, is(ephemeralService.getServiceName())); - assertThat(registerInstance.isEphemeral(), is(true)); - assertThat(NacosMetaDataUtils.getValue(registerInstance), is("value4")); - Map<String, String> metaData = registerInstance.getMetadata(); - long timeToLiveSeconds = Long.parseLong(NacosPropertyKey.TIME_TO_LIVE_SECONDS.getDefaultValue()); - assertThat(metaData.get(PreservedMetadataKeys.HEART_BEAT_INTERVAL), is(String.valueOf(timeToLiveSeconds * 1000 / 3))); - assertThat(metaData.get(PreservedMetadataKeys.HEART_BEAT_TIMEOUT), is(String.valueOf(timeToLiveSeconds * 1000 * 2 / 3))); - assertThat(metaData.get(PreservedMetadataKeys.IP_DELETE_TIMEOUT), is(String.valueOf(timeToLiveSeconds * 1000))); - } - - private void buildParentPath(final String key, final List<Instance> instances) { - StringBuilder parentPath = new StringBuilder(); - final String[] partPath = key.split(PersistRepository.PATH_SEPARATOR); - for (int index = 1; index < partPath.length - 1; index++) { - parentPath.append(PersistRepository.PATH_SEPARATOR); - parentPath.append(partPath[index]); - String path = parentPath.toString(); - Instance instance = new Instance(); - instance.setEphemeral(false); - instance.setMetadata(Collections.singletonMap(path, "")); - instances.add(instance); - } - } - - @Test - void assertPersistEphemeralNotExistKey() throws NacosException { - String key = "/test/children/keys/ephemeral/0"; - doAnswer(AdditionalAnswers.answerVoid(getRegisterInstanceAnswer())).when(client).registerInstance(anyString(), any(Instance.class)); - REPOSITORY.persistEphemeral(key, "value0"); - ArgumentCaptor<Instance> instanceArgumentCaptor = ArgumentCaptor.forClass(Instance.class); - ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class); - verify(client, times(5)).registerInstance(stringArgumentCaptor.capture(), instanceArgumentCaptor.capture()); - Instance registerInstance = instanceArgumentCaptor.getValue(); - String registerType = stringArgumentCaptor.getValue(); - ServiceMetaData ephemeralService = serviceController.getEphemeralService(); - assertThat(registerType, is(ephemeralService.getServiceName())); - assertThat(registerInstance.isEphemeral(), is(true)); - assertThat(NacosMetaDataUtils.getValue(registerInstance), is("value0")); - Map<String, String> metaData = registerInstance.getMetadata(); - long timeToLiveSeconds = Long.parseLong(NacosPropertyKey.TIME_TO_LIVE_SECONDS.getDefaultValue()); - assertThat(metaData.get(PreservedMetadataKeys.HEART_BEAT_INTERVAL), is(String.valueOf(timeToLiveSeconds * 1000 / 3))); - assertThat(metaData.get(PreservedMetadataKeys.HEART_BEAT_TIMEOUT), is(String.valueOf(timeToLiveSeconds * 1000 * 2 / 3))); - assertThat(metaData.get(PreservedMetadataKeys.IP_DELETE_TIMEOUT), is(String.valueOf(timeToLiveSeconds * 1000))); - } - - @Test - void assertDeleteExistKey() throws NacosException { - int total = 3; - List<Instance> instances = new LinkedList<>(); - for (int count = 1; count <= total; count++) { - String key = "/test/children/keys/ephemeral/" + count; - Instance instance = new Instance(); - instance.setEphemeral(true); - instance.setMetadata(Collections.singletonMap(key, "value" + count)); - instances.add(instance); - } - ServiceMetaData ephemeralService = serviceController.getEphemeralService(); - when(client.getAllInstances(ephemeralService.getServiceName(), false)).thenReturn(instances); - instances = new LinkedList<>(); - String key = "/test/children/keys/persistent/0"; - Instance instance = new Instance(); - instance.setEphemeral(false); - instance.setMetadata(Collections.singletonMap(key, "value0")); - instances.add(instance); - ServiceMetaData persistentService = serviceController.getPersistentService(); - when(client.getAllInstances(persistentService.getServiceName(), false)).thenReturn(instances); - doAnswer(AdditionalAnswers.answerVoid(getDeregisterInstanceAnswer())).when(client).deregisterInstance(anyString(), any(Instance.class)); - REPOSITORY.delete("/test/children/keys"); - verify(client, times(4)).deregisterInstance(anyString(), any(Instance.class)); - } - - @Test - void assertDeleteNotExistKey() throws NacosException { - REPOSITORY.delete("/test/children/keys/persistent/1"); - verify(client, times(0)).deregisterInstance(anyString(), any(Instance.class)); - } - - @Test - void assertWatchAdded() throws NacosException, ExecutionException, InterruptedException { - ServiceMetaData ephemeralService = serviceController.getEphemeralService(); - ephemeralService.setListener(null); - String key = "key/key"; - String value = "value2"; - Instance instance = new Instance(); - instance.setMetadata(Collections.singletonMap(key, value)); - Event event = new NamingEvent(ephemeralService.getServiceName(), Collections.singletonList(instance)); - doAnswer(AdditionalAnswers.answerVoid(getListenerAnswer(null, event))).when(client).subscribe(anyString(), any(EventListener.class)); - SettableFuture<DataChangedEvent> settableFuture = SettableFuture.create(); - REPOSITORY.watch(key, settableFuture::set); - DataChangedEvent dataChangedEvent = settableFuture.get(); - assertThat(dataChangedEvent.getType(), is(DataChangedEvent.Type.ADDED)); - assertThat(dataChangedEvent.getKey(), is(key)); - assertThat(dataChangedEvent.getValue(), is(value)); - } - - @Test - void assertWatchUpdate() throws NacosException, ExecutionException, InterruptedException { - ServiceMetaData persistentService = serviceController.getPersistentService(); - persistentService.setListener(null); - String key = "key/key"; - long epochMilliseconds = NacosMetaDataUtils.getTimestamp(); - Instance preInstance = new Instance(); - Map<String, String> metaDataMap = new HashMap<>(); - metaDataMap.put(key, "value1"); - metaDataMap.put(NacosMetaDataUtils.UTC_ZONE_OFFSET.toString(), String.valueOf(epochMilliseconds)); - preInstance.setMetadata(metaDataMap); - final Instance instance = new Instance(); - metaDataMap = new HashMap<>(); - metaDataMap.put(key, "value2"); - metaDataMap.put(NacosMetaDataUtils.UTC_ZONE_OFFSET.toString(), String.valueOf(epochMilliseconds + 1)); - instance.setMetadata(metaDataMap); - Event event = new NamingEvent(persistentService.getServiceName(), Collections.singletonList(instance)); - doAnswer(AdditionalAnswers.answerVoid(getListenerAnswer(preInstance, event))).when(client).subscribe(anyString(), any(EventListener.class)); - SettableFuture<DataChangedEvent> settableFuture = SettableFuture.create(); - REPOSITORY.watch(key, settableFuture::set); - DataChangedEvent dataChangedEvent = settableFuture.get(); - assertThat(dataChangedEvent.getType(), is(DataChangedEvent.Type.UPDATED)); - assertThat(dataChangedEvent.getKey(), is(key)); - assertThat(dataChangedEvent.getValue(), is("value2")); - } - - @Test - void assertWatchDelete() throws NacosException, ExecutionException, InterruptedException { - ServiceMetaData persistentService = serviceController.getPersistentService(); - persistentService.setListener(null); - String key = "key/key"; - Instance preInstance = new Instance(); - preInstance.setMetadata(Collections.singletonMap(key, "value1")); - Event event = new NamingEvent(persistentService.getServiceName(), Collections.emptyList()); - doAnswer(AdditionalAnswers.answerVoid(getListenerAnswer(preInstance, event))).when(client).subscribe(anyString(), any(EventListener.class)); - SettableFuture<DataChangedEvent> settableFuture = SettableFuture.create(); - REPOSITORY.watch(key, settableFuture::set); - DataChangedEvent dataChangedEvent = settableFuture.get(); - assertThat(dataChangedEvent.getType(), is(DataChangedEvent.Type.DELETED)); - assertThat(dataChangedEvent.getKey(), is(key)); - assertThat(dataChangedEvent.getValue(), is("value1")); - } - - @Test - void assertClose() throws NacosException { - REPOSITORY.close(); - verify(client).shutDown(); - } - - @Test - void assertPersistNotAvailable() { - assertThrows(ClusterPersistRepositoryException.class, () -> REPOSITORY.persist("/test/children/keys/persistent/1", "value4")); - } - - @Test - void assertExceededMaximum() { - ServiceMetaData ephemeralService = serviceController.getEphemeralService(); - ephemeralService.setPort(new AtomicInteger(Integer.MAX_VALUE)); - assertThrows(IllegalStateException.class, () -> REPOSITORY.persistEphemeral("/key2", "value")); - } - - private VoidAnswer2<String, EventListener> getListenerAnswer(final Instance preInstance, final Event event) { - return (serviceName, listener) -> { - MemberAccessor accessor = Plugins.getMemberAccessor(); - if (null != preInstance) { - Map<String, Instance> preInstances = new HashMap<>(); - preInstances.put(NacosMetaDataUtils.getKey(preInstance), preInstance); - accessor.set(listener.getClass().getDeclaredField("preInstances"), listener, preInstances); - } - listener.onEvent(event); - }; - } - - private VoidAnswer2<String, Instance> getRegisterInstanceAnswer() { - return (serviceName, instance) -> { - List<Instance> instances = client.getAllInstances(serviceName, false); - instances.removeIf(each -> Objects.equals(each.getIp(), instance.getIp()) && each.getPort() == instance.getPort()); - instances.add(instance); - when(client.getAllInstances(serviceName, false)).thenReturn(instances); - }; - } - - private VoidAnswer2<String, Instance> getDeregisterInstanceAnswer() { - return (serviceName, instance) -> { - List<Instance> instances = client.getAllInstances(serviceName, false); - instances.remove(instance); - when(client.getAllInstances(serviceName, false)).thenReturn(instances); - }; - } -} diff --git a/mode/cluster/repository/nacos/src/test/java/org/apache/shardingsphere/mode/repository/cluster/nacos/props/NacosPropertiesTest.java b/mode/cluster/repository/nacos/src/test/java/org/apache/shardingsphere/mode/repository/cluster/nacos/props/NacosPropertiesTest.java deleted file mode 100644 index 7e04a08..0000000 --- a/mode/cluster/repository/nacos/src/test/java/org/apache/shardingsphere/mode/repository/cluster/nacos/props/NacosPropertiesTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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.shardingsphere.mode.repository.cluster.nacos.props; - -import org.apache.shardingsphere.test.util.PropertiesBuilder; -import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; -import org.junit.jupiter.api.Test; - -import java.util.Properties; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; - -class NacosPropertiesTest { - - @Test - void assertGetValue() { - NacosProperties actual = new NacosProperties(createProperties()); - assertThat(actual.getValue(NacosPropertyKey.CLUSTER_IP), is("127.0.0.1")); - assertThat(actual.getValue(NacosPropertyKey.RETRY_INTERVAL_MILLISECONDS), is(1000L)); - assertThat(actual.getValue(NacosPropertyKey.MAX_RETRIES), is(5)); - assertThat(actual.getValue(NacosPropertyKey.TIME_TO_LIVE_SECONDS), is(60)); - assertThat(actual.getValue(NacosPropertyKey.USERNAME), is("nacos")); - assertThat(actual.getValue(NacosPropertyKey.PASSWORD), is("nacos")); - } - - private Properties createProperties() { - return PropertiesBuilder.build( - new Property(NacosPropertyKey.CLUSTER_IP.getKey(), "127.0.0.1"), - new Property(NacosPropertyKey.RETRY_INTERVAL_MILLISECONDS.getKey(), "1000"), - new Property(NacosPropertyKey.MAX_RETRIES.getKey(), "5"), - new Property(NacosPropertyKey.TIME_TO_LIVE_SECONDS.getKey(), "60"), - new Property(NacosPropertyKey.USERNAME.getKey(), "nacos"), - new Property(NacosPropertyKey.PASSWORD.getKey(), "nacos")); - } - - @Test - void assertGetDefaultValue() { - NacosProperties actual = new NacosProperties(new Properties()); - assertThat(actual.getValue(NacosPropertyKey.CLUSTER_IP), is("")); - assertThat(actual.getValue(NacosPropertyKey.RETRY_INTERVAL_MILLISECONDS), is(500L)); - assertThat(actual.getValue(NacosPropertyKey.MAX_RETRIES), is(3)); - assertThat(actual.getValue(NacosPropertyKey.TIME_TO_LIVE_SECONDS), is(30)); - assertThat(actual.getValue(NacosPropertyKey.USERNAME), is("")); - assertThat(actual.getValue(NacosPropertyKey.PASSWORD), is("")); - } -}
