2424import static org .mockito .ArgumentMatchers .anyLong ;
2525import static org .mockito .ArgumentMatchers .eq ;
2626import static org .mockito .Mockito .mock ;
27- import static org .mockito .Mockito .times ;
28- import static org .mockito .Mockito .verify ;
27+ import static org .mockito .Mockito .mockStatic ;
2928import static org .mockito .Mockito .when ;
3029import static org .mockito .MockitoAnnotations .initMocks ;
3130
4039import org .apache .cloudstack .engine .subsystem .api .storage .HypervisorHostListener ;
4140import org .apache .cloudstack .engine .subsystem .api .storage .PrimaryDataStore ;
4241import org .apache .cloudstack .engine .subsystem .api .storage .ZoneScope ;
43- import org .apache .cloudstack .storage .datastore .client .ScaleIOGatewayClient ;
4442import org .apache .cloudstack .storage .datastore .client .ScaleIOGatewayClientConnectionPool ;
4543import org .apache .cloudstack .storage .datastore .client .ScaleIOGatewayClientImpl ;
4644import org .apache .cloudstack .storage .datastore .db .PrimaryDataStoreDao ;
4745import org .apache .cloudstack .storage .datastore .db .StoragePoolDetailsDao ;
4846import org .apache .cloudstack .storage .datastore .db .StoragePoolVO ;
49- import org .apache .cloudstack .storage .datastore .provider .ScaleIOHostListener ;
5047import org .apache .cloudstack .storage .datastore .util .ScaleIOUtil ;
5148import org .apache .cloudstack .storage .volume .datastore .PrimaryDataStoreHelper ;
5249import org .junit .Before ;
5350import org .junit .Test ;
5451import org .junit .runner .RunWith ;
5552import org .mockito .InjectMocks ;
5653import org .mockito .Mock ;
54+ import org .mockito .MockedStatic ;
5755import org .mockito .Mockito ;
5856import org .mockito .Spy ;
59- import org .powermock .api .mockito .PowerMockito ;
60- import org .powermock .core .classloader .annotations .PrepareForTest ;
61- import org .powermock .modules .junit4 .PowerMockRunner ;
57+ import org .mockito .junit .MockitoJUnitRunner ;
6258
63- import com .cloud .agent .AgentManager ;
64- import com .cloud .agent .api .ModifyStoragePoolAnswer ;
65- import com .cloud .agent .api .ModifyStoragePoolCommand ;
6659import com .cloud .host .Host ;
6760import com .cloud .host .HostVO ;
6861import com .cloud .host .Status ;
69- import com .cloud .host .dao .HostDao ;
7062import com .cloud .hypervisor .Hypervisor ;
7163import com .cloud .resource .ResourceManager ;
7264import com .cloud .resource .ResourceState ;
8072import com .cloud .storage .dao .StoragePoolHostDao ;
8173import com .cloud .template .TemplateManager ;
8274import com .cloud .utils .exception .CloudRuntimeException ;
75+ import org .springframework .test .util .ReflectionTestUtils ;
8376
84- @ PrepareForTest (ScaleIOGatewayClient .class )
85- @ RunWith (PowerMockRunner .class )
77+ @ RunWith (MockitoJUnitRunner .class )
8678public class ScaleIOPrimaryDataStoreLifeCycleTest {
8779
8880 @ Mock
@@ -96,8 +88,6 @@ public class ScaleIOPrimaryDataStoreLifeCycleTest {
9688 @ Mock
9789 private StoragePoolAutomation storagePoolAutomation ;
9890 @ Mock
99- private HostDao hostDao ;
100- @ Mock
10191 private StoragePoolHostDao storagePoolHostDao ;
10292 @ Mock
10393 private DataStoreProviderManager dataStoreProviderMgr ;
@@ -109,35 +99,32 @@ public class ScaleIOPrimaryDataStoreLifeCycleTest {
10999 private PrimaryDataStore store ;
110100 @ Mock
111101 private TemplateManager templateMgr ;
112- @ Mock
113- private AgentManager agentMgr ;
114- @ Mock
115- ModifyStoragePoolAnswer answer ;
116102
117- @ Spy
118103 @ InjectMocks
119104 private StorageManager storageMgr = new StorageManagerImpl ();
120105
121106 @ Spy
122- @ InjectMocks
123- private HypervisorHostListener hostListener = new ScaleIOHostListener ();
107+ private HypervisorHostListener hostListener ;
124108
125109 @ InjectMocks
126110 private ScaleIOPrimaryDataStoreLifeCycle scaleIOPrimaryDataStoreLifeCycleTest ;
127111
128112 @ Before
129113 public void setUp () {
130114 initMocks (this );
115+ ReflectionTestUtils .setField (scaleIOPrimaryDataStoreLifeCycleTest , "storageMgr" , storageMgr );
131116 }
132117
133118 @ Test
134119 public void testAttachZone () throws Exception {
135120 final DataStore dataStore = mock (DataStore .class );
136121 when (dataStore .getId ()).thenReturn (1L );
137122
138- PowerMockito . mockStatic (ScaleIOGatewayClient .class );
123+ MockedStatic < ScaleIOGatewayClientConnectionPool > scaleIOGatewayClientConnectionPoolMocked = mockStatic (ScaleIOGatewayClientConnectionPool .class );
139124 ScaleIOGatewayClientImpl client = mock (ScaleIOGatewayClientImpl .class );
140- when (ScaleIOGatewayClientConnectionPool .getInstance ().getClient (1L , storagePoolDetailsDao )).thenReturn (client );
125+ ScaleIOGatewayClientConnectionPool pool = mock (ScaleIOGatewayClientConnectionPool .class );
126+ scaleIOGatewayClientConnectionPoolMocked .when (() -> ScaleIOGatewayClientConnectionPool .getInstance ()).thenReturn (pool );
127+ when (pool .getClient (1L , storagePoolDetailsDao )).thenReturn (client );
141128
142129 when (client .haveConnectedSdcs ()).thenReturn (true );
143130
@@ -157,28 +144,19 @@ public void testAttachZone() throws Exception {
157144
158145 when (dataStoreMgr .getDataStore (anyLong (), eq (DataStoreRole .Primary ))).thenReturn (store );
159146 when (store .getId ()).thenReturn (1L );
160- when (store .getPoolType ()).thenReturn (Storage .StoragePoolType .PowerFlex );
161147 when (store .isShared ()).thenReturn (true );
162148 when (store .getName ()).thenReturn ("ScaleIOPool" );
163149 when (store .getStorageProviderName ()).thenReturn (ScaleIOUtil .PROVIDER_NAME );
164150
165151 when (dataStoreProviderMgr .getDataStoreProvider (ScaleIOUtil .PROVIDER_NAME )).thenReturn (dataStoreProvider );
166152 when (dataStoreProvider .getName ()).thenReturn (ScaleIOUtil .PROVIDER_NAME );
153+ when (hostListener .hostConnect (Mockito .anyLong (), Mockito .anyLong ())).thenReturn (true );
167154 storageMgr .registerHostListener (ScaleIOUtil .PROVIDER_NAME , hostListener );
168155
169- when (agentMgr .easySend (anyLong (), Mockito .any (ModifyStoragePoolCommand .class ))).thenReturn (answer );
170- when (answer .getResult ()).thenReturn (true );
171-
172- when (storagePoolHostDao .findByPoolHost (anyLong (), anyLong ())).thenReturn (null );
173-
174- when (hostDao .findById (1L )).thenReturn (host1 );
175- when (hostDao .findById (2L )).thenReturn (host2 );
176-
177156 when (dataStoreHelper .attachZone (Mockito .any (DataStore .class ))).thenReturn (null );
178157
179- scaleIOPrimaryDataStoreLifeCycleTest .attachZone (dataStore , scope , Hypervisor .HypervisorType .KVM );
180- verify (storageMgr ,times (2 )).connectHostToSharedPool (Mockito .any (Long .class ), Mockito .any (Long .class ));
181- verify (storagePoolHostDao ,times (2 )).persist (Mockito .any (StoragePoolHostVO .class ));
158+ boolean result = scaleIOPrimaryDataStoreLifeCycleTest .attachZone (dataStore , scope , Hypervisor .HypervisorType .KVM );
159+ assertThat (result ).isTrue ();
182160 }
183161
184162 @ Test (expected = CloudRuntimeException .class )
@@ -224,7 +202,6 @@ public void testDisableStoragePool() {
224202 public void testDeleteDataStoreWithStoragePoolNull () {
225203 final PrimaryDataStore store = mock (PrimaryDataStore .class );
226204 when (primaryDataStoreDao .findById (anyLong ())).thenReturn (null );
227- when (dataStoreHelper .deletePrimaryDataStore (any (DataStore .class ))).thenReturn (true );
228205 final boolean result = scaleIOPrimaryDataStoreLifeCycleTest .deleteDataStore (store );
229206 assertThat (result ).isFalse ();
230207 }
@@ -233,6 +210,7 @@ public void testDeleteDataStoreWithStoragePoolNull() {
233210 public void testDeleteDataStore () {
234211 final PrimaryDataStore store = mock (PrimaryDataStore .class );
235212 final StoragePoolVO storagePoolVO = mock (StoragePoolVO .class );
213+ when (store .getId ()).thenReturn (1L );
236214 when (primaryDataStoreDao .findById (anyLong ())).thenReturn (storagePoolVO );
237215 List <VMTemplateStoragePoolVO > unusedTemplates = new ArrayList <>();
238216 when (templateMgr .getUnusedTemplatesInPool (storagePoolVO )).thenReturn (unusedTemplates );
0 commit comments