1616// under the License.
1717package org .apache .cloudstack .backup ;
1818
19+ import com .cloud .event .ActionEventUtils ;
1920import com .cloud .exception .InvalidParameterValueException ;
2021import com .cloud .storage .Volume ;
2122import com .cloud .storage .VolumeApiService ;
3233import org .apache .cloudstack .backup .dao .BackupOfferingDao ;
3334import org .junit .Before ;
3435import org .junit .Test ;
36+ import org .junit .runner .RunWith ;
37+ import org .mockito .junit .MockitoJUnitRunner ;
3538import org .mockito .InjectMocks ;
3639import org .mockito .Mock ;
40+ import org .mockito .MockedStatic ;
3741import org .mockito .Mockito ;
3842import org .mockito .MockitoAnnotations ;
3943import org .mockito .Spy ;
4549import static org .mockito .Mockito .times ;
4650import static org .mockito .Mockito .when ;
4751
52+ @ RunWith (MockitoJUnitRunner .class )
4853public class BackupManagerTest {
4954 @ Spy
5055 @ InjectMocks
@@ -75,15 +80,11 @@ public void setup() throws Exception {
7580 when (backupOfferingDao .findById (123l )).thenReturn (null );
7681
7782 BackupOfferingVO offering = Mockito .spy (BackupOfferingVO .class );
78- when (offering .getId ()).thenReturn (1234l );
7983 when (offering .getName ()).thenCallRealMethod ();
8084 when (offering .getDescription ()).thenCallRealMethod ();
8185 when (offering .isUserDrivenBackupAllowed ()).thenCallRealMethod ();
8286
8387 BackupOfferingVO offeringUpdate = Mockito .spy (BackupOfferingVO .class );
84- when (offeringUpdate .getId ()).thenReturn (1234l );
85- when (offeringUpdate .getName ()).thenReturn ("Old name" );
86- when (offeringUpdate .getDescription ()).thenReturn ("Old description" );
8788
8889 when (backupOfferingDao .findById (1234l )).thenReturn (offering );
8990 when (backupOfferingDao .createForUpdate (1234l )).thenReturn (offeringUpdate );
@@ -218,18 +219,25 @@ public void tryRestoreVMTestRestoreSucceeded() throws NoTransitionException {
218219 VMInstanceVO vm = Mockito .mock (VMInstanceVO .class );
219220 BackupVO backup = Mockito .mock (BackupVO .class );
220221
221- Mockito .when (volumeDao .findIncludingRemovedByInstanceAndType (1L , null )).thenReturn (Collections .singletonList (volumeVO ));
222- Mockito .when (virtualMachineManager .stateTransitTo (Mockito .eq (vm ), Mockito .eq (VirtualMachine .Event .RestoringRequested ), Mockito .any ())).thenReturn (true );
223- Mockito .when (volumeApiService .stateTransitTo (Mockito .eq (volumeVO ), Mockito .eq (Volume .Event .RestoreRequested ))).thenReturn (true );
222+ try (MockedStatic <ActionEventUtils > utils = Mockito .mockStatic (ActionEventUtils .class )) {
223+ Mockito .when (ActionEventUtils .onStartedActionEvent (Mockito .anyLong (), Mockito .anyLong (),
224+ Mockito .anyString (), Mockito .anyString (), Mockito .anyLong (), Mockito .anyString (),
225+ Mockito .eq (true ), Mockito .eq (0 ))).thenReturn (1L );
226+ Mockito .when (ActionEventUtils .onCompletedActionEvent (Mockito .anyLong (), Mockito .anyLong (),
227+ Mockito .anyString (), Mockito .anyString (), Mockito .anyString (), Mockito .anyLong (),
228+ Mockito .anyString (), Mockito .eq (0 ))).thenReturn (2L );
224229
230+ Mockito .when (volumeDao .findIncludingRemovedByInstanceAndType (1L , null )).thenReturn (Collections .singletonList (volumeVO ));
231+ Mockito .when (virtualMachineManager .stateTransitTo (Mockito .eq (vm ), Mockito .eq (VirtualMachine .Event .RestoringRequested ), Mockito .any ())).thenReturn (true );
232+ Mockito .when (volumeApiService .stateTransitTo (Mockito .eq (volumeVO ), Mockito .eq (Volume .Event .RestoreRequested ))).thenReturn (true );
225233
234+ Mockito .when (vm .getId ()).thenReturn (1L );
235+ Mockito .when (offering .getProvider ()).thenReturn ("veeam" );
236+ Mockito .doReturn (backupProvider ).when (backupManager ).getBackupProvider ("veeam" );
237+ Mockito .when (backupProvider .restoreVMFromBackup (vm , backup )).thenReturn (true );
226238
227- Mockito .when (vm .getId ()).thenReturn (1L );
228- Mockito .when (offering .getProvider ()).thenReturn ("veeam" );
229- Mockito .doReturn (backupProvider ).when (backupManager ).getBackupProvider ("veeam" );
230- Mockito .when (backupProvider .restoreVMFromBackup (vm , backup )).thenReturn (true );
231-
232- backupManager .tryRestoreVM (backup , vm , offering , "Nothing to write here." );
239+ backupManager .tryRestoreVM (backup , vm , offering , "Nothing to write here." );
240+ }
233241 }
234242
235243 @ Test
@@ -239,21 +247,30 @@ public void tryRestoreVMTestRestoreFails() throws NoTransitionException {
239247 VMInstanceVO vm = Mockito .mock (VMInstanceVO .class );
240248 BackupVO backup = Mockito .mock (BackupVO .class );
241249
242- Mockito .when (volumeDao .findIncludingRemovedByInstanceAndType (1L , null )).thenReturn (Collections .singletonList (volumeVO ));
243- Mockito .when (virtualMachineManager .stateTransitTo (Mockito .eq (vm ), Mockito .eq (VirtualMachine .Event .RestoringRequested ), Mockito .any ())).thenReturn (true );
244- Mockito .when (volumeApiService .stateTransitTo (Mockito .eq (volumeVO ), Mockito .eq (Volume .Event .RestoreRequested ))).thenReturn (true );
245- Mockito .when (virtualMachineManager .stateTransitTo (Mockito .eq (vm ), Mockito .eq (VirtualMachine .Event .RestoringFailed ), Mockito .any ())).thenReturn (true );
246- Mockito .when (volumeApiService .stateTransitTo (Mockito .eq (volumeVO ), Mockito .eq (Volume .Event .RestoreFailed ))).thenReturn (true );
247-
248- Mockito .when (vm .getId ()).thenReturn (1L );
249- Mockito .when (offering .getProvider ()).thenReturn ("veeam" );
250- Mockito .doReturn (backupProvider ).when (backupManager ).getBackupProvider ("veeam" );
251- Mockito .when (backupProvider .restoreVMFromBackup (vm , backup )).thenReturn (false );
252- try {
253- backupManager .tryRestoreVM (backup , vm , offering , "Checking message error." );
254- fail ("An exception is needed." );
255- } catch (CloudRuntimeException e ) {
256- assertEquals ("Error restoring VM from backup [Checking message error.]." , e .getMessage ());
250+ try (MockedStatic <ActionEventUtils > utils = Mockito .mockStatic (ActionEventUtils .class )) {
251+ Mockito .when (ActionEventUtils .onStartedActionEvent (Mockito .anyLong (), Mockito .anyLong (),
252+ Mockito .anyString (), Mockito .anyString (), Mockito .anyLong (), Mockito .anyString (),
253+ Mockito .eq (true ), Mockito .eq (0 ))).thenReturn (1L );
254+ Mockito .when (ActionEventUtils .onCompletedActionEvent (Mockito .anyLong (), Mockito .anyLong (),
255+ Mockito .anyString (), Mockito .anyString (), Mockito .anyString (), Mockito .anyLong (),
256+ Mockito .anyString (), Mockito .eq (0 ))).thenReturn (2L );
257+
258+ Mockito .when (volumeDao .findIncludingRemovedByInstanceAndType (1L , null )).thenReturn (Collections .singletonList (volumeVO ));
259+ Mockito .when (virtualMachineManager .stateTransitTo (Mockito .eq (vm ), Mockito .eq (VirtualMachine .Event .RestoringRequested ), Mockito .any ())).thenReturn (true );
260+ Mockito .when (volumeApiService .stateTransitTo (Mockito .eq (volumeVO ), Mockito .eq (Volume .Event .RestoreRequested ))).thenReturn (true );
261+ Mockito .when (virtualMachineManager .stateTransitTo (Mockito .eq (vm ), Mockito .eq (VirtualMachine .Event .RestoringFailed ), Mockito .any ())).thenReturn (true );
262+ Mockito .when (volumeApiService .stateTransitTo (Mockito .eq (volumeVO ), Mockito .eq (Volume .Event .RestoreFailed ))).thenReturn (true );
263+
264+ Mockito .when (vm .getId ()).thenReturn (1L );
265+ Mockito .when (offering .getProvider ()).thenReturn ("veeam" );
266+ Mockito .doReturn (backupProvider ).when (backupManager ).getBackupProvider ("veeam" );
267+ Mockito .when (backupProvider .restoreVMFromBackup (vm , backup )).thenReturn (false );
268+ try {
269+ backupManager .tryRestoreVM (backup , vm , offering , "Checking message error." );
270+ fail ("An exception is needed." );
271+ } catch (CloudRuntimeException e ) {
272+ assertEquals ("Error restoring VM from backup [Checking message error.]." , e .getMessage ());
273+ }
257274 }
258275 }
259276}
0 commit comments