blob: d675b51205509f6713576e02655a92d042d8656e [file] [log] [blame]
Frank Wang4e848042017-08-03 19:48:11 +08001/*
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.net.pi.runtime;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.testing.EqualsTester;
21import org.apache.commons.collections.CollectionUtils;
22import org.junit.Test;
23import org.onlab.util.ImmutableByteSequence;
Carmelo Cascone87892e22017-11-13 16:01:29 -080024import org.onosproject.net.DeviceId;
25import org.onosproject.net.pi.model.PiControlMetadataId;
Frank Wang4e848042017-08-03 19:48:11 +080026
27import static org.hamcrest.MatcherAssert.assertThat;
28import static org.hamcrest.Matchers.is;
29import static org.hamcrest.Matchers.notNullValue;
30import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
31import static org.onlab.util.ImmutableByteSequence.copyFrom;
Carmelo Cascone87892e22017-11-13 16:01:29 -080032import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_OUT;
Frank Wang4e848042017-08-03 19:48:11 +080033import static org.onosproject.net.pi.runtime.PiConstantsTest.EGRESS_PORT;
Frank Wang4e848042017-08-03 19:48:11 +080034
35/**
36 * Unit tests for PiPacketOperation class.
37 */
38public class PiPacketOperationTest {
39
Carmelo Cascone87892e22017-11-13 16:01:29 -080040 private final DeviceId deviceId = DeviceId.deviceId("dummy");
41
42 private final PiPacketOperation piPacketOperation1 = PiPacketOperation.builder()
43 .forDevice(deviceId)
Frank Wang4e848042017-08-03 19:48:11 +080044 .withData(ImmutableByteSequence.ofOnes(512))
45 .withType(PACKET_OUT)
Carmelo Cascone87892e22017-11-13 16:01:29 -080046 .withMetadata(PiControlMetadata.builder()
47 .withId(PiControlMetadataId.of(EGRESS_PORT))
Frank Wang4e848042017-08-03 19:48:11 +080048 .withValue(copyFrom((short) 255))
49 .build())
50 .build();
51
Carmelo Cascone87892e22017-11-13 16:01:29 -080052 private final PiPacketOperation sameAsPiPacketOperation1 = PiPacketOperation.builder()
53 .forDevice(deviceId)
Frank Wang4e848042017-08-03 19:48:11 +080054 .withData(ImmutableByteSequence.ofOnes(512))
55 .withType(PACKET_OUT)
Carmelo Cascone87892e22017-11-13 16:01:29 -080056 .withMetadata(PiControlMetadata.builder()
57 .withId(PiControlMetadataId.of(EGRESS_PORT))
Frank Wang4e848042017-08-03 19:48:11 +080058 .withValue(copyFrom((short) 255))
59 .build())
60 .build();
61
Carmelo Cascone87892e22017-11-13 16:01:29 -080062 private final PiPacketOperation piPacketOperation2 = PiPacketOperation.builder()
63 .forDevice(deviceId)
Frank Wang4e848042017-08-03 19:48:11 +080064 .withData(ImmutableByteSequence.ofOnes(512))
65 .withType(PACKET_OUT)
Carmelo Cascone87892e22017-11-13 16:01:29 -080066 .withMetadata(PiControlMetadata.builder()
67 .withId(PiControlMetadataId.of(EGRESS_PORT))
Frank Wang4e848042017-08-03 19:48:11 +080068 .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 Cascone87892e22017-11-13 16:01:29 -0800100 .forDevice(deviceId)
Frank Wang4e848042017-08-03 19:48:11 +0800101 .withData(ImmutableByteSequence.ofOnes(512))
102 .withType(PACKET_OUT)
Carmelo Cascone87892e22017-11-13 16:01:29 -0800103 .withMetadata(PiControlMetadata.builder()
104 .withId(PiControlMetadataId.of(EGRESS_PORT))
Frank Wang4e848042017-08-03 19:48:11 +0800105 .withValue(copyFrom((short) 10))
106 .build())
107 .build();
108
109 assertThat(piPacketOperation, is(notNullValue()));
Carmelo Cascone87892e22017-11-13 16:01:29 -0800110 assertThat(piPacketOperation.deviceId(), is(deviceId));
Frank Wang4e848042017-08-03 19:48:11 +0800111 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 Cascone87892e22017-11-13 16:01:29 -0800115 ImmutableList.of(PiControlMetadata.builder()
116 .withId(PiControlMetadataId
Frank Wang4e848042017-08-03 19:48:11 +0800117 .of(EGRESS_PORT))
118 .withValue(copyFrom((short) 10))
119 .build())));
120 }
121}