Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.flow; |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.LinkedList; |
| 20 | import java.util.List; |
| 21 | |
| 22 | import org.junit.Test; |
| 23 | |
| 24 | import com.google.common.testing.EqualsTester; |
| 25 | |
| 26 | import static org.hamcrest.MatcherAssert.assertThat; |
| 27 | import static org.hamcrest.Matchers.hasSize; |
| 28 | import static org.hamcrest.Matchers.is; |
| 29 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; |
| 30 | |
| 31 | /** |
| 32 | * Unit tests for the BatchOperationTest object. |
| 33 | */ |
| 34 | public class BatchOperationTest { |
| 35 | |
| 36 | private enum TestType { |
| 37 | OP1, |
| 38 | OP2, |
| 39 | OP3 |
| 40 | } |
| 41 | |
| 42 | final TestEntry entry1 = new TestEntry(TestType.OP1, new TestTarget(1)); |
| 43 | final TestEntry entry2 = new TestEntry(TestType.OP2, new TestTarget(2)); |
| 44 | |
| 45 | |
Sho SHIMIZU | 5e5d4aa | 2015-01-26 16:12:11 -0800 | [diff] [blame] | 46 | private static final class TestTarget { |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 47 | private int id; |
| 48 | |
| 49 | private TestTarget(int id) { |
| 50 | this.id = id; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public int hashCode() { |
| 55 | return id; |
| 56 | } |
| 57 | @Override |
| 58 | public boolean equals(Object o) { |
| 59 | if (this == o) { |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | if (o == null) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | if (getClass() != o.getClass()) { |
| 68 | return false; |
| 69 | } |
| 70 | TestTarget that = (TestTarget) o; |
| 71 | return this.id == that.id; |
| 72 | } |
| 73 | |
| 74 | } |
| 75 | |
| 76 | private static final class TestEntry extends BatchOperationEntry<TestType, TestTarget> { |
| 77 | public TestEntry(TestType operator, TestTarget target) { |
| 78 | super(operator, target); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | private static final class TestOperation extends BatchOperation<TestEntry> { |
| 83 | private TestOperation() { |
| 84 | super(); |
| 85 | } |
| 86 | |
| 87 | private TestOperation(Collection<TestEntry> batchOperations) { |
| 88 | super(batchOperations); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Checks that the DefaultFlowRule class is immutable. |
| 94 | */ |
| 95 | @Test |
| 96 | public void testImmutability() { |
| 97 | assertThatClassIsImmutableBaseClass(BatchOperation.class); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Tests the equals(), hashCode() and toString() operations. |
| 102 | */ |
| 103 | @Test |
| 104 | public void testEquals() { |
| 105 | final List<TestEntry> ops1 = new LinkedList<>(); |
| 106 | ops1.add(entry1); |
| 107 | final List<TestEntry> ops2 = new LinkedList<>(); |
| 108 | ops2.add(entry2); |
| 109 | |
| 110 | final TestOperation op1 = new TestOperation(ops1); |
| 111 | final TestOperation sameAsOp1 = new TestOperation(ops1); |
| 112 | final TestOperation op2 = new TestOperation(ops2); |
| 113 | |
| 114 | new EqualsTester() |
| 115 | .addEqualityGroup(op1, sameAsOp1) |
| 116 | .addEqualityGroup(op2) |
| 117 | .testEquals(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Tests the constructors for a BatchOperation. |
| 122 | */ |
| 123 | @Test |
| 124 | public void testConstruction() { |
| 125 | final List<TestEntry> ops = new LinkedList<>(); |
| 126 | ops.add(entry2); |
| 127 | |
| 128 | final TestOperation op1 = new TestOperation(); |
| 129 | assertThat(op1.size(), is(0)); |
| 130 | assertThat(op1.getOperations(), hasSize(0)); |
| 131 | |
| 132 | final TestOperation op2 = new TestOperation(ops); |
| 133 | op1.addOperation(entry1); |
| 134 | op1.addAll(op2); |
| 135 | assertThat(op1.size(), is(2)); |
| 136 | assertThat(op1.getOperations(), hasSize(2)); |
| 137 | |
| 138 | op2.clear(); |
| 139 | assertThat(op2.size(), is(0)); |
| 140 | assertThat(op2.getOperations(), hasSize(0)); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Tests the constructor for BatchOperationEntries. |
| 145 | */ |
| 146 | @Test |
| 147 | public void testEntryConstruction() { |
| 148 | final TestEntry entry = new TestEntry(TestType.OP3, new TestTarget(3)); |
| 149 | |
Sho SHIMIZU | aba9d00 | 2015-01-29 14:51:04 -0800 | [diff] [blame] | 150 | assertThat(entry.operator(), is(TestType.OP3)); |
| 151 | assertThat(entry.target().id, is(3)); |
Ray Milkey | f19b715 | 2014-11-21 10:56:52 -0800 | [diff] [blame] | 152 | } |
| 153 | } |