2323import com .cloud .agent .api .ModifyStoragePoolAnswer ;
2424import com .cloud .agent .api .ModifyStoragePoolCommand ;
2525import com .cloud .agent .api .StoragePoolInfo ;
26+ import com .cloud .exception .StorageConflictException ;
2627import com .cloud .host .Host ;
2728import com .cloud .host .HostVO ;
2829import com .cloud .host .Status ;
3233import com .cloud .storage .Storage ;
3334import com .cloud .storage .StorageManager ;
3435import com .cloud .storage .StorageManagerImpl ;
35- import com .cloud .storage .StoragePoolHostVO ;
3636import com .cloud .storage .dao .StoragePoolHostDao ;
3737import junit .framework .TestCase ;
3838import org .apache .cloudstack .engine .subsystem .api .storage .ClusterScope ;
4545import org .apache .cloudstack .engine .subsystem .api .storage .PrimaryDataStoreLifeCycle ;
4646import org .apache .cloudstack .storage .datastore .db .PrimaryDataStoreDao ;
4747import org .apache .cloudstack .storage .datastore .db .StoragePoolVO ;
48- import org .apache .cloudstack .storage .datastore .provider .DefaultHostListener ;
4948import org .apache .cloudstack .storage .volume .datastore .PrimaryDataStoreHelper ;
49+ import org .junit .After ;
50+ import org .junit .Assert ;
5051import org .junit .Before ;
5152import org .junit .Test ;
5253import org .junit .runner .RunWith ;
5354import org .mockito .InjectMocks ;
5455import org .mockito .Mock ;
5556import org .mockito .Mockito ;
5657import org .mockito .MockitoAnnotations ;
57- import org .mockito .Spy ;
5858import org .mockito .runners .MockitoJUnitRunner ;
59+ import org .springframework .test .util .ReflectionTestUtils ;
5960
6061import java .util .ArrayList ;
6162import java .util .List ;
6465import static org .mockito .Matchers .anyLong ;
6566import static org .mockito .Matchers .anyString ;
6667import static org .mockito .Matchers .eq ;
67- import static org .mockito .Mockito .times ;
68- import static org .mockito .Mockito .verify ;
6968import static org .mockito .Mockito .when ;
7069
7170/**
@@ -77,7 +76,6 @@ public class CloudStackPrimaryDataStoreLifeCycleImplTest extends TestCase {
7776 @ InjectMocks
7877 PrimaryDataStoreLifeCycle _cloudStackPrimaryDataStoreLifeCycle = new CloudStackPrimaryDataStoreLifeCycleImpl ();
7978
80- @ Spy
8179 @ InjectMocks
8280 StorageManager storageMgr = new StorageManagerImpl ();
8381
@@ -93,9 +91,8 @@ public class CloudStackPrimaryDataStoreLifeCycleImplTest extends TestCase {
9391 @ Mock
9492 DataStoreProviderManager _dataStoreProviderMgr ;
9593
96- @ Spy
97- @ InjectMocks
98- HypervisorHostListener hostListener = new DefaultHostListener ();
94+ @ Mock
95+ HypervisorHostListener hostListener ;
9996
10097 @ Mock
10198 StoragePoolHostDao storagePoolHostDao ;
@@ -121,10 +118,16 @@ public class CloudStackPrimaryDataStoreLifeCycleImplTest extends TestCase {
121118 @ Mock
122119 PrimaryDataStoreHelper primaryDataStoreHelper ;
123120
121+ AutoCloseable closeable ;
122+
124123 @ Before
125- public void initMocks () {
124+ public void initMocks () throws StorageConflictException {
125+ closeable = MockitoAnnotations .openMocks (this );
126126
127- MockitoAnnotations .initMocks (this );
127+ ReflectionTestUtils .setField (storageMgr , "_storagePoolDao" , primaryStoreDao );
128+ ReflectionTestUtils .setField (storageMgr , "_dataStoreProviderMgr" , _dataStoreProviderMgr );
129+ ReflectionTestUtils .setField (storageMgr , "_dataStoreMgr" , _dataStoreMgr );
130+ ReflectionTestUtils .setField (_cloudStackPrimaryDataStoreLifeCycle , "storageMgr" , storageMgr );
128131
129132 List <HostVO > hostList = new ArrayList <HostVO >();
130133 HostVO host1 = new HostVO (1L , "aa01" , Host .Type .Routing , "192.168.1.1" , "255.255.255.0" , null , null , null , null , null , null , null , null , null , null ,
@@ -141,30 +144,31 @@ public void initMocks() {
141144 when (store .getPoolType ()).thenReturn (Storage .StoragePoolType .NetworkFilesystem );
142145 when (store .isShared ()).thenReturn (true );
143146 when (store .getName ()).thenReturn ("newPool" );
147+ when (store .getStorageProviderName ()).thenReturn ("default" );
148+
144149
145150 when (_dataStoreProviderMgr .getDataStoreProvider (anyString ())).thenReturn (dataStoreProvider );
146151 when (dataStoreProvider .getName ()).thenReturn ("default" );
147- ((StorageManagerImpl )storageMgr ).registerHostListener ("default" , hostListener );
152+
153+ when (hostListener .hostConnect (Mockito .anyLong (), Mockito .anyLong ())).thenReturn (true );
154+ storageMgr .registerHostListener ("default" , hostListener );
155+
148156
149157 when (_resourceMgr .listAllUpHosts (eq (Host .Type .Routing ), anyLong (), anyLong (), anyLong ())).thenReturn (hostList );
150158 when (agentMgr .easySend (anyLong (), Mockito .any (ModifyStoragePoolCommand .class ))).thenReturn (answer );
151159 when (answer .getResult ()).thenReturn (true );
152- when (answer .getPoolInfo ()).thenReturn (info );
153-
154- when (info .getLocalPath ()).thenReturn ("/mnt/1" );
155- when (info .getCapacityBytes ()).thenReturn (0L );
156- when (info .getAvailableBytes ()).thenReturn (0L );
157160
158- when (storagePoolHostDao .findByPoolHost (anyLong (), anyLong ())).thenReturn (null );
159161 when (primaryStoreDao .findById (anyLong ())).thenReturn (storagePool );
160- when (primaryStoreDao .update (anyLong (), Mockito .any (StoragePoolVO .class ))).thenReturn (true );
161162 when (primaryDataStoreHelper .attachCluster (Mockito .any (DataStore .class ))).thenReturn (null );
162163 }
163164
165+ @ After
166+ public void tearDown () throws Exception {
167+ closeable .close ();
168+ }
169+
164170 @ Test
165171 public void testAttachCluster () throws Exception {
166- _cloudStackPrimaryDataStoreLifeCycle .attachCluster (store , new ClusterScope (1L , 1L , 1L ));
167- verify (storagePoolHostDao ,times (2 )).persist (Mockito .any (StoragePoolHostVO .class ));
168-
172+ Assert .assertTrue (_cloudStackPrimaryDataStoreLifeCycle .attachCluster (store , new ClusterScope (1L , 1L , 1L )));
169173 }
170174}
0 commit comments