Martin Mucha has uploaded a new change for review.

Change subject: core: tests for NetworkAttachmentValidator
......................................................................

core: tests for NetworkAttachmentValidator

• getNetworkValidator —> createNetworkValidator: a) changed
method visibility to package local to be available for
mocking (stubbing) b) removed caching of NetworkValidator for
easier handling in tests + it was probably premature optmization.

• notManagementNetwork —> notRemovingManagementNetwork: renamed method
to be more descriptive; produced error complaints about unability to
remove… not about not-being management network.

Change-Id: Ia4ba4327b93f8628e234e0e2b23234d8b89902b5
Signed-off-by: Martin Mucha <[email protected]>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/HostSetupNetworksValidator.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/NetworkAttachmentValidator.java
A 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/NetworkAttachmentValidatorTest.java
3 files changed, 457 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/70/36570/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/HostSetupNetworksValidator.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/HostSetupNetworksValidator.java
index f9d4cd9..cf4bef6 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/HostSetupNetworksValidator.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/HostSetupNetworksValidator.java
@@ -322,7 +322,7 @@
             NetworkAttachmentValidator validator = new 
NetworkAttachmentValidator(attachmentToValidate, host);
             if (!(validate(validator.networkAttachmentIsSet())
                     && validate(validator.notExternalNetwork())
-                    && validate(validator.notManagementNetwork())
+                    && validate(validator.notRemovingManagementNetwork())
                     && notRemovingLabeledNetworks(attachment, 
getExistingIfaces()))) {
                 passed = false;
             }
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/NetworkAttachmentValidator.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/NetworkAttachmentValidator.java
index 7187516..b9f44ca 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/NetworkAttachmentValidator.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/NetworkAttachmentValidator.java
@@ -26,7 +26,6 @@
     protected VDS host;
 
     private NetworkCluster networkCluster;
-    private NetworkValidator networkValidator;
 
     public NetworkAttachmentValidator(NetworkAttachment attachment, VDS host) {
         this.attachment = attachment;
@@ -38,7 +37,7 @@
     }
 
     public ValidationResult networkExists() {
-        return getNetworkValidator().networkIsSet();
+        return createNetworkValidator().networkIsSet();
     }
 
     public ValidationResult notExternalNetwork() {
@@ -46,8 +45,8 @@
                 .when(getNetwork().isExternal());
     }
 
