blob: a6dde8bc754ced070c882e4bee93c7dd9d0cf474 [file] [log] [blame]
Ray Milkeye8274232014-05-27 16:03:12 -07001package net.onrc.onos.core.intent.runtime;
2
Ray Milkeye8274232014-05-27 16:03:12 -07003import static org.easymock.EasyMock.anyObject;
4import static org.easymock.EasyMock.createMock;
5import static org.easymock.EasyMock.eq;
6import static org.easymock.EasyMock.expect;
7import static org.easymock.EasyMock.expectLastCall;
8import static org.easymock.EasyMock.replay;
9import static org.easymock.EasyMock.verify;
TeruU5d2c9392014-06-09 20:02:02 -070010import net.floodlightcontroller.core.module.FloodlightModuleContext;
11import net.floodlightcontroller.restserver.IRestApiService;
12import net.onrc.onos.core.datagrid.IDatagridService;
13import net.onrc.onos.core.datagrid.IEventChannel;
14import net.onrc.onos.core.datagrid.IEventChannelListener;
15import net.onrc.onos.core.intent.IntentOperationList;
16import net.onrc.onos.core.intent.runtime.web.IntentWebRoutable;
17import net.onrc.onos.core.registry.IControllerRegistryService;
18import net.onrc.onos.core.topology.ITopologyListener;
19import net.onrc.onos.core.topology.ITopologyService;
20import net.onrc.onos.core.topology.MockTopology;
21
22import org.powermock.api.easymock.PowerMock;
Ray Milkeye8274232014-05-27 16:03:12 -070023
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 */
36public 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 Milkey9d671002014-05-29 17:24:29 -070044 private MockTopology topology;
Ray Milkeye8274232014-05-27 16:03:12 -070045
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 Milkeye8274232014-05-27 16:03:12 -070055 */
56 @SuppressWarnings("unchecked")
Ray Milkey531bb232014-06-03 09:08:15 -070057 public void setUpIntentMocks() {
58 try {
59 topology = new MockTopology();
60 topology.createSampleTopology1();
Ray Milkeye8274232014-05-27 16:03:12 -070061
Ray Milkey531bb232014-06-03 09:08:15 -070062 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 HIGUCHI91a8f502014-06-17 10:15:29 -070068 final IEventChannel<Long, IntentStateList> intentStateChannel =
Ray Milkey531bb232014-06-03 09:08:15 -070069 createMock(IEventChannel.class);
70 persistIntent = PowerMock.createMock(PersistIntent.class);
71 restApi = createMock(IRestApiService.class);
Ray Milkeye8274232014-05-27 16:03:12 -070072
Ray Milkey531bb232014-06-03 09:08:15 -070073 PowerMock.expectNew(PersistIntent.class,
74 anyObject(IControllerRegistryService.class)).andReturn(persistIntent);
Ray Milkeye8274232014-05-27 16:03:12 -070075
Ray Milkey531bb232014-06-03 09:08:15 -070076 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 Milkeye8274232014-05-27 16:03:12 -070088
Ray Milkey531bb232014-06-03 09:08:15 -070089 expect(topologyService.getTopology()).andReturn(topology)
90 .anyTimes();
91 topologyService.registerTopologyListener(
92 anyObject(ITopologyListener.class));
93 expectLastCall();
Ray Milkeye8274232014-05-27 16:03:12 -070094
Ray Milkey531bb232014-06-03 09:08:15 -070095 expect(datagridService.createChannel("onos.pathintent",
96 Long.class, IntentOperationList.class))
97 .andReturn(intentOperationChannel).once();
Ray Milkeye8274232014-05-27 16:03:12 -070098
Ray Milkey531bb232014-06-03 09:08:15 -070099 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 Milkeye8274232014-05-27 16:03:12 -0700106
Ray Milkey531bb232014-06-03 09:08:15 -0700107 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 Milkeye8274232014-05-27 16:03:12 -0700118 }
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 Milkey9d671002014-05-29 17:24:29 -0700134 * Fetch the Floodlight module context being used by the mock. Some tests
Ray Milkeye8274232014-05-27 16:03:12 -0700135 * 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 Milkey9d671002014-05-29 17:24:29 -0700143
144 /**
145 * Fetch the mocked topology.
Ray Milkey531bb232014-06-03 09:08:15 -0700146 *
Ray Milkey9d671002014-05-29 17:24:29 -0700147 * @return mocked topology
148 */
149 public MockTopology getTopology() {
150 return topology;
151 }
Ray Milkey0fe43852014-06-05 14:41:46 -0700152
153 /**
154 * Fetch the mocked topology service.
155 *
156 * @return mocked topology service
157 */
158 public ITopologyService getTopologyService() {
159 return topologyService;
160 }
Ray Milkeye8274232014-05-27 16:03:12 -0700161}