blob: 2724da0f5ddef0d8e7cf57d6576f22a3f37ba159 [file] [log] [blame]
Carmelo Cascone87892e22017-11-13 16:01:29 -08001/*
2 * Copyright 2017-present Open Networking Foundation
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
17package org.onosproject.net.pi.runtime;
18
19import com.google.common.testing.EqualsTester;
20import org.junit.Test;
21import org.onosproject.net.pi.model.PiMatchFieldId;
22
23import static org.hamcrest.MatcherAssert.assertThat;
24import static org.hamcrest.Matchers.is;
25import static org.hamcrest.Matchers.notNullValue;
26import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
27import static org.onosproject.net.pi.runtime.PiConstantsTest.DOT;
28import static org.onosproject.net.pi.runtime.PiConstantsTest.DST_ADDR;
29import static org.onosproject.net.pi.runtime.PiConstantsTest.ETH_HEADER_NAME;
30import static org.onosproject.net.pi.runtime.PiConstantsTest.ETH_TYPE;
31import static org.onosproject.net.pi.runtime.PiConstantsTest.IPV4_HEADER_NAME;
32
33/**
34 * Unit tests for PiMatchFieldId class.
35 */
36public class PiMatchFieldIdTest {
37 private final String headerName = ETH_HEADER_NAME;
38 private final String dstAddr = DST_ADDR;
39 private final String etherType = ETH_TYPE;
40
41 private final PiMatchFieldId piMatchFieldId1 = PiMatchFieldId.of(headerName + DOT + dstAddr);
42 private final PiMatchFieldId sameAsPiMatchFieldId1 = PiMatchFieldId.of(headerName + DOT + dstAddr);
43 private final PiMatchFieldId piMatchFieldId2 = PiMatchFieldId.of(headerName + DOT + etherType);
44
45 private int index = 10;
46 private final PiMatchFieldId piMatchFieldId1WithIndex = PiMatchFieldId
47 .of(headerName + DOT + dstAddr + "[" + index + "]");
48 private final PiMatchFieldId sameAsPiMatchFieldId1WithIndex = PiMatchFieldId
49 .of(headerName + DOT + dstAddr + "[" + index + "]");
50 private final PiMatchFieldId piMatchFieldId2WithIndex = PiMatchFieldId
51 .of(headerName + DOT + etherType + "[" + index + "]");
52
53 /**
54 * Checks that the PiMatchFieldId class is immutable.
55 */
56 @Test
57 public void testImmutability() {
58 assertThatClassIsImmutable(PiMatchFieldId.class);
59 }
60
61 /**
62 * Checks the operation of equals(), hashCode() and toString() methods.
63 */
64 @Test
65 public void testEquals() {
66 new EqualsTester()
67 .addEqualityGroup(piMatchFieldId1, sameAsPiMatchFieldId1)
68 .addEqualityGroup(piMatchFieldId2)
69 .testEquals();
70 }
71
72 /**
73 * Checks the operation of equals(), hashCode() and toString() methods.
74 */
75 @Test
76 public void testEqualsWithIndex() {
77 new EqualsTester()
78 .addEqualityGroup(piMatchFieldId1WithIndex, sameAsPiMatchFieldId1WithIndex)
79 .addEqualityGroup(piMatchFieldId2WithIndex)
80 .testEquals();
81 }
82
83 /**
84 * Checks the construction of a PiMatchFieldId object.
85 */
86 @Test
87 public void testConstruction() {
88 final String name = IPV4_HEADER_NAME + DOT + DST_ADDR;
89 final PiMatchFieldId piMatchFieldId = PiMatchFieldId.of(name);
90 assertThat(piMatchFieldId, is(notNullValue()));
91 assertThat(piMatchFieldId.id(), is(name));
92 }
93
94 /**
95 * Checks the construction of a PiMatchFieldId object with index.
96 */
97 @Test
98 public void testConstructionWithIndex() {
99 final String name = IPV4_HEADER_NAME + DOT + DST_ADDR + "[1]";
100 final PiMatchFieldId piMatchFieldId = PiMatchFieldId.of(name);
101 assertThat(piMatchFieldId, is(notNullValue()));
102 assertThat(piMatchFieldId.id(), is(name));
103 }
104}