blob: e924914a731d4708c39c55ea85aabab3f1caf5a0 [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 Cascone4c289b72019-01-22 15:30:45 -080024import org.onosproject.net.pi.model.PiPacketMetadataId;
Frank Wang4e848042017-08-03 19:48:11 +080025
26import static org.hamcrest.MatcherAssert.assertThat;
27import static org.hamcrest.Matchers.is;
28import static org.hamcrest.Matchers.notNullValue;
29import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
30import static org.onlab.util.ImmutableByteSequence.copyFrom;
Carmelo Cascone87892e22017-11-13 16:01:29 -080031import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_OUT;
Frank Wang4e848042017-08-03 19:48:11 +080032import static org.onosproject.net.pi.runtime.PiConstantsTest.EGRESS_PORT;
Frank Wang4e848042017-08-03 19:48:11 +080033
34/**
35 * Unit tests for PiPacketOperation class.
36 */
37public class PiPacketOperationTest {
38
Carmelo Cascone87892e22017-11-13 16:01:29 -080039 private final PiPacketOperation piPacketOperation1 = PiPacketOperation.builder()
Frank Wang4e848042017-08-03 19:48:11 +080040 .withData(ImmutableByteSequence.ofOnes(512))
41 .withType(PACKET_OUT)
Carmelo Cascone4c289b72019-01-22 15:30:45 -080042 .withMetadata(PiPacketMetadata.builder()
43 .withId(PiPacketMetadataId.of(EGRESS_PORT))
Frank Wang4e848042017-08-03 19:48:11 +080044 .withValue(copyFrom((short) 255))
45 .build())
46 .build();
47
Carmelo Cascone87892e22017-11-13 16:01:29 -080048 private final PiPacketOperation sameAsPiPacketOperation1 = PiPacketOperation.builder()
Frank Wang4e848042017-08-03 19:48:11 +080049 .withData(ImmutableByteSequence.ofOnes(512))
50 .withType(PACKET_OUT)
Carmelo Cascone4c289b72019-01-22 15:30:45 -080051 .withMetadata(PiPacketMetadata.builder()
52 .withId(PiPacketMetadataId.of(EGRESS_PORT))
Frank Wang4e848042017-08-03 19:48:11 +080053 .withValue(copyFrom((short) 255))
54 .build())
55 .build();
56
Carmelo Cascone87892e22017-11-13 16:01:29 -080057 private final PiPacketOperation piPacketOperation2 = PiPacketOperation.builder()
Frank Wang4e848042017-08-03 19:48:11 +080058 .withData(ImmutableByteSequence.ofOnes(512))
59 .withType(PACKET_OUT)
Carmelo Cascone4c289b72019-01-22 15:30:45 -080060 .withMetadata(PiPacketMetadata.builder()
61 .withId(PiPacketMetadataId.of(EGRESS_PORT))
Frank Wang4e848042017-08-03 19:48:11 +080062 .withValue(copyFrom((short) 200))
63 .build())
64 .build();
65
66 /**
67 * Checks that the PiPacketOperation class is immutable.
68 */
69 @Test
70 public void testImmutability() {
71
72 assertThatClassIsImmutable(PiPacketOperation.class);
73 }
74
75 /**
76 * Checks the operation of equals(), hashCode() and toString() methods.
77 */
78 @Test
79 public void testEquals() {
80
81 new EqualsTester()
82 .addEqualityGroup(piPacketOperation1, sameAsPiPacketOperation1)
83 .addEqualityGroup(piPacketOperation2)
84 .testEquals();
85 }
86
87 /**
88 * Checks the methods of PiPacketOperation.
89 */
90 @Test
91 public void testMethods() {
92
93 final PiPacketOperation piPacketOperation = PiPacketOperation.builder()
94 .withData(ImmutableByteSequence.ofOnes(512))
95 .withType(PACKET_OUT)
Carmelo Cascone4c289b72019-01-22 15:30:45 -080096 .withMetadata(PiPacketMetadata.builder()
97 .withId(PiPacketMetadataId.of(EGRESS_PORT))
Frank Wang4e848042017-08-03 19:48:11 +080098 .withValue(copyFrom((short) 10))
99 .build())
100 .build();
101
102 assertThat(piPacketOperation, is(notNullValue()));
103 assertThat(piPacketOperation.type(), is(PACKET_OUT));
104 assertThat(piPacketOperation.data(), is(ImmutableByteSequence.ofOnes(512)));
105 assertThat("Incorrect metadatas value",
106 CollectionUtils.isEqualCollection(piPacketOperation.metadatas(),
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800107 ImmutableList.of(PiPacketMetadata.builder()
108 .withId(PiPacketMetadataId
Frank Wang4e848042017-08-03 19:48:11 +0800109 .of(EGRESS_PORT))
110 .withValue(copyFrom((short) 10))
111 .build())));
112 }
113}