Search in sources :

Example 6 with Event

use of org.apache.cloudstack.framework.events.Event in project cloudstack by apache.

the class HypervisorTemplateAdapterTest method testEmitDeleteEventUuid.

@Test
public void testEmitDeleteEventUuid() throws InterruptedException, ExecutionException, EventBusException {
    //All the mocks required for this test to work.
    ImageStoreEntity store = mock(ImageStoreEntity.class);
    when(store.getId()).thenReturn(1l);
    when(store.getDataCenterId()).thenReturn(1l);
    when(store.getName()).thenReturn("Test Store");
    TemplateDataStoreVO dataStoreVO = mock(TemplateDataStoreVO.class);
    when(dataStoreVO.getDownloadState()).thenReturn(Status.DOWNLOADED);
    TemplateInfo info = mock(TemplateInfo.class);
    when(info.getDataStore()).thenReturn(store);
    VMTemplateVO template = mock(VMTemplateVO.class);
    when(template.getId()).thenReturn(1l);
    when(template.getName()).thenReturn("Test Template");
    when(template.getFormat()).thenReturn(ImageFormat.QCOW2);
    when(template.getAccountId()).thenReturn(1l);
    //TODO possibly return this from method for comparison, if things work how i want
    when(template.getUuid()).thenReturn("Test UUID");
    TemplateProfile profile = mock(TemplateProfile.class);
    when(profile.getTemplate()).thenReturn(template);
    when(profile.getZoneId()).thenReturn(1l);
    TemplateApiResult result = mock(TemplateApiResult.class);
    when(result.isSuccess()).thenReturn(true);
    when(result.isFailed()).thenReturn(false);
    @SuppressWarnings("unchecked") AsyncCallFuture<TemplateApiResult> future = mock(AsyncCallFuture.class);
    when(future.get()).thenReturn(result);
    AccountVO acct = mock(AccountVO.class);
    when(acct.getId()).thenReturn(1l);
    when(acct.getDomainId()).thenReturn(1l);
    when(_templateMgr.getImageStoreByTemplate(anyLong(), anyLong())).thenReturn(Collections.singletonList((DataStore) store));
    when(_templateStoreDao.listByTemplateStore(anyLong(), anyLong())).thenReturn(Collections.singletonList(dataStoreVO));
    when(_dataFactory.getTemplate(anyLong(), any(DataStore.class))).thenReturn(info);
    when(_dataFactory.listTemplateOnCache(anyLong())).thenReturn(Collections.singletonList(info));
    when(_templateService.deleteTemplateAsync(any(TemplateInfo.class))).thenReturn(future);
    when(_accountDao.findById(anyLong())).thenReturn(acct);
    when(_accountDao.findByIdIncludingRemoved(anyLong())).thenReturn(acct);
    //Test actually begins here.
    setupUsageUtils();
    _adapter.delete(profile);
    Assert.assertNotNull(usageEvents);
    Assert.assertNotNull(events);
    Assert.assertEquals(1, events.size());
    Event event = events.get(0);
    Assert.assertNotNull(event);
    Assert.assertNotNull(event.getResourceType());
    Assert.assertEquals(VirtualMachineTemplate.class.getName(), event.getResourceType());
    Assert.assertNotNull(event.getResourceUUID());
    Assert.assertEquals("Test UUID", event.getResourceUUID());
    Assert.assertEquals(EventTypes.EVENT_TEMPLATE_DELETE, event.getEventType());
    cleanupUsageUtils();
}
Also used : TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) VMTemplateVO(com.cloud.storage.VMTemplateVO) Event(org.apache.cloudstack.framework.events.Event) ImageStoreEntity(org.apache.cloudstack.storage.image.datastore.ImageStoreEntity) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) AccountVO(com.cloud.user.AccountVO) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult) TemplateProfile(com.cloud.storage.TemplateProfile) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with Event

use of org.apache.cloudstack.framework.events.Event in project cloudstack by apache.

the class ActionEventUtilsTest method testPopulateFirstClassEntities.

@Test
public void testPopulateFirstClassEntities() {
    AccountVO account = new AccountVO("testaccount", 1L, "networkdomain", (short) 0, "uuid");
    account.setId(ACCOUNT_ID);
    UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    Mockito.when(accountDao.findById(ACCOUNT_ID)).thenReturn(account);
    Mockito.when(userDao.findById(USER_ID)).thenReturn(user);
    CallContext.register(user, account);
    //Inject some entity UUIDs into the call context
    String instanceUuid = UUID.randomUUID().toString();
    String ipUuid = UUID.randomUUID().toString();
    CallContext.current().putContextParameter(VirtualMachine.class, instanceUuid);
    CallContext.current().putContextParameter(IpAddress.class, ipUuid);
    ActionEventUtils.onActionEvent(USER_ID, ACCOUNT_ID, account.getDomainId(), "StaticNat", "Test event");
    //Assertions
    Assert.assertNotEquals(publishedEvents.size(), 0);
    Assert.assertEquals(publishedEvents.size(), 1);
    Event event = publishedEvents.get(0);
    Assert.assertNotNull(event.getDescription());
    JsonObject json = new JsonParser().parse(event.getDescription()).getAsJsonObject();
    Assert.assertTrue(json.has("VirtualMachine"));
    Assert.assertTrue(json.has("IpAddress"));
    Assert.assertEquals(json.get("VirtualMachine").getAsString(), instanceUuid);
    Assert.assertEquals(json.get("IpAddress").getAsString(), ipUuid);
    CallContext.unregister();
}
Also used : UserVO(com.cloud.user.UserVO) Event(org.apache.cloudstack.framework.events.Event) JsonObject(com.google.gson.JsonObject) AccountVO(com.cloud.user.AccountVO) JsonParser(com.google.gson.JsonParser) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Event (org.apache.cloudstack.framework.events.Event)7 Test (org.junit.Test)4 EventSubscriber (org.apache.cloudstack.framework.events.EventSubscriber)3 AccountVO (com.cloud.user.AccountVO)2 HashMap (java.util.HashMap)2 UUID (java.util.UUID)2 EventBus (org.apache.cloudstack.framework.events.EventBus)2 EventBusException (org.apache.cloudstack.framework.events.EventBusException)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 DataCenterVO (com.cloud.dc.DataCenterVO)1 UsageEventUtils (com.cloud.event.UsageEventUtils)1 UsageEventVO (com.cloud.event.UsageEventVO)1 TemplateProfile (com.cloud.storage.TemplateProfile)1 VMTemplateVO (com.cloud.storage.VMTemplateVO)1 Account (com.cloud.user.Account)1 UserVO (com.cloud.user.UserVO)1 Ternary (com.cloud.utils.Ternary)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1