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>
This commit is contained in:
Abhishek Kumar 2024-07-12 18:40:55 +05:30 committed by GitHub
parent 32cc1d46a5
commit c71ab9598e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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