Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 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.Maps; |
| 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.Map; |
| 26 | |
| 27 | import static org.hamcrest.MatcherAssert.assertThat; |
| 28 | import static org.hamcrest.Matchers.is; |
| 29 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 30 | import static org.onosproject.net.pi.runtime.PiConstantsTest.*; |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Unit tests for PiTableEntry class. |
| 34 | */ |
| 35 | public class PiTableEntryTest { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 36 | private final PiTableEntry piTableEntry1 = PiTableEntry.builder() |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 37 | .forTable(PiTableId.of("Table10")) |
| 38 | .withCookie(0xac) |
| 39 | .withPriority(10) |
| 40 | .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build()) |
| 41 | .withTimeout(100) |
| 42 | .build(); |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 43 | private final PiTableEntry sameAsPiTableEntry1 = PiTableEntry.builder() |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 44 | .forTable(PiTableId.of("Table10")) |
| 45 | .withCookie(0xac) |
| 46 | .withPriority(10) |
| 47 | .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build()) |
| 48 | .withTimeout(100) |
| 49 | .build(); |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 50 | private final PiTableEntry piTableEntry2 = PiTableEntry.builder() |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 51 | .forTable(PiTableId.of("Table20")) |
| 52 | .withCookie(0xac) |
| 53 | .withPriority(10) |
| 54 | .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build()) |
| 55 | .withTimeout(1000) |
| 56 | .build(); |
| 57 | |
| 58 | /** |
| 59 | * Checks that the PiTableEntry class is immutable. |
| 60 | */ |
| 61 | @Test |
| 62 | public void testImmutability() { |
| 63 | assertThatClassIsImmutable(PiTableEntry.class); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Tests the equals, hashCode and toString methods using Guava EqualsTester. |
| 68 | */ |
| 69 | @Test |
| 70 | public void testEquals() { |
| 71 | new EqualsTester() |
| 72 | .addEqualityGroup(piTableEntry1, sameAsPiTableEntry1) |
| 73 | .addEqualityGroup(piTableEntry2) |
| 74 | .testEquals(); |
| 75 | } |
| 76 | |
| 77 | /** |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 78 | * Tests equality of the empty table entry. |
| 79 | */ |
| 80 | @Test |
| 81 | public void testEmptyEquals() { |
| 82 | new EqualsTester() |
| 83 | .addEqualityGroup(PiTableEntry.EMTPY, PiTableEntry.EMTPY) |
| 84 | .testEquals(); |
| 85 | } |
| 86 | |
| 87 | /** |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 88 | * Tests creation of a DefaultFlowRule using a FlowRule constructor. |
| 89 | */ |
| 90 | @Test |
| 91 | public void testBuilder() { |
| 92 | |
| 93 | PiTableId piTableId = PiTableId.of("table10"); |
| 94 | long cookie = 0xfff0323; |
| 95 | int priority = 100; |
| 96 | double timeout = 1000; |
| 97 | PiHeaderFieldId piHeaderFieldId = PiHeaderFieldId.of(IPV4_HEADER_NAME, DST_ADDR); |
| 98 | PiFieldMatch piFieldMatch = new PiExactFieldMatch(piHeaderFieldId, ImmutableByteSequence.copyFrom(0x0a010101)); |
| 99 | PiAction piAction = PiAction.builder().withId(PiActionId.of(DROP)).build(); |
| 100 | final Map<PiHeaderFieldId, PiFieldMatch> fieldMatches = Maps.newHashMap(); |
| 101 | fieldMatches.put(piHeaderFieldId, piFieldMatch); |
| 102 | final PiTableEntry piTableEntry = PiTableEntry.builder() |
| 103 | .forTable(piTableId) |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 104 | .withMatchKey(PiMatchKey.builder() |
| 105 | .addFieldMatches(fieldMatches.values()) |
| 106 | .build()) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 107 | .withAction(piAction) |
| 108 | .withCookie(cookie) |
| 109 | .withPriority(priority) |
| 110 | .withTimeout(timeout) |
| 111 | .build(); |
| 112 | |
| 113 | assertThat(piTableEntry.table(), is(piTableId)); |
| 114 | assertThat(piTableEntry.cookie(), is(cookie)); |
| 115 | assertThat(piTableEntry.priority().get(), is(priority)); |
| 116 | assertThat(piTableEntry.timeout().get(), is(timeout)); |
| 117 | assertThat("Incorrect match param value", |
Carmelo Cascone | 0e896a0 | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 118 | CollectionUtils.isEqualCollection(piTableEntry.matchKey().fieldMatches(), fieldMatches.values())); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 119 | assertThat(piTableEntry.action(), is(piAction)); |
| 120 | } |
| 121 | } |