blob: 9a69ab3615f7f512512f8c8bbc969a2c6e2f00c6 [file] [log] [blame]
Frank Wange33e4ed2017-06-28 10:01:07 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Frank Wange33e4ed2017-06-28 10:01:07 +08003 *
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.Lists;
20import com.google.common.testing.EqualsTester;
21import org.junit.Test;
Carmelo Cascone87892e22017-11-13 16:01:29 -080022import org.onosproject.net.pi.model.PiActionId;
23import org.onosproject.net.pi.model.PiActionParamId;
Frank Wange33e4ed2017-06-28 10:01:07 +080024
25import java.util.Collection;
26
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;
32import static org.onosproject.net.pi.runtime.PiConstantsTest.DST_ADDR;
33import static org.onosproject.net.pi.runtime.PiConstantsTest.MOD_NW_DST;
34
35/**
36 * Unit tests for PiAction class.
37 */
38public class PiActionTest {
Carmelo Cascone87892e22017-11-13 16:01:29 -080039 private final PiAction piAction1 = PiAction.builder().withId(PiActionId.of(MOD_NW_DST))
Frank Wange33e4ed2017-06-28 10:01:07 +080040 .withParameter(new PiActionParam(PiActionParamId.of(DST_ADDR), copyFrom(0x0a010101)))
41 .build();
Carmelo Cascone87892e22017-11-13 16:01:29 -080042 private final PiAction sameAsPiAction1 = PiAction.builder().withId(PiActionId.of(MOD_NW_DST))
Frank Wange33e4ed2017-06-28 10:01:07 +080043 .withParameter(new PiActionParam(PiActionParamId.of(DST_ADDR), copyFrom(0x0a010101)))
44 .build();
Carmelo Cascone87892e22017-11-13 16:01:29 -080045 private final PiAction piAction2 = PiAction.builder().withId(PiActionId.of("set_egress_port_0")).build();
Frank Wange33e4ed2017-06-28 10:01:07 +080046
47 /**
48 * Checks that the PiAction class is immutable.
49 */
50 @Test
51 public void testImmutability() {
52 assertThatClassIsImmutable(PiAction.class);
53 }
54
55 /**
56 * Checks the operation of equals(), hashCode() and toString() methods.
57 */
58 @Test
59 public void testEquals() {
60 new EqualsTester()
61 .addEqualityGroup(piAction1, sameAsPiAction1)
62 .addEqualityGroup(piAction2)
63 .testEquals();
64 }
65
66 /**
67 * Checks the construction of a PiAction object with parameter.
68 */
69 @Test
70 public void testMethodWithParameter() {
71 PiActionId piActionId = PiActionId.of(MOD_NW_DST);
72 PiActionParam piActionParam = new PiActionParam(PiActionParamId.of(DST_ADDR), copyFrom(0x0a010101));
73 final PiAction piAction = PiAction.builder().withId(piActionId)
74 .withParameter(piActionParam)
75 .build();
76 assertThat(piAction, is(notNullValue()));
77 assertThat(piAction.id(), is(piActionId));
78 assertThat(piAction.type(), is(PiTableAction.Type.ACTION));
79 }
80
81 /**
82 * Checks the construction of a PiAction object with parameters.
83 */
84 @Test
85 public void testMethodWithParameters() {
86 PiActionId piActionId = PiActionId.of(MOD_NW_DST);
87 Collection<PiActionParam> runtimeParams = Lists.newArrayList();
88 PiActionParam piActionParam = new PiActionParam(PiActionParamId.of(DST_ADDR), copyFrom(0x0a010101));
89
90 runtimeParams.add(piActionParam);
91 final PiAction piAction = PiAction.builder().withId(piActionId)
92 .withParameters(runtimeParams)
93 .build();
94 assertThat(piAction, is(notNullValue()));
95 assertThat(piAction.id(), is(piActionId));
96 assertThat(piAction.parameters(), is(runtimeParams));
97 assertThat(piAction.type(), is(PiTableAction.Type.ACTION));
98 }
99}