Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020-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 | |
| 17 | package org.onosproject.net.pi.runtime; |
| 18 | |
| 19 | import com.google.common.testing.EqualsTester; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.util.ImmutableByteSequence; |
| 22 | import org.onosproject.net.pi.model.PiMatchFieldId; |
| 23 | import org.onosproject.net.pi.model.PiMatchType; |
| 24 | |
| 25 | import static org.hamcrest.MatcherAssert.assertThat; |
| 26 | import static org.hamcrest.Matchers.is; |
| 27 | import static org.hamcrest.Matchers.notNullValue; |
| 28 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 29 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
| 30 | import static org.onosproject.net.pi.runtime.PiConstantsTest.*; |
| 31 | |
| 32 | /** |
| 33 | * Unit tests for PiOptionalFieldMatch class. |
| 34 | */ |
| 35 | public class PiOptionalFieldMatchTest { |
| 36 | |
| 37 | private final ImmutableByteSequence value1 = copyFrom(0x0800); |
| 38 | private final ImmutableByteSequence value2 = copyFrom(0x0806); |
| 39 | private final PiMatchFieldId piMatchField = PiMatchFieldId.of(ETH_HEADER_NAME + DOT + ETH_TYPE); |
| 40 | private PiOptionalFieldMatch piOptionalFieldMatch1 = new PiOptionalFieldMatch(piMatchField, value1); |
| 41 | private PiOptionalFieldMatch sameAsPiOptionalFieldMatch1 = new PiOptionalFieldMatch(piMatchField, value1); |
| 42 | private PiOptionalFieldMatch piOptionalFieldMatch2 = new PiOptionalFieldMatch(piMatchField, value2); |
| 43 | |
| 44 | /** |
| 45 | * Checks that the PiOptionalFieldMatch class is immutable. |
| 46 | */ |
| 47 | @Test |
| 48 | public void testImmutability() { |
| 49 | assertThatClassIsImmutable(PiOptionalFieldMatch.class); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Checks the operation of equals(), hashCode() and toString() methods. |
| 54 | */ |
| 55 | @Test |
| 56 | public void testEquals() { |
| 57 | new EqualsTester() |
| 58 | .addEqualityGroup(piOptionalFieldMatch1, sameAsPiOptionalFieldMatch1) |
| 59 | .addEqualityGroup(piOptionalFieldMatch2) |
| 60 | .testEquals(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Checks the construction of a PiOptionalFieldMatch object. |
| 65 | */ |
| 66 | @Test |
| 67 | public void testConstruction() { |
| 68 | final ImmutableByteSequence value = copyFrom(0x0806); |
| 69 | final PiMatchFieldId piMatchField = PiMatchFieldId.of(ETH_HEADER_NAME + DOT + ETH_TYPE); |
| 70 | PiOptionalFieldMatch piOptionalFieldMatch = new PiOptionalFieldMatch(piMatchField, value); |
| 71 | assertThat(piOptionalFieldMatch, is(notNullValue())); |
| 72 | assertThat(piOptionalFieldMatch.value(), is(value)); |
| 73 | assertThat(piOptionalFieldMatch.type(), is(PiMatchType.OPTIONAL)); |
| 74 | } |
| 75 | } |