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.ImmutableList; |
| 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 | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 24 | import org.onosproject.net.pi.model.PiPacketMetadataId; |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 25 | |
| 26 | import static org.hamcrest.MatcherAssert.assertThat; |
| 27 | import static org.hamcrest.Matchers.is; |
| 28 | import static org.hamcrest.Matchers.notNullValue; |
| 29 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 30 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 31 | import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_OUT; |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 32 | import static org.onosproject.net.pi.runtime.PiConstantsTest.EGRESS_PORT; |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Unit tests for PiPacketOperation class. |
| 36 | */ |
| 37 | public class PiPacketOperationTest { |
| 38 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 39 | private final PiPacketOperation piPacketOperation1 = PiPacketOperation.builder() |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 40 | .withData(ImmutableByteSequence.ofOnes(512)) |
| 41 | .withType(PACKET_OUT) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 42 | .withMetadata(PiPacketMetadata.builder() |
| 43 | .withId(PiPacketMetadataId.of(EGRESS_PORT)) |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 44 | .withValue(copyFrom((short) 255)) |
| 45 | .build()) |
| 46 | .build(); |
| 47 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 48 | private final PiPacketOperation sameAsPiPacketOperation1 = PiPacketOperation.builder() |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 49 | .withData(ImmutableByteSequence.ofOnes(512)) |
| 50 | .withType(PACKET_OUT) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 51 | .withMetadata(PiPacketMetadata.builder() |
| 52 | .withId(PiPacketMetadataId.of(EGRESS_PORT)) |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 53 | .withValue(copyFrom((short) 255)) |
| 54 | .build()) |
| 55 | .build(); |
| 56 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 57 | private final PiPacketOperation piPacketOperation2 = PiPacketOperation.builder() |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 58 | .withData(ImmutableByteSequence.ofOnes(512)) |
| 59 | .withType(PACKET_OUT) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 60 | .withMetadata(PiPacketMetadata.builder() |
| 61 | .withId(PiPacketMetadataId.of(EGRESS_PORT)) |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 62 | .withValue(copyFrom((short) 200)) |
| 63 | .build()) |
| 64 | .build(); |
| 65 | |
| 66 | /** |
| 67 | * Checks that the PiPacketOperation class is immutable. |
| 68 | */ |
| 69 | @Test |
| 70 | public void testImmutability() { |
| 71 | |
| 72 | assertThatClassIsImmutable(PiPacketOperation.class); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Checks the operation of equals(), hashCode() and toString() methods. |
| 77 | */ |
| 78 | @Test |
| 79 | public void testEquals() { |
| 80 | |
| 81 | new EqualsTester() |
| 82 | .addEqualityGroup(piPacketOperation1, sameAsPiPacketOperation1) |
| 83 | .addEqualityGroup(piPacketOperation2) |
| 84 | .testEquals(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Checks the methods of PiPacketOperation. |
| 89 | */ |
| 90 | @Test |
| 91 | public void testMethods() { |
| 92 | |
| 93 | final PiPacketOperation piPacketOperation = PiPacketOperation.builder() |
| 94 | .withData(ImmutableByteSequence.ofOnes(512)) |
| 95 | .withType(PACKET_OUT) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 96 | .withMetadata(PiPacketMetadata.builder() |
| 97 | .withId(PiPacketMetadataId.of(EGRESS_PORT)) |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 98 | .withValue(copyFrom((short) 10)) |
| 99 | .build()) |
| 100 | .build(); |
| 101 | |
| 102 | assertThat(piPacketOperation, is(notNullValue())); |
| 103 | assertThat(piPacketOperation.type(), is(PACKET_OUT)); |
| 104 | assertThat(piPacketOperation.data(), is(ImmutableByteSequence.ofOnes(512))); |
| 105 | assertThat("Incorrect metadatas value", |
| 106 | CollectionUtils.isEqualCollection(piPacketOperation.metadatas(), |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 107 | ImmutableList.of(PiPacketMetadata.builder() |
| 108 | .withId(PiPacketMetadataId |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 109 | .of(EGRESS_PORT)) |
| 110 | .withValue(copyFrom((short) 10)) |
| 111 | .build()))); |
| 112 | } |
| 113 | } |