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