package org.ovirt.engine.core.bll;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import java.util.ArrayList;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.ovirt.engine.core.bll.interfaces.BackendInternal;
import org.ovirt.engine.core.common.businessentities.VDS;
import org.ovirt.engine.core.common.businessentities.storage_domains;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;
import org.ovirt.engine.core.dao.StorageDomainDAO;
import org.ovirt.engine.core.dao.VdsDAO;
import org.ovirt.engine.core.utils.MockConfigRule;

public class AutoRecoveryManagerTest {
    @Rule
    public static MockConfigRule mcr = new MockConfigRule();
    private AutoRecoveryManager manager;

    @Before
    public void setup() {
        manager = spy(AutoRecoveryManager.getInstance());
        final BackendInternal backendMock = mock(BackendInternal.class);
        doReturn(backendMock).when(manager).getBackend();

        final DbFacade dbFacadeMock = mock(DbFacade.class);
        doReturn(dbFacadeMock).when(manager).getDbFacade();
        final VdsDAO vdsDaoMock = mock(VdsDAO.class);
        final ArrayList<VDS> vdss = new ArrayList<VDS>();
        final VDS vds = new VDS();
        vdss.add(vds);
        when(vdsDaoMock.listFailedAutorecoverables()).thenReturn(vdss);
        when(dbFacadeMock.getVdsDAO()).thenReturn(vdsDaoMock);

        final StorageDomainDAO storageDomainDaoMock = mock(StorageDomainDAO.class);
        final ArrayList<storage_domains> storageDomains = new ArrayList<storage_domains>();
        when(storageDomainDaoMock.listFailedAutorecoverables()).thenReturn(storageDomains);
        when(dbFacadeMock.getStorageDomainDAO()).thenReturn(storageDomainDaoMock);
    }

    @Test
    public void onTimer() {
        manager.onTimer();
    }
}

@@ -29,16 +29,95 @@
 e.bll;%0A%0A
+import static org.mockito.Matchers.any;%0Aimport static org.mockito.Matchers.eq;%0A
 import s
@@ -231,16 +231,99 @@
 to.spy;%0A
+import static org.mockito.Mockito.times;%0Aimport static org.mockito.Mockito.verify;%0A
 import s
@@ -382,16 +382,39 @@
 rayList;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -493,56 +493,287 @@
 org.
-ovirt.engine.core.bll.interfaces.BackendInternal
+junit.runner.RunWith;%0Aimport org.mockito.Mock;%0Aimport org.mockito.runners.MockitoJUnitRunner;%0Aimport org.ovirt.engine.core.bll.interfaces.BackendInternal;%0Aimport org.ovirt.engine.core.common.action.VdcActionParametersBase;%0Aimport org.ovirt.engine.core.common.action.VdcActionType
 ;%0Aim
@@ -898,16 +898,58 @@
 omains;%0A
+import org.ovirt.engine.core.compat.Guid;%0A
 import o
@@ -1136,16 +1136,51 @@
 gRule;%0A%0A
+@RunWith(MockitoJUnitRunner.class)%0A
 public c
@@ -1323,16 +1323,238 @@
 nager;%0A%0A
+    @Mock%0A    private BackendInternal backendMock;%0A%0A    // Entities needing recovery%0A    private List%3CVDS%3E vdss = new ArrayList%3CVDS%3E();%0A    private List%3Cstorage_domains%3E storageDomains = new ArrayList%3Cstorage_domains%3E();%0A%0A
     @Bef
@@ -1645,81 +1645,8 @@
 ));%0A
-        final BackendInternal backendMock = mock(BackendInternal.class);%0A
     
@@ -1695,25 +1695,24 @@
 tBackend();%0A
-%0A
         fina
@@ -1877,65 +1877,8 @@
 s);%0A
-        final ArrayList%3CVDS%3E vdss = new ArrayList%3CVDS%3E();
 %0A   
@@ -2080,38 +2080,44 @@
     
-final S
+s
 torage
-D
+_d
 omain
-DAO
+s domain = new
  storage
 Doma
@@ -2116,51 +2116,104 @@
 rage
-D
+_d
 omain
-DaoMock = mock(StorageDomainDAO.class
+s();%0A        domain.setstorage_pool_id(Guid.NewGuid());%0A        storageDomains.add(domain
 );%0A 
@@ -2229,34 +2229,24 @@
 nal 
-ArrayList%3Cs
+S
 torage
-_d
+D
 omain
-s%3E
+DAO
  sto
@@ -2259,43 +2259,45 @@
 main
-s = new ArrayList%3Cs
+DaoMock = mock(S
 torage
-_d
+D
 omain
-s%3E(
+DAO.class
 );%0A 
@@ -2538,20 +2538,340 @@
 .onTimer();%0A
+        verify(backendMock, times(vdss.size())).runInternalAction(eq(VdcActionType.ActivateVds),%0A                any(VdcActionParametersBase.class));%0A        verify(backendMock, times(storageDomains.size())).runInternalAction(eq(VdcActionType.ActivateStorageDomain),%0A                any(VdcActionParametersBase.class));%0A
     %7D%0A%7D%0A

Improve AutoRecoveryManagerTest