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