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