kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 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 | * |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 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 | */ |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 16 | package org.onosproject.alarm; |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 17 | |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 18 | import org.junit.Test; |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 19 | import org.onosproject.net.DeviceId; |
| 20 | |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 21 | import static org.hamcrest.MatcherAssert.assertThat; |
| 22 | import static org.hamcrest.Matchers.*; |
| 23 | import static org.junit.Assert.*; |
| 24 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 25 | |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 26 | public class DefaultAlarmTest { |
| 27 | |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 28 | private static final AlarmEntityId ALARM_ENTITY_ID = AlarmEntityId.alarmEntityId("port:bar"); |
| 29 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("foo:bar"); |
| 30 | private static final String UNIQUE_ID_1 = "unique_id_1"; |
| 31 | private static final AlarmId ALARM_ID = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_1); |
| 32 | private static final String UNIQUE_ID_2 = "unique_id_1"; |
| 33 | private static final AlarmId ALARM_ID_2 = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_2); |
| 34 | |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 35 | @Test |
| 36 | public void testImmutability() { |
| 37 | assertThatClassIsImmutable(DefaultAlarm.class); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Checks the construction of a DefaultAlarm object. |
| 42 | */ |
| 43 | @Test |
| 44 | public void testConstruction() { |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 45 | final DefaultAlarm a = generate(); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 46 | assertThat(a, is(notNullValue())); |
| 47 | final DefaultAlarm b = new DefaultAlarm.Builder(a).build(); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 48 | assertEquals(a, b); |
| 49 | } |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 50 | |
| 51 | @Test |
| 52 | public void testEquals() { |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 53 | final DefaultAlarm a = new DefaultAlarm.Builder(ALARM_ID_2, |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 54 | DeviceId.NONE, "desc", Alarm.SeverityLevel.MINOR, 3).build(); |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 55 | final DefaultAlarm b = new DefaultAlarm.Builder(ALARM_ID, |
| 56 | DeviceId.NONE, "desc", Alarm.SeverityLevel.MINOR, a.timeRaised() + 1) |
| 57 | .withTimeUpdated(a.timeUpdated() + 1).build(); |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 58 | assertEquals("id or timeRaised or timeUpdated may differ", a, b); |
| 59 | |
| 60 | assertNotEquals(a, new DefaultAlarm.Builder(a).withAcknowledged(!a.acknowledged()).build()); |
| 61 | assertNotEquals(a, new DefaultAlarm.Builder(a).withManuallyClearable(!a.manuallyClearable()).build()); |
| 62 | assertNotEquals(a, new DefaultAlarm.Builder(a).withServiceAffecting(!a.serviceAffecting()).build()); |
| 63 | assertNotEquals(a, new DefaultAlarm.Builder(a).withAssignedUser("Changed" + a.assignedUser()).build()); |
| 64 | |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void testClear() { |
| 69 | final DefaultAlarm active = generate(); |
| 70 | final DefaultAlarm cleared = new DefaultAlarm.Builder(active).clear().build(); |
| 71 | assertNotEquals(active, cleared); |
| 72 | assertThat(cleared.timeRaised(), is(active.timeRaised())); |
| 73 | assertThat(cleared.severity(), is(Alarm.SeverityLevel.CLEARED)); |
| 74 | assertThat(cleared.timeUpdated(), greaterThan(active.timeUpdated())); |
| 75 | assertNotNull(cleared.timeCleared()); |
| 76 | |
| 77 | } |
| 78 | |
| 79 | @Test |
| 80 | public void testId() { |
| 81 | final DefaultAlarm a = generate(); |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 82 | final DefaultAlarm b = new DefaultAlarm.Builder(a).build(); |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 83 | |
| 84 | assertEquals("id ignored in equals", a, b); |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 85 | assertEquals(ALARM_ID, a.id()); |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 86 | assertEquals(ALARM_ID, b.id()); |
| 87 | assertEquals(ALARM_ENTITY_ID, b.source()); |
| 88 | |
| 89 | } |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 90 | |
| 91 | private static DefaultAlarm generate() { |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 92 | return new DefaultAlarm.Builder(ALARM_ID, |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 93 | DeviceId.NONE, "desc", Alarm.SeverityLevel.MINOR, 3).forSource(ALARM_ENTITY_ID).build(); |
| 94 | } |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 95 | } |