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; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 23 | import org.onosproject.net.pi.model.PiActionId; |
| 24 | import org.onosproject.net.pi.model.PiActionParamId; |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 25 | |
| 26 | import java.util.Collection; |
| 27 | |
| 28 | import static org.hamcrest.MatcherAssert.assertThat; |
| 29 | import static org.hamcrest.Matchers.is; |
| 30 | import static org.hamcrest.Matchers.notNullValue; |
| 31 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 32 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 33 | import static org.onosproject.net.pi.runtime.PiConstantsTest.ACTION_PROF_ID; |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 34 | import static org.onosproject.net.pi.runtime.PiConstantsTest.DST_ADDR; |
| 35 | import static org.onosproject.net.pi.runtime.PiConstantsTest.MOD_NW_DST; |
| 36 | |
| 37 | /** |
| 38 | * Unit tests for PiActionGroup class. |
| 39 | */ |
| 40 | public class PiActionGroupTest { |
| 41 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 42 | private final PiActionGroupMemberId piActionGroupMemberId = PiActionGroupMemberId.of(10); |
| 43 | private final PiAction piAction = PiAction.builder().withId(PiActionId.of(MOD_NW_DST)) |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 44 | .withParameter(new PiActionParam(PiActionParamId.of(DST_ADDR), copyFrom(0x0a010101))) |
| 45 | .build(); |
| 46 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 47 | private final PiActionGroupMember piActionGroupMember = PiActionGroupMember.builder() |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 48 | .forActionProfile(ACTION_PROF_ID) |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 49 | .withId(piActionGroupMemberId) |
| 50 | .withAction(piAction) |
| 51 | .withWeight(10) |
| 52 | .build(); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 53 | private PiActionGroupId piActionGroupId = PiActionGroupId.of(10); |
| 54 | private PiActionGroup piActionGroup1 = PiActionGroup.builder() |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 55 | .addMember(piActionGroupMember) |
| 56 | .withId(piActionGroupId) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 57 | .withActionProfileId(ACTION_PROF_ID) |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 58 | .build(); |
| 59 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 60 | private PiActionGroup sameAsPiActionGroup1 = PiActionGroup.builder() |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 61 | .addMember(piActionGroupMember) |
| 62 | .withId(piActionGroupId) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 63 | .withActionProfileId(ACTION_PROF_ID) |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 64 | .build(); |
| 65 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 66 | private PiActionGroupId piActionGroupId2 = PiActionGroupId.of(20); |
| 67 | private PiActionGroup piActionGroup2 = PiActionGroup.builder() |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 68 | .addMember(piActionGroupMember) |
| 69 | .withId(piActionGroupId2) |
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)); |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 105 | assertThat("Incorrect members value", |
| 106 | CollectionUtils.isEqualCollection(piActionGroup1.members(), piActionGroupMembers)); |
| 107 | } |
| 108 | } |