This is an automated email from the ASF dual-hosted git repository.
hefengen 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 7c47cee3c4 [type:test][ISSUE #5278] add unit test for
shenyu-discovery-api. (#5285)
7c47cee3c4 is described below
commit 7c47cee3c4d505d5ab9894158f609965dcbcc5c6
Author: YxYL <[email protected]>
AuthorDate: Thu Nov 9 15:13:18 2023 +0800
[type:test][ISSUE #5278] add unit test for shenyu-discovery-api. (#5285)
* [type:test][ISSUE #5278] add test cas for shenyu-discovery-api.
* [type:test][ISSUE #5278] add test cas for shenyu-discovery-api.
---
.../discovery/api/config/DiscoveryConfigTest.java | 72 ++++++++++++++++++++++
.../api/listener/DataChangedEventListenerTest.java | 67 ++++++++++++++++++++
.../listener/DiscoveryDataChangedEventTest.java | 40 ++++++++++++
3 files changed, 179 insertions(+)
diff --git
a/shenyu-discovery/shenyu-discovery-api/src/test/java/org/apache/shenyu/discovery/api/config/DiscoveryConfigTest.java
b/shenyu-discovery/shenyu-discovery-api/src/test/java/org/apache/shenyu/discovery/api/config/DiscoveryConfigTest.java
new file mode 100644
index 0000000000..cd06624f62
--- /dev/null
+++
b/shenyu-discovery/shenyu-discovery-api/src/test/java/org/apache/shenyu/discovery/api/config/DiscoveryConfigTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.discovery.api.config;
+
+import org.junit.Test;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mockito;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Properties;
+import java.util.function.BiConsumer;
+import java.util.function.Function;
+
+/**
+ * The Test Case For {@link DiscoveryConfig}.
+ */
+@ExtendWith(MockitoExtension.class)
+public class DiscoveryConfigTest {
+
+ private final DiscoveryConfig discoveryConfig = new DiscoveryConfig();
+
+ @Test
+ public void testDiscoveryConfigProperties() {
+ // the discovery name
+ String name = "divide_default_discovery";
+ // zookeeper nacos etcd consul
+ String type = "local";
+ // the discovery pops (json)
+ String serverList = "{\"host\": \"localhost\"}";
+
+ testGetSet(DiscoveryConfig::getName, DiscoveryConfig::setName, name);
+ testGetSet(DiscoveryConfig::getType, DiscoveryConfig::setType, type);
+ testGetSet(DiscoveryConfig::getServerList,
DiscoveryConfig::setServerList, serverList);
+
+ Properties properties = Mockito.mock(Properties.class);
+ Assertions.assertNotNull(discoveryConfig.getProps());
+ discoveryConfig.setProps(properties);
+ Assertions.assertEquals(properties, discoveryConfig.getProps());
+ }
+
+ /**
+ * abstract function to test properties get and set.
+ *
+ * @param getter Object Get Function
+ * @param setter Object Set Function
+ * @param value Object Function Value
+ * @param <T> T
+ */
+ private <T> void testGetSet(final Function<DiscoveryConfig, T> getter,
+ final BiConsumer<DiscoveryConfig, T> setter,
+ final T value) {
+ Assertions.assertNull(getter.apply(discoveryConfig));
+ setter.accept(discoveryConfig, value);
+ Assertions.assertEquals(getter.apply(discoveryConfig), value);
+ }
+}
diff --git
a/shenyu-discovery/shenyu-discovery-api/src/test/java/org/apache/shenyu/discovery/api/listener/DataChangedEventListenerTest.java
b/shenyu-discovery/shenyu-discovery-api/src/test/java/org/apache/shenyu/discovery/api/listener/DataChangedEventListenerTest.java
new file mode 100644
index 0000000000..f237e1c03b
--- /dev/null
+++
b/shenyu-discovery/shenyu-discovery-api/src/test/java/org/apache/shenyu/discovery/api/listener/DataChangedEventListenerTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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.discovery.api.listener;
+
+import org.junit.Test;
+import org.junit.jupiter.api.Assertions;
+
+import java.util.concurrent.atomic.AtomicReference;
+
+import static
org.apache.shenyu.discovery.api.listener.DiscoveryDataChangedEvent.Event.ADDED;
+import static
org.apache.shenyu.discovery.api.listener.DiscoveryDataChangedEvent.Event.DELETED;
+import static
org.apache.shenyu.discovery.api.listener.DiscoveryDataChangedEvent.Event.IGNORED;
+import static
org.apache.shenyu.discovery.api.listener.DiscoveryDataChangedEvent.Event.UPDATED;
+
+/**
+ * The Test Case For {@link DataChangedEventListener}.
+ */
+public class DataChangedEventListenerTest {
+
+ @Test
+ public void testOnChange() {
+ AtomicReference<String> eventResult = new AtomicReference<>("");
+
+ DataChangedEventListener dataChangedEventListener = event -> {
+ DiscoveryDataChangedEvent.Event currentEvent = event.getEvent();
+ switch (currentEvent) {
+ case ADDED:
+ // added logic...
+ eventResult.set(ADDED.name());
+ break;
+ case UPDATED:
+ // updated logic...
+ eventResult.set(UPDATED.name());
+ break;
+ case DELETED:
+ // deleted logic...
+ eventResult.set(DELETED.name());
+ break;
+ case IGNORED:
+ // ignored logic...
+ eventResult.set(IGNORED.name());
+ break;
+ default:
+ break;
+ }
+ };
+ DiscoveryDataChangedEvent dataChangedEvent = new
DiscoveryDataChangedEvent("key", "value", IGNORED);
+ dataChangedEventListener.onChange(dataChangedEvent);
+
+ Assertions.assertEquals(eventResult.get(), IGNORED.name());
+ }
+}
diff --git
a/shenyu-discovery/shenyu-discovery-api/src/test/java/org/apache/shenyu/discovery/api/listener/DiscoveryDataChangedEventTest.java
b/shenyu-discovery/shenyu-discovery-api/src/test/java/org/apache/shenyu/discovery/api/listener/DiscoveryDataChangedEventTest.java
new file mode 100644
index 0000000000..a6ce92b0f2
--- /dev/null
+++
b/shenyu-discovery/shenyu-discovery-api/src/test/java/org/apache/shenyu/discovery/api/listener/DiscoveryDataChangedEventTest.java
@@ -0,0 +1,40 @@
+/*
+ * 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.discovery.api.listener;
+
+import org.junit.Test;
+import org.junit.jupiter.api.Assertions;
+
+/**
+ * The Test Case For {@link DiscoveryDataChangedEvent}.
+ */
+public class DiscoveryDataChangedEventTest {
+
+ private final DiscoveryDataChangedEvent.Event event =
DiscoveryDataChangedEvent.Event.IGNORED;
+
+ @Test
+ public void testDiscoveryDataChangedEvent() {
+ String key = "key";
+ String value = "value";
+ DiscoveryDataChangedEvent discoveryDataChangedEvent = new
DiscoveryDataChangedEvent(key, value, event);
+ Assertions.assertEquals(key, discoveryDataChangedEvent.getKey());
+ Assertions.assertEquals(value, discoveryDataChangedEvent.getValue());
+ Assertions.assertEquals(event, discoveryDataChangedEvent.getEvent());
+ }
+
+}