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