Search in sources :

Example 1 with UsageEventUtils

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);
}
Also used : Field(java.lang.reflect.Field) Mockito.anyString(org.mockito.Mockito.anyString) Method(java.lang.reflect.Method) UsageEventUtils(com.cloud.event.UsageEventUtils) After(org.junit.After)

Example 2 with UsageEventUtils

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();
    }
}
Also used : Field(java.lang.reflect.Field) Method(java.lang.reflect.Method) UsageEventUtils(com.cloud.event.UsageEventUtils) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with UsageEventUtils

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;
}
Also used : HashMap(java.util.HashMap) UsageEventVO(com.cloud.event.UsageEventVO) EventBus(org.apache.cloudstack.framework.events.EventBus) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Event(org.apache.cloudstack.framework.events.Event) UsageEventUtils(com.cloud.event.UsageEventUtils)

Example 4 with UsageEventUtils

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;
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) Mockito.anyString(org.mockito.Mockito.anyString) Method(java.lang.reflect.Method) UsageEventUtils(com.cloud.event.UsageEventUtils)

Aggregations

UsageEventUtils (com.cloud.event.UsageEventUtils)4 Field (java.lang.reflect.Field)4 Method (java.lang.reflect.Method)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Mockito.anyString (org.mockito.Mockito.anyString)2 UsageEventVO (com.cloud.event.UsageEventVO)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Event (org.apache.cloudstack.framework.events.Event)1 EventBus (org.apache.cloudstack.framework.events.EventBus)1 After (org.junit.After)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1