This is an automated email from the ASF dual-hosted git repository. weizhou pushed a commit to branch 4.19 in repository https://gitbox.apache.org/repos/asf/cloudstack.git
The following commit(s) were added to refs/heads/4.19 by this push: new c71ab9598e3 test: refactor ActionEventInterceptorTest to prevent failures (#9384) c71ab9598e3 is described below commit c71ab9598e3634ec4a6816d65befb3f60523c32a Author: Abhishek Kumar <abhishek.mr...@gmail.com> AuthorDate: Fri Jul 12 18:40:55 2024 +0530 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.mr...@gmail.com> --- .../cloud/event/ActionEventInterceptorTest.java | 41 +++++++++++----------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/server/src/test/java/com/cloud/event/ActionEventInterceptorTest.java b/server/src/test/java/com/cloud/event/ActionEventInterceptorTest.java index 9211ce16759..35b39dc3d53 100644 --- a/server/src/test/java/com/cloud/event/ActionEventInterceptorTest.java +++ b/server/src/test/java/com/cloud/event/ActionEventInterceptorTest.java @@ -140,6 +140,7 @@ public class ActionEventInterceptorTest { } utils.init(); + CallContext.register(user, account); } /** @@ -152,6 +153,7 @@ public class ActionEventInterceptorTest { Mockito.when(configDao.getValue(Config.PublishActionEvent.key())).thenReturn("true"); componentContextMocked = Mockito.mockStatic(ComponentContext.class); Mockito.when(ComponentContext.getComponent(EventBus.class)).thenReturn(eventBus); + persistedEvents = new ArrayList<>(); //Needed for persist to actually set an ID that can be returned from the ActionEventUtils //methods. @@ -198,6 +200,7 @@ public class ActionEventInterceptorTest { utils.init(); componentContextMocked.close(); + CallContext.unregister(); } @Test @@ -228,11 +231,11 @@ public class ActionEventInterceptorTest { Object event = actionEventInterceptor.interceptStart(m, tester); Assert.assertNull(event); - Assert.assertEquals(persistedEvents.size(), 1); + Assert.assertEquals(1, persistedEvents.size()); EventVO eventVO = persistedEvents.get(0); - Assert.assertEquals(eventVO.getType(), EventTypes.EVENT_VM_START); - Assert.assertEquals(eventVO.getDescription(), "Starting VM"); - Assert.assertEquals(eventVO.getState(), com.cloud.event.Event.State.Started); + Assert.assertEquals(EventTypes.EVENT_VM_START, eventVO.getType()); + Assert.assertEquals(eventDescription, eventVO.getDescription()); + Assert.assertEquals(com.cloud.event.Event.State.Started, eventVO.getState()); } @Test @@ -241,12 +244,12 @@ public class ActionEventInterceptorTest { Method m = tester.getClass().getMethod("testMethod"); actionEventInterceptor.interceptComplete(m, tester, null); - Assert.assertEquals(persistedEvents.size(), 1); + Assert.assertEquals(1, persistedEvents.size()); EventVO eventVO = persistedEvents.get(0); - Assert.assertEquals(eventVO.getType(), eventType); + Assert.assertEquals(eventType, eventVO.getType()); Assert.assertTrue(eventVO.getDescription().endsWith(eventDescription)); - Assert.assertEquals(eventVO.getLevel(), EventVO.LEVEL_INFO); - Assert.assertEquals(eventVO.getState(), com.cloud.event.Event.State.Completed); + Assert.assertEquals(EventVO.LEVEL_INFO, eventVO.getLevel()); + Assert.assertEquals(com.cloud.event.Event.State.Completed, eventVO.getState()); } @Test @@ -255,17 +258,16 @@ public class ActionEventInterceptorTest { Method m = tester.getClass().getMethod("testMethod"); actionEventInterceptor.interceptException(m, tester, null); - Assert.assertEquals(persistedEvents.size(), 1); + Assert.assertEquals(1, persistedEvents.size()); EventVO eventVO = persistedEvents.get(0); - Assert.assertEquals(eventVO.getType(), eventType); + Assert.assertEquals(eventType, eventVO.getType()); Assert.assertTrue(eventVO.getDescription().endsWith(eventDescription)); - Assert.assertEquals(eventVO.getLevel(), EventVO.LEVEL_ERROR); - Assert.assertEquals(eventVO.getState(), com.cloud.event.Event.State.Completed); + Assert.assertEquals(EventVO.LEVEL_ERROR, eventVO.getLevel()); + Assert.assertEquals(com.cloud.event.Event.State.Completed, eventVO.getState()); } @Test public void testInterceptExceptionResource() throws NoSuchMethodException { - CallContext.register(user, account); Long resourceId = 1L; ApiCommandResourceType resourceType = ApiCommandResourceType.VirtualMachine; CallContext.current().setEventResourceId(resourceId); @@ -274,15 +276,14 @@ public class ActionEventInterceptorTest { Method m = tester.getClass().getMethod("testMethod"); actionEventInterceptor.interceptException(m, tester, null); - Assert.assertEquals(persistedEvents.size(), 1); + Assert.assertEquals(1, persistedEvents.size()); EventVO eventVO = persistedEvents.get(0); - Assert.assertEquals(eventVO.getType(), eventType); + Assert.assertEquals(eventType, eventVO.getType()); Assert.assertTrue(eventVO.getDescription().endsWith(eventDescription)); - Assert.assertEquals(eventVO.getLevel(), EventVO.LEVEL_ERROR); - Assert.assertEquals(eventVO.getState(), com.cloud.event.Event.State.Completed); - Assert.assertEquals(eventVO.getResourceId(), resourceId); - Assert.assertEquals(eventVO.getResourceType(), resourceType.toString()); - CallContext.unregister(); + Assert.assertEquals(EventVO.LEVEL_ERROR, eventVO.getLevel()); + Assert.assertEquals(com.cloud.event.Event.State.Completed, eventVO.getState()); + Assert.assertEquals(resourceId, eventVO.getResourceId()); + Assert.assertEquals(resourceType.toString(), eventVO.getResourceType()); } @Test