blob: 5d760e8e827197d7f612cb65db2b5316adc576cd [file] [log] [blame]
Mehmed Mustafa3e269df2017-11-24 17:19:49 +03001/*
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
17package org.onosproject.p4runtime.model;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.testing.EqualsTester;
21import org.junit.Test;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080022import org.onosproject.net.pi.model.PiPacketMetadataId;
23import org.onosproject.net.pi.model.PiPacketMetadataModel;
Mehmed Mustafa3e269df2017-11-24 17:19:49 +030024import org.onosproject.net.pi.model.PiPacketOperationType;
25import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
26
27/**
28 * Unit tests for P4PacketOperationModel class.
29 */
30public 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 Cascone4c289b72019-01-22 15:30:45 -080035 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 Mustafa3e269df2017-11-24 17:19:49 +030037
38 private static final int BIT_WIDTH_1 = 8;
39 private static final int BIT_WIDTH_2 = 9;
40
Carmelo Cascone4c289b72019-01-22 15:30:45 -080041 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 Mustafa3e269df2017-11-24 17:19:49 +030045
Carmelo Cascone4c289b72019-01-22 15:30:45 -080046 private static final ImmutableList<PiPacketMetadataModel> METADATAS_1 =
47 new ImmutableList.Builder<PiPacketMetadataModel>()
Mehmed Mustafa3e269df2017-11-24 17:19:49 +030048 .add(PI_CONTROL_METADATA_MODEL_1)
49 .build();
50
Carmelo Cascone4c289b72019-01-22 15:30:45 -080051 private static final ImmutableList<PiPacketMetadataModel> METADATAS_2 =
52 new ImmutableList.Builder<PiPacketMetadataModel>()
Mehmed Mustafa3e269df2017-11-24 17:19:49 +030053 .add(PI_CONTROL_METADATA_MODEL_2)
54 .build();
55
Carmelo Cascone4c289b72019-01-22 15:30:45 -080056 private static final ImmutableList<PiPacketMetadataModel> METADATAS_3 =
57 new ImmutableList.Builder<PiPacketMetadataModel>()
Mehmed Mustafa3e269df2017-11-24 17:19:49 +030058 .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 Cascone4c289b72019-01-22 15:30:45 -080090}