Ekber Aziz | 123ad5d | 2017-11-27 12:36:35 -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 | package org.onosproject.p4runtime.model; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableMap; |
| 19 | import com.google.common.testing.EqualsTester; |
| 20 | import org.junit.Test; |
| 21 | import org.onosproject.net.pi.model.PiActionId; |
| 22 | import org.onosproject.net.pi.model.PiActionParamId; |
| 23 | import org.onosproject.net.pi.model.PiActionParamModel; |
| 24 | |
| 25 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 26 | |
| 27 | /** |
| 28 | * Test for P4ActionModel class. |
| 29 | */ |
| 30 | public class P4ActionModelTest { |
| 31 | |
| 32 | final PiActionId actionId = PiActionId.of("dec_ttl"); |
| 33 | final PiActionId sameAsActionId = PiActionId.of("dec_ttl"); |
| 34 | final PiActionId actionId2 = PiActionId.of("mod_vlan_vid"); |
| 35 | |
| 36 | private final PiActionParamId piActionParamId = PiActionParamId.of("port"); |
| 37 | private final PiActionParamId sameAsPiActionParamId = PiActionParamId.of("port"); |
| 38 | private final PiActionParamId piActionParamId2 = PiActionParamId.of("dstAddr"); |
| 39 | |
| 40 | private static final int BIT_WIDTH = 32; |
| 41 | |
| 42 | private final P4ActionParamModel actionParamModel = new P4ActionParamModel(piActionParamId, BIT_WIDTH); |
| 43 | |
| 44 | private final P4ActionParamModel actionParamModel2 = new P4ActionParamModel(piActionParamId2, BIT_WIDTH); |
| 45 | |
| 46 | |
| 47 | private final ImmutableMap<PiActionParamId, PiActionParamModel> params = new |
| 48 | ImmutableMap.Builder<PiActionParamId, PiActionParamModel>() |
| 49 | .put(piActionParamId, actionParamModel) |
| 50 | .build(); |
| 51 | private final ImmutableMap<PiActionParamId, PiActionParamModel> sameAsParams = new |
| 52 | ImmutableMap.Builder<PiActionParamId, PiActionParamModel>() |
| 53 | .put(sameAsPiActionParamId, actionParamModel) |
| 54 | .build(); |
| 55 | private final ImmutableMap<PiActionParamId, PiActionParamModel> params2 = new |
| 56 | ImmutableMap.Builder<PiActionParamId, PiActionParamModel>() |
| 57 | .put(piActionParamId2, actionParamModel) |
| 58 | .build(); |
| 59 | private final ImmutableMap<PiActionParamId, PiActionParamModel> params3 = new |
| 60 | ImmutableMap.Builder<PiActionParamId, PiActionParamModel>() |
| 61 | .put(piActionParamId, actionParamModel2) |
| 62 | .build(); |
| 63 | |
| 64 | private final P4ActionModel actionModel = new P4ActionModel(actionId, params); |
| 65 | |
| 66 | private final P4ActionModel sameAsActionModel = new P4ActionModel(sameAsActionId, params); |
| 67 | |
| 68 | private final P4ActionModel sameAsActionModel2 = new P4ActionModel(actionId, sameAsParams); |
| 69 | |
| 70 | private final P4ActionModel actionModel2 = new P4ActionModel(actionId2, params); |
| 71 | |
| 72 | private final P4ActionModel actionModel3 = new P4ActionModel(actionId, params2); |
| 73 | |
| 74 | private final P4ActionModel actionModel4 = new P4ActionModel(actionId, params3); |
| 75 | |
| 76 | /** |
| 77 | * Checks that the P4ActionModel class is immutable. |
| 78 | */ |
| 79 | @Test |
| 80 | public void testImmutability() { |
| 81 | assertThatClassIsImmutable(P4ActionModel.class); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Checks the operation of equals(), hashCode() and toString() methods. |
| 86 | */ |
| 87 | @Test |
| 88 | public void testEquals() { |
| 89 | new EqualsTester() |
| 90 | .addEqualityGroup(actionModel, sameAsActionModel, sameAsActionModel2) |
| 91 | .addEqualityGroup(actionModel2) |
| 92 | .addEqualityGroup(actionModel3) |
| 93 | .addEqualityGroup(actionModel4) |
| 94 | .testEquals(); |
| 95 | } |
| 96 | } |