use of com.cloud.event.UsageEventUtils in project cloudstack by apache.
the class AccountManagerImplVolumeDeleteEventTest method cleanupUsageUtils.
@After
public // Restores static fields of the UsageEventUtil class to previous values.
void cleanupUsageUtils() throws ReflectiveOperationException, SecurityException {
UsageEventUtils utils = new UsageEventUtils();
for (String fieldName : oldFields.keySet()) {
Field f = UsageEventUtils.class.getDeclaredField(fieldName);
f.setAccessible(true);
f.set(utils, oldFields.get(fieldName));
}
Method method = UsageEventUtils.class.getDeclaredMethod("init");
method.setAccessible(true);
method.invoke(utils);
}
use of com.cloud.event.UsageEventUtils in project cloudstack by apache.
the class HypervisorTemplateAdapterTest method cleanupUsageUtils.
public void cleanupUsageUtils() {
UsageEventUtils utils = new UsageEventUtils();
for (String fieldName : oldFields.keySet()) {
try {
Field f = UsageEventUtils.class.getDeclaredField(fieldName);
f.setAccessible(true);
f.set(utils, oldFields.get(fieldName));
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
e.printStackTrace();
}
}
try {
Method method = UsageEventUtils.class.getDeclaredMethod("init");
method.setAccessible(true);
method.invoke(utils);
} catch (SecurityException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
use of com.cloud.event.UsageEventUtils in project cloudstack by apache.
the class HypervisorTemplateAdapterTest method setupUsageUtils.
public UsageEventUtils setupUsageUtils() throws EventBusException {
Mockito.when(_configDao.getValue(eq("publish.usage.events"))).thenReturn("true");
Mockito.when(_usageEventDao.persist(Mockito.any(UsageEventVO.class))).then(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
UsageEventVO vo = (UsageEventVO) invocation.getArguments()[0];
usageEvents.add(vo);
return null;
}
});
Mockito.when(_usageEventDao.listAll()).thenReturn(usageEvents);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Event event = (Event) invocation.getArguments()[0];
events.add(event);
return null;
}
}).when(_bus).publish(any(Event.class));
PowerMockito.mockStatic(ComponentContext.class);
when(ComponentContext.getComponent(eq(EventBus.class))).thenReturn(_bus);
UsageEventUtils utils = new UsageEventUtils();
Map<String, String> usageUtilsFields = new HashMap<String, String>();
usageUtilsFields.put("usageEventDao", "_usageEventDao");
usageUtilsFields.put("accountDao", "_accountDao");
usageUtilsFields.put("dcDao", "_dcDao");
usageUtilsFields.put("configDao", "_configDao");
for (String fieldName : usageUtilsFields.keySet()) {
try {
Field f = UsageEventUtils.class.getDeclaredField(fieldName);
f.setAccessible(true);
//Remember the old fields for cleanup later (see cleanupUsageUtils)
Field staticField = UsageEventUtils.class.getDeclaredField("s_" + fieldName);
staticField.setAccessible(true);
oldFields.put(f.getName(), staticField.get(null));
f.set(utils, this.getClass().getDeclaredField(usageUtilsFields.get(fieldName)).get(this));
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
e.printStackTrace();
}
}
try {
Method method = UsageEventUtils.class.getDeclaredMethod("init");
method.setAccessible(true);
method.invoke(utils);
} catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
return utils;
}
use of com.cloud.event.UsageEventUtils in project cloudstack by apache.
the class AccountManagerImplVolumeDeleteEventTest method setupUsageUtils.
// Configures the static fields of the UsageEventUtils class, Storing the
// previous values to be restored during the cleanup phase, to avoid
// interference with other unit tests.
protected UsageEventUtils setupUsageUtils() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
_usageEventDao = new MockUsageEventDao();
UsageEventUtils utils = new UsageEventUtils();
List<String> usageUtilsFields = new ArrayList<String>();
usageUtilsFields.add("usageEventDao");
usageUtilsFields.add("accountDao");
usageUtilsFields.add("dcDao");
usageUtilsFields.add("configDao");
for (String fieldName : usageUtilsFields) {
try {
Field f = UsageEventUtils.class.getDeclaredField(fieldName);
f.setAccessible(true);
Field staticField = UsageEventUtils.class.getDeclaredField("s_" + fieldName);
staticField.setAccessible(true);
oldFields.put(f.getName(), staticField.get(null));
f.set(utils, this.getClass().getSuperclass().getDeclaredField("_" + fieldName).get(this));
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
e.printStackTrace();
}
}
Method method;
method = UsageEventUtils.class.getDeclaredMethod("init");
method.setAccessible(true);
method.invoke(utils);
return utils;
}
Aggregations