Moti Asayag has uploaded a new change for review.

Change subject: core: Add null/empty support to IP Address pattern (#852076)
......................................................................

core: Add null/empty support to IP Address pattern (#852076)

https://bugzilla.redhat.com/852076

The IP Address pattern support also empty string or nulls as values
which might be sent in the parameter classes. Also added a unit-test to
assure the pattern well-covers the various addresses.

Change-Id: If8d38ebf880c72d86f956f7c08aefa12e1894109
Signed-off-by: Moti Asayag <[email protected]>
---
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ValidationUtils.java
A 
backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/IPAddressPatternTest.java
2 files changed, 77 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/34/7634/1

diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ValidationUtils.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ValidationUtils.java
index 13a6603..79215c7 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ValidationUtils.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ValidationUtils.java
@@ -26,7 +26,7 @@
             
"^([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}$";
     public static final String NO_WHITES_SPACE_PATTERN = "\\S+";
     public static final String IP_PATTERN =
-            
"^\\b((25[0-5]|2[0-4]\\d|[01]\\d\\d|\\d?\\d)\\.){3}(25[0-5]|2[0-4]\\d|[01]\\d\\d|\\d?\\d)\\b$";
+            
"^\\b((25[0-5]|2[0-4]\\d|[01]\\d\\d|\\d?\\d)\\.){3}(25[0-5]|2[0-4]\\d|[01]\\d\\d|\\d?\\d)\\b$|^$";
     // NULLABLE_MAC_ADDRESS can be valid mac address: xx:xx:xx:xx:xx:xx or 
empty string,
     // We need it for VMs that is not sending custom MAC address and we 
provide MAC address from
     // the MAC pool - this values came as empty string and we don't want the 
validation fail.
diff --git 
a/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/IPAddressPatternTest.java
 
b/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/IPAddressPatternTest.java
new file mode 100644
index 0000000..d2a2db7
--- /dev/null
+++ 
b/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/IPAddressPatternTest.java
@@ -0,0 +1,76 @@
+package org.ovirt.engine.core.common.utils;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Set;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+import javax.validation.constraints.Pattern;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class IPAddressPatternTest {
+
+    private Validator validator = ValidationUtils.getValidator();
+    private String address;
+    private boolean expectedResult;
+
+    public IPAddressPatternTest(String address, Boolean expectedResult) {
+        this.address = address;
+        this.expectedResult = expectedResult;
+    }
+
+    @Test
+    public void checkIPAdress() {
+        Set<ConstraintViolation<IPAdress>> validate = validator.validate(new 
IPAdress(address));
+        assertEquals(validate.isEmpty(), expectedResult);
+    }
+
+    @Parameterized.Parameters
+    public static Collection<Object[]> ipAddressParams() {
+        return Arrays.asList(new Object[][] {
+                { "1.1.1.1", true },
+                { "255.255.255.255", true },
+                { "192.168.1.1", true },
+                { "10.10.1.1", true },
+                { "127.0.0.1", true },
+                { "", true },
+                { null, true },
+                { "10.10.10", false },
+                { "10.10", false },
+                { "10", false },
+                { "...", false },
+                { "a.10.10.10", false },
+                { "10.a.10.10", false },
+                { "10.10.a.10", false },
+                { "10.10.10.a", false },
+                { "a.a.a.a", false },
+                { "256.10.10.10", false },
+                { "10.256.10.10", false },
+                { "10.10.256.10", false },
+                { "10.10.10.256", false },
+                { "-1.10.10.10", false },
+                { "10.-1.10.10", false },
+                { "10.10.-1.10", false },
+                { "10.10.10.-1", false },
+                { " ", false },
+        });
+    }
+
+    private class IPAdress {
+
+        @Pattern(regexp = ValidationUtils.IP_PATTERN, message = 
"NETWROK_ADDR_IN_STATIC_IP_BAD_FORMAT")
+        private String address;
+
+        public IPAdress(String address) {
+            this.address = address;
+        }
+    }
+
+}


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If8d38ebf880c72d86f956f7c08aefa12e1894109
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Moti Asayag <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to