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() |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 48 | .withId(piActionGroupMemberId) |
| 49 | .withAction(piAction) |
| 50 | .withWeight(10) |
| 51 | .build(); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 52 | private PiActionGroupId piActionGroupId = PiActionGroupId.of(10); |
| 53 | private PiActionGroup piActionGroup1 = PiActionGroup.builder() |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 54 | .addMember(piActionGroupMember) |
| 55 | .withId(piActionGroupId) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 56 | .withActionProfileId(ACTION_PROF_ID) |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 57 | .build(); |
| 58 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 59 | private PiActionGroup sameAsPiActionGroup1 = PiActionGroup.builder() |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 60 | .addMember(piActionGroupMember) |
| 61 | .withId(piActionGroupId) |
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 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 65 | private PiActionGroupId piActionGroupId2 = PiActionGroupId.of(20); |
| 66 | private PiActionGroup piActionGroup2 = PiActionGroup.builder() |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 67 | .addMember(piActionGroupMember) |
| 68 | .withId(piActionGroupId2) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 69 | .withActionProfileId(ACTION_PROF_ID) |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 70 | .build(); |
| 71 | |
| 72 | /** |
| 73 | * Checks that the PiActionGroup class is immutable. |
| 74 | */ |
| 75 | @Test |
| 76 | public void testImmutability() { |
| 77 | |
| 78 | assertThatClassIsImmutable(PiActionGroup.class); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Checks the operation of equals(), hashCode() and toString() methods. |
| 83 | */ |
| 84 | @Test |
| 85 | public void testEquals() { |
| 86 | |
| 87 | new EqualsTester() |
| 88 | .addEqualityGroup(piActionGroup1, sameAsPiActionGroup1) |
| 89 | .addEqualityGroup(piActionGroup2) |
| 90 | .testEquals(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Checks the methods of PiActionGroup. |
| 95 | */ |
| 96 | @Test |
| 97 | public void testMethods() { |
| 98 | |
| 99 | Collection<PiActionGroupMember> piActionGroupMembers = Lists.newArrayList(); |
| 100 | |
| 101 | piActionGroupMembers.add(piActionGroupMember); |
| 102 | assertThat(piActionGroup1, is(notNullValue())); |
| 103 | assertThat(piActionGroup1.id(), is(piActionGroupId)); |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 104 | assertThat("Incorrect members value", |
| 105 | CollectionUtils.isEqualCollection(piActionGroup1.members(), piActionGroupMembers)); |
| 106 | } |
| 107 | } |