tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 1 | package org.onlab.onos.event; |
| 2 | |
| 3 | import org.junit.Test; |
| 4 | |
| 5 | import static org.junit.Assert.assertEquals; |
| 6 | import static org.junit.Assert.assertTrue; |
| 7 | import static org.onlab.onos.event.TestEvent.Type.FOO; |
| 8 | |
| 9 | /** |
| 10 | * Tests of the base event abstraction. |
| 11 | */ |
| 12 | public class AbstractEventTest { |
| 13 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 14 | /** |
| 15 | * Validates the base attributes of an event. |
| 16 | * |
| 17 | * @param event event to validate |
| 18 | * @param type event type |
| 19 | * @param subject event subject |
| 20 | * @param time event time |
| 21 | * @param <T> type of event |
| 22 | * @param <S> type of subject |
| 23 | */ |
| 24 | protected static <T extends Enum, S> |
| 25 | void validateEvent(Event<T, S> event, T type, S subject, long time) { |
| 26 | assertEquals("incorrect type", type, event.type()); |
| 27 | assertEquals("incorrect subject", subject, event.subject()); |
| 28 | assertEquals("incorrect time", time, event.time()); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Validates the base attributes of an event. |
| 33 | * |
| 34 | * @param event event to validate |
| 35 | * @param type event type |
| 36 | * @param subject event subject |
| 37 | * @param minTime minimum event time inclusive |
| 38 | * @param maxTime maximum event time inclusive |
| 39 | * @param <T> type of event |
| 40 | * @param <S> type of subject |
| 41 | */ |
| 42 | protected static <T extends Enum, S> |
| 43 | void validateEvent(Event<T, S> event, T type, S subject, |
| 44 | long minTime, long maxTime) { |
| 45 | assertEquals("incorrect type", type, event.type()); |
| 46 | assertEquals("incorrect subject", subject, event.subject()); |
| 47 | assertTrue("incorrect time", minTime <= event.time() && event.time() <= maxTime); |
| 48 | } |
| 49 | |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 50 | @Test |
| 51 | public void withTime() { |
| 52 | TestEvent event = new TestEvent(FOO, "foo", 123L); |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 53 | validateEvent(event, FOO, "foo", 123L); |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Test |
| 57 | public void withoutTime() { |
| 58 | long before = System.currentTimeMillis(); |
| 59 | TestEvent event = new TestEvent(FOO, "foo"); |
| 60 | long after = System.currentTimeMillis(); |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 61 | validateEvent(event, FOO, "foo", before, after); |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 62 | } |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 63 | |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 64 | } |