Skip to content

Commit d39c5e8

Browse files
committed
replace statics
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 91a71a6 commit d39c5e8

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,29 @@
1818

1919
import java.io.UnsupportedEncodingException;
2020
import java.util.List;
21-
import java.util.concurrent.Callable;
22-
import java.util.concurrent.ExecutorService;
23-
import java.util.concurrent.Executors;
24-
import java.util.concurrent.Future;
2521

2622
import javax.mail.MessagingException;
2723

2824
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
25+
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
2926
import org.apache.cloudstack.utils.mailing.SMTPMailSender;
3027
import org.apache.logging.log4j.Logger;
3128
import org.junit.Assert;
3229
import org.junit.Test;
3330
import org.junit.runner.RunWith;
3431
import org.mockito.InjectMocks;
3532
import org.mockito.Mock;
36-
import org.mockito.MockedStatic;
3733
import org.mockito.Mockito;
3834
import org.mockito.Spy;
3935
import org.mockito.junit.MockitoJUnitRunner;
4036

4137
import com.cloud.alert.dao.AlertDao;
38+
import com.cloud.capacity.Capacity;
4239
import com.cloud.capacity.CapacityManager;
4340
import com.cloud.host.Host;
41+
import com.cloud.host.HostVO;
4442
import com.cloud.host.dao.HostDao;
43+
import com.cloud.storage.StorageManager;
4544

4645
@RunWith(MockitoJUnitRunner.class)
4746
public class AlertManagerImplTest {
@@ -65,6 +64,9 @@ public class AlertManagerImplTest {
6564
@Mock
6665
CapacityManager capacityManager;
6766

67+
@Mock
68+
StorageManager storageManager;
69+
6870
@Mock
6971
Logger loggerMock;
7072

@@ -119,27 +121,30 @@ public void sendAlertTestWarnLogging() {
119121
@Test
120122
public void testRecalculateHostCapacities() {
121123
List<Long> mockHostIds = List.of(1L, 2L, 3L);
122-
try (MockedStatic<Executors> ignored = Mockito.mockStatic(Executors.class)) {
123-
Mockito.when(hostDao.listIdsByType(Host.Type.Routing)).thenReturn(mockHostIds);
124-
ExecutorService executorService = Mockito.mock(ExecutorService.class);
125-
Mockito.when(executorService.submit(Mockito.any(Callable.class))).thenReturn(Mockito.mock(Future.class));
126-
Mockito.when(Executors.newFixedThreadPool(Mockito.anyInt())).thenReturn(executorService);
127-
alertManagerImplMock.recalculateHostCapacities();
128-
Mockito.verify(executorService, Mockito.times(1)).shutdown();
129-
}
124+
Mockito.when(hostDao.listIdsByType(Host.Type.Routing)).thenReturn(mockHostIds);
125+
HostVO host = Mockito.mock(HostVO.class);
126+
Mockito.when(hostDao.findById(Mockito.anyLong())).thenReturn(host);
127+
Mockito.doNothing().when(capacityManager).updateCapacityForHost(host);
128+
alertManagerImplMock.recalculateHostCapacities();
129+
Mockito.verify(hostDao, Mockito.times(3)).findById(Mockito.anyLong());
130+
Mockito.verify(capacityManager, Mockito.times(3)).updateCapacityForHost(host);
130131
}
131132

132133
@Test
133134
public void testRecalculateStorageCapacities() {
134135
List<Long> mockPoolIds = List.of(101L, 102L, 103L);
135-
try (MockedStatic<Executors> ignored = Mockito.mockStatic(Executors.class)) {
136-
Mockito.when(primaryDataStoreDao.listAllIds()).thenReturn(mockPoolIds);
137-
ExecutorService executorService = Mockito.mock(ExecutorService.class);
138-
Mockito.when(executorService.submit(Mockito.any(Callable.class))).thenReturn(Mockito.mock(Future.class));
139-
Mockito.when(Executors.newFixedThreadPool(Mockito.anyInt())).thenReturn(executorService);
140-
alertManagerImplMock.recalculateStorageCapacities();
141-
Mockito.verify(executorService, Mockito.times(1)).shutdown();
142-
}
136+
Mockito.when(primaryDataStoreDao.listAllIds()).thenReturn(mockPoolIds);
137+
StoragePoolVO sharedPool = Mockito.mock(StoragePoolVO.class);
138+
Mockito.when(sharedPool.isShared()).thenReturn(true);
139+
Mockito.when(primaryDataStoreDao.findById(mockPoolIds.get(0))).thenReturn(sharedPool);
140+
Mockito.when(primaryDataStoreDao.findById(mockPoolIds.get(1))).thenReturn(sharedPool);
141+
StoragePoolVO nonSharedPool = Mockito.mock(StoragePoolVO.class);
142+
Mockito.when(nonSharedPool.isShared()).thenReturn(false);
143+
Mockito.when(primaryDataStoreDao.findById(mockPoolIds.get(2))).thenReturn(nonSharedPool);
144+
Mockito.when(capacityManager.getAllocatedPoolCapacity(sharedPool, null)).thenReturn(10L);
145+
Mockito.when(capacityManager.getAllocatedPoolCapacity(nonSharedPool, null)).thenReturn(20L);
146+
alertManagerImplMock.recalculateStorageCapacities();
147+
Mockito.verify(storageManager, Mockito.times(2)).createCapacityEntry(sharedPool, Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED, 10L);
148+
Mockito.verify(storageManager, Mockito.times(1)).createCapacityEntry(nonSharedPool, Capacity.CAPACITY_TYPE_LOCAL_STORAGE, 20L);
143149
}
144-
145150
}

0 commit comments

Comments
 (0)