Toshio Koide | 4ba8b7c | 2014-08-29 13:17:54 -0700 | [diff] [blame^] | 1 | package net.onrc.onos.api.flowmanager; |
| 2 | |
| 3 | import static org.hamcrest.Matchers.hasItem; |
| 4 | import static org.hamcrest.Matchers.hasSize; |
| 5 | import static org.junit.Assert.assertEquals; |
| 6 | import static org.junit.Assert.assertNotNull; |
| 7 | import static org.junit.Assert.assertThat; |
| 8 | |
| 9 | import java.util.LinkedList; |
| 10 | import java.util.List; |
| 11 | |
| 12 | import net.onrc.onos.api.batchoperation.BatchOperationEntry; |
| 13 | import net.onrc.onos.api.flowmanager.FlowBatchOperation.Operator; |
| 14 | import net.onrc.onos.core.matchaction.MatchActionId; |
| 15 | import net.onrc.onos.core.matchaction.MatchActionOperations; |
| 16 | import net.onrc.onos.core.matchaction.MatchActionOperationsId; |
| 17 | import net.onrc.onos.core.matchaction.match.Match; |
| 18 | import net.onrc.onos.core.util.IdGenerator; |
| 19 | |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | |
| 23 | /** |
| 24 | * Unit tests for {@link FlowBatchOperation}. |
| 25 | */ |
| 26 | public class FlowBatchOperationTest { |
| 27 | private Flow flow1; |
| 28 | private Flow flow2; |
| 29 | private Flow flow3; |
| 30 | |
| 31 | /** |
| 32 | * A subclass of {@link Flow} for testing purpose. |
| 33 | */ |
| 34 | final class TestFlow extends Flow { |
| 35 | public TestFlow(FlowId id) { |
| 36 | super(id); |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public Match getMatch() { |
| 41 | return null; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public List<MatchActionOperations> compile(Operator op, |
| 46 | IdGenerator<MatchActionId> maIdGenerator, |
| 47 | IdGenerator<MatchActionOperationsId> maoIdGenerator) { |
| 48 | return null; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | @Before |
| 53 | public void setUp() throws Exception { |
| 54 | flow1 = new TestFlow(new FlowId(123L)); |
| 55 | flow2 = new TestFlow(new FlowId(456L)); |
| 56 | flow3 = new TestFlow(new FlowId(789L)); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Tests {@link FlowBatchOperation#FlowBatchOperation()} constructor. |
| 61 | */ |
| 62 | @Test |
| 63 | public void testConstructor() { |
| 64 | FlowBatchOperation op1 = new FlowBatchOperation(); |
| 65 | assertNotNull(op1); |
| 66 | assertEquals(0, op1.size()); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Tests {@link FlowBatchOperation#FlowBatchOperation(java.util.List)} |
| 71 | * constructor. |
| 72 | */ |
| 73 | @Test |
| 74 | public void testConstructorWithList() { |
| 75 | List<BatchOperationEntry<Operator, ?>> batchOperations; |
| 76 | batchOperations = new LinkedList<>(); |
| 77 | batchOperations.add(new BatchOperationEntry<Operator, Flow>( |
| 78 | Operator.ADD, flow1)); |
| 79 | batchOperations.add(new BatchOperationEntry<Operator, Flow>( |
| 80 | Operator.ADD, flow2)); |
| 81 | batchOperations.add(new BatchOperationEntry<Operator, Flow>( |
| 82 | Operator.ADD, flow3)); |
| 83 | batchOperations.add(new BatchOperationEntry<Operator, FlowId>( |
| 84 | Operator.REMOVE, new FlowId(1L))); |
| 85 | batchOperations.add(new BatchOperationEntry<Operator, FlowId>( |
| 86 | Operator.REMOVE, new FlowId(2L))); |
| 87 | batchOperations.add(new BatchOperationEntry<Operator, FlowId>( |
| 88 | Operator.REMOVE, new FlowId(3L))); |
| 89 | |
| 90 | FlowBatchOperation op1 = new FlowBatchOperation(batchOperations); |
| 91 | |
| 92 | assertNotNull(op1); |
| 93 | assertEquals(6, op1.size()); |
| 94 | assertThat(op1.getOperations(), hasSize(6)); |
| 95 | assertThat(op1.getOperations(), hasItem( |
| 96 | new BatchOperationEntry<Operator, Flow>(Operator.ADD, flow1))); |
| 97 | assertThat(op1.getOperations(), hasItem( |
| 98 | new BatchOperationEntry<Operator, Flow>(Operator.ADD, flow2))); |
| 99 | assertThat(op1.getOperations(), hasItem( |
| 100 | new BatchOperationEntry<Operator, Flow>(Operator.ADD, flow3))); |
| 101 | assertThat(op1.getOperations(), |
| 102 | hasItem(new BatchOperationEntry<Operator, FlowId>(Operator.REMOVE, |
| 103 | new FlowId(1L)))); |
| 104 | assertThat(op1.getOperations(), |
| 105 | hasItem(new BatchOperationEntry<Operator, FlowId>(Operator.REMOVE, |
| 106 | new FlowId(2L)))); |
| 107 | assertThat(op1.getOperations(), |
| 108 | hasItem(new BatchOperationEntry<Operator, FlowId>(Operator.REMOVE, |
| 109 | new FlowId(3L)))); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Tests {@link FlowBatchOperation#addAddFlowOperation(Flow)} method. |
| 114 | */ |
| 115 | @Test |
| 116 | public void testAddAddFlowOperation() { |
| 117 | FlowBatchOperation op1 = new FlowBatchOperation(); |
| 118 | |
| 119 | FlowBatchOperation op2 = op1.addAddFlowOperation(flow1); |
| 120 | |
| 121 | assertEquals(op1, op2); |
| 122 | assertEquals(1, op1.size()); |
| 123 | assertThat(op1.getOperations(), hasSize(1)); |
| 124 | assertThat(op1.getOperations(), hasItem( |
| 125 | new BatchOperationEntry<Operator, Flow>(Operator.ADD, flow1))); |
| 126 | |
| 127 | op1.addAddFlowOperation(flow2).addAddFlowOperation(flow3); |
| 128 | |
| 129 | assertEquals(3, op1.size()); |
| 130 | assertThat(op1.getOperations(), hasSize(3)); |
| 131 | assertThat(op1.getOperations(), hasItem( |
| 132 | new BatchOperationEntry<Operator, Flow>(Operator.ADD, flow1))); |
| 133 | assertThat(op1.getOperations(), hasItem( |
| 134 | new BatchOperationEntry<Operator, Flow>(Operator.ADD, flow2))); |
| 135 | assertThat(op1.getOperations(), hasItem( |
| 136 | new BatchOperationEntry<Operator, Flow>(Operator.ADD, flow3))); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Tests {@link FlowBatchOperation#addRemoveFlowOperation(Flow)} method. |
| 141 | */ |
| 142 | @Test |
| 143 | public void testAddRemoveFlowOperation() { |
| 144 | FlowBatchOperation op1 = new FlowBatchOperation(); |
| 145 | |
| 146 | FlowBatchOperation op2 = op1.addRemoveFlowOperation(new FlowId(123L)); |
| 147 | |
| 148 | assertEquals(op1, op2); |
| 149 | assertEquals(1, op1.size()); |
| 150 | assertThat(op1.getOperations(), hasSize(1)); |
| 151 | assertThat(op1.getOperations(), |
| 152 | hasItem(new BatchOperationEntry<Operator, FlowId>( |
| 153 | Operator.REMOVE, new FlowId(123L)))); |
| 154 | |
| 155 | op1.addRemoveFlowOperation(new FlowId(456L)) |
| 156 | .addRemoveFlowOperation(new FlowId(789L)); |
| 157 | |
| 158 | assertEquals(3, op1.size()); |
| 159 | assertThat(op1.getOperations(), hasSize(3)); |
| 160 | assertThat(op1.getOperations(), |
| 161 | hasItem(new BatchOperationEntry<Operator, FlowId>( |
| 162 | Operator.REMOVE, new FlowId(123L)))); |
| 163 | assertThat(op1.getOperations(), |
| 164 | hasItem(new BatchOperationEntry<Operator, FlowId>( |
| 165 | Operator.REMOVE, new FlowId(456L)))); |
| 166 | assertThat(op1.getOperations(), |
| 167 | hasItem(new BatchOperationEntry<Operator, FlowId>( |
| 168 | Operator.REMOVE, new FlowId(789L)))); |
| 169 | } |
| 170 | } |