Ray Milkey | abbdcb7 | 2015-02-11 09:32:17 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | abbdcb7 | 2015-02-11 09:32:17 -0800 | [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 | */ |
| 16 | package org.onosproject.net.intent; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | import org.onosproject.net.NetTestTools; |
| 20 | |
| 21 | import com.google.common.testing.EqualsTester; |
| 22 | |
| 23 | import static org.hamcrest.MatcherAssert.assertThat; |
| 24 | import static org.hamcrest.Matchers.is; |
| 25 | import static org.hamcrest.Matchers.notNullValue; |
| 26 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; |
| 27 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 28 | |
| 29 | /** |
| 30 | * Unit tests for the intent key class. |
| 31 | */ |
| 32 | public class KeyTest { |
| 33 | |
| 34 | private static final String KEY_1 = "key1"; |
| 35 | private static final String KEY_2 = "key2"; |
| 36 | private static final String KEY_3 = "key3"; |
| 37 | |
| 38 | private static final long LONG_KEY_1 = 0x1111; |
| 39 | private static final long LONG_KEY_2 = 0x2222; |
| 40 | private static final long LONG_KEY_3 = 0x3333; |
| 41 | |
| 42 | /** |
| 43 | * Tests that keys are properly immutable. |
| 44 | */ |
| 45 | @Test |
| 46 | public void keysAreImmutable() { |
| 47 | assertThatClassIsImmutableBaseClass(Key.class); |
| 48 | |
| 49 | // Will be a long based key, class is private so cannot be |
| 50 | // accessed directly |
| 51 | Key longKey = Key.of(0xabcdefL, NetTestTools.APP_ID); |
| 52 | assertThatClassIsImmutable(longKey.getClass()); |
| 53 | |
| 54 | // Will be a String based key, class is private so cannot be |
| 55 | // accessed directly. |
| 56 | Key stringKey = Key.of("some key", NetTestTools.APP_ID); |
| 57 | assertThatClassIsImmutable(stringKey.getClass()); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Tests string key construction. |
| 62 | */ |
| 63 | @Test |
| 64 | public void stringKeyConstruction() { |
| 65 | Key stringKey1 = Key.of(KEY_3, NetTestTools.APP_ID); |
| 66 | assertThat(stringKey1, notNullValue()); |
| 67 | Key stringKey2 = Key.of(KEY_3, NetTestTools.APP_ID); |
| 68 | assertThat(stringKey2, notNullValue()); |
| 69 | |
| 70 | assertThat(stringKey1.hash(), is(stringKey2.hash())); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Tests long key construction. |
| 75 | */ |
| 76 | @Test |
| 77 | public void longKeyConstruction() { |
| 78 | Key longKey1 = Key.of(LONG_KEY_3, NetTestTools.APP_ID); |
| 79 | assertThat(longKey1, notNullValue()); |
| 80 | Key longKey2 = Key.of(LONG_KEY_3, NetTestTools.APP_ID); |
| 81 | assertThat(longKey2, notNullValue()); |
| 82 | |
| 83 | assertThat(longKey1.hash(), is(longKey2.hash())); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Tests equals for string based keys. |
| 88 | */ |
| 89 | @Test |
| 90 | public void stringKey() { |
| 91 | Key stringKey1 = Key.of(KEY_1, NetTestTools.APP_ID); |
| 92 | Key copyOfStringKey1 = Key.of(KEY_1, NetTestTools.APP_ID); |
| 93 | Key stringKey2 = Key.of(KEY_2, NetTestTools.APP_ID); |
| 94 | Key copyOfStringKey2 = Key.of(KEY_2, NetTestTools.APP_ID); |
| 95 | Key stringKey3 = Key.of(KEY_3, NetTestTools.APP_ID); |
| 96 | |
| 97 | new EqualsTester() |
| 98 | .addEqualityGroup(stringKey1, copyOfStringKey1) |
| 99 | .addEqualityGroup(stringKey2, copyOfStringKey2) |
| 100 | .addEqualityGroup(stringKey3) |
| 101 | .testEquals(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Tests equals for long based keys. |
| 106 | */ |
| 107 | @Test |
| 108 | public void longKey() { |
| 109 | Key longKey1 = Key.of(LONG_KEY_1, NetTestTools.APP_ID); |
| 110 | Key copyOfLongKey1 = Key.of(LONG_KEY_1, NetTestTools.APP_ID); |
| 111 | Key longKey2 = Key.of(LONG_KEY_2, NetTestTools.APP_ID); |
| 112 | Key copyOfLongKey2 = Key.of(LONG_KEY_2, NetTestTools.APP_ID); |
| 113 | Key longKey3 = Key.of(LONG_KEY_3, NetTestTools.APP_ID); |
| 114 | |
| 115 | new EqualsTester() |
| 116 | .addEqualityGroup(longKey1, copyOfLongKey1) |
| 117 | .addEqualityGroup(longKey2, copyOfLongKey2) |
| 118 | .addEqualityGroup(longKey3) |
| 119 | .testEquals(); |
| 120 | } |
| 121 | } |