Skip to content

Commit

Permalink
[type:test][ISSUE apache#5278] add unit test for shenyu-discovery-api. (
Browse files Browse the repository at this point in the history
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
YxYL6125 authored Nov 9, 2023
1 parent c19f7eb commit 7c47cee
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 0 deletions.
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);
}
}
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());
}
}
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());
}

}

0 comments on commit 7c47cee

Please sign in to comment.