Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.matchaction; |
| 2 | |
| 3 | import org.junit.Test; |
| 4 | |
| 5 | import java.util.ArrayList; |
| 6 | import java.util.Set; |
| 7 | |
| 8 | import static org.hamcrest.MatcherAssert.assertThat; |
| 9 | import static org.hamcrest.Matchers.containsInAnyOrder; |
| 10 | import static org.hamcrest.Matchers.hasSize; |
| 11 | import static org.hamcrest.Matchers.is; |
| 12 | import static org.hamcrest.Matchers.notNullValue; |
| 13 | |
| 14 | /** |
| 15 | * Unit tests for the MatchActionModule. |
| 16 | */ |
| 17 | public class MatchActionModuleTest { |
| 18 | |
| 19 | /** |
| 20 | * Tests that MatchAction objects added by the executeOperations() |
| 21 | * method are properly returned by the getMatchActions() method. |
| 22 | */ |
| 23 | @Test |
| 24 | public void testMatchActionModuleGlobalEntriesSet() { |
| 25 | |
| 26 | final int iterations = 5; |
| 27 | final MatchActionModule module = new MatchActionModule(); |
| 28 | final ArrayList<MatchAction> generatedMatchActions = new ArrayList<>(); |
| 29 | |
| 30 | // Add some test MatchAction objects. 25 will be added, in 5 blocks |
| 31 | // of 5. |
| 32 | for (int operationsIteration = 1; |
| 33 | operationsIteration <= iterations; |
| 34 | operationsIteration++) { |
| 35 | final MatchActionOperationsId id = |
| 36 | MatchActionOperationsId.createNewOperationsId(); |
| 37 | assertThat(id, is(notNullValue())); |
| 38 | final MatchActionOperations operations = |
| 39 | MatchActionOperations.createMatchActionsOperations(id); |
| 40 | assertThat(operations, is(notNullValue())); |
| 41 | |
| 42 | for (int entriesIteration = 1; |
| 43 | entriesIteration <= iterations; |
| 44 | entriesIteration++) { |
| 45 | |
| 46 | final String entryId = "MA" + |
| 47 | Integer.toString(operationsIteration) + |
| 48 | Integer.toString(entriesIteration); |
| 49 | final MatchAction matchAction = |
| 50 | new MatchAction(entryId, null, null, null); |
| 51 | final MatchActionOperationEntry entry = |
| 52 | new MatchActionOperationEntry(MatchActionOperations.Operator.ADD, |
| 53 | matchAction); |
| 54 | operations.addOperation(entry); |
| 55 | generatedMatchActions.add(matchAction); |
| 56 | } |
| 57 | |
| 58 | // Add the MatchActions generated by this iteration |
| 59 | final boolean result = module.executeOperations(operations); |
| 60 | assertThat(result, is(true)); |
| 61 | } |
| 62 | |
| 63 | // Get the list of generated MatchAction objects and make sure its |
| 64 | // length is correct. |
| 65 | final int generatedCount = generatedMatchActions.size(); |
| 66 | final Set<MatchAction> matchActions = module.getMatchActions(); |
| 67 | assertThat(matchActions, hasSize(generatedCount)); |
| 68 | |
| 69 | // Make sure that all the created items are in the list |
| 70 | final MatchAction[] generatedArray = |
| 71 | generatedMatchActions.toArray(new MatchAction[generatedCount]); |
| 72 | assertThat(matchActions, containsInAnyOrder(generatedArray)); |
| 73 | |
| 74 | // Make sure that the returned list cannot be modified |
| 75 | Throwable errorThrown = null; |
| 76 | try { |
| 77 | matchActions.add(new MatchAction("", null, null, null)); |
| 78 | } catch (UnsupportedOperationException e) { |
| 79 | errorThrown = e; |
| 80 | } |
| 81 | assertThat(errorThrown, is(notNullValue())); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Tests that adding a duplicate MatchAction via executeOperations() |
| 86 | * returns an error. |
| 87 | */ |
| 88 | @Test |
| 89 | public void testAddingDuplicateMatchAction() { |
| 90 | |
| 91 | // Create two MatchAction objects using the same ID |
| 92 | final MatchAction matchAction = |
| 93 | new MatchAction("ID", null, null, null); |
| 94 | final MatchAction duplicateMatchAction = |
| 95 | new MatchAction("ID", null, null, null); |
| 96 | |
| 97 | // create Operation Entries for the two MatchAction objects |
| 98 | final MatchActionOperationEntry entry = |
| 99 | new MatchActionOperationEntry(MatchActionOperations.Operator.ADD, |
| 100 | matchAction); |
| 101 | final MatchActionOperationEntry duplicateEntry = |
| 102 | new MatchActionOperationEntry(MatchActionOperations.Operator.ADD, |
| 103 | duplicateMatchAction); |
| 104 | |
| 105 | // Create an Operations object to execute the first MatchAction |
| 106 | final MatchActionOperationsId id = |
| 107 | MatchActionOperationsId.createNewOperationsId(); |
| 108 | assertThat(id, is(notNullValue())); |
| 109 | final MatchActionOperations operations = |
| 110 | MatchActionOperations.createMatchActionsOperations(id); |
| 111 | operations.addOperation(entry); |
| 112 | |
| 113 | // Create a module to use to execute the Operations. |
| 114 | final MatchActionModule module = new MatchActionModule(); |
| 115 | |
| 116 | // Execute the first set of Operations. This |
| 117 | // should succeed. |
| 118 | final boolean result = module.executeOperations(operations); |
| 119 | assertThat(result, is(true)); |
| 120 | |
| 121 | // Now add the duplicate entry. This should fail. |
| 122 | final MatchActionOperationsId idForDuplicate = |
| 123 | MatchActionOperationsId.createNewOperationsId(); |
| 124 | assertThat(idForDuplicate, is(notNullValue())); |
| 125 | final MatchActionOperations operationsForDuplicate = |
| 126 | MatchActionOperations.createMatchActionsOperations(idForDuplicate); |
| 127 | operationsForDuplicate.addOperation(duplicateEntry); |
| 128 | |
| 129 | final boolean resultForDuplicate = |
| 130 | module.executeOperations(operationsForDuplicate); |
| 131 | assertThat(resultForDuplicate, is(false)); |
| 132 | |
| 133 | // Now add the original entry again. This should fail. |
| 134 | final boolean resultForAddAgain = module.executeOperations(operations); |
| 135 | assertThat(resultForAddAgain, is(false)); |
| 136 | } |
| 137 | } |