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 | * |
| 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 | */ |
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 | |
| 18 | import com.google.common.testing.EqualsTester; |
| 19 | import org.junit.Test; |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 20 | import org.onosproject.net.DeviceId; |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 21 | |
| 22 | import static org.hamcrest.Matchers.is; |
| 23 | import static org.hamcrest.Matchers.not; |
| 24 | import static org.junit.Assert.assertEquals; |
| 25 | import static org.junit.Assert.assertThat; |
| 26 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 27 | |
| 28 | /** |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 29 | * This class tests the immutability, equality, and non-equality of {@link AlarmId}. |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 30 | */ |
| 31 | public class AlarmIdTest { |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 32 | |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 33 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("foo:bar"); |
| 34 | private static final String UNIQUE_ID_1 = "unique_id_1"; |
| 35 | private static final AlarmId ID_A = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_1); |
| 36 | |
| 37 | private static final String UNIQUE_ID_2 = "unique_id_2"; |
| 38 | |
| 39 | private static final String UNIQUE_ID_3 = "unique_id_3"; |
| 40 | private static final AlarmId ID_Z = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_3); |
| 41 | |
| 42 | private static final String ID_STRING = "foo:bar:unique_id_3"; |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Tests the immutability of {@link AlarmId}. |
| 46 | */ |
| 47 | @Test |
| 48 | public void intentIdFollowsGuidelineForImmutableObject() { |
| 49 | assertThatClassIsImmutable(AlarmId.class); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Tests equality of {@link AlarmId}. |
| 54 | */ |
| 55 | @Test |
| 56 | public void testEquality() { |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 57 | final AlarmId id1 = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_1); |
| 58 | final AlarmId id2 = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_1); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 59 | |
| 60 | assertThat(id1, is(id2)); |
| 61 | } |
| 62 | |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 63 | /** |
| 64 | * Tests non-equality of {@link AlarmId}. |
| 65 | */ |
| 66 | @Test |
| 67 | public void testNonEquality() { |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 68 | final AlarmId id1 = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_1); |
| 69 | final AlarmId id2 = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_2); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 70 | |
| 71 | assertThat(id1, is(not(id2))); |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void valueOf() { |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 76 | final AlarmId id = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_1); |
| 77 | assertEquals("incorrect valueOf", id, ID_A); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Tests the equals(), hashCode() and toString() methods. |
| 82 | */ |
| 83 | @Test |
| 84 | public void testEquals() { |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 85 | final AlarmId id1 = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_1); |
| 86 | final AlarmId sameAsId1 = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_1); |
| 87 | final AlarmId id2 = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_2); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 88 | |
| 89 | new EqualsTester() |
| 90 | .addEqualityGroup(id1, sameAsId1) |
| 91 | .addEqualityGroup(id2) |
| 92 | .testEquals(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Tests construction of an AlarmId object. |
| 97 | */ |
| 98 | @Test |
| 99 | public void testConstruction() { |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 100 | final AlarmId id1 = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_3); |
| 101 | assertEquals(id1.toString(), ID_Z.toString()); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 102 | |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 103 | final AlarmId idString = AlarmId.alarmId(ID_STRING); |
| 104 | assertEquals(id1, idString); |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 105 | |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 106 | } |
| 107 | } |