blob: b76702717d9594770f482995a54547995b17200a [file] [log] [blame]
Ray Milkeyf19b7152014-11-21 10:56:52 -08001/*
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow;
Ray Milkeyf19b7152014-11-21 10:56:52 -080017
18import java.util.Collection;
19import java.util.LinkedList;
20import java.util.List;
21
22import org.junit.Test;
23
24import com.google.common.testing.EqualsTester;
25
26import static org.hamcrest.MatcherAssert.assertThat;
27import static org.hamcrest.Matchers.hasSize;
28import static org.hamcrest.Matchers.is;
29import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
30
31/**
32 * Unit tests for the BatchOperationTest object.
33 */
34public 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
46 private static final class TestTarget implements BatchOperationTarget {
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
150 assertThat(entry.getOperator(), is(TestType.OP3));
151 assertThat(entry.getTarget().id, is(3));
152 }
153}