@@ -54,20 +54,20 @@ public class UserVmManagerTest {
5454 @ Spy UserVmManagerImpl _userVmMgr = new UserVmManagerImpl ();
5555 @ Mock VirtualMachineManager _itMgr ;
5656 @ Mock StorageManager _storageMgr ;
57- @ Mock Account account ;
57+ @ Mock Account _account ;
5858 @ Mock AccountManager _accountMgr ;
5959 @ Mock AccountDao _accountDao ;
6060 @ Mock UserDao _userDao ;
6161 @ Mock UserVmDao _vmDao ;
6262 @ Mock VMTemplateDao _templateDao ;
6363 @ Mock VolumeDao _volsDao ;
64- @ Mock RestoreVMCmd restoreVMCmd ;
65- @ Mock AccountVO accountMock ;
66- @ Mock UserVO userMock ;
67- @ Mock UserVmVO vmMock ;
68- @ Mock VMTemplateVO templateMock ;
69- @ Mock VolumeVO volumeMock ;
70- @ Mock List <VolumeVO > rootVols ;
64+ @ Mock RestoreVMCmd _restoreVMCmd ;
65+ @ Mock AccountVO _accountMock ;
66+ @ Mock UserVO _userMock ;
67+ @ Mock UserVmVO _vmMock ;
68+ @ Mock VMTemplateVO _templateMock ;
69+ @ Mock VolumeVO _volumeMock ;
70+ @ Mock List <VolumeVO > _rootVols ;
7171 @ Before
7272 public void setup (){
7373 MockitoAnnotations .initMocks (this );
@@ -79,69 +79,102 @@ public void setup(){
7979 _userVmMgr ._storageMgr = _storageMgr ;
8080 _userVmMgr ._accountDao = _accountDao ;
8181 _userVmMgr ._userDao = _userDao ;
82+ _userVmMgr ._accountMgr = _accountMgr ;
8283
83- doReturn (3L ).when (account ).getId ();
84- doReturn (8L ).when (vmMock ).getAccountId ();
85- when (_accountDao .findById (anyLong ())).thenReturn (accountMock );
86- when (_userDao .findById (anyLong ())).thenReturn (userMock );
87- doReturn (Account .State .enabled ).when (account ).getState ();
88- when (vmMock .getId ()).thenReturn (314L );
84+ doReturn (3L ).when (_account ).getId ();
85+ doReturn (8L ).when (_vmMock ).getAccountId ();
86+ when (_accountDao .findById (anyLong ())).thenReturn (_accountMock );
87+ when (_userDao .findById (anyLong ())).thenReturn (_userMock );
88+ doReturn (Account .State .enabled ).when (_account ).getState ();
89+ when (_vmMock .getId ()).thenReturn (314L );
8990
9091 }
9192
92- // VM state not in running/stopped case
93+ // Test restoreVm when VM state not in running/stopped case
9394 @ Test (expected =CloudRuntimeException .class )
9495 public void testRestoreVMF1 () throws ResourceAllocationException {
9596
96- when (_vmDao .findById (anyLong ())).thenReturn (vmMock );
97- when (_templateDao .findById (anyLong ())).thenReturn (templateMock );
98- doReturn (VirtualMachine .State .Error ).when (vmMock ).getState ();
99- _userVmMgr .restoreVMInternal (account , vmMock );
97+ when (_vmDao .findById (anyLong ())).thenReturn (_vmMock );
98+ when (_templateDao .findById (anyLong ())).thenReturn (_templateMock );
99+ doReturn (VirtualMachine .State .Error ).when (_vmMock ).getState ();
100+ _userVmMgr .restoreVMInternal (_account , _vmMock , null );
100101 }
101102
102- // when VM is in stopped state
103+ // Test restoreVm when VM is in stopped state
103104 @ Test
104105 public void testRestoreVMF2 () throws ResourceUnavailableException , InsufficientCapacityException , ServerApiException ,
105- ConcurrentOperationException , ResourceAllocationException {
106-
107- doReturn (VirtualMachine .State .Stopped ).when (vmMock ).getState ();
108- when (_vmDao .findById (anyLong ())).thenReturn (vmMock );
109- when (_volsDao .findByInstance (anyLong ())).thenReturn (rootVols );
110- doReturn (false ).when (rootVols ).isEmpty ();
111- when (rootVols .get (eq (0 ))).thenReturn (volumeMock );
112- doReturn (3L ).when (volumeMock ).getTemplateId ();
113- when (_templateDao .findById (anyLong ())).thenReturn (templateMock );
114- when (_storageMgr .allocateDuplicateVolume (volumeMock , null )).thenReturn (volumeMock );
106+ ConcurrentOperationException , ResourceAllocationException {
107+
108+ doReturn (VirtualMachine .State .Stopped ).when (_vmMock ).getState ();
109+ when (_vmDao .findById (anyLong ())).thenReturn (_vmMock );
110+ when (_volsDao .findByInstance (anyLong ())).thenReturn (_rootVols );
111+ doReturn (false ).when (_rootVols ).isEmpty ();
112+ when (_rootVols .get (eq (0 ))).thenReturn (_volumeMock );
113+ doReturn (3L ).when (_volumeMock ).getTemplateId ();
114+ when (_templateDao .findById (anyLong ())).thenReturn (_templateMock );
115+ when (_storageMgr .allocateDuplicateVolume (_volumeMock , null )).thenReturn (_volumeMock );
115116 doNothing ().when (_volsDao ).attachVolume (anyLong (), anyLong (), anyLong ());
116- when (volumeMock .getId ()).thenReturn (3L );
117+ when (_volumeMock .getId ()).thenReturn (3L );
117118 doNothing ().when (_volsDao ).detachVolume (anyLong ());
118- when (_storageMgr .destroyVolume (volumeMock )).thenReturn (true );
119+ when (_storageMgr .destroyVolume (_volumeMock )).thenReturn (true );
120+ when (_templateMock .getUuid ()).thenReturn ("e0552266-7060-11e2-bbaa-d55f5db67735" );
119121
120- _userVmMgr .restoreVMInternal (account , vmMock );
122+ _userVmMgr .restoreVMInternal (_account , _vmMock , null );
121123
122124 }
123125
124- // when VM is in running state
126+ // Test restoreVM when VM is in running state
125127 @ Test
126128 public void testRestoreVMF3 () throws ResourceUnavailableException , InsufficientCapacityException , ServerApiException ,
127- ConcurrentOperationException , ResourceAllocationException {
128-
129- doReturn (VirtualMachine .State .Running ).when (vmMock ).getState ();
130- when (_vmDao .findById (anyLong ())).thenReturn (vmMock );
131- when (_volsDao .findByInstance (anyLong ())).thenReturn (rootVols );
132- doReturn (false ).when (rootVols ).isEmpty ();
133- when (rootVols .get (eq (0 ))).thenReturn (volumeMock );
134- doReturn (3L ).when (volumeMock ).getTemplateId ();
135- when (_templateDao .findById (anyLong ())).thenReturn (templateMock );
136- when (_itMgr .stop (vmMock , userMock , account )).thenReturn (true );
137- when (_itMgr .start (vmMock , null , userMock , account )).thenReturn (vmMock );
138- when (_storageMgr .allocateDuplicateVolume (volumeMock , null )).thenReturn (volumeMock );
129+ ConcurrentOperationException , ResourceAllocationException {
130+
131+ doReturn (VirtualMachine .State .Running ).when (_vmMock ).getState ();
132+ when (_vmDao .findById (anyLong ())).thenReturn (_vmMock );
133+ when (_volsDao .findByInstance (anyLong ())).thenReturn (_rootVols );
134+ doReturn (false ).when (_rootVols ).isEmpty ();
135+ when (_rootVols .get (eq (0 ))).thenReturn (_volumeMock );
136+ doReturn (3L ).when (_volumeMock ).getTemplateId ();
137+ when (_templateDao .findById (anyLong ())).thenReturn (_templateMock );
138+ when (_itMgr .stop (_vmMock , _userMock , _account )).thenReturn (true );
139+ when (_itMgr .start (_vmMock , null , _userMock , _account )).thenReturn (_vmMock );
140+ when (_storageMgr .allocateDuplicateVolume (_volumeMock , null )).thenReturn (_volumeMock );
139141 doNothing ().when (_volsDao ).attachVolume (anyLong (), anyLong (), anyLong ());
140- when (volumeMock .getId ()).thenReturn (3L );
142+ when (_volumeMock .getId ()).thenReturn (3L );
141143 doNothing ().when (_volsDao ).detachVolume (anyLong ());
142- when (_storageMgr .destroyVolume (volumeMock )).thenReturn (true );
144+ when (_storageMgr .destroyVolume (_volumeMock )).thenReturn (true );
145+ when (_templateMock .getUuid ()).thenReturn ("e0552266-7060-11e2-bbaa-d55f5db67735" );
143146
144- _userVmMgr .restoreVMInternal (account , vmMock );
147+ _userVmMgr .restoreVMInternal (_account , _vmMock , null );
148+
149+ }
150+
151+ // Test restoreVM on providing new template Id, when VM is in running state
152+ @ Test
153+ public void testRestoreVMF4 () throws ResourceUnavailableException , InsufficientCapacityException , ServerApiException ,
154+ ConcurrentOperationException , ResourceAllocationException {
155+ doReturn (VirtualMachine .State .Running ).when (_vmMock ).getState ();
156+ when (_vmDao .findById (anyLong ())).thenReturn (_vmMock );
157+ when (_volsDao .findByInstance (anyLong ())).thenReturn (_rootVols );
158+ doReturn (false ).when (_rootVols ).isEmpty ();
159+ when (_rootVols .get (eq (0 ))).thenReturn (_volumeMock );
160+ doReturn (3L ).when (_volumeMock ).getTemplateId ();
161+ when (_templateDao .findById (anyLong ())).thenReturn (_templateMock );
162+ doNothing ().when (_accountMgr ).checkAccess (_account , null , true , _templateMock );
163+ when (_itMgr .stop (_vmMock , _userMock , _account )).thenReturn (true );
164+ when (_storageMgr .allocateDuplicateVolume (_volumeMock , 14L )).thenReturn (_volumeMock );
165+ when (_templateMock .getGuestOSId ()).thenReturn (5L );
166+ doNothing ().when (_vmMock ).setGuestOSId (anyLong ());
167+ doNothing ().when (_vmMock ).setTemplateId (3L );
168+ when (_vmDao .update (314L , _vmMock )).thenReturn (true );
169+ when (_itMgr .start (_vmMock , null , _userMock , _account )).thenReturn (_vmMock );
170+ when (_storageMgr .allocateDuplicateVolume (_volumeMock , null )).thenReturn (_volumeMock );
171+ doNothing ().when (_volsDao ).attachVolume (anyLong (), anyLong (), anyLong ());
172+ when (_volumeMock .getId ()).thenReturn (3L );
173+ doNothing ().when (_volsDao ).detachVolume (anyLong ());
174+ when (_storageMgr .destroyVolume (_volumeMock )).thenReturn (true );
175+ when (_templateMock .getUuid ()).thenReturn ("b1a3626e-72e0-4697-8c7c-a110940cc55d" );
176+
177+ _userVmMgr .restoreVMInternal (_account , _vmMock , 14L );
145178
146179 }
147180
0 commit comments