summaryrefslogtreecommitdiffstats
path: root/framework/src/onos/core/api/src/test/java/org/onosproject/net/intent/KeyTest.java
blob: dfc73442460d2646f3a12229408475d01605867b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.net.intent;

import org.junit.Test;
import org.onosproject.net.NetTestTools;

import com.google.common.testing.EqualsTester;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;

/**
 * Unit tests for the intent key class.
 */
public class KeyTest {

    private static final String KEY_1 = "key1";
    private static final String KEY_2 = "key2";
    private static final String KEY_3 = "key3";

    private static final long LONG_KEY_1 = 0x1111;
    private static final long LONG_KEY_2 = 0x2222;
    private static final long LONG_KEY_3 = 0x3333;

    /**
     * Tests that keys are properly immutable.
     */
    @Test
    public void keysAreImmutable() {
        assertThatClassIsImmutableBaseClass(Key.class);

        // Will be a long based key, class is private so cannot be
        // accessed directly
        Key longKey = Key.of(0xabcdefL, NetTestTools.APP_ID);
        assertThatClassIsImmutable(longKey.getClass());

        // Will be a String based key, class is private so cannot be
        // accessed directly.
        Key stringKey = Key.of("some key", NetTestTools.APP_ID);
        assertThatClassIsImmutable(stringKey.getClass());
    }

    /**
     * Tests string key construction.
     */
    @Test
    public void stringKeyConstruction() {
        Key stringKey1 = Key.of(KEY_3, NetTestTools.APP_ID);
        assertThat(stringKey1, notNullValue());
        Key stringKey2 = Key.of(KEY_3, NetTestTools.APP_ID);
        assertThat(stringKey2, notNullValue());

        assertThat(stringKey1.hash(), is(stringKey2.hash()));
    }

    /**
     * Tests long key construction.
     */
    @Test
    public void longKeyConstruction() {
        Key longKey1 = Key.of(LONG_KEY_3, NetTestTools.APP_ID);
        assertThat(longKey1, notNullValue());
        Key longKey2 = Key.of(LONG_KEY_3, NetTestTools.APP_ID);
        assertThat(longKey2, notNullValue());

        assertThat(longKey1.hash(), is(longKey2.hash()));
    }

    /**
     * Tests equals for string based keys.
     */
    @Test
    public void stringKey() {
        Key stringKey1 = Key.of(KEY_1, NetTestTools.APP_ID);
        Key copyOfStringKey1 = Key.of(KEY_1, NetTestTools.APP_ID);
        Key stringKey2 = Key.of(KEY_2, NetTestTools.APP_ID);
        Key copyOfStringKey2 = Key.of(KEY_2, NetTestTools.APP_ID);
        Key stringKey3 = Key.of(KEY_3, NetTestTools.APP_ID);

        new EqualsTester()
                .addEqualityGroup(stringKey1, copyOfStringKey1)
                .addEqualityGroup(stringKey2, copyOfStringKey2)
                .addEqualityGroup(stringKey3)
                .testEquals();
    }

    /**
     * Tests equals for long based keys.
     */
    @Test
    public void longKey() {
        Key longKey1 = Key.of(LONG_KEY_1, NetTestTools.APP_ID);
        Key copyOfLongKey1 = Key.of(LONG_KEY_1, NetTestTools.APP_ID);
        Key longKey2 = Key.of(LONG_KEY_2, NetTestTools.APP_ID);
        Key copyOfLongKey2 = Key.of(LONG_KEY_2, NetTestTools.APP_ID);
        Key longKey3 = Key.of(LONG_KEY_3, NetTestTools.APP_ID);

        new EqualsTester()
                .addEqualityGroup(longKey1, copyOfLongKey1)
                .addEqualityGroup(longKey2, copyOfLongKey2)
                .addEqualityGroup(longKey3)
                .testEquals();
    }
}