mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
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:
parent
32cc1d46a5
commit
c71ab9598e
@ -140,6 +140,7 @@ public class ActionEventInterceptorTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
utils.init();
|
utils.init();
|
||||||
|
CallContext.register(user, account);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -152,6 +153,7 @@ public class ActionEventInterceptorTest {
|
|||||||
Mockito.when(configDao.getValue(Config.PublishActionEvent.key())).thenReturn("true");
|
Mockito.when(configDao.getValue(Config.PublishActionEvent.key())).thenReturn("true");
|
||||||
componentContextMocked = Mockito.mockStatic(ComponentContext.class);
|
componentContextMocked = Mockito.mockStatic(ComponentContext.class);
|
||||||
Mockito.when(ComponentContext.getComponent(EventBus.class)).thenReturn(eventBus);
|
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
|
//Needed for persist to actually set an ID that can be returned from the ActionEventUtils
|
||||||
//methods.
|
//methods.
|
||||||
@ -198,6 +200,7 @@ public class ActionEventInterceptorTest {
|
|||||||
utils.init();
|
utils.init();
|
||||||
|
|
||||||
componentContextMocked.close();
|
componentContextMocked.close();
|
||||||
|
CallContext.unregister();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -228,11 +231,11 @@ public class ActionEventInterceptorTest {
|
|||||||
Object event = actionEventInterceptor.interceptStart(m, tester);
|
Object event = actionEventInterceptor.interceptStart(m, tester);
|
||||||
Assert.assertNull(event);
|
Assert.assertNull(event);
|
||||||
|
|
||||||
Assert.assertEquals(persistedEvents.size(), 1);
|
Assert.assertEquals(1, persistedEvents.size());
|
||||||
EventVO eventVO = persistedEvents.get(0);
|
EventVO eventVO = persistedEvents.get(0);
|
||||||
Assert.assertEquals(eventVO.getType(), EventTypes.EVENT_VM_START);
|
Assert.assertEquals(EventTypes.EVENT_VM_START, eventVO.getType());
|
||||||
Assert.assertEquals(eventVO.getDescription(), "Starting VM");
|
Assert.assertEquals(eventDescription, eventVO.getDescription());
|
||||||
Assert.assertEquals(eventVO.getState(), com.cloud.event.Event.State.Started);
|
Assert.assertEquals(com.cloud.event.Event.State.Started, eventVO.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -241,12 +244,12 @@ public class ActionEventInterceptorTest {
|
|||||||
Method m = tester.getClass().getMethod("testMethod");
|
Method m = tester.getClass().getMethod("testMethod");
|
||||||
actionEventInterceptor.interceptComplete(m, tester, null);
|
actionEventInterceptor.interceptComplete(m, tester, null);
|
||||||
|
|
||||||
Assert.assertEquals(persistedEvents.size(), 1);
|
Assert.assertEquals(1, persistedEvents.size());
|
||||||
EventVO eventVO = persistedEvents.get(0);
|
EventVO eventVO = persistedEvents.get(0);
|
||||||
Assert.assertEquals(eventVO.getType(), eventType);
|
Assert.assertEquals(eventType, eventVO.getType());
|
||||||
Assert.assertTrue(eventVO.getDescription().endsWith(eventDescription));
|
Assert.assertTrue(eventVO.getDescription().endsWith(eventDescription));
|
||||||
Assert.assertEquals(eventVO.getLevel(), EventVO.LEVEL_INFO);
|
Assert.assertEquals(EventVO.LEVEL_INFO, eventVO.getLevel());
|
||||||
Assert.assertEquals(eventVO.getState(), com.cloud.event.Event.State.Completed);
|
Assert.assertEquals(com.cloud.event.Event.State.Completed, eventVO.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -255,17 +258,16 @@ public class ActionEventInterceptorTest {
|
|||||||
Method m = tester.getClass().getMethod("testMethod");
|
Method m = tester.getClass().getMethod("testMethod");
|
||||||
actionEventInterceptor.interceptException(m, tester, null);
|
actionEventInterceptor.interceptException(m, tester, null);
|
||||||
|
|
||||||
Assert.assertEquals(persistedEvents.size(), 1);
|
Assert.assertEquals(1, persistedEvents.size());
|
||||||
EventVO eventVO = persistedEvents.get(0);
|
EventVO eventVO = persistedEvents.get(0);
|
||||||
Assert.assertEquals(eventVO.getType(), eventType);
|
Assert.assertEquals(eventType, eventVO.getType());
|
||||||
Assert.assertTrue(eventVO.getDescription().endsWith(eventDescription));
|
Assert.assertTrue(eventVO.getDescription().endsWith(eventDescription));
|
||||||
Assert.assertEquals(eventVO.getLevel(), EventVO.LEVEL_ERROR);
|
Assert.assertEquals(EventVO.LEVEL_ERROR, eventVO.getLevel());
|
||||||
Assert.assertEquals(eventVO.getState(), com.cloud.event.Event.State.Completed);
|
Assert.assertEquals(com.cloud.event.Event.State.Completed, eventVO.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInterceptExceptionResource() throws NoSuchMethodException {
|
public void testInterceptExceptionResource() throws NoSuchMethodException {
|
||||||
CallContext.register(user, account);
|
|
||||||
Long resourceId = 1L;
|
Long resourceId = 1L;
|
||||||
ApiCommandResourceType resourceType = ApiCommandResourceType.VirtualMachine;
|
ApiCommandResourceType resourceType = ApiCommandResourceType.VirtualMachine;
|
||||||
CallContext.current().setEventResourceId(resourceId);
|
CallContext.current().setEventResourceId(resourceId);
|
||||||
@ -274,15 +276,14 @@ public class ActionEventInterceptorTest {
|
|||||||
Method m = tester.getClass().getMethod("testMethod");
|
Method m = tester.getClass().getMethod("testMethod");
|
||||||
actionEventInterceptor.interceptException(m, tester, null);
|
actionEventInterceptor.interceptException(m, tester, null);
|
||||||
|
|
||||||
Assert.assertEquals(persistedEvents.size(), 1);
|
Assert.assertEquals(1, persistedEvents.size());
|
||||||
EventVO eventVO = persistedEvents.get(0);
|
EventVO eventVO = persistedEvents.get(0);
|
||||||
Assert.assertEquals(eventVO.getType(), eventType);
|
Assert.assertEquals(eventType, eventVO.getType());
|
||||||
Assert.assertTrue(eventVO.getDescription().endsWith(eventDescription));
|
Assert.assertTrue(eventVO.getDescription().endsWith(eventDescription));
|
||||||
Assert.assertEquals(eventVO.getLevel(), EventVO.LEVEL_ERROR);
|
Assert.assertEquals(EventVO.LEVEL_ERROR, eventVO.getLevel());
|
||||||
Assert.assertEquals(eventVO.getState(), com.cloud.event.Event.State.Completed);
|
Assert.assertEquals(com.cloud.event.Event.State.Completed, eventVO.getState());
|
||||||
Assert.assertEquals(eventVO.getResourceId(), resourceId);
|
Assert.assertEquals(resourceId, eventVO.getResourceId());
|
||||||
Assert.assertEquals(eventVO.getResourceType(), resourceType.toString());
|
Assert.assertEquals(resourceType.toString(), eventVO.getResourceType());
|
||||||
CallContext.unregister();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user