Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.matchaction; |
| 2 | |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame^] | 3 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 4 | import net.onrc.onos.core.datagrid.IDatagridService; |
| 5 | import net.onrc.onos.core.datagrid.IEventChannel; |
| 6 | import net.onrc.onos.core.datagrid.IEventChannelListener; |
| 7 | import org.junit.Before; |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 8 | |
| 9 | import java.util.ArrayList; |
| 10 | import java.util.Set; |
| 11 | |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame^] | 12 | import static org.easymock.EasyMock.anyObject; |
| 13 | import static org.easymock.EasyMock.createMock; |
| 14 | import static org.easymock.EasyMock.eq; |
| 15 | import static org.easymock.EasyMock.expect; |
| 16 | import static org.easymock.EasyMock.replay; |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 17 | import static org.hamcrest.MatcherAssert.assertThat; |
| 18 | import static org.hamcrest.Matchers.containsInAnyOrder; |
| 19 | import static org.hamcrest.Matchers.hasSize; |
| 20 | import static org.hamcrest.Matchers.is; |
| 21 | import static org.hamcrest.Matchers.notNullValue; |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame^] | 22 | import static org.easymock.EasyMock.createNiceMock; |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Unit tests for the MatchActionModule. |
| 26 | */ |
| 27 | public class MatchActionModuleTest { |
| 28 | |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame^] | 29 | private IDatagridService datagridService; |
| 30 | private FloodlightModuleContext modContext; |
| 31 | |
| 32 | @Before |
| 33 | @SuppressWarnings("unchecked") |
| 34 | public void setUpMocks() { |
| 35 | final IEventChannel<String, MatchActionOperations> installSetChannel = |
| 36 | createMock(IEventChannel.class); |
| 37 | final IEventChannel<String, SwitchResultList> installSetReplyChannel = |
| 38 | createMock(IEventChannel.class); |
| 39 | |
| 40 | datagridService = createNiceMock(IDatagridService.class); |
| 41 | modContext = createMock(FloodlightModuleContext.class); |
| 42 | |
| 43 | expect(modContext.getServiceImpl(IDatagridService.class)) |
| 44 | .andReturn(datagridService).once(); |
| 45 | |
| 46 | expect(datagridService.createChannel("onos.matchaction.installSetChannel", |
| 47 | String.class, |
| 48 | MatchActionOperations.class)) |
| 49 | .andReturn(installSetChannel).once(); |
| 50 | |
| 51 | expect(datagridService.addListener( |
| 52 | eq("onos.matchaction.installSetChannel"), |
| 53 | anyObject(IEventChannelListener.class), |
| 54 | eq(String.class), |
| 55 | eq(MatchActionOperations.class))) |
| 56 | .andReturn(installSetChannel).once(); |
| 57 | |
| 58 | expect(datagridService.createChannel("onos.matchaction.installSetReplyChannel", |
| 59 | String.class, |
| 60 | SwitchResultList.class)) |
| 61 | .andReturn(installSetReplyChannel).once(); |
| 62 | |
| 63 | expect(datagridService.addListener( |
| 64 | eq("onos.matchaction.installSetReplyChannel"), |
| 65 | anyObject(IEventChannelListener.class), |
| 66 | eq(String.class), |
| 67 | eq(SwitchResultList.class))) |
| 68 | .andReturn(installSetReplyChannel).once(); |
| 69 | |
| 70 | replay(datagridService); |
| 71 | } |
| 72 | |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Tests that MatchAction objects added by the executeOperations() |
| 75 | * method are properly returned by the getMatchActions() method. |
| 76 | */ |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame^] | 77 | //@Test |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 78 | public void testMatchActionModuleGlobalEntriesSet() { |
| 79 | |
| 80 | final int iterations = 5; |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame^] | 81 | final MatchActionComponent matchActionComponent = |
| 82 | new MatchActionComponent(datagridService, null, null); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 83 | final ArrayList<MatchAction> generatedMatchActions = new ArrayList<>(); |
| 84 | |
| 85 | // Add some test MatchAction objects. 25 will be added, in 5 blocks |
| 86 | // of 5. |
| 87 | for (int operationsIteration = 1; |
| 88 | operationsIteration <= iterations; |
| 89 | operationsIteration++) { |
| 90 | final MatchActionOperationsId id = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 91 | new MatchActionOperationsId(1L); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 92 | assertThat(id, is(notNullValue())); |
| 93 | final MatchActionOperations operations = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 94 | new MatchActionOperations(id); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 95 | assertThat(operations, is(notNullValue())); |
| 96 | |
| 97 | for (int entriesIteration = 1; |
| 98 | entriesIteration <= iterations; |
| 99 | entriesIteration++) { |
| 100 | |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 101 | final MatchActionId entryId = |
| 102 | new MatchActionId( |
| 103 | (operationsIteration * 10) + |
| 104 | entriesIteration); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 105 | final MatchAction matchAction = |
| 106 | new MatchAction(entryId, null, null, null); |
| 107 | final MatchActionOperationEntry entry = |
| 108 | new MatchActionOperationEntry(MatchActionOperations.Operator.ADD, |
| 109 | matchAction); |
| 110 | operations.addOperation(entry); |
| 111 | generatedMatchActions.add(matchAction); |
| 112 | } |
| 113 | |
| 114 | // Add the MatchActions generated by this iteration |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame^] | 115 | final boolean result = matchActionComponent.executeOperations(operations); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 116 | assertThat(result, is(true)); |
| 117 | } |
| 118 | |
| 119 | // Get the list of generated MatchAction objects and make sure its |
| 120 | // length is correct. |
| 121 | final int generatedCount = generatedMatchActions.size(); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame^] | 122 | final Set<MatchAction> matchActions = matchActionComponent.getMatchActions(); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 123 | assertThat(matchActions, hasSize(generatedCount)); |
| 124 | |
| 125 | // Make sure that all the created items are in the list |
| 126 | final MatchAction[] generatedArray = |
| 127 | generatedMatchActions.toArray(new MatchAction[generatedCount]); |
| 128 | assertThat(matchActions, containsInAnyOrder(generatedArray)); |
| 129 | |
| 130 | // Make sure that the returned list cannot be modified |
| 131 | Throwable errorThrown = null; |
| 132 | try { |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 133 | matchActions.add(new MatchAction(new MatchActionId(1L), null, null, null)); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 134 | } catch (UnsupportedOperationException e) { |
| 135 | errorThrown = e; |
| 136 | } |
| 137 | assertThat(errorThrown, is(notNullValue())); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Tests that adding a duplicate MatchAction via executeOperations() |
| 142 | * returns an error. |
| 143 | */ |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame^] | 144 | //@Test |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 145 | public void testAddingDuplicateMatchAction() { |
| 146 | |
| 147 | // Create two MatchAction objects using the same ID |
| 148 | final MatchAction matchAction = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 149 | new MatchAction(new MatchActionId(111L), null, null, null); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 150 | final MatchAction duplicateMatchAction = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 151 | new MatchAction(new MatchActionId(111L), null, null, null); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 152 | |
| 153 | // create Operation Entries for the two MatchAction objects |
| 154 | final MatchActionOperationEntry entry = |
| 155 | new MatchActionOperationEntry(MatchActionOperations.Operator.ADD, |
| 156 | matchAction); |
| 157 | final MatchActionOperationEntry duplicateEntry = |
| 158 | new MatchActionOperationEntry(MatchActionOperations.Operator.ADD, |
| 159 | duplicateMatchAction); |
| 160 | |
| 161 | // Create an Operations object to execute the first MatchAction |
| 162 | final MatchActionOperationsId id = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 163 | new MatchActionOperationsId(11L); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 164 | assertThat(id, is(notNullValue())); |
| 165 | final MatchActionOperations operations = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 166 | new MatchActionOperations(id); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 167 | operations.addOperation(entry); |
| 168 | |
| 169 | // Create a module to use to execute the Operations. |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame^] | 170 | final MatchActionComponent matchActionComponent = new MatchActionComponent(null, null, null); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 171 | |
| 172 | // Execute the first set of Operations. This |
| 173 | // should succeed. |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame^] | 174 | final boolean result = matchActionComponent.executeOperations(operations); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 175 | assertThat(result, is(true)); |
| 176 | |
| 177 | // Now add the duplicate entry. This should fail. |
| 178 | final MatchActionOperationsId idForDuplicate = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 179 | new MatchActionOperationsId(22L); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 180 | assertThat(idForDuplicate, is(notNullValue())); |
| 181 | final MatchActionOperations operationsForDuplicate = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 182 | new MatchActionOperations(idForDuplicate); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 183 | operationsForDuplicate.addOperation(duplicateEntry); |
| 184 | |
| 185 | final boolean resultForDuplicate = |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame^] | 186 | matchActionComponent.executeOperations(operationsForDuplicate); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 187 | assertThat(resultForDuplicate, is(false)); |
| 188 | |
| 189 | // Now add the original entry again. This should fail. |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame^] | 190 | final boolean resultForAddAgain = matchActionComponent.executeOperations(operations); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 191 | assertThat(resultForAddAgain, is(false)); |
| 192 | } |
| 193 | } |