blob: 1de9fbb59a2c5b40a4f7b4162673588673097478 [file] [log] [blame]
Ray Milkeyabbdcb72015-02-11 09:32:17 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkeyabbdcb72015-02-11 09:32:17 -08003 *
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.net.intent;
17
18import org.junit.Test;
19import org.onosproject.net.NetTestTools;
20
21import com.google.common.testing.EqualsTester;
22
23import static org.hamcrest.MatcherAssert.assertThat;
David Glantzc3e4c0d2021-11-18 13:41:14 -060024import static org.hamcrest.Matchers.comparesEqualTo;
25import static org.hamcrest.Matchers.greaterThan;
Ray Milkeyabbdcb72015-02-11 09:32:17 -080026import static org.hamcrest.Matchers.is;
David Glantzc3e4c0d2021-11-18 13:41:14 -060027import static org.hamcrest.Matchers.lessThan;
Ray Milkeyabbdcb72015-02-11 09:32:17 -080028import static org.hamcrest.Matchers.notNullValue;
29import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
30import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
31
32/**
33 * Unit tests for the intent key class.
34 */
35public class KeyTest {
36
37 private static final String KEY_1 = "key1";
38 private static final String KEY_2 = "key2";
39 private static final String KEY_3 = "key3";
40
41 private static final long LONG_KEY_1 = 0x1111;
42 private static final long LONG_KEY_2 = 0x2222;
43 private static final long LONG_KEY_3 = 0x3333;
44
45 /**
46 * Tests that keys are properly immutable.
47 */
48 @Test
49 public void keysAreImmutable() {
50 assertThatClassIsImmutableBaseClass(Key.class);
51
52 // Will be a long based key, class is private so cannot be
53 // accessed directly
54 Key longKey = Key.of(0xabcdefL, NetTestTools.APP_ID);
55 assertThatClassIsImmutable(longKey.getClass());
56
57 // Will be a String based key, class is private so cannot be
58 // accessed directly.
59 Key stringKey = Key.of("some key", NetTestTools.APP_ID);
60 assertThatClassIsImmutable(stringKey.getClass());
61 }
62
63 /**
64 * Tests string key construction.
65 */
66 @Test
67 public void stringKeyConstruction() {
68 Key stringKey1 = Key.of(KEY_3, NetTestTools.APP_ID);
69 assertThat(stringKey1, notNullValue());
70 Key stringKey2 = Key.of(KEY_3, NetTestTools.APP_ID);
71 assertThat(stringKey2, notNullValue());
72
73 assertThat(stringKey1.hash(), is(stringKey2.hash()));
74 }
75
76 /**
77 * Tests long key construction.
78 */
79 @Test
80 public void longKeyConstruction() {
81 Key longKey1 = Key.of(LONG_KEY_3, NetTestTools.APP_ID);
82 assertThat(longKey1, notNullValue());
83 Key longKey2 = Key.of(LONG_KEY_3, NetTestTools.APP_ID);
84 assertThat(longKey2, notNullValue());
85
86 assertThat(longKey1.hash(), is(longKey2.hash()));
87 }
88
89 /**
90 * Tests equals for string based keys.
91 */
92 @Test
93 public void stringKey() {
94 Key stringKey1 = Key.of(KEY_1, NetTestTools.APP_ID);
95 Key copyOfStringKey1 = Key.of(KEY_1, NetTestTools.APP_ID);
96 Key stringKey2 = Key.of(KEY_2, NetTestTools.APP_ID);
97 Key copyOfStringKey2 = Key.of(KEY_2, NetTestTools.APP_ID);
98 Key stringKey3 = Key.of(KEY_3, NetTestTools.APP_ID);
99
100 new EqualsTester()
101 .addEqualityGroup(stringKey1, copyOfStringKey1)
102 .addEqualityGroup(stringKey2, copyOfStringKey2)
103 .addEqualityGroup(stringKey3)
104 .testEquals();
105 }
106
107 /**
108 * Tests equals for long based keys.
109 */
110 @Test
111 public void longKey() {
112 Key longKey1 = Key.of(LONG_KEY_1, NetTestTools.APP_ID);
113 Key copyOfLongKey1 = Key.of(LONG_KEY_1, NetTestTools.APP_ID);
114 Key longKey2 = Key.of(LONG_KEY_2, NetTestTools.APP_ID);
115 Key copyOfLongKey2 = Key.of(LONG_KEY_2, NetTestTools.APP_ID);
116 Key longKey3 = Key.of(LONG_KEY_3, NetTestTools.APP_ID);
117
118 new EqualsTester()
119 .addEqualityGroup(longKey1, copyOfLongKey1)
120 .addEqualityGroup(longKey2, copyOfLongKey2)
121 .addEqualityGroup(longKey3)
122 .testEquals();
123 }
David Glantzc3e4c0d2021-11-18 13:41:14 -0600124
125 /**
126 * Tests compareTo for string based keys.
127 */
128 @Test
129 public void stringKeyCompare() {
130 Key stringKey1 = Key.of(KEY_1, NetTestTools.APP_ID);
131 Key copyOfStringKey1 = Key.of(KEY_1, NetTestTools.APP_ID);
132 Key stringKey2 = Key.of(KEY_2, NetTestTools.APP_ID);
133 Key copyOfStringKey2 = Key.of(KEY_2, NetTestTools.APP_ID);
134 Key stringKey3 = Key.of(KEY_3, NetTestTools.APP_ID);
135 Key copyOfStringKey3 = Key.of(KEY_3, NetTestTools.APP_ID);
136
137 assertThat(stringKey1, comparesEqualTo(copyOfStringKey1));
138 assertThat(stringKey1, lessThan(stringKey2));
139 assertThat(stringKey1, lessThan(stringKey3));
140
141 assertThat(stringKey2, greaterThan(stringKey1));
142 assertThat(stringKey2, comparesEqualTo(copyOfStringKey2));
143 assertThat(stringKey2, lessThan(stringKey3));
144
145 assertThat(stringKey3, greaterThan(stringKey1));
146 assertThat(stringKey3, greaterThan(stringKey2));
147 assertThat(stringKey3, comparesEqualTo(copyOfStringKey3));
148 }
149
150 /**
151 * Tests compareTo for long based keys.
152 */
153 @Test
154 public void longKeyCompare() {
155 Key longKey1 = Key.of(LONG_KEY_1, NetTestTools.APP_ID);
156 Key copyOfLongKey1 = Key.of(LONG_KEY_1, NetTestTools.APP_ID);
157 Key longKey2 = Key.of(LONG_KEY_2, NetTestTools.APP_ID);
158 Key copyOfLongKey2 = Key.of(LONG_KEY_2, NetTestTools.APP_ID);
159 Key longKey3 = Key.of(LONG_KEY_3, NetTestTools.APP_ID);
160 Key copyOfLongKey3 = Key.of(LONG_KEY_3, NetTestTools.APP_ID);
161
162 assertThat(longKey1, comparesEqualTo(copyOfLongKey1));
163 assertThat(longKey1, lessThan(longKey2));
164 assertThat(longKey1, lessThan(longKey3));
165
166 assertThat(longKey2, greaterThan(longKey1));
167 assertThat(longKey2, comparesEqualTo(copyOfLongKey2));
168 assertThat(longKey2, lessThan(longKey3));
169
170 assertThat(longKey3, greaterThan(longKey1));
171 assertThat(longKey3, greaterThan(longKey2));
172 assertThat(longKey3, comparesEqualTo(copyOfLongKey3));
173 }
174
175 /**
176 * Tests compareTo for string and long based keys.
177 */
178 @Test
179 public void stringAndLongKeyCompare() {
180 Key stringKey0 = Key.of("0" + KEY_1, NetTestTools.APP_ID);
181 Key longKey1 = Key.of(LONG_KEY_1, NetTestTools.APP_ID);
182 Key stringKey2 = Key.of(KEY_2, NetTestTools.APP_ID);
183
184
185 assertThat(stringKey0, lessThan(longKey1));
186 assertThat(stringKey0, lessThan(stringKey2));
187
188 assertThat(longKey1, greaterThan(stringKey0));
189 assertThat(longKey1, lessThan(stringKey2));
190
191 assertThat(stringKey2, greaterThan(stringKey0));
192 assertThat(stringKey2, greaterThan(longKey1));
193 }
Ray Milkeyabbdcb72015-02-11 09:32:17 -0800194}