Bug 6413: Fix ipv4 and ipv6 patterns used by IpAddressNoZoneBuilder 90/44590/1
authorFilip Gregor <fgregor@cisco.com>
Tue, 16 Aug 2016 08:23:57 +0000 (10:23 +0200)
committerRobert Varga <nite@hq.sk>
Wed, 24 Aug 2016 09:48:28 +0000 (09:48 +0000)
due to vague regex definition there was no way to distinguish
if shorter ip address was ipv6 or ipv4 resulting in exception
added tests

Change-Id: Id81e26f018e718675fd58469204c7ad1b647a7b1
Signed-off-by: Filip Gregor <fgregor@cisco.com>
(cherry picked from commit 25b6f8582bce7502cbfe70bbe04860d5d75ff8f8)

model/ietf/ietf-inet-types-2013-07-15/src/main/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/inet/types/rev130715/IpAddressNoZoneBuilder.java
model/ietf/ietf-inet-types-2013-07-15/src/test/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/inet/types/rev130715/IpAddressNoZoneBuilderTest.java [new file with mode: 0644]

index 4d63a3588eba953a9e9240d3f31bd875c9840aa7..57cd66682883e0bbd035a8a861d69f705c5c3894 100644 (file)
@@ -8,12 +8,8 @@
 
 package org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715;
 
-import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-
-/**
- */
 public class IpAddressNoZoneBuilder {
 
     private static final Pattern IPV4_NO_ZONE_PATTERN =
@@ -21,17 +17,8 @@ public class IpAddressNoZoneBuilder {
     private static final Pattern IPV6_NO_ZONE_PATTERN1 =
         Pattern.compile("[0-9a-fA-F:\\.]*");
 
-
     public static IpAddressNoZone getDefaultInstance(final String defaultValue) {
-        final Matcher ipv4NoZoneMatcher = IPV4_NO_ZONE_PATTERN.matcher(defaultValue);
-
-        if (ipv4NoZoneMatcher.matches()) {
-            if (IPV6_NO_ZONE_PATTERN1.matcher(defaultValue).matches()) {
-                throw new IllegalArgumentException(
-                    String.format("Cannot create IpNoZoneAddress from \"%s\", matches both %s and %s",
-                        defaultValue, Ipv4AddressNoZone.class.getSimpleName(), Ipv6AddressNoZone.class.getSimpleName()));
-
-            }
+        if (IPV4_NO_ZONE_PATTERN.matcher(defaultValue).matches()) {
             return new IpAddressNoZone((new Ipv4AddressNoZone(defaultValue)));
         } else if (IPV6_NO_ZONE_PATTERN1.matcher(defaultValue).matches()) {
             return new IpAddressNoZone((new Ipv6AddressNoZone(defaultValue)));
@@ -39,5 +26,4 @@ public class IpAddressNoZoneBuilder {
             throw new IllegalArgumentException("Cannot create IpAddress from " + defaultValue);
         }
     }
-
-}
+}
\ No newline at end of file
diff --git a/model/ietf/ietf-inet-types-2013-07-15/src/test/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/inet/types/rev130715/IpAddressNoZoneBuilderTest.java b/model/ietf/ietf-inet-types-2013-07-15/src/test/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/inet/types/rev130715/IpAddressNoZoneBuilderTest.java
new file mode 100644 (file)
index 0000000..58acc17
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZoneBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
+
+public class IpAddressNoZoneBuilderTest {
+
+    @Test
+    public void testGetDefaultInstance() throws Exception {
+        testIpv4("1.1.1.1");
+        testIpv4("192.168.155.100");
+        testIpv4("1.192.1.221");
+        testIpv6("ABCD:EF01:2345:6789:ABCD:EF01:2345:6789");
+        testIpv6("2001:DB8:0:0:8:800:200C:417A");
+        testIpv6("0:0:0:0:0:0:0:0");
+        testIpv6("::1.2.3.4");
+        testIpv6("ABCD:EF01:2345:6789:ABCD:EF01:2345:6789");
+        testIpv6("2001:DB8:0:0:8:800:200C:417A");
+        testIpv6("2001:DB8::8:800:200C:417A");
+        testIpv6("FF01:0:0:0:0:0:0:101");
+        testIpv6("FF01::101");
+        testIpv6("0:0:0:0:0:0:0:1");
+        testIpv6("::1");
+        testIpv6("::");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testIllegalArgumentException() {
+        IpAddressNoZoneBuilder.getDefaultInstance("2001:0DB8::CD3/60");
+    }
+
+    private static void testIpv4(final String ip) {
+        final IpAddressNoZone defaultInstance = IpAddressNoZoneBuilder.getDefaultInstance(ip);
+        assertEquals(new IpAddressNoZone(new Ipv4AddressNoZone(ip)), defaultInstance);
+    }
+
+    private static void testIpv6(final String ip) {
+        final IpAddressNoZone defaultInstance = IpAddressNoZoneBuilder.getDefaultInstance(ip);
+        assertEquals(new IpAddressNoZone(new Ipv6AddressNoZone(ip)), defaultInstance);
+    }
+}
\ No newline at end of file