blob: 6b1268fdaff354f183a3768792d898e29085a486 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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 */
tomb36046e2014-08-27 00:22:24 -070016package org.onlab.onos.event;
17
18import org.junit.Test;
19
20import static org.junit.Assert.assertEquals;
21import static org.junit.Assert.assertTrue;
22import static org.onlab.onos.event.TestEvent.Type.FOO;
23
24/**
25 * Tests of the base event abstraction.
26 */
27public class AbstractEventTest {
28
tomca20e0c2014-09-03 23:22:24 -070029 /**
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
tomb36046e2014-08-27 00:22:24 -070065 @Test
66 public void withTime() {
67 TestEvent event = new TestEvent(FOO, "foo", 123L);
tomca20e0c2014-09-03 23:22:24 -070068 validateEvent(event, FOO, "foo", 123L);
tomb36046e2014-08-27 00:22:24 -070069 }
70
71 @Test
72 public void withoutTime() {
73 long before = System.currentTimeMillis();
74 TestEvent event = new TestEvent(FOO, "foo");
75 long after = System.currentTimeMillis();
tomca20e0c2014-09-03 23:22:24 -070076 validateEvent(event, FOO, "foo", before, after);
tomb36046e2014-08-27 00:22:24 -070077 }
tomca20e0c2014-09-03 23:22:24 -070078
tomb36046e2014-08-27 00:22:24 -070079}