diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesChangeEventTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesChangeEventTest.java new file mode 100644 index 000000000..c4c6d78cb --- /dev/null +++ b/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesChangeEventTest.java @@ -0,0 +1,44 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed 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 com.alibaba.nacos.client.naming.event; + +import com.alibaba.nacos.api.naming.pojo.Instance; +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +public class InstancesChangeEventTest { + + @Test + public void testGetServiceName() { + String serviceName = "a"; + String groupName = "b"; + String clusters = "c"; + List hosts = new ArrayList<>(); + Instance ins = new Instance(); + hosts.add(ins); + InstancesChangeEvent event = new InstancesChangeEvent(serviceName, groupName, clusters, hosts); + Assert.assertEquals(serviceName, event.getServiceName()); + Assert.assertEquals(clusters, event.getClusters()); + Assert.assertEquals(groupName, event.getGroupName()); + List hosts1 = event.getHosts(); + Assert.assertEquals(hosts.size(), hosts1.size()); + Assert.assertEquals(hosts.get(0), hosts1.get(0)); + } +} \ No newline at end of file diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesChangeNotifierTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesChangeNotifierTest.java new file mode 100644 index 000000000..bf7319b34 --- /dev/null +++ b/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesChangeNotifierTest.java @@ -0,0 +1,101 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed 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 com.alibaba.nacos.client.naming.event; + +import com.alibaba.nacos.api.naming.listener.EventListener; +import com.alibaba.nacos.api.naming.pojo.ServiceInfo; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import java.util.List; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; + +public class InstancesChangeNotifierTest { + + @Test + public void testRegisterListener() { + String group = "a"; + String name = "b"; + String clusters = "c"; + InstancesChangeNotifier instancesChangeNotifier = new InstancesChangeNotifier(); + EventListener listener = Mockito.mock(EventListener.class); + instancesChangeNotifier.registerListener(group, name, clusters, listener); + List subscribeServices = instancesChangeNotifier.getSubscribeServices(); + Assert.assertEquals(1, subscribeServices.size()); + Assert.assertEquals(group, subscribeServices.get(0).getGroupName()); + Assert.assertEquals(name, subscribeServices.get(0).getName()); + Assert.assertEquals(clusters, subscribeServices.get(0).getClusters()); + + } + + @Test + public void testDeregisterListener() { + String group = "a"; + String name = "b"; + String clusters = "c"; + InstancesChangeNotifier instancesChangeNotifier = new InstancesChangeNotifier(); + EventListener listener = Mockito.mock(EventListener.class); + instancesChangeNotifier.registerListener(group, name, clusters, listener); + List subscribeServices = instancesChangeNotifier.getSubscribeServices(); + Assert.assertEquals(1, subscribeServices.size()); + + instancesChangeNotifier.deregisterListener(group, name, clusters, listener); + + List subscribeServices2 = instancesChangeNotifier.getSubscribeServices(); + Assert.assertEquals(1, subscribeServices2.size()); + } + + @Test + public void testIsSubscribed() { + String group = "a"; + String name = "b"; + String clusters = "c"; + InstancesChangeNotifier instancesChangeNotifier = new InstancesChangeNotifier(); + EventListener listener = Mockito.mock(EventListener.class); + Assert.assertFalse(instancesChangeNotifier.isSubscribed(group, name, clusters)); + + instancesChangeNotifier.registerListener(group, name, clusters, listener); + Assert.assertTrue(instancesChangeNotifier.isSubscribed(group, name, clusters)); + } + + @Test + public void testOnEvent() { + String group = "a"; + String name = "b"; + String clusters = "c"; + InstancesChangeNotifier instancesChangeNotifier = new InstancesChangeNotifier(); + EventListener listener = Mockito.mock(EventListener.class); + + instancesChangeNotifier.registerListener(group, name, clusters, listener); + InstancesChangeEvent event1 = Mockito.mock(InstancesChangeEvent.class); + Mockito.when(event1.getClusters()).thenReturn(clusters); + Mockito.when(event1.getGroupName()).thenReturn(group); + Mockito.when(event1.getServiceName()).thenReturn(name); + + instancesChangeNotifier.onEvent(event1); + Mockito.verify(listener, times(1)).onEvent(any()); + } + + @Test + public void testSubscribeType() { + InstancesChangeNotifier instancesChangeNotifier = new InstancesChangeNotifier(); + Assert.assertEquals(InstancesChangeEvent.class, instancesChangeNotifier.subscribeType()); + } +} \ No newline at end of file