blob: c78622e62ecfb027edd24159357d4258f8c3475d [file] [log] [blame]
Frank Wange33e4ed2017-06-28 10:01:07 +08001/*
2 * Copyright 2017-present Open Networking Laboratory
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.onlab.util.ImmutableByteSequence;
22import org.onosproject.net.pi.model.PiMatchType;
23
24import static org.hamcrest.MatcherAssert.assertThat;
25import static org.hamcrest.Matchers.is;
26import static org.hamcrest.Matchers.notNullValue;
27import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
28import static org.onlab.util.ImmutableByteSequence.copyFrom;
29import static org.onosproject.net.pi.runtime.PiConstantsTest.VID;
30import static org.onosproject.net.pi.runtime.PiConstantsTest.VLAN_HEADER_NAME;
31
32/**
33 * Unit tests for PiRangeFieldMatch class.
34 */
35public class PiRangeFieldMatchTest {
36 final ImmutableByteSequence high1 = copyFrom(0x10);
37 final ImmutableByteSequence low1 = copyFrom(0x00);
38 final ImmutableByteSequence high2 = copyFrom(0x30);
39 final ImmutableByteSequence low2 = copyFrom(0x40);
40
41 final PiHeaderFieldId piHeaderField = PiHeaderFieldId.of(VLAN_HEADER_NAME, VID);
42 PiRangeFieldMatch piRangeFieldMatch1 = new PiRangeFieldMatch(piHeaderField, low1, high1);
43 PiRangeFieldMatch sameAsPiRangeFieldMatch1 = new PiRangeFieldMatch(piHeaderField, low1, high1);
44 PiRangeFieldMatch piRangeFieldMatch2 = new PiRangeFieldMatch(piHeaderField, low2, high2);
45
46 /**
47 * Checks that the PiRangeFieldMatch class is immutable.
48 */
49 @Test
50 public void testImmutability() {
51 assertThatClassIsImmutable(PiRangeFieldMatch.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(piRangeFieldMatch1, sameAsPiRangeFieldMatch1)
61 .addEqualityGroup(piRangeFieldMatch2)
62 .testEquals();
63 }
64
65 /**
66 * Checks the construction of a PiRangeFieldMatch object.
67 */
68 @Test
69 public void testConstruction() {
70 final ImmutableByteSequence high = copyFrom(0x50);
71 final ImmutableByteSequence low = copyFrom(0x00);
72 final PiHeaderFieldId piHeaderField = PiHeaderFieldId.of(VLAN_HEADER_NAME, VID);
73 PiRangeFieldMatch piRangeFieldMatch = new PiRangeFieldMatch(piHeaderField, low, high);
74 assertThat(piRangeFieldMatch, is(notNullValue()));
75 assertThat(piRangeFieldMatch.lowValue(), is(low));
76 assertThat(piRangeFieldMatch.highValue(), is(high));
77 assertThat(piRangeFieldMatch.type(), is(PiMatchType.RANGE));
78 }
79}