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 ;
4546import org .apache .cloudstack .engine .subsystem .api .storage .PrimaryDataStoreLifeCycle ;
4647import org .apache .cloudstack .storage .datastore .db .PrimaryDataStoreDao ;
4748import org .apache .cloudstack .storage .datastore .db .StoragePoolVO ;
48- import org .apache .cloudstack .storage .datastore .provider .DefaultHostListener ;
4949import org .apache .cloudstack .storage .volume .datastore .PrimaryDataStoreHelper ;
50+ import org .junit .After ;
51+ import org .junit .Assert ;
5052import org .junit .Before ;
5153import org .junit .Test ;
5254import org .junit .runner .RunWith ;
5355import org .mockito .InjectMocks ;
5456import org .mockito .Mock ;
5557import org .mockito .Mockito ;
5658import org .mockito .MockitoAnnotations ;
57- import org .mockito .Spy ;
5859import org .mockito .runners .MockitoJUnitRunner ;
60+ import org .springframework .test .util .ReflectionTestUtils ;
5961
6062import java .util .ArrayList ;
6163import java .util .List ;
6466import static org .mockito .Matchers .anyLong ;
6567import static org .mockito .Matchers .anyString ;
6668import static org .mockito .Matchers .eq ;
67- import static org .mockito .Mockito .times ;
68- import static org .mockito .Mockito .verify ;
6969import static org .mockito .Mockito .when ;
7070
7171/**
@@ -77,7 +77,6 @@ public class CloudStackPrimaryDataStoreLifeCycleImplTest extends TestCase {
7777 @ InjectMocks
7878 PrimaryDataStoreLifeCycle _cloudStackPrimaryDataStoreLifeCycle = new CloudStackPrimaryDataStoreLifeCycleImpl ();
7979
80- @ Spy
8180 @ InjectMocks
8281 StorageManager storageMgr = new StorageManagerImpl ();
8382
@@ -93,9 +92,8 @@ public class CloudStackPrimaryDataStoreLifeCycleImplTest extends TestCase {
9392 @ Mock
9493 DataStoreProviderManager _dataStoreProviderMgr ;
9594
96- @ Spy
97- @ InjectMocks
98- HypervisorHostListener hostListener = new DefaultHostListener ();
95+ @ Mock
96+ HypervisorHostListener hostListener ;
9997
10098 @ Mock
10199 StoragePoolHostDao storagePoolHostDao ;
@@ -121,10 +119,16 @@ public class CloudStackPrimaryDataStoreLifeCycleImplTest extends TestCase {
121119 @ Mock
122120 PrimaryDataStoreHelper primaryDataStoreHelper ;
123121
122+ AutoCloseable closeable ;
123+
124124 @ Before
125- public void initMocks () {
125+ public void initMocks () throws StorageConflictException {
126+ closeable = MockitoAnnotations .openMocks (this );
126127
127- MockitoAnnotations .initMocks (this );
128+ ReflectionTestUtils .setField (storageMgr , "_storagePoolDao" , primaryStoreDao );
129+ ReflectionTestUtils .setField (storageMgr , "_dataStoreProviderMgr" , _dataStoreProviderMgr );
130+ ReflectionTestUtils .setField (storageMgr , "_dataStoreMgr" , _dataStoreMgr );
131+ ReflectionTestUtils .setField (_cloudStackPrimaryDataStoreLifeCycle , "storageMgr" , storageMgr );
128132
129133 List <HostVO > hostList = new ArrayList <HostVO >();
130134 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 +145,31 @@ public void initMocks() {
141145 when (store .getPoolType ()).thenReturn (Storage .StoragePoolType .NetworkFilesystem );
142146 when (store .isShared ()).thenReturn (true );
143147 when (store .getName ()).thenReturn ("newPool" );
148+ when (store .getStorageProviderName ()).thenReturn ("default" );
149+
144150
145151 when (_dataStoreProviderMgr .getDataStoreProvider (anyString ())).thenReturn (dataStoreProvider );
146152 when (dataStoreProvider .getName ()).thenReturn ("default" );
147- ((StorageManagerImpl )storageMgr ).registerHostListener ("default" , hostListener );
153+
154+ when (hostListener .hostConnect (Mockito .anyLong (), Mockito .anyLong ())).thenReturn (true );
155+ storageMgr .registerHostListener ("default" , hostListener );
156+
148157
149158 when (_resourceMgr .listAllUpHosts (eq (Host .Type .Routing ), anyLong (), anyLong (), anyLong ())).thenReturn (hostList );
150159 when (agentMgr .easySend (anyLong (), Mockito .any (ModifyStoragePoolCommand .class ))).thenReturn (answer );
151160 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 );
157161
158- when (storagePoolHostDao .findByPoolHost (anyLong (), anyLong ())).thenReturn (null );
159162 when (primaryStoreDao .findById (anyLong ())).thenReturn (storagePool );
160- when (primaryStoreDao .update (anyLong (), Mockito .any (StoragePoolVO .class ))).thenReturn (true );
161163 when (primaryDataStoreHelper .attachCluster (Mockito .any (DataStore .class ))).thenReturn (null );
162164 }
163165
166+ @ After
167+ public void tearDown () throws Exception {
168+ closeable .close ();
169+ }
170+
164171 @ Test
165172 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-
173+ Assert .assertTrue (_cloudStackPrimaryDataStoreLifeCycle .attachCluster (store , new ClusterScope (1L , 1L , 1L )));
169174 }
170175}
0 commit comments