blob: 8521d5c832cb8f383330807a1f4be457961b93cf [file] [log] [blame]
Frank Wange33e4ed2017-06-28 10:01:07 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Frank Wange33e4ed2017-06-28 10:01:07 +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 */
16
17package org.onosproject.net.pi.runtime;
18
19import com.google.common.testing.EqualsTester;
20import org.junit.Test;
21
22import static org.hamcrest.MatcherAssert.assertThat;
23import static org.hamcrest.Matchers.is;
24import static org.hamcrest.Matchers.notNullValue;
25import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
26import static org.onosproject.net.pi.runtime.PiConstantsTest.DST_ADDR;
27import static org.onosproject.net.pi.runtime.PiConstantsTest.ETH_HEADER_NAME;
28import static org.onosproject.net.pi.runtime.PiConstantsTest.ETH_TYPE;
29import static org.onosproject.net.pi.runtime.PiConstantsTest.IPV4_HEADER_NAME;
30
31/**
32 * Unit tests for PiHeaderFieldId class.
33 */
34public class PiHeaderFieldIdTest {
35 final String headerName = ETH_HEADER_NAME;
36 final String dstAddr = DST_ADDR;
37 final String etherType = ETH_TYPE;
38
39 final PiHeaderFieldId piHeaderFieldId1 = PiHeaderFieldId.of(headerName, dstAddr);
40 final PiHeaderFieldId sameAsPiHeaderFieldId1 = PiHeaderFieldId.of(headerName, dstAddr);
41 final PiHeaderFieldId piHeaderFieldId2 = PiHeaderFieldId.of(headerName, etherType);
42
43 int index = 10;
44 final PiHeaderFieldId piHeaderFieldId1WithIndex = PiHeaderFieldId.of(headerName, dstAddr, index);
45 final PiHeaderFieldId sameAsPiHeaderFieldId1WithIndex = PiHeaderFieldId.of(headerName, dstAddr, index);
46 final PiHeaderFieldId piHeaderFieldId2WithIndex = PiHeaderFieldId.of(headerName, etherType, index);
47
48 /**
49 * Checks that the PiHeaderFieldId class is immutable.
50 */
51 @Test
52 public void testImmutability() {
53 assertThatClassIsImmutable(PiHeaderFieldId.class);
54 }
55
56 /**
57 * Checks the operation of equals(), hashCode() and toString() methods.
58 */
59 @Test
60 public void testEquals() {
61 new EqualsTester()
62 .addEqualityGroup(piHeaderFieldId1, sameAsPiHeaderFieldId1)
63 .addEqualityGroup(piHeaderFieldId2)
64 .testEquals();
65 }
66
67 /**
68 * Checks the operation of equals(), hashCode() and toString() methods.
69 */
70 @Test
71 public void testEqualsWithIndex() {
72 new EqualsTester()
73 .addEqualityGroup(piHeaderFieldId1WithIndex, sameAsPiHeaderFieldId1WithIndex)
74 .addEqualityGroup(piHeaderFieldId2WithIndex)
75 .testEquals();
76 }
77
78 /**
79 * Checks the construction of a PiHeaderFieldId object.
80 */
81 @Test
82 public void testConstruction() {
83 final String name = IPV4_HEADER_NAME;
84 final String field = DST_ADDR;
85
86 final PiHeaderFieldId piHeaderFieldId = PiHeaderFieldId.of(name, field);
87 assertThat(piHeaderFieldId, is(notNullValue()));
88 assertThat(piHeaderFieldId.headerName(), is(name));
89 assertThat(piHeaderFieldId.fieldName(), is(field));
90 }
91
92 /**
93 * Checks the construction of a PiHeaderFieldId object with index.
94 */
95 @Test
96 public void testConstructionWithIndex() {
97 final String name = IPV4_HEADER_NAME;
98 final String field = DST_ADDR;
99 final int index = 1;
100 final PiHeaderFieldId piHeaderFieldId = PiHeaderFieldId.of(name, field, index);
101 assertThat(piHeaderFieldId, is(notNullValue()));
102 assertThat(piHeaderFieldId.headerName(), is(name));
103 assertThat(piHeaderFieldId.fieldName(), is(field));
104 assertThat(piHeaderFieldId.index(), is(index));
105 }
106}