diff options
Diffstat (limited to 'framework/src/onos/core/api/src/test/java/org/onosproject/event')
8 files changed, 416 insertions, 0 deletions
diff --git a/framework/src/onos/core/api/src/test/java/org/onosproject/event/AbstractEventTest.java b/framework/src/onos/core/api/src/test/java/org/onosproject/event/AbstractEventTest.java new file mode 100644 index 00000000..c66c4b84 --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/event/AbstractEventTest.java @@ -0,0 +1,79 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * 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 org.onosproject.event; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.onosproject.event.TestEvent.Type.FOO; + +/** + * Tests of the base event abstraction. + */ +public class AbstractEventTest { + + /** + * Validates the base attributes of an event. + * + * @param event event to validate + * @param type event type + * @param subject event subject + * @param time event time + * @param <T> type of event + * @param <S> type of subject + */ + protected static <T extends Enum, S> + void validateEvent(Event<T, S> event, T type, S subject, long time) { + assertEquals("incorrect type", type, event.type()); + assertEquals("incorrect subject", subject, event.subject()); + assertEquals("incorrect time", time, event.time()); + } + + /** + * Validates the base attributes of an event. + * + * @param event event to validate + * @param type event type + * @param subject event subject + * @param minTime minimum event time inclusive + * @param maxTime maximum event time inclusive + * @param <T> type of event + * @param <S> type of subject + */ + protected static <T extends Enum, S> + void validateEvent(Event<T, S> event, T type, S subject, + long minTime, long maxTime) { + assertEquals("incorrect type", type, event.type()); + assertEquals("incorrect subject", subject, event.subject()); + assertTrue("incorrect time", minTime <= event.time() && event.time() <= maxTime); + } + + @Test + public void withTime() { + TestEvent event = new TestEvent(FOO, "foo", 123L); + validateEvent(event, FOO, "foo", 123L); + } + + @Test + public void withoutTime() { + long before = System.currentTimeMillis(); + TestEvent event = new TestEvent(FOO, "foo"); + long after = System.currentTimeMillis(); + validateEvent(event, FOO, "foo", before, after); + } + +} diff --git a/framework/src/onos/core/api/src/test/java/org/onosproject/event/BrokenListener.java b/framework/src/onos/core/api/src/test/java/org/onosproject/event/BrokenListener.java new file mode 100644 index 00000000..1a013ada --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/event/BrokenListener.java @@ -0,0 +1,28 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * 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 org.onosproject.event; + +/** + * Test event listener fixture. + */ +public class BrokenListener extends TestListener { + + public void event(TestEvent event) { + throw new IllegalStateException("boom"); + } + +} + diff --git a/framework/src/onos/core/api/src/test/java/org/onosproject/event/DefaultEventSinkRegistryTest.java b/framework/src/onos/core/api/src/test/java/org/onosproject/event/DefaultEventSinkRegistryTest.java new file mode 100644 index 00000000..2e24228a --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/event/DefaultEventSinkRegistryTest.java @@ -0,0 +1,71 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * 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 org.onosproject.event; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests of the default event sink registry. + */ +public class DefaultEventSinkRegistryTest { + + private DefaultEventSinkRegistry registry; + + private static class FooEvent extends TestEvent { + public FooEvent(String subject) { + super(Type.FOO, subject); + } + } + + private static class BarEvent extends TestEvent { + public BarEvent(String subject) { + super(Type.BAR, subject); + } + } + + private static class FooSink implements EventSink<FooEvent> { + @Override public void process(FooEvent event) {} + } + + private static class BarSink implements EventSink<BarEvent> { + @Override public void process(BarEvent event) {} + } + + @Before + public void setUp() { + registry = new DefaultEventSinkRegistry(); + } + + @Test + public void basics() { + FooSink fooSink = new FooSink(); + BarSink barSink = new BarSink(); + registry.addSink(FooEvent.class, fooSink); + registry.addSink(BarEvent.class, barSink); + + assertEquals("incorrect sink count", 2, registry.getSinks().size()); + assertEquals("incorrect sink", fooSink, registry.getSink(FooEvent.class)); + assertEquals("incorrect sink", barSink, registry.getSink(BarEvent.class)); + + registry.removeSink(FooEvent.class); + assertNull("incorrect sink", registry.getSink(FooEvent.class)); + assertEquals("incorrect sink", barSink, registry.getSink(BarEvent.class)); + + } +} diff --git a/framework/src/onos/core/api/src/test/java/org/onosproject/event/EventDeliveryServiceAdapter.java b/framework/src/onos/core/api/src/test/java/org/onosproject/event/EventDeliveryServiceAdapter.java new file mode 100644 index 00000000..6d5e8934 --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/event/EventDeliveryServiceAdapter.java @@ -0,0 +1,60 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * 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 org.onosproject.event; + +import java.util.Set; + +import static org.junit.Assert.*; + +/** + * Testing adapter for the event delivery service. + */ +public class EventDeliveryServiceAdapter implements EventDeliveryService { + @Override + public void setDispatchTimeLimit(long millis) { + + } + + @Override + public long getDispatchTimeLimit() { + return 0; + } + + @Override + public void post(Event event) { + + } + + @Override + public <E extends Event> void addSink(Class<E> eventClass, EventSink<E> sink) { + + } + + @Override + public <E extends Event> void removeSink(Class<E> eventClass) { + + } + + @Override + public <E extends Event> EventSink<E> getSink(Class<E> eventClass) { + return null; + } + + @Override + public Set<Class<? extends Event>> getSinks() { + return null; + } +} diff --git a/framework/src/onos/core/api/src/test/java/org/onosproject/event/ListenerRegistryTest.java b/framework/src/onos/core/api/src/test/java/org/onosproject/event/ListenerRegistryTest.java new file mode 100644 index 00000000..8cce6417 --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/event/ListenerRegistryTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * 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 org.onosproject.event; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * Unit tests for {@link ListenerRegistry}. + */ +public class ListenerRegistryTest { + + private static final TestEvent FOO_EVENT = + new TestEvent(TestEvent.Type.FOO, "foo"); + private static final TestEvent BAR_EVENT = + new TestEvent(TestEvent.Type.BAR, "bar"); + + private TestListener listener; + private TestListener secondListener; + private TestListenerRegistry manager; + + @Before + public void setUp() { + listener = new TestListener(); + secondListener = new TestListener(); + manager = new TestListenerRegistry(); + } + + @Test + public void basics() { + manager.addListener(listener); + manager.addListener(secondListener); + + manager.process(BAR_EVENT); + assertTrue("BAR not processed", listener.events.contains(BAR_EVENT)); + assertTrue("BAR not processed", secondListener.events.contains(BAR_EVENT)); + + manager.removeListener(listener); + + manager.process(FOO_EVENT); + assertFalse("FOO processed", listener.events.contains(FOO_EVENT)); + assertTrue("FOO not processed", secondListener.events.contains(FOO_EVENT)); + } + + @Test + public void badListener() { + listener = new BrokenListener(); + + manager.addListener(listener); + manager.addListener(secondListener); + + manager.process(BAR_EVENT); + assertFalse("BAR processed", listener.events.contains(BAR_EVENT)); + assertFalse("error not reported", manager.errors.isEmpty()); + assertTrue("BAR not processed", secondListener.events.contains(BAR_EVENT)); + } + +} diff --git a/framework/src/onos/core/api/src/test/java/org/onosproject/event/TestEvent.java b/framework/src/onos/core/api/src/test/java/org/onosproject/event/TestEvent.java new file mode 100644 index 00000000..8a507151 --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/event/TestEvent.java @@ -0,0 +1,34 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * 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 org.onosproject.event; + +/** + * Test event fixture. + */ +public class TestEvent extends AbstractEvent<TestEvent.Type, String> { + + public enum Type { FOO, BAR } + + public TestEvent(Type type, String subject) { + super(type, subject); + } + + public TestEvent(Type type, String subject, long timestamp) { + super(type, subject, timestamp); + } + +} + diff --git a/framework/src/onos/core/api/src/test/java/org/onosproject/event/TestListener.java b/framework/src/onos/core/api/src/test/java/org/onosproject/event/TestListener.java new file mode 100644 index 00000000..213f8c59 --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/event/TestListener.java @@ -0,0 +1,34 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * 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 org.onosproject.event; + +import java.util.ArrayList; +import java.util.List; + +/** + * Test event listener fixture. + */ +public class TestListener implements EventListener<TestEvent> { + + public final List<TestEvent> events = new ArrayList<>(); + + @Override + public void event(TestEvent event) { + events.add(event); + } + +} + diff --git a/framework/src/onos/core/api/src/test/java/org/onosproject/event/TestListenerRegistry.java b/framework/src/onos/core/api/src/test/java/org/onosproject/event/TestListenerRegistry.java new file mode 100644 index 00000000..fd9b6d08 --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/event/TestListenerRegistry.java @@ -0,0 +1,36 @@ +/* + * Copyright 2014 Open Networking Laboratory + * + * 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 org.onosproject.event; + +import java.util.ArrayList; +import java.util.List; + +/** + * Test event listener manager fixture. + */ +public class TestListenerRegistry + extends ListenerRegistry<TestEvent, TestListener> { + + public final List<Throwable> errors = new ArrayList<>(); + + @Override + protected void reportProblem(TestEvent event, Throwable error) { + super.reportProblem(event, error); + errors.add(error); + } + +} + |