Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.net.pi.runtime; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.testing.EqualsTester; |
| 21 | import org.apache.commons.collections.CollectionUtils; |
| 22 | import org.junit.Test; |
| 23 | |
| 24 | import java.util.Collection; |
| 25 | |
| 26 | import static org.hamcrest.MatcherAssert.assertThat; |
| 27 | import static org.hamcrest.Matchers.is; |
| 28 | import static org.hamcrest.Matchers.notNullValue; |
| 29 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 30 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame^] | 31 | import static org.onosproject.net.pi.runtime.PiConstantsTest.ACTION_PROF_ID; |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 32 | import static org.onosproject.net.pi.runtime.PiConstantsTest.DST_ADDR; |
| 33 | import static org.onosproject.net.pi.runtime.PiConstantsTest.MOD_NW_DST; |
| 34 | |
| 35 | /** |
| 36 | * Unit tests for PiActionGroup class. |
| 37 | */ |
| 38 | public class PiActionGroupTest { |
| 39 | |
| 40 | final PiActionGroupMemberId piActionGroupMemberId = PiActionGroupMemberId.of(10); |
| 41 | final PiAction piAction = PiAction.builder().withId(PiActionId.of(MOD_NW_DST)) |
| 42 | .withParameter(new PiActionParam(PiActionParamId.of(DST_ADDR), copyFrom(0x0a010101))) |
| 43 | .build(); |
| 44 | |
| 45 | final PiActionGroupMember piActionGroupMember = PiActionGroupMember.builder() |
| 46 | .withId(piActionGroupMemberId) |
| 47 | .withAction(piAction) |
| 48 | .withWeight(10) |
| 49 | .build(); |
| 50 | PiActionGroupId piActionGroupId = PiActionGroupId.of(10); |
| 51 | PiActionGroup piActionGroup1 = PiActionGroup.builder() |
| 52 | .addMember(piActionGroupMember) |
| 53 | .withId(piActionGroupId) |
| 54 | .withType(PiActionGroup.Type.SELECT) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame^] | 55 | .withActionProfileId(ACTION_PROF_ID) |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 56 | .build(); |
| 57 | |
| 58 | PiActionGroup sameAsPiActionGroup1 = PiActionGroup.builder() |
| 59 | .addMember(piActionGroupMember) |
| 60 | .withId(piActionGroupId) |
| 61 | .withType(PiActionGroup.Type.SELECT) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame^] | 62 | .withActionProfileId(ACTION_PROF_ID) |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 63 | .build(); |
| 64 | |
| 65 | PiActionGroupId piActionGroupId2 = PiActionGroupId.of(20); |
| 66 | PiActionGroup piActionGroup2 = PiActionGroup.builder() |
| 67 | .addMember(piActionGroupMember) |
| 68 | .withId(piActionGroupId2) |
| 69 | .withType(PiActionGroup.Type.SELECT) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame^] | 70 | .withActionProfileId(ACTION_PROF_ID) |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 71 | .build(); |
| 72 | |
| 73 | /** |
| 74 | * Checks that the PiActionGroup class is immutable. |
| 75 | */ |
| 76 | @Test |
| 77 | public void testImmutability() { |
| 78 | |
| 79 | assertThatClassIsImmutable(PiActionGroup.class); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Checks the operation of equals(), hashCode() and toString() methods. |
| 84 | */ |
| 85 | @Test |
| 86 | public void testEquals() { |
| 87 | |
| 88 | new EqualsTester() |
| 89 | .addEqualityGroup(piActionGroup1, sameAsPiActionGroup1) |
| 90 | .addEqualityGroup(piActionGroup2) |
| 91 | .testEquals(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Checks the methods of PiActionGroup. |
| 96 | */ |
| 97 | @Test |
| 98 | public void testMethods() { |
| 99 | |
| 100 | Collection<PiActionGroupMember> piActionGroupMembers = Lists.newArrayList(); |
| 101 | |
| 102 | piActionGroupMembers.add(piActionGroupMember); |
| 103 | assertThat(piActionGroup1, is(notNullValue())); |
| 104 | assertThat(piActionGroup1.id(), is(piActionGroupId)); |
| 105 | assertThat(piActionGroup1.type(), is(PiActionGroup.Type.SELECT)); |
| 106 | assertThat("Incorrect members value", |
| 107 | CollectionUtils.isEqualCollection(piActionGroup1.members(), piActionGroupMembers)); |
| 108 | } |
| 109 | } |