Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.net.pi.runtime; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.testing.EqualsTester; |
| 21 | import org.apache.commons.collections.CollectionUtils; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.util.ImmutableByteSequence; |
| 24 | |
| 25 | import java.util.Collection; |
| 26 | |
| 27 | import static org.hamcrest.MatcherAssert.assertThat; |
| 28 | import static org.hamcrest.Matchers.is; |
| 29 | import static org.hamcrest.Matchers.notNullValue; |
| 30 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 31 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
| 32 | import static org.onosproject.net.pi.runtime.PiConstantsTest.DST_ADDR; |
| 33 | import static org.onosproject.net.pi.runtime.PiConstantsTest.IPV4_HEADER_NAME; |
| 34 | import static org.onosproject.net.pi.runtime.PiConstantsTest.SRC_ADDR; |
| 35 | |
| 36 | /** |
| 37 | * Unit tests for PiMatchKey class. |
| 38 | */ |
| 39 | public class PiMatchKeyTest { |
| 40 | |
| 41 | final ImmutableByteSequence value1 = copyFrom(0x0a010101); |
| 42 | final ImmutableByteSequence value2 = copyFrom(0x0a010102); |
| 43 | int prefixLength = 24; |
| 44 | final PiHeaderFieldId piHeaderField1 = PiHeaderFieldId.of(IPV4_HEADER_NAME, SRC_ADDR); |
| 45 | final PiHeaderFieldId piHeaderField2 = PiHeaderFieldId.of(IPV4_HEADER_NAME, DST_ADDR); |
| 46 | PiLpmFieldMatch piLpmFieldMatch1 = new PiLpmFieldMatch(piHeaderField1, value1, prefixLength); |
| 47 | PiLpmFieldMatch piLpmFieldMatch2 = new PiLpmFieldMatch(piHeaderField2, value2, prefixLength); |
| 48 | |
| 49 | final PiMatchKey piMatchKey1 = PiMatchKey.builder() |
| 50 | .addFieldMatch(piLpmFieldMatch1) |
| 51 | .build(); |
| 52 | final PiMatchKey sameAsPiMatchKey1 = PiMatchKey.builder() |
| 53 | .addFieldMatch(piLpmFieldMatch1) |
| 54 | .build(); |
| 55 | final PiMatchKey piMatchKey2 = PiMatchKey.builder() |
| 56 | .addFieldMatch(piLpmFieldMatch2) |
| 57 | .build(); |
| 58 | |
| 59 | /** |
| 60 | * Checks that the PiMatchKey class is immutable. |
| 61 | */ |
| 62 | @Test |
| 63 | public void testImmutability() { |
| 64 | |
| 65 | assertThatClassIsImmutable(PiMatchKey.class); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Checks the operation of equals(), hashCode() and toString() methods. |
| 70 | */ |
| 71 | @Test |
| 72 | public void testEquals() { |
| 73 | |
| 74 | new EqualsTester() |
| 75 | .addEqualityGroup(piMatchKey1, sameAsPiMatchKey1) |
| 76 | .addEqualityGroup(piMatchKey2) |
| 77 | .testEquals(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Checks the methods of PiMatchKey. |
| 82 | */ |
| 83 | @Test |
| 84 | public void testMethods() { |
| 85 | |
| 86 | Collection<PiFieldMatch> piFieldMatches = Lists.newArrayList(); |
| 87 | piFieldMatches.add(piLpmFieldMatch1); |
| 88 | piFieldMatches.add(piLpmFieldMatch2); |
| 89 | |
| 90 | final PiMatchKey piMatchKey = PiMatchKey.builder() |
| 91 | .addFieldMatches(piFieldMatches) |
| 92 | .build(); |
| 93 | |
| 94 | assertThat(piMatchKey, is(notNullValue())); |
| 95 | assertThat("Incorrect members value", |
| 96 | CollectionUtils.isEqualCollection(piMatchKey.fieldMatches(), piFieldMatches)); |
| 97 | } |
| 98 | } |