Skip to content

Commit c71ab95

Browse files
authored
test: refactor ActionEventInterceptorTest to prevent failures (#9384)
Try to intercept test calss methods in new CallContext to prevent getting any leftover data during assertions. Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 32cc1d4 commit c71ab95

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

server/src/test/java/com/cloud/event/ActionEventInterceptorTest.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public void setup() throws Exception {
140140
}
141141

142142
utils.init();
143+
CallContext.register(user, account);
143144
}
144145

145146
/**
@@ -152,6 +153,7 @@ public void setupCommonMocks() throws Exception {
152153
Mockito.when(configDao.getValue(Config.PublishActionEvent.key())).thenReturn("true");
153154
componentContextMocked = Mockito.mockStatic(ComponentContext.class);
154155
Mockito.when(ComponentContext.getComponent(EventBus.class)).thenReturn(eventBus);
156+
persistedEvents = new ArrayList<>();
155157

156158
//Needed for persist to actually set an ID that can be returned from the ActionEventUtils
157159
//methods.
@@ -198,6 +200,7 @@ public void teardown() {
198200
utils.init();
199201

200202
componentContextMocked.close();
203+
CallContext.unregister();
201204
}
202205

203206
@Test
@@ -228,11 +231,11 @@ public void testInterceptStartAsync() throws NoSuchMethodException {
228231
Object event = actionEventInterceptor.interceptStart(m, tester);
229232
Assert.assertNull(event);
230233

231-
Assert.assertEquals(persistedEvents.size(), 1);
234+
Assert.assertEquals(1, persistedEvents.size());
232235
EventVO eventVO = persistedEvents.get(0);
233-
Assert.assertEquals(eventVO.getType(), EventTypes.EVENT_VM_START);
234-
Assert.assertEquals(eventVO.getDescription(), "Starting VM");
235-
Assert.assertEquals(eventVO.getState(), com.cloud.event.Event.State.Started);
236+
Assert.assertEquals(EventTypes.EVENT_VM_START, eventVO.getType());
237+
Assert.assertEquals(eventDescription, eventVO.getDescription());
238+
Assert.assertEquals(com.cloud.event.Event.State.Started, eventVO.getState());
236239
}
237240

238241
@Test
@@ -241,12 +244,12 @@ public void testInterceptComplete() throws NoSuchMethodException {
241244
Method m = tester.getClass().getMethod("testMethod");
242245
actionEventInterceptor.interceptComplete(m, tester, null);
243246

244-
Assert.assertEquals(persistedEvents.size(), 1);
247+
Assert.assertEquals(1, persistedEvents.size());
245248
EventVO eventVO = persistedEvents.get(0);
246-
Assert.assertEquals(eventVO.getType(), eventType);
249+
Assert.assertEquals(eventType, eventVO.getType());
247250
Assert.assertTrue(eventVO.getDescription().endsWith(eventDescription));
248-
Assert.assertEquals(eventVO.getLevel(), EventVO.LEVEL_INFO);
249-
Assert.assertEquals(eventVO.getState(), com.cloud.event.Event.State.Completed);
251+
Assert.assertEquals(EventVO.LEVEL_INFO, eventVO.getLevel());
252+
Assert.assertEquals(com.cloud.event.Event.State.Completed, eventVO.getState());
250253
}
251254

252255
@Test
@@ -255,17 +258,16 @@ public void testInterceptException() throws NoSuchMethodException {
255258
Method m = tester.getClass().getMethod("testMethod");
256259
actionEventInterceptor.interceptException(m, tester, null);
257260

258-
Assert.assertEquals(persistedEvents.size(), 1);
261+
Assert.assertEquals(1, persistedEvents.size());
259262
EventVO eventVO = persistedEvents.get(0);
260-
Assert.assertEquals(eventVO.getType(), eventType);
263+
Assert.assertEquals(eventType, eventVO.getType());
261264
Assert.assertTrue(eventVO.getDescription().endsWith(eventDescription));
262-
Assert.assertEquals(eventVO.getLevel(), EventVO.LEVEL_ERROR);
263-
Assert.assertEquals(eventVO.getState(), com.cloud.event.Event.State.Completed);
265+
Assert.assertEquals(EventVO.LEVEL_ERROR, eventVO.getLevel());
266+
Assert.assertEquals(com.cloud.event.Event.State.Completed, eventVO.getState());
264267
}
265268

266269
@Test
267270
public void testInterceptExceptionResource() throws NoSuchMethodException {
268-
CallContext.register(user, account);
269271
Long resourceId = 1L;
270272
ApiCommandResourceType resourceType = ApiCommandResourceType.VirtualMachine;
271273
CallContext.current().setEventResourceId(resourceId);
@@ -274,15 +276,14 @@ public void testInterceptExceptionResource() throws NoSuchMethodException {
274276
Method m = tester.getClass().getMethod("testMethod");
275277
actionEventInterceptor.interceptException(m, tester, null);
276278

277-
Assert.assertEquals(persistedEvents.size(), 1);
279+
Assert.assertEquals(1, persistedEvents.size());
278280
EventVO eventVO = persistedEvents.get(0);
279-
Assert.assertEquals(eventVO.getType(), eventType);
281+
Assert.assertEquals(eventType, eventVO.getType());
280282
Assert.assertTrue(eventVO.getDescription().endsWith(eventDescription));
281-
Assert.assertEquals(eventVO.getLevel(), EventVO.LEVEL_ERROR);
282-
Assert.assertEquals(eventVO.getState(), com.cloud.event.Event.State.Completed);
283-
Assert.assertEquals(eventVO.getResourceId(), resourceId);
284-
Assert.assertEquals(eventVO.getResourceType(), resourceType.toString());
285-
CallContext.unregister();
283+
Assert.assertEquals(EventVO.LEVEL_ERROR, eventVO.getLevel());
284+
Assert.assertEquals(com.cloud.event.Event.State.Completed, eventVO.getState());
285+
Assert.assertEquals(resourceId, eventVO.getResourceId());
286+
Assert.assertEquals(resourceType.toString(), eventVO.getResourceType());
286287
}
287288

288289
@Test

0 commit comments

Comments
 (0)