-    public ValidationResult notManagementNetwork() {
-        return getNetworkValidator().notManagementNetwork();
+    public ValidationResult notRemovingManagementNetwork() {
+        return createNetworkValidator().notManagementNetwork();
     }
 
     public ValidationResult networkAttachedToCluster() {
@@ -144,12 +143,8 @@
         return network;
     }
 
-    private NetworkValidator getNetworkValidator() {
-        if (networkValidator == null) {
-            networkValidator = new NetworkValidator(getNetwork());
-        }
-
-        return networkValidator;
+    NetworkValidator createNetworkValidator() {
+        return new NetworkValidator(getNetwork());
     }
 
     private NetworkCluster getNetworkCluster() {
diff --git 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/NetworkAttachmentValidatorTest.java
 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/NetworkAttachmentValidatorTest.java
new file mode 100644
index 0000000..0642931
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/NetworkAttachmentValidatorTest.java
@@ -0,0 +1,451 @@
+package org.ovirt.engine.core.bll.validator;
+
+import static org.junit.Assert.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+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.Map;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.ovirt.engine.core.bll.ValidationResult;
+import org.ovirt.engine.core.common.businessentities.VDS;
+import org.ovirt.engine.core.common.businessentities.network.IpConfiguration;
+import org.ovirt.engine.core.common.businessentities.network.Network;
+import org.ovirt.engine.core.common.businessentities.network.NetworkAttachment;
+import 
org.ovirt.engine.core.common.businessentities.network.NetworkBootProtocol;
+import org.ovirt.engine.core.common.businessentities.network.NetworkCluster;
+import org.ovirt.engine.core.common.businessentities.network.NetworkClusterId;
+import org.ovirt.engine.core.common.businessentities.network.ProviderNetwork;
+import 
org.ovirt.engine.core.common.businessentities.network.VdsNetworkInterface;
+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.compat.Version;
+import org.ovirt.engine.core.dal.dbbroker.DbFacade;
+import org.ovirt.engine.core.dal.dbbroker.DbFacadeLocator;
+import org.ovirt.engine.core.dao.VdsDAO;
+import org.ovirt.engine.core.dao.network.NetworkClusterDao;
+import org.ovirt.engine.core.dao.network.NetworkDao;
+
+public class NetworkAttachmentValidatorTest {
+
+    private DbFacade dbFacadeMock;
+    private NetworkDao networkDaoMock;
+    private NetworkClusterDao networkClusterDaoMock;
+    private VdsDAO vdsDaoMock;
+
+    @Before
+    public void setUp() throws Exception {
+        dbFacadeMock = mock(DbFacade.class);
+        DbFacadeLocator.setDbFacade(dbFacadeMock);
+
+        networkDaoMock = mock(NetworkDao.class);
+        networkClusterDaoMock = mock(NetworkClusterDao.class);
+        vdsDaoMock = mock(VdsDAO.class);
+
+        when(dbFacadeMock.getNetworkDao()).thenReturn(networkDaoMock);
+        
when(dbFacadeMock.getNetworkClusterDao()).thenReturn(networkClusterDaoMock);
+        when(dbFacadeMock.getVdsDao()).thenReturn(vdsDaoMock);
+    }
+
+    @Test
+    public void testNetworkAttachmentIsSetWhenAttachmentIsNull() throws 
Exception {
+        assertThat(new NetworkAttachmentValidator(null, new 
VDS()).networkAttachmentIsSet(),
+                failsWith(VdcBllMessages.NETWORK_ATTACHMENT_NOT_EXISTS));
+    }
+
+    @Test
+    public void testNetworkAttachmentIsSetWhenAttachmentIsNotNull() throws 
Exception {
+        assertThat(new NetworkAttachmentValidator(new NetworkAttachment(), new 
VDS()).networkAttachmentIsSet(),
+                isValid());
+    }
+
+    /* tests whether validation is properly delegated. 
NetworkAttachmentValidator#networkExists
+    delegates to NetworkValidator#networkIsSet. This test spies on creation of 
NetworkValidator, and returns mocked
+    implementation which returns failing ValidationResult on 
NetworkValidator#networkIsSet. Finally it's tested, whether
+    this ValidationResult was propagated correctly.
+     */
+    @Test
+    public void testNetworkExists() throws Exception {
+        NetworkAttachmentValidator networkAttachmentValidatorSpy = Mockito.spy(
+                new NetworkAttachmentValidator(new NetworkAttachment(), new 
VDS()));
+
+        NetworkValidator networkValidatorSpy = Mockito.spy(new 
NetworkValidator(new Network()));
+        
doReturn(networkValidatorSpy).when(networkAttachmentValidatorSpy).createNetworkValidator();
+
+        ValidationResult propagatedResult = new 
ValidationResult(VdcBllMessages.NETWORK_NOT_EXISTS, "a");
+        doReturn(propagatedResult).when(networkValidatorSpy).networkIsSet();
+
+        assertThat("ValidationResult is not correctly propagated",
+                networkAttachmentValidatorSpy.networkExists(),
+                failsWith(VdcBllMessages.NETWORK_NOT_EXISTS, "a"));
+    }
+
+    @Test
+    public void testNotExternalNetworkWhenExternalNetworkIsProvided() throws 
Exception {
+        Network externalNetwork = new Network();
+        externalNetwork.setProvidedBy(new ProviderNetwork(Guid.newGuid(), ""));
+
+        when(networkDaoMock.get(any(Guid.class))).thenReturn(externalNetwork);
+
+        assertThat(new NetworkAttachmentValidator(new NetworkAttachment(), new 
VDS()).notExternalNetwork(),
+                
failsWith(VdcBllMessages.EXTERNAL_NETWORK_CANNOT_BE_PROVISIONED));
+    }
+
+    @Test
+    public void testNotExternalNetwork() throws Exception {
+        Network notExternalNetwork = new Network();
+        notExternalNetwork.setProvidedBy(null);
+
+        
when(networkDaoMock.get(any(Guid.class))).thenReturn(notExternalNetwork);
+
+        assertThat(new NetworkAttachmentValidator(new NetworkAttachment(), new 
VDS()).notExternalNetwork(), isValid());
+
+    }
+
+    @Test
+    public void testNotRemovingManagementNetwork() throws Exception {
+        NetworkAttachmentValidator networkAttachmentValidatorSpy = Mockito.spy(
+                new NetworkAttachmentValidator(new NetworkAttachment(), new 
VDS()));
+
+        NetworkValidator networkValidatorSpy = Mockito.spy(new 
NetworkValidator(new Network()));
+        
doReturn(networkValidatorSpy).when(networkAttachmentValidatorSpy).createNetworkValidator();
+
+        ValidationResult propagatedResult =
+                new 
ValidationResult(VdcBllMessages.NETWORK_CANNOT_REMOVE_MANAGEMENT_NETWORK, "a");
+        
doReturn(propagatedResult).when(networkValidatorSpy).notManagementNetwork();
+
+        assertThat("ValidationResult is not correctly propagated",
+                networkAttachmentValidatorSpy.notRemovingManagementNetwork(),
+                
failsWith(VdcBllMessages.NETWORK_CANNOT_REMOVE_MANAGEMENT_NETWORK, "a"));
+    }
+
+    @Test
+    public void testNetworkAttachedToClusterWhenAttached() throws Exception {
+        Network network = new Network();
+        network.setId(Guid.newGuid());
+
+        VDS host = new VDS();
+        host.setVdsGroupId(Guid.newGuid());
+
+        NetworkAttachment attachment = new NetworkAttachment();
+        attachment.setNetworkId(network.getId());
+
+        NetworkClusterId networkClusterId = new 
NetworkClusterId(host.getVdsGroupId(), attachment.getNetworkId());
+        when(networkClusterDaoMock.get(eq(networkClusterId))).thenReturn(new 
NetworkCluster());
+        
when(networkDaoMock.get(Mockito.eq(network.getId()))).thenReturn(network);
+
+        assertThat(new NetworkAttachmentValidator(attachment, 
host).networkAttachedToCluster(), isValid());
+    }
+
+    @Test
+    public void testNetworkAttachedToClusterWhenNotAttached() throws Exception 
{
+        Network network = new Network();
+        network.setId(Guid.newGuid());
+
+        VDS host = new VDS();
+        host.setVdsGroupId(Guid.newGuid());
+
+        NetworkAttachment attachment = new NetworkAttachment();
+        attachment.setNetworkId(network.getId());
+
+        
when(networkClusterDaoMock.get(any(NetworkClusterId.class))).thenReturn(null);
+        when(networkDaoMock.get(any(Guid.class))).thenReturn(network);
+
+        assertThat(new NetworkAttachmentValidator(attachment, 
host).networkAttachedToCluster(),
+                failsWith(VdcBllMessages.NETWORK_NOT_EXISTS_IN_CLUSTER));
+    }
+
+    @Test
+    public void 
testIpConfiguredForStaticBootProtocol_WhenIpConfigurationIsNull() throws 
Exception {
+        NetworkAttachment attachment = new NetworkAttachment();
+        attachment.setIpConfiguration(null);
+
+        NetworkAttachmentValidator validator = new 
NetworkAttachmentValidator(attachment, new VDS());
+        assertThat(validator.ipConfiguredForStaticBootProtocol(), isValid());
+    }
+
+    @Test
+    public void 
testIpConfiguredForStaticBootProtocol_WhenIpConfigurationIsNotNullAndBootProtocolIsNotStatic()
 throws Exception {
+
+        assertThat(
+                new NetworkAttachmentValidator(
+                        
createNetworkAttachmentWithIpConfiguration(NetworkBootProtocol.DHCP, null, 
null), new VDS())
+                        .ipConfiguredForStaticBootProtocol(), isValid());
+
+        assertThat(
+                new NetworkAttachmentValidator(
+                        
createNetworkAttachmentWithIpConfiguration(NetworkBootProtocol.NONE, null, 
null), new VDS())
+                        .ipConfiguredForStaticBootProtocol(), isValid());
+    }
+
+    @Test
+    public void 
testIpConfiguredForStaticBootProtocol_WhenIpConfigurationIsNotNullAndBootProtocolIsStaticAndAddressIsNull()
 throws Exception {
+        NetworkAttachment attachment =
+                
createNetworkAttachmentWithIpConfiguration(NetworkBootProtocol.STATIC_IP, null, 
"");
+
+        assertThat(new NetworkAttachmentValidator(attachment, new 
VDS()).ipConfiguredForStaticBootProtocol(),
+                failsWith(VdcBllMessages.NETWORK_ADDR_MANDATORY_IN_STATIC_IP));
+    }
+
+    @Test
+    public void 
testIpConfiguredForStaticBootProtocol_WhenIpConfigurationIsNotNullAndBootProtocolIsStaticAndNetmaskIsNull()
 throws Exception {
+        NetworkAttachment attachment =
+                
createNetworkAttachmentWithIpConfiguration(NetworkBootProtocol.STATIC_IP, "", 
null);
+
+        assertThat(new NetworkAttachmentValidator(attachment, new 
VDS()).ipConfiguredForStaticBootProtocol(),
+                failsWith(VdcBllMessages.NETWORK_ADDR_MANDATORY_IN_STATIC_IP));
+    }
+
+    @Test
+    public void 
testIpConfiguredForStaticBootProtocol_WhenIpConfigurationIsNotNullAndBootProtocolIsStaticAndAddressAndNetmaskIsNotNull()
 throws Exception {
+        NetworkAttachment attachment =
+                
createNetworkAttachmentWithIpConfiguration(NetworkBootProtocol.STATIC_IP, "", 
"");
+
+        assertThat(new NetworkAttachmentValidator(attachment, new 
VDS()).ipConfiguredForStaticBootProtocol(),
+                isValid());
+    }
+
+    private NetworkAttachment 
createNetworkAttachmentWithIpConfiguration(NetworkBootProtocol staticIp,
+            String address,
+            String netmask) {
+
+        IpConfiguration ipConfiguration = new IpConfiguration();
+        ipConfiguration.setAddress(address);
+        ipConfiguration.setNetmask(netmask);
+        ipConfiguration.setBootProtocol(staticIp);
+
+        NetworkAttachment attachment = new NetworkAttachment();
+        attachment.setIpConfiguration(ipConfiguration);
+
+        return attachment;
+    }
+
+    @Test
+    public void 
testBootProtocolSetForDisplayNetworkWhenIpConfigurationWhenNetworkClusterDisplayIsFalse()
 throws Exception {
+        Network network = new Network();
+        network.setId(Guid.newGuid());
+
+        NetworkAttachment attachment = new NetworkAttachment();
+        attachment.setIpConfiguration(null);
+        attachment.setNetworkId(network.getId());
+
+        VDS host = new VDS();
+        host.setVdsGroupId(Guid.newGuid());
+
+        NetworkCluster networkCluster = new NetworkCluster();
+        networkCluster.setDisplay(false);
+
+        NetworkClusterId networkClusterId = new 
NetworkClusterId(host.getVdsGroupId(), attachment.getNetworkId());
+        
when(networkClusterDaoMock.get(eq(networkClusterId))).thenReturn(networkCluster);
+        
when(networkDaoMock.get(Mockito.eq(network.getId()))).thenReturn(network);
+
+        assertThat(new NetworkAttachmentValidator(attachment, 
host).bootProtocolSetForDisplayNetwork(), isValid());
+    }
+
+    @Test
+    public void 
testBootProtocolSetForDisplayNetworkWhenIpConfigurationIsNull() throws 
Exception {
+        Network network = new Network();
+        network.setId(Guid.newGuid());
+
+        NetworkAttachment attachment = new NetworkAttachment();
+        attachment.setIpConfiguration(null);
+        attachment.setNetworkId(network.getId());
+
+        VDS host = new VDS();
+        host.setVdsGroupId(Guid.newGuid());
+
+        NetworkCluster networkCluster = new NetworkCluster();
+        networkCluster.setDisplay(true);
+
+        NetworkClusterId networkClusterId = new 
NetworkClusterId(host.getVdsGroupId(), attachment.getNetworkId());
+        
when(networkClusterDaoMock.get(eq(networkClusterId))).thenReturn(networkCluster);
+        
when(networkDaoMock.get(Mockito.eq(network.getId()))).thenReturn(network);
+
+        assertThat(new NetworkAttachmentValidator(attachment, 
host).bootProtocolSetForDisplayNetwork(),
+                
failsWith(VdcBllMessages.ACTION_TYPE_FAILED_DISPLAY_NETWORK_HAS_NO_BOOT_PROTOCOL));
+    }
+
+    @Test
+    public void testBootProtocolSetForDisplayNetworkWhenBootProtocolIsNone() 
throws Exception {
+        Network network = new Network();
+        network.setId(Guid.newGuid());
+
+        NetworkAttachment attachment =
+                
createNetworkAttachmentWithIpConfiguration(NetworkBootProtocol.NONE, null, 
null);
+        attachment.setNetworkId(network.getId());
+
+        VDS host = new VDS();
+        host.setVdsGroupId(Guid.newGuid());
+
+        NetworkCluster networkCluster = new NetworkCluster();
+        networkCluster.setDisplay(true);
+
+        NetworkClusterId networkClusterId = new 
NetworkClusterId(host.getVdsGroupId(), attachment.getNetworkId());
+        
when(networkClusterDaoMock.get(eq(networkClusterId))).thenReturn(networkCluster);
+        
when(networkDaoMock.get(Mockito.eq(network.getId()))).thenReturn(network);
+
+        assertThat(new NetworkAttachmentValidator(attachment, 
host).bootProtocolSetForDisplayNetwork(),
+                
failsWith(VdcBllMessages.ACTION_TYPE_FAILED_DISPLAY_NETWORK_HAS_NO_BOOT_PROTOCOL));
+    }
+
+    @Test
+    public void testNicExists_WhenNicNameIsNull() throws Exception {
+        NetworkAttachment attachment = new NetworkAttachment();
+        attachment.setNicName(null);
+        assertThat(new NetworkAttachmentValidator(attachment, new 
VDS()).nicExists(),
+                failsWith(VdcBllMessages.HOST_NETWORK_INTERFACE_NOT_EXIST));
+    }
+
+    @Test
+    public void testNicExists_WhenNicNameIsNotNull() throws Exception {
+        NetworkAttachment attachment = new NetworkAttachment();
+        attachment.setNicId(null);
+        attachment.setNicName("whatever");
+
+        assertThat(new NetworkAttachmentValidator(attachment, new 
VDS()).nicExists(), isValid());
+    }
+
+    @Test
+    public void 
testNetworkIpAddressWasSameAsHostnameAndChangedWhenIpConfigurationIsNull() 
throws Exception {
+
+        NetworkAttachment attachment = new NetworkAttachment();
+        attachment.setIpConfiguration(null);
+
+        assertThat(new NetworkAttachmentValidator(attachment, 
null).networkIpAddressWasSameAsHostnameAndChanged(null),
+                isValid());
+    }
+
+    @Test
+    public void 
testNetworkIpAddressWasSameAsHostnameAndChangedWhenIpConfigurationIsNotStatic() 
throws Exception {
+
+        for (NetworkBootProtocol networkBootProtocol : 
NetworkBootProtocol.values()) {
+            if (networkBootProtocol == NetworkBootProtocol.STATIC_IP) {
+                continue;
+            }
+            NetworkAttachment attachment =
+                    
createNetworkAttachmentWithIpConfiguration(networkBootProtocol, null, null);
+
+            assertThat(new NetworkAttachmentValidator(attachment, 
null).networkIpAddressWasSameAsHostnameAndChanged(null),
+                    isValid());
+        }
+    }
+
+    @Test
+    public void 
testNetworkIpAddressWasSameAsHostnameAndChangedWhenIfaceDoesNotExist() throws 
Exception {
+
+        NetworkAttachment attachment =
+                
createNetworkAttachmentWithIpConfiguration(NetworkBootProtocol.STATIC_IP, null, 
null);
+        attachment.setNicName("nicName");
+
+        NetworkAttachmentValidator validator = new 
NetworkAttachmentValidator(attachment, null);
+        Map<String, VdsNetworkInterface> nics = Collections.emptyMap();
+        
assertThat(validator.networkIpAddressWasSameAsHostnameAndChanged(nics), 
isValid());
+    }
+
+    @Test
+    public void testNetworkIpAddressWasSameAsHostnameAndChanged() throws 
Exception {
+
+        VdsNetworkInterface existingInterface = new VdsNetworkInterface();
+        existingInterface.setName("nicName");
+        existingInterface.setAddress("anyAddress");
+
+        NetworkAttachment attachment =
+                
createNetworkAttachmentWithIpConfiguration(NetworkBootProtocol.STATIC_IP, null, 
null);
+        attachment.setNicName(existingInterface.getName());
+
+        VDS host = new VDS();
+        host.setHostName(existingInterface.getAddress());
+
+        NetworkAttachmentValidator validator = new 
NetworkAttachmentValidator(attachment, host);
+        Map<String, VdsNetworkInterface> nics = 
Collections.singletonMap(existingInterface.getName(), existingInterface);
+        assertThat(validator.networkIpAddressWasSameAsHostnameAndChanged(nics),
+                
failsWith(VdcBllMessages.ACTION_TYPE_FAILED_NETWORK_ADDRESS_CANNOT_BE_CHANGED));
+    }
+
+    @Test
+    public void testNetworkNotChangedWhenOldAttachmentIsNull() throws 
Exception {
+        assertThat(new NetworkAttachmentValidator(null, 
null).networkNotChanged(null), isValid());
+    }
+
+    @Test
+    public void testNetworkNotChangedWhenDifferentNetworkIds() throws 
Exception {
+        NetworkAttachment oldAttachment = new NetworkAttachment();
+        oldAttachment.setNetworkId(Guid.newGuid());
+
+        NetworkAttachment attachment = new NetworkAttachment();
+        attachment.setNetworkId(Guid.newGuid());
+
+        assertThat(new NetworkAttachmentValidator(attachment, 
null).networkNotChanged(oldAttachment),
+                failsWith(VdcBllMessages.CANNOT_CHANGE_ATTACHED_NETWORK));
+    }
+
+    @Test
+    public void testNetworkNotChanged() throws Exception {
+        Guid networkId = Guid.newGuid();
+
+        NetworkAttachment oldAttachment = new NetworkAttachment();
+        oldAttachment.setNetworkId(networkId);
+
+        NetworkAttachment attachment = new NetworkAttachment();
+        attachment.setNetworkId(networkId);
+
+        assertThat(new NetworkAttachmentValidator(attachment, 
null).networkNotChanged(oldAttachment), isValid());
+    }
+
+    @Test
+    public void testValidateGateway() throws Exception {
+        NetworkAttachment attachment =
+                
createNetworkAttachmentWithIpConfiguration(NetworkBootProtocol.NONE, null, 
null);
+
+        attachment.getIpConfiguration().setGateway("someGateway");
+
+        Network network = new Network();
+        network.setName("networkName");
+
+        IConfigUtilsInterface iConfigUtilsInterfaceMock = 
mock(IConfigUtilsInterface.class);
+        Config.setConfigUtils(iConfigUtilsInterfaceMock);
+
+        
when(iConfigUtilsInterfaceMock.getValue(eq(ConfigValues.ManagementNetwork), 
anyString())).thenReturn("managementNetworkName");
+        
when(iConfigUtilsInterfaceMock.getValue(eq(ConfigValues.MultipleGatewaysSupported),
 anyString())).thenReturn(false);
+        when(networkDaoMock.get(any(Guid.class))).thenReturn(network);
+
+        VDS host = new VDS();
+        host.setVdsGroupCompatibilityVersion(Version.getLast());
+
+        assertThat(new NetworkAttachmentValidator(attachment, 
host).validateGateway(),
+                failsWith(VdcBllMessages.NETWORK_ATTACH_ILLEGAL_GATEWAY));
+    }
+
+    @Test
+    public void testNetworkNotAttachedToHost() throws Exception {
+        //no vds for network id.
+        
when(vdsDaoMock.getAllForNetwork(any(Guid.class))).thenReturn(Collections.<VDS>emptyList());
+
+        assertThat(new NetworkAttachmentValidator(new NetworkAttachment(), 
null).networkNotAttachedToHost(), isValid());
+    }
+
+    @Test
+    public void testNetworkNotAttachedToHostWhenAttached() throws Exception {
+        VDS host = new VDS();
+        host.setId(Guid.newGuid());
+
+        //no vds for network id.
+        
when(vdsDaoMock.getAllForNetwork(any(Guid.class))).thenReturn(Collections.singletonList(host));
+
+        assertThat(new NetworkAttachmentValidator(new NetworkAttachment(), 
host).networkNotAttachedToHost(),
+                failsWith(VdcBllMessages.NETWORK_ALREADY_ATTACHED_TO_HOST));
+    }
+
+}


-- 
To view, visit http://gerrit.ovirt.org/36570
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia4ba4327b93f8628e234e0e2b23234d8b89902b5
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

Reply via email to