Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 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 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 17 | package org.onosproject.net.flow; |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 18 | |
| 19 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.net.intent.IntentTestsMocks; |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 21 | |
| 22 | import com.google.common.testing.EqualsTester; |
| 23 | |
| 24 | import static org.hamcrest.MatcherAssert.assertThat; |
| 25 | import static org.hamcrest.Matchers.is; |
| 26 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 28 | import static org.onosproject.net.NetTestTools.did; |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Unit tests for the default flow rule class. |
| 32 | */ |
| 33 | public class DefaultFlowRuleTest { |
| 34 | private static final IntentTestsMocks.MockSelector SELECTOR = |
| 35 | new IntentTestsMocks.MockSelector(); |
Ayaka Koshibe | b1224ab | 2015-01-31 00:48:58 +0000 | [diff] [blame] | 36 | private static final IntentTestsMocks.MockTreatment TREATMENT = |
| 37 | new IntentTestsMocks.MockTreatment(); |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 38 | |
Ray Milkey | 930fc66 | 2014-11-11 16:07:45 -0800 | [diff] [blame] | 39 | final FlowRule flowRule1 = new IntentTestsMocks.MockFlowRule(1); |
| 40 | final FlowRule sameAsFlowRule1 = new IntentTestsMocks.MockFlowRule(1); |
| 41 | final FlowRule flowRule2 = new IntentTestsMocks.MockFlowRule(2); |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 42 | final DefaultFlowRule defaultFlowRule1 = new DefaultFlowRule(flowRule1); |
| 43 | final DefaultFlowRule sameAsDefaultFlowRule1 = new DefaultFlowRule(sameAsFlowRule1); |
| 44 | final DefaultFlowRule defaultFlowRule2 = new DefaultFlowRule(flowRule2); |
| 45 | |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 46 | /** |
| 47 | * Checks that the DefaultFlowRule class is immutable but can be inherited |
| 48 | * from. |
| 49 | */ |
| 50 | @Test |
| 51 | public void testImmutability() { |
| 52 | assertThatClassIsImmutableBaseClass(DefaultFlowRule.class); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Tests the equals, hashCode and toString methods using Guava EqualsTester. |
| 57 | */ |
| 58 | @Test |
| 59 | public void testEquals() { |
| 60 | new EqualsTester() |
| 61 | .addEqualityGroup(defaultFlowRule1, sameAsDefaultFlowRule1) |
| 62 | .addEqualityGroup(defaultFlowRule2) |
| 63 | .testEquals(); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Tests creation of a DefaultFlowRule using a FlowRule constructor. |
| 68 | */ |
| 69 | @Test |
| 70 | public void testCreationFromFlowRule() { |
| 71 | assertThat(defaultFlowRule1.deviceId(), is(flowRule1.deviceId())); |
| 72 | assertThat(defaultFlowRule1.appId(), is(flowRule1.appId())); |
| 73 | assertThat(defaultFlowRule1.id(), is(flowRule1.id())); |
| 74 | assertThat(defaultFlowRule1.isPermanent(), is(flowRule1.isPermanent())); |
| 75 | assertThat(defaultFlowRule1.priority(), is(flowRule1.priority())); |
| 76 | assertThat(defaultFlowRule1.selector(), is(flowRule1.selector())); |
| 77 | assertThat(defaultFlowRule1.treatment(), is(flowRule1.treatment())); |
| 78 | assertThat(defaultFlowRule1.timeout(), is(flowRule1.timeout())); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Tests creation of a DefaultFlowRule using a FlowId constructor. |
| 83 | */ |
| 84 | @Test |
| 85 | public void testCreationWithFlowId() { |
| 86 | final DefaultFlowRule rule = |
| 87 | new DefaultFlowRule(did("1"), SELECTOR, |
Ayaka Koshibe | b1224ab | 2015-01-31 00:48:58 +0000 | [diff] [blame] | 88 | TREATMENT, 22, 33, |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 89 | 44, false); |
| 90 | assertThat(rule.deviceId(), is(did("1"))); |
| 91 | assertThat(rule.id().value(), is(33L)); |
| 92 | assertThat(rule.isPermanent(), is(false)); |
| 93 | assertThat(rule.priority(), is(22)); |
| 94 | assertThat(rule.selector(), is(SELECTOR)); |
Ayaka Koshibe | b1224ab | 2015-01-31 00:48:58 +0000 | [diff] [blame] | 95 | assertThat(rule.treatment(), is(TREATMENT)); |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 96 | assertThat(rule.timeout(), is(44)); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Tests the creation of a DefaultFlowRule using an AppId constructor. |
| 101 | */ |
| 102 | @Test |
| 103 | public void testCreationWithAppId() { |
| 104 | final DefaultFlowRule rule = |
| 105 | new DefaultFlowRule(did("1"), SELECTOR, |
Ayaka Koshibe | b1224ab | 2015-01-31 00:48:58 +0000 | [diff] [blame] | 106 | TREATMENT, 22, APP_ID, |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 107 | 44, false); |
| 108 | assertThat(rule.deviceId(), is(did("1"))); |
| 109 | assertThat(rule.isPermanent(), is(false)); |
| 110 | assertThat(rule.priority(), is(22)); |
| 111 | assertThat(rule.selector(), is(SELECTOR)); |
Ayaka Koshibe | b1224ab | 2015-01-31 00:48:58 +0000 | [diff] [blame] | 112 | assertThat(rule.treatment(), is(TREATMENT)); |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 113 | assertThat(rule.timeout(), is(44)); |
| 114 | } |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 115 | } |