Skip to content

Commit aba797c

Browse files
committed
add unit tests
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent c4657a9 commit aba797c

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

engine/orchestration/src/main/java/com/cloud/vm/VirtualMachinePowerStateSyncImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public void processHostVmStatePingReport(long hostId, Map<String, HostVmStateRep
7777
processReport(hostId, translatedInfo, force);
7878
}
7979

80-
private void updateAndPublishVmPowerStates(long hostId, Map<Long, VirtualMachine.PowerState> instancePowerStates,
81-
Date updateTime) {
80+
protected void updateAndPublishVmPowerStates(long hostId, Map<Long, VirtualMachine.PowerState> instancePowerStates,
81+
Date updateTime) {
8282
if (instancePowerStates.isEmpty()) {
8383
return;
8484
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.cloud.vm;
2+
3+
import java.util.Date;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import org.apache.cloudstack.framework.messagebus.MessageBus;
8+
import org.apache.cloudstack.framework.messagebus.PublishScope;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.mockito.InjectMocks;
13+
import org.mockito.Mock;
14+
import org.mockito.Mockito;
15+
import org.mockito.junit.MockitoJUnitRunner;
16+
17+
import com.cloud.host.HostVO;
18+
import com.cloud.host.dao.HostDao;
19+
import com.cloud.vm.dao.VMInstanceDao;
20+
21+
@RunWith(MockitoJUnitRunner.class)
22+
public class VirtualMachinePowerStateSyncImplTest {
23+
@Mock
24+
MessageBus messageBus;
25+
@Mock
26+
VMInstanceDao instanceDao;
27+
@Mock
28+
HostDao hostDao;
29+
30+
@InjectMocks
31+
VirtualMachinePowerStateSyncImpl virtualMachinePowerStateSync = new VirtualMachinePowerStateSyncImpl();
32+
33+
@Before
34+
public void setup() {
35+
Mockito.when(instanceDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(VMInstanceVO.class));
36+
Mockito.when(hostDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(HostVO.class));
37+
}
38+
39+
@Test
40+
public void test_updateAndPublishVmPowerStates_emptyStates() {
41+
virtualMachinePowerStateSync.updateAndPublishVmPowerStates(1L, new HashMap<>(), new Date());
42+
Mockito.verify(instanceDao, Mockito.never()).updatePowerState(Mockito.anyMap(), Mockito.anyLong(),
43+
Mockito.any(Date.class));
44+
}
45+
46+
@Test
47+
public void test_updateAndPublishVmPowerStates_moreNotUpdated() {
48+
Map<Long, VirtualMachine.PowerState> powerStates = new HashMap<>();
49+
powerStates.put(1L, VirtualMachine.PowerState.PowerOff);
50+
Map<Long, VirtualMachine.PowerState> notUpdated = new HashMap<>(powerStates);
51+
notUpdated.put(2L, VirtualMachine.PowerState.PowerOn);
52+
Mockito.when(instanceDao.updatePowerState(Mockito.anyMap(), Mockito.anyLong(),
53+
Mockito.any(Date.class))).thenReturn(notUpdated);
54+
virtualMachinePowerStateSync.updateAndPublishVmPowerStates(1L, powerStates, new Date());
55+
Mockito.verify(messageBus, Mockito.never()).publish(Mockito.nullable(String.class), Mockito.anyString(),
56+
Mockito.any(PublishScope.class), Mockito.anyLong());
57+
}
58+
59+
@Test
60+
public void test_updateAndPublishVmPowerStates_allUpdated() {
61+
Map<Long, VirtualMachine.PowerState> powerStates = new HashMap<>();
62+
powerStates.put(1L, VirtualMachine.PowerState.PowerOff);
63+
Mockito.when(instanceDao.updatePowerState(Mockito.anyMap(), Mockito.anyLong(),
64+
Mockito.any(Date.class))).thenReturn(new HashMap<>());
65+
virtualMachinePowerStateSync.updateAndPublishVmPowerStates(1L, powerStates, new Date());
66+
Mockito.verify(messageBus, Mockito.times(1)).publish(null,
67+
VirtualMachineManager.Topics.VM_POWER_STATE,
68+
PublishScope.GLOBAL,
69+
1L);
70+
}
71+
72+
@Test
73+
public void test_updateAndPublishVmPowerStates_partialUpdated() {
74+
Map<Long, VirtualMachine.PowerState> powerStates = new HashMap<>();
75+
powerStates.put(1L, VirtualMachine.PowerState.PowerOn);
76+
powerStates.put(2L, VirtualMachine.PowerState.PowerOff);
77+
Map<Long, VirtualMachine.PowerState> notUpdated = new HashMap<>();
78+
notUpdated.put(2L, VirtualMachine.PowerState.PowerOff);
79+
Mockito.when(instanceDao.updatePowerState(Mockito.anyMap(), Mockito.anyLong(),
80+
Mockito.any(Date.class))).thenReturn(notUpdated);
81+
virtualMachinePowerStateSync.updateAndPublishVmPowerStates(1L, powerStates, new Date());
82+
Mockito.verify(messageBus, Mockito.times(1)).publish(null,
83+
VirtualMachineManager.Topics.VM_POWER_STATE,
84+
PublishScope.GLOBAL,
85+
1L);
86+
Mockito.verify(messageBus, Mockito.never()).publish(null,
87+
VirtualMachineManager.Topics.VM_POWER_STATE,
88+
PublishScope.GLOBAL,
89+
2L);
90+
}
91+
}

0 commit comments

Comments
 (0)