Mike Kolesnik has uploaded a new change for review.

Change subject: engine: Added VM NIC validator
......................................................................

engine: Added VM NIC validator

Validator will be used to check if the NIC is valid for unlinking or for
setting no network on it.

Change-Id: Ib4b5ddd5530fd1435bec471f9b07aa7858abe172
Signed-off-by: Mike Kolesnik <mkole...@redhat.com>
---
A 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/VmNicValidator.java
A 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/VmNicValidatorTest.java
2 files changed, 135 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/68/10268/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/VmNicValidator.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/VmNicValidator.java
new file mode 100644
index 0000000..fd1e034
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/VmNicValidator.java
@@ -0,0 +1,48 @@
+package org.ovirt.engine.core.bll.validator;
+
+import org.ovirt.engine.core.bll.ValidationResult;
+import org.ovirt.engine.core.common.businessentities.VmNetworkInterface;
+import org.ovirt.engine.core.common.config.Config;
+import org.ovirt.engine.core.common.config.ConfigValues;
+import org.ovirt.engine.core.dal.VdcBllMessages;
+
+/**
+ * A class that can validate a {@link VmNetworkInterface} is valid from 
certain aspects.
+ */
+public class VmNicValidator {
+
+    private VmNetworkInterface nic;
+
+    private String version;
+
+    public VmNicValidator(VmNetworkInterface nic, String version) {
+        this.nic = nic;
+        this.version = version;
+    }
+
+    /**
+     * @return An error if unlinking is not supported and the interface is 
unlinked, otherwise it's OK.
+     */
+    public ValidationResult linkedCorrectly() {
+        return !networkLinkingSupported() && !nic.isLinked()
+                ? new 
ValidationResult(VdcBllMessages.UNLINKING_IS_NOT_SUPPORTED, clusterVersion())
+                : ValidationResult.VALID;
+    }
+
+    /**
+     * @return An error if unlinking is not supported and the network is not 
set, otherwise it's OK.
+     */
+    public ValidationResult networkNameValid() {
+        return !networkLinkingSupported() && nic.getNetworkName() == null
+                ? new 
ValidationResult(VdcBllMessages.NULL_NETWORK_IS_NOT_SUPPORTED, clusterVersion())
+                : ValidationResult.VALID;
+    }
+
+    private boolean networkLinkingSupported() {
+        return Config.<Boolean> GetValue(ConfigValues.NetworkLinkingSupported, 
version);
+    }
+
+    private String clusterVersion() {
+        return String.format("$clusterVersion %s", version);
+    }
+}
diff --git 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/VmNicValidatorTest.java
 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/VmNicValidatorTest.java
new file mode 100644
index 0000000..7398204
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/VmNicValidatorTest.java
@@ -0,0 +1,87 @@
+package org.ovirt.engine.core.bll.validator;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.when;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.ovirt.engine.core.bll.ValidationResult;
+import org.ovirt.engine.core.common.businessentities.VmNetworkInterface;
+import org.ovirt.engine.core.common.config.ConfigValues;
+import org.ovirt.engine.core.dal.VdcBllMessages;
+import org.ovirt.engine.core.utils.MockConfigRule;
+
+@RunWith(MockitoJUnitRunner.class)
+public class VmNicValidatorTest {
+
+    @Rule
+    public MockConfigRule mockConfigRule = new MockConfigRule();
+
+    @Mock
+    private VmNetworkInterface nic;
+
+    private VmNicValidator validator;
+
+    @Before
+    public void setup() {
+        validator = new VmNicValidator(nic, null);
+    }
+
+    @Test
+    public void unlinkedWhenUnlinkingNotSupported() throws Exception {
+        unlinkingTest(new 
ValidationResult(VdcBllMessages.UNLINKING_IS_NOT_SUPPORTED), false, false);
+    }
+
+    @Test
+    public void linkedWhenUnlinkingNotSupported() throws Exception {
+        unlinkingTest(ValidationResult.VALID, false, true);
+    }
+
+    @Test
+    public void unlinkedWhenUnlinkingSupported() throws Exception {
+        unlinkingTest(ValidationResult.VALID, true, false);
+    }
+
+    @Test
+    public void linkedWhenUnlinkingSupported() throws Exception {
+        unlinkingTest(ValidationResult.VALID, true, true);
+    }
+
+    @Test
+    public void nullNetworkNameWhenUnlinkingNotSupported() throws Exception {
+        networkNameTest(new 
ValidationResult(VdcBllMessages.NULL_NETWORK_IS_NOT_SUPPORTED), false, null);
+    }
+
+    @Test
+    public void validNetworkNameWhenUnlinkingNotSupported() throws Exception {
+        networkNameTest(ValidationResult.VALID, false, "net");
+    }
+
+    @Test
+    public void nullNetworkNameWhenUnlinkingSupported() throws Exception {
+        networkNameTest(ValidationResult.VALID, true, null);
+    }
+
+    @Test
+    public void validNetworkNameWhenUnlinkingSupported() throws Exception {
+        networkNameTest(ValidationResult.VALID, true, "net");
+    }
+
+    private void unlinkingTest(ValidationResult expected, boolean 
networkLinkingSupported, boolean nicLinked) {
+        mockConfigRule.mockConfigValue(ConfigValues.NetworkLinkingSupported, 
null, networkLinkingSupported);
+        when(nic.isLinked()).thenReturn(nicLinked);
+
+        assertEquals(expected, validator.linkedCorrectly());
+    }
+
+    private void networkNameTest(ValidationResult expected, boolean 
networkLinkingSupported, String networkName) {
+        mockConfigRule.mockConfigValue(ConfigValues.NetworkLinkingSupported, 
null, networkLinkingSupported);
+        when(nic.getNetworkName()).thenReturn(networkName);
+
+        assertEquals(expected, validator.networkNameValid());
+    }
+}


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib4b5ddd5530fd1435bec471f9b07aa7858abe172
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Mike Kolesnik <mkole...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to