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 | /** |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 38 | * Unit tests for PiActionProfileGroup class. |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 39 | */ |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 40 | public class PiActionProfileGroupTest { |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 41 | |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 42 | private final PiActionProfileMemberId piActionProfileMemberId = PiActionProfileMemberId.of(10); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 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 | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 47 | private final PiActionProfileMember piActionProfileMember = PiActionProfileMember.builder() |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 48 | .forActionProfile(ACTION_PROF_ID) |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 49 | .withId(piActionProfileMemberId) |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 50 | .withAction(piAction) |
| 51 | .withWeight(10) |
| 52 | .build(); |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 53 | private PiActionProfileGroupId piActionGroupId = PiActionProfileGroupId.of(10); |
| 54 | private PiActionProfileGroup piActionGroup1 = PiActionProfileGroup.builder() |
| 55 | .addMember(piActionProfileMember) |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 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 | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 60 | private PiActionProfileGroup sameAsPiActionProfileGroup1 = PiActionProfileGroup.builder() |
| 61 | .addMember(piActionProfileMember) |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 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 | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 66 | private PiActionProfileGroupId piActionGroupId2 = PiActionProfileGroupId.of(20); |
| 67 | private PiActionProfileGroup piActionGroup2 = PiActionProfileGroup.builder() |
| 68 | .addMember(piActionProfileMember) |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 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 | /** |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 74 | * Checks that the PiActionProfileGroup class is immutable. |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 75 | */ |
| 76 | @Test |
| 77 | public void testImmutability() { |
| 78 | |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 79 | assertThatClassIsImmutable(PiActionProfileGroup.class); |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Checks the operation of equals(), hashCode() and toString() methods. |
| 84 | */ |
| 85 | @Test |
| 86 | public void testEquals() { |
| 87 | |
| 88 | new EqualsTester() |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 89 | .addEqualityGroup(piActionGroup1, sameAsPiActionProfileGroup1) |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 90 | .addEqualityGroup(piActionGroup2) |
| 91 | .testEquals(); |
| 92 | } |
| 93 | |
| 94 | /** |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 95 | * Checks the methods of PiActionProfileGroup. |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 96 | */ |
| 97 | @Test |
| 98 | public void testMethods() { |
| 99 | |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 100 | Collection<PiActionProfileMember> piActionProfileMembers = Lists.newArrayList(); |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 101 | |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 102 | piActionProfileMembers.add(piActionProfileMember); |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 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", |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 106 | CollectionUtils.isEqualCollection(piActionGroup1.members(), piActionProfileMembers)); |
Frank Wang | 4e84804 | 2017-08-03 19:48:11 +0800 | [diff] [blame] | 107 | } |
| 108 | } |