blob: 38b676522833c0a655e91291232097efc5a5e1d0 [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;
21import org.onlab.util.ImmutableByteSequence;
Carmelo Cascone87892e22017-11-13 16:01:29 -080022import org.onosproject.net.pi.model.PiMatchFieldId;
Frank Wange33e4ed2017-06-28 10:01:07 +080023import org.onosproject.net.pi.model.PiMatchType;
24
25import static org.hamcrest.MatcherAssert.assertThat;
26import static org.hamcrest.Matchers.is;
27import static org.hamcrest.Matchers.notNullValue;
28import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
29import static org.onlab.util.ImmutableByteSequence.copyFrom;
Carmelo Cascone87892e22017-11-13 16:01:29 -080030import static org.onosproject.net.pi.runtime.PiConstantsTest.DOT;
Frank Wange33e4ed2017-06-28 10:01:07 +080031import static org.onosproject.net.pi.runtime.PiConstantsTest.ETH_HEADER_NAME;
32import static org.onosproject.net.pi.runtime.PiConstantsTest.ETH_TYPE;
33
34/**
35 * Unit tests for PiExactFieldMatch class.
36 */
37public class PiExactFieldMatchTest {
Carmelo Cascone87892e22017-11-13 16:01:29 -080038
39 private final ImmutableByteSequence value1 = copyFrom(0x0800);
40 private final ImmutableByteSequence value2 = copyFrom(0x0806);
41 private final PiMatchFieldId piMatchField = PiMatchFieldId.of(ETH_HEADER_NAME + DOT + ETH_TYPE);
42 private PiExactFieldMatch piExactFieldMatch1 = new PiExactFieldMatch(piMatchField, value1);
43 private PiExactFieldMatch sameAsPiExactFieldMatch1 = new PiExactFieldMatch(piMatchField, value1);
44 private PiExactFieldMatch piExactFieldMatch2 = new PiExactFieldMatch(piMatchField, value2);
Frank Wange33e4ed2017-06-28 10:01:07 +080045
46 /**
47 * Checks that the PiExactFieldMatch class is immutable.
48 */
49 @Test
50 public void testImmutability() {
51 assertThatClassIsImmutable(PiExactFieldMatch.class);
52 }
53
54 /**
55 * Checks the operation of equals(), hashCode() and toString() methods.
56 */
57 @Test
58 public void testEquals() {
59 new EqualsTester()
60 .addEqualityGroup(piExactFieldMatch1, sameAsPiExactFieldMatch1)
61 .addEqualityGroup(piExactFieldMatch2)
62 .testEquals();
63 }
64
65 /**
66 * Checks the construction of a PiExactFieldMatch object.
67 */
68 @Test
69 public void testConstruction() {
70 final ImmutableByteSequence value = copyFrom(0x0806);
Carmelo Cascone87892e22017-11-13 16:01:29 -080071 final PiMatchFieldId piMatchField = PiMatchFieldId.of(ETH_HEADER_NAME + DOT + ETH_TYPE);
72 PiExactFieldMatch piExactFieldMatch = new PiExactFieldMatch(piMatchField, value);
Frank Wange33e4ed2017-06-28 10:01:07 +080073 assertThat(piExactFieldMatch, is(notNullValue()));
74 assertThat(piExactFieldMatch.value(), is(value));
75 assertThat(piExactFieldMatch.type(), is(PiMatchType.EXACT));
76 }
77}