blob: 6aebbe8135cea378ff4e66e64791e8134541db8a [file] [log] [blame]
kmcpeake4fe18c82015-11-17 20:07:39 +00001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
kmcpeake4fe18c82015-11-17 20:07:39 +00003 *
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 */
16package org.onosproject.incubator.net.faultmanagement.alarm;
17
18import com.google.common.testing.EqualsTester;
19import org.junit.Test;
Andrea Campanella65f9eb92017-05-02 11:36:14 -070020import org.onosproject.net.DeviceId;
kmcpeake4fe18c82015-11-17 20:07:39 +000021
22import static org.hamcrest.Matchers.is;
23import static org.hamcrest.Matchers.not;
24import static org.junit.Assert.assertEquals;
25import static org.junit.Assert.assertThat;
26import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
27
28/**
kmcpeakeb172d5f2015-12-10 11:30:43 +000029 * This class tests the immutability, equality, and non-equality of {@link AlarmId}.
kmcpeake4fe18c82015-11-17 20:07:39 +000030 */
31public class AlarmIdTest {
kmcpeakeb172d5f2015-12-10 11:30:43 +000032
Andrea Campanella65f9eb92017-05-02 11:36:14 -070033 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";
kmcpeake4fe18c82015-11-17 20:07:39 +000043
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 Campanella65f9eb92017-05-02 11:36:14 -070057 final AlarmId id1 = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_1);
58 final AlarmId id2 = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_1);
kmcpeake4fe18c82015-11-17 20:07:39 +000059
60 assertThat(id1, is(id2));
61 }
62
kmcpeake4fe18c82015-11-17 20:07:39 +000063 /**
64 * Tests non-equality of {@link AlarmId}.
65 */
66 @Test
67 public void testNonEquality() {
Andrea Campanella65f9eb92017-05-02 11:36:14 -070068 final AlarmId id1 = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_1);
69 final AlarmId id2 = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_2);
kmcpeake4fe18c82015-11-17 20:07:39 +000070
71 assertThat(id1, is(not(id2)));
72 }
73
74 @Test
75 public void valueOf() {
Andrea Campanella65f9eb92017-05-02 11:36:14 -070076 final AlarmId id = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_1);
77 assertEquals("incorrect valueOf", id, ID_A);
kmcpeake4fe18c82015-11-17 20:07:39 +000078 }
79
80 /**
81 * Tests the equals(), hashCode() and toString() methods.
82 */
83 @Test
84 public void testEquals() {
Andrea Campanella65f9eb92017-05-02 11:36:14 -070085 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);
kmcpeake4fe18c82015-11-17 20:07:39 +000088
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 Campanella65f9eb92017-05-02 11:36:14 -0700100 final AlarmId id1 = AlarmId.alarmId(DEVICE_ID, UNIQUE_ID_3);
101 assertEquals(id1.toString(), ID_Z.toString());
kmcpeake4fe18c82015-11-17 20:07:39 +0000102
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700103 final AlarmId idString = AlarmId.alarmId(ID_STRING);
104 assertEquals(id1, idString);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000105
kmcpeake4fe18c82015-11-17 20:07:39 +0000106 }
107}