blob: 37fb57951012e184df502219b0d2b2591ce4e94d [file] [log] [blame]
Ray Milkeyc127a5a2014-08-20 11:22:12 -07001package net.onrc.onos.core.matchaction;
2
3import org.junit.Test;
4
5import java.util.ArrayList;
6import java.util.Set;
7
8import static org.hamcrest.MatcherAssert.assertThat;
9import static org.hamcrest.Matchers.containsInAnyOrder;
10import static org.hamcrest.Matchers.hasSize;
11import static org.hamcrest.Matchers.is;
12import static org.hamcrest.Matchers.notNullValue;
13
14/**
15 * Unit tests for the MatchActionModule.
16 */
17public 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 Milkey9ed4b962014-08-20 15:43:40 -070036 new MatchActionOperationsId(1L);
Ray Milkeyc127a5a2014-08-20 11:22:12 -070037 assertThat(id, is(notNullValue()));
38 final MatchActionOperations operations =
Ray Milkey9ed4b962014-08-20 15:43:40 -070039 new MatchActionOperations(id);
Ray Milkeyc127a5a2014-08-20 11:22:12 -070040 assertThat(operations, is(notNullValue()));
41
42 for (int entriesIteration = 1;
43 entriesIteration <= iterations;
44 entriesIteration++) {
45
Ray Milkey9ed4b962014-08-20 15:43:40 -070046 final MatchActionId entryId =
47 new MatchActionId(
48 (operationsIteration * 10) +
49 entriesIteration);
Ray Milkeyc127a5a2014-08-20 11:22:12 -070050 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 Milkey9ed4b962014-08-20 15:43:40 -070078 matchActions.add(new MatchAction(new MatchActionId(1L), null, null, null));
Ray Milkeyc127a5a2014-08-20 11:22:12 -070079 } 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 Milkey9ed4b962014-08-20 15:43:40 -070094 new MatchAction(new MatchActionId(111L), null, null, null);
Ray Milkeyc127a5a2014-08-20 11:22:12 -070095 final MatchAction duplicateMatchAction =
Ray Milkey9ed4b962014-08-20 15:43:40 -070096 new MatchAction(new MatchActionId(111L), null, null, null);
Ray Milkeyc127a5a2014-08-20 11:22:12 -070097
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 Milkey9ed4b962014-08-20 15:43:40 -0700108 new MatchActionOperationsId(11L);
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700109 assertThat(id, is(notNullValue()));
110 final MatchActionOperations operations =
Ray Milkey9ed4b962014-08-20 15:43:40 -0700111 new MatchActionOperations(id);
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700112 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 Milkey9ed4b962014-08-20 15:43:40 -0700124 new MatchActionOperationsId(22L);
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700125 assertThat(idForDuplicate, is(notNullValue()));
126 final MatchActionOperations operationsForDuplicate =
Ray Milkey9ed4b962014-08-20 15:43:40 -0700127 new MatchActionOperations(idForDuplicate);
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700128 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}