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