Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import javax.inject.Inject;

import com.cloud.network.Network;

import org.apache.cloudstack.context.CallContext;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;

Expand Down Expand Up @@ -214,7 +216,9 @@ private static void publishUsageEvent(String usageEventType, Long accountId, Lon
return; // no provider is configured to provide events distributor, so just return
}

Account account = s_accountDao.findById(accountId);
Account account = (accountId == CallContext.current().getCallingAccountId())
? CallContext.current().getCallingAccount()
: s_accountDao.findById(accountId);
DataCenterVO dc = s_dcDao.findById(zoneId);

// if account has been deleted, this might be called during cleanup of resources and results in null pointer
Expand Down
4 changes: 3 additions & 1 deletion server/src/main/java/com/cloud/event/ActionEventUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ private static void publishOnEventBus(Event eventRecord, long userId, long accou

Map<String, String> eventDescription = new HashMap<String, String>();
Project project = s_projectDao.findByProjectAccountId(accountId);
Account account = s_accountDao.findById(accountId);
Account account = (accountId == CallContext.current().getCallingAccountId())
? CallContext.current().getCallingAccount()
: s_accountDao.findById(accountId);
User user = s_userDao.findById(userId);
// if account has been deleted, this might be called during cleanup of resources and results in null pointer
if (account == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,6 @@ public EventVO answer(InvocationOnMock invocation) throws Throwable {
user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone",
UUID.randomUUID().toString(), User.Source.UNKNOWN);
CallContext.register(user, account);

Mockito.when(accountDao.findById(ACCOUNT_ID)).thenReturn(account);
}

/**
Expand Down
29 changes: 2 additions & 27 deletions server/src/test/java/com/cloud/event/ActionEventUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,8 @@ public EventVO answer(InvocationOnMock invocation) throws Throwable {
account.setId(ACCOUNT_ID);
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);
}

/**
Expand All @@ -204,12 +203,11 @@ public void teardown() {
utils.init();

componentContextMocked.close();
CallContext.unregister();
}

@Test
public void testPopulateFirstClassEntities() {
CallContext.register(user, account);

//Inject some entity UUIDs into the call context
String instanceUuid = UUID.randomUUID().toString();
String ipUuid = UUID.randomUUID().toString();
Expand All @@ -231,8 +229,6 @@ public void testPopulateFirstClassEntities() {
Assert.assertTrue(json.has("IpAddress"));
Assert.assertEquals(json.get("VirtualMachine").getAsString(), instanceUuid);
Assert.assertEquals(json.get("IpAddress").getAsString(), ipUuid);

CallContext.unregister();
}

private void checkEventResourceAndUnregisterContext(Long resourceId, String resourceUuid, String resourceType) {
Expand All @@ -248,14 +244,10 @@ private void checkEventResourceAndUnregisterContext(Long resourceId, String reso
EventVO eventVO = persistedEvents.get(0);
Assert.assertEquals(eventVO.getResourceType(), resourceType);
Assert.assertEquals(eventVO.getResourceId(), resourceId);

CallContext.unregister();
}

@Test
public void testPublishedEventResource() {
CallContext.register(user, account);

final Long resourceId = 1L;
final String resourceType = ApiCommandResourceType.VirtualMachine.toString();
final String resourceUuid = UUID.randomUUID().toString();
Expand All @@ -270,8 +262,6 @@ public void testPublishedEventResource() {

@Test
public void testPublishedEventResourceWithCallContext() {
CallContext.register(user, account);

final Long resourceId = 1L;
final String resourceType = ApiCommandResourceType.VirtualMachine.toString();
final String resourceUuid = UUID.randomUUID().toString();
Expand All @@ -287,8 +277,6 @@ public void testPublishedEventResourceWithCallContext() {

@Test
public void testScheduledEvent() {
CallContext.register(user, account);

final Long resourceId = 1L;
final String resourceType = ApiCommandResourceType.VirtualMachine.toString();
final String resourceUuid = UUID.randomUUID().toString();
Expand All @@ -304,14 +292,10 @@ public void testScheduledEvent() {
Assert.assertEquals(persistedEvents.size(), 1);
EventVO eventVO = persistedEvents.get(0);
Assert.assertEquals(eventVO.getState(), com.cloud.event.Event.State.Scheduled);

CallContext.unregister();
}

@Test
public void testCreatedEvent() {
CallContext.register(user, account);

final Long resourceId = 1L;
final String resourceType = ApiCommandResourceType.VirtualMachine.toString();
final String resourceUuid = UUID.randomUUID().toString();
Expand All @@ -327,14 +311,10 @@ public void testCreatedEvent() {
Assert.assertEquals(persistedEvents.size(), 1);
EventVO eventVO = persistedEvents.get(0);
Assert.assertEquals(eventVO.getState(), com.cloud.event.Event.State.Created);

CallContext.unregister();
}

@Test
public void testNestedEvent() {
CallContext.register(user, account);

final Long resourceId = 1L;
final String resourceType = ApiCommandResourceType.VirtualMachine.toString();
final String resourceUuid = UUID.randomUUID().toString();
Expand All @@ -344,14 +324,10 @@ public void testNestedEvent() {
Assert.assertEquals(persistedEvents.size(), 1);
EventVO eventVO = persistedEvents.get(0);
Assert.assertEquals(eventVO.getState(), com.cloud.event.Event.State.Started);

CallContext.unregister();
}

@Test
public void testSnapshotEventResource() {
CallContext.register(user, account);

final Long snapshotResourceId = 100L;
final String snapshotResourceType = ApiCommandResourceType.Snapshot.toString();
final String snapshotResourceUuid = UUID.randomUUID().toString();
Expand All @@ -366,7 +342,6 @@ public void testSnapshotEventResource() {

@Test
public void testVmSnapshotEventResource() {
CallContext.register(user, account);

final Long vmSnapshotResourceId = 100L;
final String vmSnapshotResourceType = ApiCommandResourceType.VmSnapshot.toString();
Expand Down
Loading