blob: a6d62afb75f2ebb9d16e1fb030f15d380d7e3e47 [file] [log] [blame]
Frank Wang4e848042017-08-03 19:48:11 +08001/*
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
17package org.onosproject.net.pi.runtime;
18
19import com.google.common.collect.Lists;
20import com.google.common.testing.EqualsTester;
21import org.apache.commons.collections.CollectionUtils;
22import org.junit.Test;
23import org.onlab.util.ImmutableByteSequence;
Carmelo Cascone87892e22017-11-13 16:01:29 -080024import org.onosproject.net.pi.model.PiMatchFieldId;
Frank Wang4e848042017-08-03 19:48:11 +080025
26import java.util.Collection;
27
28import static org.hamcrest.MatcherAssert.assertThat;
29import static org.hamcrest.Matchers.is;
30import static org.hamcrest.Matchers.notNullValue;
31import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
32import static org.onlab.util.ImmutableByteSequence.copyFrom;
Carmelo Cascone87892e22017-11-13 16:01:29 -080033import static org.onosproject.net.pi.runtime.PiConstantsTest.DOT;
Frank Wang4e848042017-08-03 19:48:11 +080034import static org.onosproject.net.pi.runtime.PiConstantsTest.DST_ADDR;
35import static org.onosproject.net.pi.runtime.PiConstantsTest.IPV4_HEADER_NAME;
36import static org.onosproject.net.pi.runtime.PiConstantsTest.SRC_ADDR;
37
38/**
39 * Unit tests for PiMatchKey class.
40 */
41public class PiMatchKeyTest {
42
Carmelo Cascone87892e22017-11-13 16:01:29 -080043 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 Wang4e848042017-08-03 19:48:11 +080050
Carmelo Cascone87892e22017-11-13 16:01:29 -080051 private final PiMatchKey piMatchKey1 = PiMatchKey.builder()
Frank Wang4e848042017-08-03 19:48:11 +080052 .addFieldMatch(piLpmFieldMatch1)
53 .build();
Carmelo Cascone87892e22017-11-13 16:01:29 -080054 private final PiMatchKey sameAsPiMatchKey1 = PiMatchKey.builder()
Frank Wang4e848042017-08-03 19:48:11 +080055 .addFieldMatch(piLpmFieldMatch1)
56 .build();
Carmelo Cascone87892e22017-11-13 16:01:29 -080057 private final PiMatchKey piMatchKey2 = PiMatchKey.builder()
Frank Wang4e848042017-08-03 19:48:11 +080058 .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)
78 .addEqualityGroup(piMatchKey2)
79 .testEquals();
80 }
81
82 /**
83 * Checks the methods of PiMatchKey.
84 */
85 @Test
86 public void testMethods() {
87
88 Collection<PiFieldMatch> piFieldMatches = Lists.newArrayList();
89 piFieldMatches.add(piLpmFieldMatch1);
90 piFieldMatches.add(piLpmFieldMatch2);
91
92 final PiMatchKey piMatchKey = PiMatchKey.builder()
93 .addFieldMatches(piFieldMatches)
94 .build();
95
96 assertThat(piMatchKey, is(notNullValue()));
97 assertThat("Incorrect members value",
98 CollectionUtils.isEqualCollection(piMatchKey.fieldMatches(), piFieldMatches));
99 }
100}