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 = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 36 | new MatchActionOperationsId(1L); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 37 | assertThat(id, is(notNullValue())); |
| 38 | final MatchActionOperations operations = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 39 | new MatchActionOperations(id); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 40 | assertThat(operations, is(notNullValue())); |
| 41 | |
| 42 | for (int entriesIteration = 1; |
| 43 | entriesIteration <= iterations; |
| 44 | entriesIteration++) { |
| 45 | |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 46 | final MatchActionId entryId = |
| 47 | new MatchActionId( |
| 48 | (operationsIteration * 10) + |
| 49 | entriesIteration); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 50 | final MatchAction matchAction = |
| 51 | new MatchAction(entryId, null, null, null); |
| 52 | final MatchActionOperationEntry entry = |
| 53 | new MatchActionOperationEntry(MatchActionOperations.Operator.ADD, |
| 54 | matchAction); |
| 55 | operations.addOperation(entry); |
| 56 | generatedMatchActions.add(matchAction); |
| 57 | } |
| 58 | |
| 59 | // Add the MatchActions generated by this iteration |
| 60 | final boolean result = module.executeOperations(operations); |
| 61 | assertThat(result, is(true)); |
| 62 | } |
| 63 | |
| 64 | // Get the list of generated MatchAction objects and make sure its |
| 65 | // length is correct. |
| 66 | final int generatedCount = generatedMatchActions.size(); |
| 67 | final Set<MatchAction> matchActions = module.getMatchActions(); |
| 68 | assertThat(matchActions, hasSize(generatedCount)); |
| 69 | |
| 70 | // Make sure that all the created items are in the list |
| 71 | final MatchAction[] generatedArray = |
| 72 | generatedMatchActions.toArray(new MatchAction[generatedCount]); |
| 73 | assertThat(matchActions, containsInAnyOrder(generatedArray)); |
| 74 | |
| 75 | // Make sure that the returned list cannot be modified |
| 76 | Throwable errorThrown = null; |
| 77 | try { |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 78 | matchActions.add(new MatchAction(new MatchActionId(1L), null, null, null)); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 79 | } catch (UnsupportedOperationException e) { |
| 80 | errorThrown = e; |
| 81 | } |
| 82 | assertThat(errorThrown, is(notNullValue())); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Tests that adding a duplicate MatchAction via executeOperations() |
| 87 | * returns an error. |
| 88 | */ |
| 89 | @Test |
| 90 | public void testAddingDuplicateMatchAction() { |
| 91 | |
| 92 | // Create two MatchAction objects using the same ID |
| 93 | final MatchAction matchAction = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 94 | new MatchAction(new MatchActionId(111L), null, null, null); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 95 | final MatchAction duplicateMatchAction = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 96 | new MatchAction(new MatchActionId(111L), null, null, null); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 97 | |
| 98 | // create Operation Entries for the two MatchAction objects |
| 99 | final MatchActionOperationEntry entry = |
| 100 | new MatchActionOperationEntry(MatchActionOperations.Operator.ADD, |
| 101 | matchAction); |
| 102 | final MatchActionOperationEntry duplicateEntry = |
| 103 | new MatchActionOperationEntry(MatchActionOperations.Operator.ADD, |
| 104 | duplicateMatchAction); |
| 105 | |
| 106 | // Create an Operations object to execute the first MatchAction |
| 107 | final MatchActionOperationsId id = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 108 | new MatchActionOperationsId(11L); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 109 | assertThat(id, is(notNullValue())); |
| 110 | final MatchActionOperations operations = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 111 | new MatchActionOperations(id); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 112 | operations.addOperation(entry); |
| 113 | |
| 114 | // Create a module to use to execute the Operations. |
| 115 | final MatchActionModule module = new MatchActionModule(); |
| 116 | |
| 117 | // Execute the first set of Operations. This |
| 118 | // should succeed. |
| 119 | final boolean result = module.executeOperations(operations); |
| 120 | assertThat(result, is(true)); |
| 121 | |
| 122 | // Now add the duplicate entry. This should fail. |
| 123 | final MatchActionOperationsId idForDuplicate = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 124 | new MatchActionOperationsId(22L); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 125 | assertThat(idForDuplicate, is(notNullValue())); |
| 126 | final MatchActionOperations operationsForDuplicate = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 127 | new MatchActionOperations(idForDuplicate); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 128 | operationsForDuplicate.addOperation(duplicateEntry); |
| 129 | |
| 130 | final boolean resultForDuplicate = |
| 131 | module.executeOperations(operationsForDuplicate); |
| 132 | assertThat(resultForDuplicate, is(false)); |
| 133 | |
| 134 | // Now add the original entry again. This should fail. |
| 135 | final boolean resultForAddAgain = module.executeOperations(operations); |
| 136 | assertThat(resultForAddAgain, is(false)); |
| 137 | } |
| 138 | } |