Committed by
Gerrit Code Review
Merge "Unit tests for the DefaultFlowRule class"
Showing
3 changed files
with
201 additions
and
8 deletions
| ... | @@ -58,7 +58,7 @@ public class DefaultFlowRule implements FlowRule { | ... | @@ -58,7 +58,7 @@ public class DefaultFlowRule implements FlowRule { |
| 58 | } | 58 | } |
| 59 | 59 | ||
| 60 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, | 60 | public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, |
| 61 | - TrafficTreatment treatement, int priority, ApplicationId appId, | 61 | + TrafficTreatment treatment, int priority, ApplicationId appId, |
| 62 | int timeout, boolean permanent) { | 62 | int timeout, boolean permanent) { |
| 63 | 63 | ||
| 64 | if (priority < FlowRule.MIN_PRIORITY) { | 64 | if (priority < FlowRule.MIN_PRIORITY) { |
| ... | @@ -68,7 +68,7 @@ public class DefaultFlowRule implements FlowRule { | ... | @@ -68,7 +68,7 @@ public class DefaultFlowRule implements FlowRule { |
| 68 | this.deviceId = deviceId; | 68 | this.deviceId = deviceId; |
| 69 | this.priority = priority; | 69 | this.priority = priority; |
| 70 | this.selector = selector; | 70 | this.selector = selector; |
| 71 | - this.treatment = treatement; | 71 | + this.treatment = treatment; |
| 72 | this.appId = appId.id(); | 72 | this.appId = appId.id(); |
| 73 | this.timeout = timeout; | 73 | this.timeout = timeout; |
| 74 | this.permanent = permanent; | 74 | this.permanent = permanent; | ... | ... |
| 1 | +/* | ||
| 2 | + * Copyright 2014 Open Networking Laboratory | ||
| 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.onlab.onos.net.flow; | ||
| 18 | + | ||
| 19 | +import org.junit.Test; | ||
| 20 | +import org.onlab.onos.net.DeviceId; | ||
| 21 | +import org.onlab.onos.net.intent.IntentTestsMocks; | ||
| 22 | + | ||
| 23 | +import com.google.common.testing.EqualsTester; | ||
| 24 | + | ||
| 25 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 26 | +import static org.hamcrest.Matchers.is; | ||
| 27 | +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; | ||
| 28 | +import static org.onlab.onos.net.NetTestTools.did; | ||
| 29 | +import static org.onlab.onos.net.NetTestTools.APP_ID; | ||
| 30 | + | ||
| 31 | +/** | ||
| 32 | + * Unit tests for the default flow rule class. | ||
| 33 | + */ | ||
| 34 | +public class DefaultFlowRuleTest { | ||
| 35 | + private static final IntentTestsMocks.MockSelector SELECTOR = | ||
| 36 | + new IntentTestsMocks.MockSelector(); | ||
| 37 | + private static final IntentTestsMocks.MockTreatment TREATMENT = | ||
| 38 | + new IntentTestsMocks.MockTreatment(); | ||
| 39 | + | ||
| 40 | + final FlowRule flowRule1 = new MockFlowRule(1); | ||
| 41 | + final FlowRule sameAsFlowRule1 = new MockFlowRule(1); | ||
| 42 | + final FlowRule flowRule2 = new MockFlowRule(2); | ||
| 43 | + final DefaultFlowRule defaultFlowRule1 = new DefaultFlowRule(flowRule1); | ||
| 44 | + final DefaultFlowRule sameAsDefaultFlowRule1 = new DefaultFlowRule(sameAsFlowRule1); | ||
| 45 | + final DefaultFlowRule defaultFlowRule2 = new DefaultFlowRule(flowRule2); | ||
| 46 | + | ||
| 47 | + private static class MockFlowRule implements FlowRule { | ||
| 48 | + | ||
| 49 | + int priority; | ||
| 50 | + MockFlowRule(int priority) { | ||
| 51 | + this.priority = priority; | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + @Override | ||
| 55 | + public FlowId id() { | ||
| 56 | + return FlowId.valueOf(1); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + @Override | ||
| 60 | + public short appId() { | ||
| 61 | + return 0; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + @Override | ||
| 65 | + public int priority() { | ||
| 66 | + return priority; | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + @Override | ||
| 70 | + public DeviceId deviceId() { | ||
| 71 | + return did("1"); | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + @Override | ||
| 75 | + public TrafficSelector selector() { | ||
| 76 | + return SELECTOR; | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + @Override | ||
| 80 | + public TrafficTreatment treatment() { | ||
| 81 | + return TREATMENT; | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + @Override | ||
| 85 | + public int timeout() { | ||
| 86 | + return 0; | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + @Override | ||
| 90 | + public boolean isPermanent() { | ||
| 91 | + return false; | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + /** | ||
| 98 | + * Checks that the DefaultFlowRule class is immutable but can be inherited | ||
| 99 | + * from. | ||
| 100 | + */ | ||
| 101 | + @Test | ||
| 102 | + public void testImmutability() { | ||
| 103 | + assertThatClassIsImmutableBaseClass(DefaultFlowRule.class); | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + /** | ||
| 107 | + * Tests the equals, hashCode and toString methods using Guava EqualsTester. | ||
| 108 | + */ | ||
| 109 | + @Test | ||
| 110 | + public void testEquals() { | ||
| 111 | + new EqualsTester() | ||
| 112 | + .addEqualityGroup(defaultFlowRule1, sameAsDefaultFlowRule1) | ||
| 113 | + .addEqualityGroup(defaultFlowRule2) | ||
| 114 | + .testEquals(); | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + /** | ||
| 118 | + * Tests creation of a DefaultFlowRule using a FlowRule constructor. | ||
| 119 | + */ | ||
| 120 | + @Test | ||
| 121 | + public void testCreationFromFlowRule() { | ||
| 122 | + assertThat(defaultFlowRule1.deviceId(), is(flowRule1.deviceId())); | ||
| 123 | + assertThat(defaultFlowRule1.appId(), is(flowRule1.appId())); | ||
| 124 | + assertThat(defaultFlowRule1.id(), is(flowRule1.id())); | ||
| 125 | + assertThat(defaultFlowRule1.isPermanent(), is(flowRule1.isPermanent())); | ||
| 126 | + assertThat(defaultFlowRule1.priority(), is(flowRule1.priority())); | ||
| 127 | + assertThat(defaultFlowRule1.selector(), is(flowRule1.selector())); | ||
| 128 | + assertThat(defaultFlowRule1.treatment(), is(flowRule1.treatment())); | ||
| 129 | + assertThat(defaultFlowRule1.timeout(), is(flowRule1.timeout())); | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + /** | ||
| 133 | + * Tests creation of a DefaultFlowRule using a FlowId constructor. | ||
| 134 | + */ | ||
| 135 | + @Test | ||
| 136 | + public void testCreationWithFlowId() { | ||
| 137 | + final DefaultFlowRule rule = | ||
| 138 | + new DefaultFlowRule(did("1"), SELECTOR, | ||
| 139 | + TREATMENT, 22, 33, | ||
| 140 | + 44, false); | ||
| 141 | + assertThat(rule.deviceId(), is(did("1"))); | ||
| 142 | + assertThat(rule.id().value(), is(33L)); | ||
| 143 | + assertThat(rule.isPermanent(), is(false)); | ||
| 144 | + assertThat(rule.priority(), is(22)); | ||
| 145 | + assertThat(rule.selector(), is(SELECTOR)); | ||
| 146 | + assertThat(rule.treatment(), is(TREATMENT)); | ||
| 147 | + assertThat(rule.timeout(), is(44)); | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + /** | ||
| 151 | + * Tests the creation of a DefaultFlowRule using an AppId constructor. | ||
| 152 | + */ | ||
| 153 | + @Test | ||
| 154 | + public void testCreationWithAppId() { | ||
| 155 | + final DefaultFlowRule rule = | ||
| 156 | + new DefaultFlowRule(did("1"), SELECTOR, | ||
| 157 | + TREATMENT, 22, APP_ID, | ||
| 158 | + 44, false); | ||
| 159 | + assertThat(rule.deviceId(), is(did("1"))); | ||
| 160 | + assertThat(rule.isPermanent(), is(false)); | ||
| 161 | + assertThat(rule.priority(), is(22)); | ||
| 162 | + assertThat(rule.selector(), is(SELECTOR)); | ||
| 163 | + assertThat(rule.treatment(), is(TREATMENT)); | ||
| 164 | + assertThat(rule.timeout(), is(44)); | ||
| 165 | + } | ||
| 166 | +} |
| ... | @@ -43,9 +43,9 @@ public class ImmutableClassChecker { | ... | @@ -43,9 +43,9 @@ public class ImmutableClassChecker { |
| 43 | * @param clazz the class to check | 43 | * @param clazz the class to check |
| 44 | * @return true if the given class is a properly specified immutable class. | 44 | * @return true if the given class is a properly specified immutable class. |
| 45 | */ | 45 | */ |
| 46 | - private boolean isImmutableClass(Class<?> clazz) { | 46 | + private boolean isImmutableClass(Class<?> clazz, boolean allowNonFinalClass) { |
| 47 | // class must be declared final | 47 | // class must be declared final |
| 48 | - if (!Modifier.isFinal(clazz.getModifiers())) { | 48 | + if (!allowNonFinalClass && !Modifier.isFinal(clazz.getModifiers())) { |
| 49 | failureReason = "a class that is not final"; | 49 | failureReason = "a class that is not final"; |
| 50 | return false; | 50 | return false; |
| 51 | } | 51 | } |
| ... | @@ -113,16 +113,43 @@ public class ImmutableClassChecker { | ... | @@ -113,16 +113,43 @@ public class ImmutableClassChecker { |
| 113 | } | 113 | } |
| 114 | 114 | ||
| 115 | /** | 115 | /** |
| 116 | - * Assert that the given class adheres to the utility class rules. | 116 | + * Assert that the given class adheres to the immutable class rules. |
| 117 | * | 117 | * |
| 118 | * @param clazz the class to check | 118 | * @param clazz the class to check |
| 119 | * | 119 | * |
| 120 | - * @throws java.lang.AssertionError if the class is not a valid | 120 | + * @throws java.lang.AssertionError if the class is not an |
| 121 | - * utility class | 121 | + * immutable class |
| 122 | */ | 122 | */ |
| 123 | public static void assertThatClassIsImmutable(Class<?> clazz) { | 123 | public static void assertThatClassIsImmutable(Class<?> clazz) { |
| 124 | final ImmutableClassChecker checker = new ImmutableClassChecker(); | 124 | final ImmutableClassChecker checker = new ImmutableClassChecker(); |
| 125 | - if (!checker.isImmutableClass(clazz)) { | 125 | + if (!checker.isImmutableClass(clazz, false)) { |
| 126 | + final Description toDescription = new StringDescription(); | ||
| 127 | + final Description mismatchDescription = new StringDescription(); | ||
| 128 | + | ||
| 129 | + checker.describeTo(toDescription); | ||
| 130 | + checker.describeMismatch(mismatchDescription); | ||
| 131 | + final String reason = | ||
| 132 | + "\n" + | ||
| 133 | + "Expected: is \"" + toDescription.toString() + "\"\n" + | ||
| 134 | + " but : was \"" + mismatchDescription.toString() + "\""; | ||
| 135 | + | ||
| 136 | + throw new AssertionError(reason); | ||
| 137 | + } | ||
| 138 | + } | ||
| 139 | + | ||
| 140 | + /** | ||
| 141 | + * Assert that the given class adheres to the immutable class rules, but | ||
| 142 | + * is not declared final. Classes that need to be inherited from cannot be | ||
| 143 | + * declared final. | ||
| 144 | + * | ||
| 145 | + * @param clazz the class to check | ||
| 146 | + * | ||
| 147 | + * @throws java.lang.AssertionError if the class is not an | ||
| 148 | + * immutable class | ||
| 149 | + */ | ||
| 150 | + public static void assertThatClassIsImmutableBaseClass(Class<?> clazz) { | ||
| 151 | + final ImmutableClassChecker checker = new ImmutableClassChecker(); | ||
| 152 | + if (!checker.isImmutableClass(clazz, true)) { | ||
| 126 | final Description toDescription = new StringDescription(); | 153 | final Description toDescription = new StringDescription(); |
| 127 | final Description mismatchDescription = new StringDescription(); | 154 | final Description mismatchDescription = new StringDescription(); |
| 128 | 155 | ... | ... |
-
Please register or login to post a comment