Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.event; |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 17 | |
| 18 | import org.junit.Test; |
| 19 | |
| 20 | import static org.junit.Assert.assertEquals; |
| 21 | import static org.junit.Assert.assertTrue; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import static org.onosproject.event.TestEvent.Type.FOO; |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Tests of the base event abstraction. |
| 26 | */ |
| 27 | public class AbstractEventTest { |
| 28 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Validates the base attributes of an event. |
| 31 | * |
| 32 | * @param event event to validate |
| 33 | * @param type event type |
| 34 | * @param subject event subject |
| 35 | * @param time event time |
| 36 | * @param <T> type of event |
| 37 | * @param <S> type of subject |
| 38 | */ |
| 39 | protected static <T extends Enum, S> |
| 40 | void validateEvent(Event<T, S> event, T type, S subject, long time) { |
| 41 | assertEquals("incorrect type", type, event.type()); |
| 42 | assertEquals("incorrect subject", subject, event.subject()); |
| 43 | assertEquals("incorrect time", time, event.time()); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Validates the base attributes of an event. |
| 48 | * |
| 49 | * @param event event to validate |
| 50 | * @param type event type |
| 51 | * @param subject event subject |
| 52 | * @param minTime minimum event time inclusive |
| 53 | * @param maxTime maximum event time inclusive |
| 54 | * @param <T> type of event |
| 55 | * @param <S> type of subject |
| 56 | */ |
| 57 | protected static <T extends Enum, S> |
| 58 | void validateEvent(Event<T, S> event, T type, S subject, |
| 59 | long minTime, long maxTime) { |
| 60 | assertEquals("incorrect type", type, event.type()); |
| 61 | assertEquals("incorrect subject", subject, event.subject()); |
| 62 | assertTrue("incorrect time", minTime <= event.time() && event.time() <= maxTime); |
| 63 | } |
| 64 | |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 65 | @Test |
| 66 | public void withTime() { |
| 67 | TestEvent event = new TestEvent(FOO, "foo", 123L); |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 68 | validateEvent(event, FOO, "foo", 123L); |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void withoutTime() { |
| 73 | long before = System.currentTimeMillis(); |
| 74 | TestEvent event = new TestEvent(FOO, "foo"); |
| 75 | long after = System.currentTimeMillis(); |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 76 | validateEvent(event, FOO, "foo", before, after); |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 77 | } |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 78 | |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 79 | } |