Mehmed Mustafa | 3e269df | 2017-11-24 17:19:49 +0300 | [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.p4runtime.model; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.testing.EqualsTester; |
| 21 | import org.junit.Test; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 22 | import org.onosproject.net.pi.model.PiPacketMetadataId; |
| 23 | import org.onosproject.net.pi.model.PiPacketMetadataModel; |
Mehmed Mustafa | 3e269df | 2017-11-24 17:19:49 +0300 | [diff] [blame] | 24 | import org.onosproject.net.pi.model.PiPacketOperationType; |
| 25 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 26 | |
| 27 | /** |
| 28 | * Unit tests for P4PacketOperationModel class. |
| 29 | */ |
| 30 | public class P4PacketOperationModelTest { |
| 31 | |
| 32 | private static final PiPacketOperationType PI_PACKET_OPERATION_TYPE_1 = PiPacketOperationType.PACKET_IN; |
| 33 | private static final PiPacketOperationType PI_PACKET_OPERATION_TYPE_2 = PiPacketOperationType.PACKET_OUT; |
| 34 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 35 | private static final PiPacketMetadataId PI_CONTROL_METADATA_ID_1 = PiPacketMetadataId.of("Metadata1"); |
| 36 | private static final PiPacketMetadataId PI_CONTROL_METADATA_ID_2 = PiPacketMetadataId.of("Metadata2"); |
Mehmed Mustafa | 3e269df | 2017-11-24 17:19:49 +0300 | [diff] [blame] | 37 | |
| 38 | private static final int BIT_WIDTH_1 = 8; |
| 39 | private static final int BIT_WIDTH_2 = 9; |
| 40 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 41 | private static final PiPacketMetadataModel PI_CONTROL_METADATA_MODEL_1 = |
| 42 | new P4PacketMetadataModel(PI_CONTROL_METADATA_ID_1, BIT_WIDTH_1); |
| 43 | private static final PiPacketMetadataModel PI_CONTROL_METADATA_MODEL_2 = |
| 44 | new P4PacketMetadataModel(PI_CONTROL_METADATA_ID_2, BIT_WIDTH_2); |
Mehmed Mustafa | 3e269df | 2017-11-24 17:19:49 +0300 | [diff] [blame] | 45 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 46 | private static final ImmutableList<PiPacketMetadataModel> METADATAS_1 = |
| 47 | new ImmutableList.Builder<PiPacketMetadataModel>() |
Mehmed Mustafa | 3e269df | 2017-11-24 17:19:49 +0300 | [diff] [blame] | 48 | .add(PI_CONTROL_METADATA_MODEL_1) |
| 49 | .build(); |
| 50 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 51 | private static final ImmutableList<PiPacketMetadataModel> METADATAS_2 = |
| 52 | new ImmutableList.Builder<PiPacketMetadataModel>() |
Mehmed Mustafa | 3e269df | 2017-11-24 17:19:49 +0300 | [diff] [blame] | 53 | .add(PI_CONTROL_METADATA_MODEL_2) |
| 54 | .build(); |
| 55 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 56 | private static final ImmutableList<PiPacketMetadataModel> METADATAS_3 = |
| 57 | new ImmutableList.Builder<PiPacketMetadataModel>() |
Mehmed Mustafa | 3e269df | 2017-11-24 17:19:49 +0300 | [diff] [blame] | 58 | .add(PI_CONTROL_METADATA_MODEL_1) |
| 59 | .add(PI_CONTROL_METADATA_MODEL_2) |
| 60 | .build(); |
| 61 | |
| 62 | private static final P4PacketOperationModel P4_PACKET_OPERATION_MODEL_1 = |
| 63 | new P4PacketOperationModel(PI_PACKET_OPERATION_TYPE_1, METADATAS_1); |
| 64 | private static final P4PacketOperationModel SAME_AS_P4_PACKET_OPERATION_MODEL_1 = |
| 65 | new P4PacketOperationModel(PI_PACKET_OPERATION_TYPE_1, METADATAS_1); |
| 66 | private static final P4PacketOperationModel P4_PACKET_OPERATION_MODEL_2 = |
| 67 | new P4PacketOperationModel(PI_PACKET_OPERATION_TYPE_2, METADATAS_2); |
| 68 | private static final P4PacketOperationModel P4_PACKET_OPERATION_MODEL_3 = |
| 69 | new P4PacketOperationModel(PI_PACKET_OPERATION_TYPE_2, METADATAS_3); |
| 70 | |
| 71 | /** |
| 72 | * Checks that the P4PacketOperationModel class is immutable. |
| 73 | */ |
| 74 | @Test |
| 75 | public void testImmutability() { |
| 76 | assertThatClassIsImmutable(P4PacketOperationModel.class); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Checks the operation of equals(), hashCode() and toString() methods. |
| 81 | */ |
| 82 | @Test |
| 83 | public void testEquals() { |
| 84 | new EqualsTester() |
| 85 | .addEqualityGroup(P4_PACKET_OPERATION_MODEL_1, SAME_AS_P4_PACKET_OPERATION_MODEL_1) |
| 86 | .addEqualityGroup(P4_PACKET_OPERATION_MODEL_2) |
| 87 | .addEqualityGroup(P4_PACKET_OPERATION_MODEL_3) |
| 88 | .testEquals(); |
| 89 | } |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 90 | } |