Sahina Bose has uploaded a new change for review. Change subject: engine: Unit test for RemoveVdsCommand ......................................................................
engine: Unit test for RemoveVdsCommand Added unit tests for RemoveVdsCommand's execute method Change-Id: Ice3e8b7ee1e35d1f96474c23b71d5265d4c00380 Signed-off-by: Sahina Bose <sab...@redhat.com> --- M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RemoveVdsCommand.java M backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/RemoveVdsCommandTest.java 2 files changed, 104 insertions(+), 3 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/54/21754/1 diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RemoveVdsCommand.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RemoveVdsCommand.java index 74db518..7088cea 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RemoveVdsCommand.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RemoveVdsCommand.java @@ -23,6 +23,8 @@ import org.ovirt.engine.core.compat.Guid; import org.ovirt.engine.core.dal.dbbroker.DbFacade; import org.ovirt.engine.core.dao.VdsDynamicDAO; +import org.ovirt.engine.core.dao.VdsStaticDAO; +import org.ovirt.engine.core.dao.VdsStatisticsDAO; import org.ovirt.engine.core.dao.gluster.GlusterBrickDao; import org.ovirt.engine.core.utils.transaction.TransactionMethod; import org.ovirt.engine.core.utils.transaction.TransactionSupport; @@ -135,12 +137,12 @@ private void RemoveVdsFromCollection() { // ResourceManager.Instance.removeVds(VdsId); - Backend.getInstance().getResourceManager() + getBackend().getResourceManager() .RunVdsCommand(VDSCommandType.RemoveVds, new RemoveVdsVDSCommandParameters(getVdsId())); } private void RemoveVdsStaticFromDb() { - DbFacade.getInstance().getVdsStaticDao().remove(getVdsId()); + getVdsStaticDao().remove(getVdsId()); } private void RemoveVdsDynamicFromDb() { @@ -148,7 +150,15 @@ } private void RemoveVdsStatisticsFromDb() { - DbFacade.getInstance().getVdsStatisticsDao().remove(getVdsId()); + getVdsStatisticsDao().remove(getVdsId()); + } + + protected VdsStatisticsDAO getVdsStatisticsDao() { + return DbFacade.getInstance().getVdsStatisticsDao(); + } + + protected VdsStaticDAO getVdsStaticDao() { + return getDbFacade().getVdsStaticDao(); } private boolean canRemoveVds(Guid vdsId, List<String> text) { diff --git a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/RemoveVdsCommandTest.java b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/RemoveVdsCommandTest.java index a38ebb6..8755283 100644 --- a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/RemoveVdsCommandTest.java +++ b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/RemoveVdsCommandTest.java @@ -1,8 +1,13 @@ package org.ovirt.engine.core.bll; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.when; import java.util.ArrayList; @@ -10,13 +15,17 @@ import java.util.Collections; import java.util.List; +import org.infinispan.transaction.tm.DummyTransactionManager; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; +import org.ovirt.engine.core.bll.interfaces.BackendInternal; import org.ovirt.engine.core.bll.utils.ClusterUtils; +import org.ovirt.engine.core.common.AuditLogType; import org.ovirt.engine.core.common.action.RemoveVdsParameters; import org.ovirt.engine.core.common.businessentities.VDS; import org.ovirt.engine.core.common.businessentities.VDSGroup; @@ -24,13 +33,22 @@ import org.ovirt.engine.core.common.businessentities.VdsDynamic; import org.ovirt.engine.core.common.businessentities.gluster.GlusterBrickEntity; import org.ovirt.engine.core.common.errors.VdcBllMessages; +import org.ovirt.engine.core.common.interfaces.VDSBrokerFrontend; +import org.ovirt.engine.core.common.vdscommands.VDSCommandType; +import org.ovirt.engine.core.common.vdscommands.VDSParametersBase; import org.ovirt.engine.core.compat.Guid; import org.ovirt.engine.core.dao.StoragePoolDAO; import org.ovirt.engine.core.dao.VdsDAO; import org.ovirt.engine.core.dao.VdsDynamicDAO; import org.ovirt.engine.core.dao.VdsGroupDAO; +import org.ovirt.engine.core.dao.VdsStaticDAO; +import org.ovirt.engine.core.dao.VdsStatisticsDAO; import org.ovirt.engine.core.dao.VmStaticDAO; import org.ovirt.engine.core.dao.gluster.GlusterBrickDao; +import org.ovirt.engine.core.dao.gluster.GlusterHooksDao; +import org.ovirt.engine.core.dao.gluster.GlusterVolumeDao; +import org.ovirt.engine.core.utils.MockEJBStrategyRule; +import org.ovirt.engine.core.utils.ejb.ContainerManagedResourceType; @RunWith(MockitoJUnitRunner.class) public class RemoveVdsCommandTest { @@ -58,6 +76,27 @@ @Mock private ClusterUtils clusterUtils; + @Mock + private GlusterVolumeDao volumeDao; + + @Mock + private GlusterHooksDao hooksDao; + + @ClassRule + public static MockEJBStrategyRule ejbRule = new MockEJBStrategyRule(); + + @Mock + private BackendInternal backend; + + @Mock + private VDSBrokerFrontend vdsBrokerFrontend; + + @Mock + private VdsStaticDAO vdsStaticDao; + + @Mock + private VdsStatisticsDAO vdsStatisticsDao; + /** * The command under test. */ @@ -77,8 +116,14 @@ doReturn(vdsDynamicDAO).when(command).getVdsDynamicDAO(); doReturn(glusterBrickDao).when(command).getGlusterBrickDao(); doReturn(vdsGroupDao).when(command).getVdsGroupDAO(); + doReturn(volumeDao).when(command).getGlusterVolumeDao(); + doReturn(hooksDao).when(command).getGlusterHooksDao(); doReturn(vdsGroup).when(vdsGroupDao).get(Mockito.any(Guid.class)); doReturn(clusterUtils).when(command).getClusterUtils(); + doReturn(backend).when(command).getBackend(); + doReturn(vdsBrokerFrontend).when(backend).getResourceManager(); + doReturn(vdsStaticDao).when(command).getVdsStaticDao(); + doReturn(vdsStatisticsDao).when(command).getVdsStatisticsDao(); when(clusterUtils.getUpServer(clusterId)).thenReturn(getVds(VDSStatus.Up)); } @@ -175,6 +220,52 @@ assertTrue("Can't find VM name in can do action messages", foundMessage); } + @Test + public void removeWhenMultipleHosts() { + command = spy(new RemoveVdsCommand<RemoveVdsParameters>(new RemoveVdsParameters(Guid.newGuid(), false))); + prepareMocks(); + + mockVdsWithStatus(VDSStatus.Maintenance); + mockVdsDynamic(); + mockIsGlusterEnabled(true); + mockHasMultipleClusters(true); + mockForExecute(); + command.executeCommand(); + assertEquals(command.getAuditLogTypeValue(), AuditLogType.USER_REMOVE_VDS); + Mockito.verify(vdsDynamicDAO, times(1)).remove(any(Guid.class)); + Mockito.verify(vdsStatisticsDao, times(1)).remove(any(Guid.class)); + Mockito.verify(volumeDao, never()).removeByClusterId(any(Guid.class)); + Mockito.verify(hooksDao, never()).removeAllInCluster(any(Guid.class)); + } + + @Test + public void removeLastHost() { + command = spy(new RemoveVdsCommand<RemoveVdsParameters>(new RemoveVdsParameters(Guid.newGuid(), false))); + prepareMocks(); + + mockVdsWithStatus(VDSStatus.Maintenance); + mockVdsDynamic(); + mockIsGlusterEnabled(true); + mockHasMultipleClusters(false); + mockForExecute(); + command.executeCommand(); + assertEquals(command.getAuditLogTypeValue(), AuditLogType.USER_REMOVE_VDS); + Mockito.verify(vdsDynamicDAO, times(1)).remove(any(Guid.class)); + Mockito.verify(vdsStatisticsDao, times(1)).remove(any(Guid.class)); + Mockito.verify(volumeDao, times(1)).removeByClusterId(any(Guid.class)); + Mockito.verify(hooksDao, times(1)).removeAllInCluster(any(Guid.class)); + } + + private void mockForExecute() { + ejbRule.mockResource(ContainerManagedResourceType.TRANSACTION_MANAGER, new DummyTransactionManager()); + doReturn(null).when(vdsBrokerFrontend).RunVdsCommand(any(VDSCommandType.class), any(VDSParametersBase.class)); + doNothing().when(vdsStaticDao).remove(any(Guid.class)); + doNothing().when(vdsStatisticsDao).remove(any(Guid.class)); + doNothing().when(vdsDynamicDAO).remove(any(Guid.class)); + doNothing().when(volumeDao).removeByClusterId(any(Guid.class)); + doNothing().when(hooksDao).removeAllInCluster(any(Guid.class)); + } + /** * Mocks that a valid {@link VdsDynamic} gets returned. */ -- To view, visit http://gerrit.ovirt.org/21754 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ice3e8b7ee1e35d1f96474c23b71d5265d4c00380 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Sahina Bose <sab...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches