Martin Mucha has uploaded a new change for review. Change subject: core: tests for ModifiedNetworkAttachmentValidator ......................................................................
core: tests for ModifiedNetworkAttachmentValidator Change-Id: I41a72755888bbaf531d3bcb6a323b962a7be7a33 Signed-off-by: Martin Mucha <[email protected]> --- M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/ModifiedNetworkAttachmentValidator.java A backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/ModifiedNetworkAttachmentValidatorTest.java 2 files changed, 144 insertions(+), 3 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/72/36572/1 diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/ModifiedNetworkAttachmentValidator.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/ModifiedNetworkAttachmentValidator.java index eaf56e4..fbaacbfc 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/ModifiedNetworkAttachmentValidator.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/ModifiedNetworkAttachmentValidator.java @@ -35,9 +35,7 @@ } public ValidationResult networkNotUsedByVms(String networkName) { - List<String> vmNames = - new VmInterfaceManager().findActiveVmsUsingNetworks(host.getId(), - Collections.singleton(networkName)); + List<String> vmNames = findActiveVmsNamesUsingNetwork(networkName); if (vmNames.isEmpty()) { return ValidationResult.VALID; @@ -49,6 +47,11 @@ } } + protected List<String> findActiveVmsNamesUsingNetwork(String networkName) { + return new VmInterfaceManager().findActiveVmsUsingNetworks(host.getId(), + Collections.singleton(networkName)); + } + public ValidationResult networkNotUsedByVms() { return networkNotUsedByVms(getNetwork().getName()); } diff --git a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/ModifiedNetworkAttachmentValidatorTest.java b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/ModifiedNetworkAttachmentValidatorTest.java new file mode 100644 index 0000000..d1e9559 --- /dev/null +++ b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/ModifiedNetworkAttachmentValidatorTest.java @@ -0,0 +1,138 @@ +package org.ovirt.engine.core.bll.validator; + +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.when; +import static org.ovirt.engine.core.bll.validator.ValidationResultMatchers.failsWith; +import static org.ovirt.engine.core.bll.validator.ValidationResultMatchers.isValid; + +import java.util.Collections; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.ovirt.engine.core.common.businessentities.VDS; +import org.ovirt.engine.core.common.businessentities.network.Network; +import org.ovirt.engine.core.common.businessentities.network.NetworkAttachment; +import org.ovirt.engine.core.common.config.Config; +import org.ovirt.engine.core.common.config.ConfigValues; +import org.ovirt.engine.core.common.config.IConfigUtilsInterface; +import org.ovirt.engine.core.common.errors.VdcBllMessages; +import org.ovirt.engine.core.compat.Guid; +import org.ovirt.engine.core.dal.dbbroker.DbFacade; +import org.ovirt.engine.core.dal.dbbroker.DbFacadeLocator; +import org.ovirt.engine.core.dao.network.NetworkAttachmentDao; +import org.ovirt.engine.core.dao.network.NetworkDao; + +@RunWith(MockitoJUnitRunner.class) +public class ModifiedNetworkAttachmentValidatorTest { + + @Mock + private DbFacade dbFacade; + + @Mock + private NetworkAttachmentDao networkAttachmentDaoMock; + + @Mock + private NetworkDao networkDaoMock; + + @Mock + private IConfigUtilsInterface iConfigUtilsInterfaceMock; + + @Before + public void setUp() throws Exception { + DbFacadeLocator.setDbFacade(dbFacade); + when(dbFacade.getNetworkAttachmentDao()).thenReturn(networkAttachmentDaoMock); + when(dbFacade.getNetworkDao()).thenReturn(networkDaoMock); + + Config.setConfigUtils(iConfigUtilsInterfaceMock); + } + + @Test + public void testNetworkAttachmentExistsWhenRecordExist() throws Exception { + NetworkAttachment networkAttachment = new NetworkAttachment(); + networkAttachment.setId(Guid.newGuid()); + when(networkAttachmentDaoMock.get(eq(networkAttachment.getId()))).thenReturn(networkAttachment); + ModifiedNetworkAttachmentValidator validator = new ModifiedNetworkAttachmentValidator( + networkAttachment, + new VDS()); + assertThat(validator.networkAttachmentExists(), isValid()); + } + + @Test + public void testNetworkAttachmentExistsWhenRecordDoesNotExist() throws Exception { + NetworkAttachment networkAttachment = new NetworkAttachment(); + when(networkAttachmentDaoMock.get(eq(networkAttachment.getId()))).thenReturn(null); + ModifiedNetworkAttachmentValidator validator = + new ModifiedNetworkAttachmentValidator(networkAttachment, new VDS()); + + assertThat(validator.networkAttachmentExists(), + failsWith(VdcBllMessages.NETWORK_ATTACHMENT_NOT_EXISTS)); + } + + @Test + public void testNotRemovingManagementNetworkWhenRemovingManagementNetwork() throws Exception { + ModifiedNetworkAttachmentValidator validator = NotRemovingManagementNetwork(true); + + assertThat(validator.notRemovingManagementNetwork(), + failsWith(VdcBllMessages.NETWORK_CANNOT_REMOVE_MANAGEMENT_NETWORK)); + } + + @Test + public void testNotRemovingManagementNetworkWhenNotRemovingManagementNetwork() throws Exception { + ModifiedNetworkAttachmentValidator validator = NotRemovingManagementNetwork(false); + + assertThat(validator.notRemovingManagementNetwork(), isValid()); + } + + private ModifiedNetworkAttachmentValidator NotRemovingManagementNetwork(boolean isManagementNetwork) { + String managementNetworkName = "managementNetworkName"; + + Network network = new Network(); + network.setId(Guid.newGuid()); + network.setName(isManagementNetwork ? managementNetworkName : "networkName"); + + NetworkAttachment networkAttachment = new NetworkAttachment(); + networkAttachment.setId(Guid.newGuid()); + networkAttachment.setNetworkId(network.getId()); + + when(networkAttachmentDaoMock.get(eq(networkAttachment.getId()))).thenReturn(networkAttachment); + when(networkDaoMock.get(eq(networkAttachment.getNetworkId()))).thenReturn(network); + when(iConfigUtilsInterfaceMock.getValue(eq(ConfigValues.ManagementNetwork), anyString())).thenReturn( + managementNetworkName); + + return new ModifiedNetworkAttachmentValidator(networkAttachment, new VDS()); + } + + @Test + public void testNetworkNotUsedByVmsWhenNotUsed() throws Exception { + ModifiedNetworkAttachmentValidator validator = new ModifiedNetworkAttachmentValidator( + new NetworkAttachment(), + new VDS()) { + @Override + protected List<String> findActiveVmsNamesUsingNetwork(String networkName) { + return Collections.emptyList(); + } + }; + + assertThat(validator.networkNotUsedByVms(""), isValid()); + } + + @Test + public void testNetworkNotUsedByVmsWhenUsed() throws Exception { + ModifiedNetworkAttachmentValidator validator = new ModifiedNetworkAttachmentValidator( + new NetworkAttachment(), + new VDS()) { + @Override + protected List<String> findActiveVmsNamesUsingNetwork(String networkName) { + return Collections.singletonList("networkName"); + } + }; + + assertThat(validator.networkNotUsedByVms(""), failsWith(VdcBllMessages.ACTION_TYPE_FAILED_NETWORK_IN_USE)); + } +} -- To view, visit http://gerrit.ovirt.org/36572 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I41a72755888bbaf531d3bcb6a323b962a7be7a33 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Martin Mucha <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
