forked from apache/shenyu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[type:test][ISSUE apache#5278] add unit test for shenyu-discovery-api. (
apache#5285) * [type:test][ISSUE apache#5278] add test cas for shenyu-discovery-api. * [type:test][ISSUE apache#5278] add test cas for shenyu-discovery-api.
- Loading branch information
Showing
3 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
...scovery-api/src/test/java/org/apache/shenyu/discovery/api/config/DiscoveryConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
.../src/test/java/org/apache/shenyu/discovery/api/listener/DataChangedEventListenerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...src/test/java/org/apache/shenyu/discovery/api/listener/DiscoveryDataChangedEventTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |