Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent.runtime; |
| 2 | |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 3 | import static org.easymock.EasyMock.anyObject; |
| 4 | import static org.easymock.EasyMock.createMock; |
| 5 | import static org.easymock.EasyMock.eq; |
| 6 | import static org.easymock.EasyMock.expect; |
| 7 | import static org.easymock.EasyMock.expectLastCall; |
| 8 | import static org.easymock.EasyMock.replay; |
| 9 | import static org.easymock.EasyMock.verify; |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 10 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 11 | import net.floodlightcontroller.restserver.IRestApiService; |
| 12 | import net.onrc.onos.core.datagrid.IDatagridService; |
| 13 | import net.onrc.onos.core.datagrid.IEventChannel; |
| 14 | import net.onrc.onos.core.datagrid.IEventChannelListener; |
| 15 | import net.onrc.onos.core.intent.IntentOperationList; |
| 16 | import net.onrc.onos.core.intent.runtime.web.IntentWebRoutable; |
| 17 | import net.onrc.onos.core.registry.IControllerRegistryService; |
| 18 | import net.onrc.onos.core.topology.ITopologyListener; |
| 19 | import net.onrc.onos.core.topology.ITopologyService; |
| 20 | import net.onrc.onos.core.topology.MockTopology; |
| 21 | |
| 22 | import org.powermock.api.easymock.PowerMock; |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * This class contains all of the mocked code required to run a test that uses |
| 26 | * the Intent framework. The normal lifecycle of an object of this class is to |
| 27 | * create the object and call setUpIntentMocks() in the @Before (setUp()) part of |
| 28 | * the test, then call tearDownIntentMocks() in the @After (tearDown()) part |
| 29 | * of the test. The intention of this class is to hide the mocking mechanics |
| 30 | * and the unchecked suppressions in one place, and to make the Intent testing |
| 31 | * reusable. |
| 32 | * |
| 33 | * This code is largely refactored from |
| 34 | * net.onrc.onos.core.intent.runtime.UseCaseTest |
| 35 | */ |
| 36 | public class IntentTestMocks { |
| 37 | |
| 38 | private FloodlightModuleContext moduleContext; |
| 39 | private IDatagridService datagridService; |
| 40 | private ITopologyService topologyService; |
| 41 | private IControllerRegistryService controllerRegistryService; |
| 42 | private PersistIntent persistIntent; |
| 43 | private IRestApiService restApi; |
Ray Milkey | 9d67100 | 2014-05-29 17:24:29 -0700 | [diff] [blame] | 44 | private MockTopology topology; |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Default constructor. Doesn't do anything interesting. |
| 48 | */ |
| 49 | public IntentTestMocks() { } |
| 50 | |
| 51 | /** |
| 52 | * Create whatever mocks are required to access the Intents framework. |
| 53 | * This method is intended to be called during @Before processing (setUp()) |
| 54 | * of the JUnit test. |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 55 | */ |
| 56 | @SuppressWarnings("unchecked") |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 57 | public void setUpIntentMocks() { |
| 58 | try { |
| 59 | topology = new MockTopology(); |
| 60 | topology.createSampleTopology1(); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 61 | |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 62 | datagridService = createMock(IDatagridService.class); |
| 63 | topologyService = createMock(ITopologyService.class); |
| 64 | controllerRegistryService = createMock(IControllerRegistryService.class); |
| 65 | moduleContext = createMock(FloodlightModuleContext.class); |
| 66 | final IEventChannel<Long, IntentOperationList> intentOperationChannel = |
| 67 | createMock(IEventChannel.class); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 68 | final IEventChannel<Long, IntentStateList> intentStateChannel = |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 69 | createMock(IEventChannel.class); |
| 70 | persistIntent = PowerMock.createMock(PersistIntent.class); |
| 71 | restApi = createMock(IRestApiService.class); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 72 | |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 73 | PowerMock.expectNew(PersistIntent.class, |
| 74 | anyObject(IControllerRegistryService.class)).andReturn(persistIntent); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 75 | |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 76 | expect(moduleContext.getServiceImpl(IDatagridService.class)) |
| 77 | .andReturn(datagridService).once(); |
| 78 | expect(moduleContext.getServiceImpl(ITopologyService.class)) |
| 79 | .andReturn(topologyService).once(); |
| 80 | expect(moduleContext.getServiceImpl(IControllerRegistryService.class)) |
| 81 | .andReturn(controllerRegistryService).once(); |
| 82 | expect(persistIntent.getKey()).andReturn(1L).anyTimes(); |
| 83 | expect(persistIntent.persistIfLeader(eq(1L), |
| 84 | anyObject(IntentOperationList.class))).andReturn(true) |
| 85 | .anyTimes(); |
| 86 | expect(moduleContext.getServiceImpl(IRestApiService.class)) |
| 87 | .andReturn(restApi).once(); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 88 | |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 89 | expect(topologyService.getTopology()).andReturn(topology) |
| 90 | .anyTimes(); |
| 91 | topologyService.registerTopologyListener( |
| 92 | anyObject(ITopologyListener.class)); |
| 93 | expectLastCall(); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 94 | |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 95 | expect(datagridService.createChannel("onos.pathintent", |
| 96 | Long.class, IntentOperationList.class)) |
| 97 | .andReturn(intentOperationChannel).once(); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 98 | |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 99 | expect(datagridService.addListener( |
| 100 | eq("onos.pathintent_state"), |
| 101 | anyObject(IEventChannelListener.class), |
| 102 | eq(Long.class), |
| 103 | eq(IntentStateList.class))) |
| 104 | .andReturn(intentStateChannel).once(); |
| 105 | restApi.addRestletRoutable(anyObject(IntentWebRoutable.class)); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 106 | |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 107 | replay(datagridService); |
| 108 | replay(topologyService); |
| 109 | replay(moduleContext); |
| 110 | replay(controllerRegistryService); |
| 111 | PowerMock.replay(persistIntent, PersistIntent.class); |
| 112 | replay(restApi); |
| 113 | } catch (Exception ex) { |
| 114 | // Convert the checked exception into an unchecked exception to |
| 115 | // abort the test that called. |
| 116 | throw new IllegalStateException(ex); |
| 117 | } |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Remove whatever mocks were put in place. This method is intended to be |
| 122 | * called as part of @After processing (tearDown()) of the JUnit test. |
| 123 | */ |
| 124 | public void tearDownIntentMocks() { |
| 125 | verify(datagridService); |
| 126 | verify(topologyService); |
| 127 | verify(moduleContext); |
| 128 | verify(controllerRegistryService); |
| 129 | PowerMock.verify(persistIntent, PersistIntent.class); |
| 130 | verify(restApi); |
| 131 | } |
| 132 | |
| 133 | /** |
Ray Milkey | 9d67100 | 2014-05-29 17:24:29 -0700 | [diff] [blame] | 134 | * Fetch the Floodlight module context being used by the mock. Some tests |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 135 | * will need to add items to the Context to allow communications with |
| 136 | * downstream classes. |
| 137 | * |
| 138 | * @return the FloodlightModuleCOntext used by the mock. |
| 139 | */ |
| 140 | public FloodlightModuleContext getModuleContext() { |
| 141 | return moduleContext; |
| 142 | } |
Ray Milkey | 9d67100 | 2014-05-29 17:24:29 -0700 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * Fetch the mocked topology. |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 146 | * |
Ray Milkey | 9d67100 | 2014-05-29 17:24:29 -0700 | [diff] [blame] | 147 | * @return mocked topology |
| 148 | */ |
| 149 | public MockTopology getTopology() { |
| 150 | return topology; |
| 151 | } |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * Fetch the mocked topology service. |
| 155 | * |
| 156 | * @return mocked topology service |
| 157 | */ |
| 158 | public ITopologyService getTopologyService() { |
| 159 | return topologyService; |
| 160 | } |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 161 | } |