From 0baa00345e533e1fac2876a4d51c7ddfd73b7b74 Mon Sep 17 00:00:00 2001 From: Peter Nosal Date: Fri, 7 Oct 2016 12:04:55 +0200 Subject: [PATCH] Bug 5947: additional tests for mdsal project #10 - added additional tests - refactored some util classes - properly fixed some issues and checkstyle validations Change-Id: Ibd483985bfc5bed23d61c7c81695398abdd427ae Signed-off-by: Peter Nosal --- .../ietf/util/AbstractIetfInetUtilTest.java | 118 +++++++++++------- .../ietf/util/AbstractIetfYangUtilTest.java | 19 ++- .../mdsal/model/ietf/util/IpClass.java | 8 +- .../mdsal/model/ietf/util/IpUtil.java | 24 ++-- .../mdsal/model/ietf/util/Ipv4UtilsTest.java | 31 +++++ .../mdsal/model/ietf/util/Ipv6UtilsTest.java | 24 +++- .../mdsal/model/ietf/util/MacClass.java | 6 +- .../mdsal/model/ietf/util/MacUtil.java | 2 +- 8 files changed, 164 insertions(+), 68 deletions(-) create mode 100644 model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/Ipv4UtilsTest.java diff --git a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtilTest.java b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtilTest.java index a83fab9119..3f7a6f82b3 100644 --- a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtilTest.java +++ b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfInetUtilTest.java @@ -5,6 +5,7 @@ * 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.mdsal.model.ietf.util; import static org.junit.Assert.assertArrayEquals; @@ -12,12 +13,14 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import com.google.common.net.InetAddresses; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; +import java.net.UnknownHostException; import org.junit.Test; public class AbstractIetfInetUtilTest { @@ -45,25 +48,29 @@ public class AbstractIetfInetUtilTest { assertV4Equals("255.254.253.252", "%5"); assertV4Equals("128.16.0.127", "%5"); - assertEquals(new IpClass("1.2.3.4")._value.toLowerCase(), - UTIL.ipAddressFor(UTIL.ipv4AddressBytes(new IpClass("1.2.3.4")))._value.toLowerCase()); - assertNotEquals(new IpClass("2.3.4.5")._value.toLowerCase(), - UTIL.ipAddressFor(UTIL.ipv4AddressBytes(new IpClass("1.2.3.4")))._value.toLowerCase()); - - assertEquals(new IpClass("FE80::2002:B3FF:FE1E:8329")._value.toLowerCase(), - UTIL.ipAddressFor(UTIL.ipv6AddressBytes(new IpClass("FE80::2002:B3FF:FE1E:8329")))._value.toLowerCase()); - assertNotEquals(new IpClass("FEFF::2002:B3FF:FE1E:8329")._value.toLowerCase(), - UTIL.ipAddressFor(UTIL.ipv6AddressBytes(new IpClass("FE80::2002:B3FF:FE1E:8329")))._value.toLowerCase()); - - assertEquals(new IpClass("1.2.3.4")._value.toLowerCase(), - UTIL.ipAddressFor(UTIL.inetAddressFor(new IpClass("1.2.3.4")))._value.toLowerCase()); - assertNotEquals(new IpClass("2.3.4.5")._value.toLowerCase(), - UTIL.ipAddressFor(UTIL.inetAddressFor(new IpClass("1.2.3.4")))._value.toLowerCase()); - - assertEquals(new IpClass("FE80::2002:B3FF:FE1E:8329")._value.toLowerCase(), - UTIL.ipAddressFor(UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329")))._value.toLowerCase()); - assertNotEquals(new IpClass("FEFF::2002:B3FF:FE1E:8329")._value.toLowerCase(), - UTIL.ipAddressFor(UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329")))._value.toLowerCase()); + assertEquals(new IpClass("1.2.3.4").getValue().toLowerCase(), + UTIL.ipAddressFor(UTIL.ipv4AddressBytes(new IpClass("1.2.3.4"))).getValue().toLowerCase()); + assertNotEquals(new IpClass("2.3.4.5").getValue().toLowerCase(), + UTIL.ipAddressFor(UTIL.ipv4AddressBytes(new IpClass("1.2.3.4"))).getValue().toLowerCase()); + + assertEquals(new IpClass("FE80::2002:B3FF:FE1E:8329").getValue().toLowerCase(), + UTIL.ipAddressFor( + UTIL.ipv6AddressBytes(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase()); + assertNotEquals(new IpClass("FEFF::2002:B3FF:FE1E:8329").getValue().toLowerCase(), + UTIL.ipAddressFor( + UTIL.ipv6AddressBytes(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase()); + + assertEquals(new IpClass("1.2.3.4").getValue().toLowerCase(), + UTIL.ipAddressFor(UTIL.inetAddressFor(new IpClass("1.2.3.4"))).getValue().toLowerCase()); + assertNotEquals(new IpClass("2.3.4.5").getValue().toLowerCase(), + UTIL.ipAddressFor(UTIL.inetAddressFor(new IpClass("1.2.3.4"))).getValue().toLowerCase()); + + assertEquals(new IpClass("FE80::2002:B3FF:FE1E:8329").getValue().toLowerCase(), + UTIL.ipAddressFor( + UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase()); + assertNotEquals(new IpClass("FEFF::2002:B3FF:FE1E:8329").getValue().toLowerCase(), + UTIL.ipAddressFor( + UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase()); } @Test(expected = IllegalArgumentException.class) @@ -93,20 +100,21 @@ public class AbstractIetfInetUtilTest { @Test public void ipv4Tests() throws Exception { IpClass ipClass = new IpClass("1.2.3.4/16"); - assertEquals("1.2.3.4", UTIL.ipv4AddressFrom(ipClass)._value); - assertTrue(UTIL.ipv4PrefixFor(UTIL.ipv4AddressBytes(ipClass))._value.contains("/32")); + assertEquals("1.2.3.4", UTIL.ipv4AddressFrom(ipClass).getValue()); + assertTrue(UTIL.ipv4PrefixFor(UTIL.ipv4AddressBytes(ipClass)).getValue().contains("/32")); ipClass = new IpClass("1.2.3.4"); - assertTrue(UTIL.ipv4PrefixFor(UTIL.inetAddressFor(ipClass))._value.contains("/32")); - assertTrue(UTIL.ipv4PrefixFor(ipClass)._value.contains("/32")); - assertTrue(UTIL.ipv4PrefixFor(ipClass, 16)._value.contains("/16")); - - assertTrue(UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 0)._value.equals("0.0.0.0/0")); - assertTrue(UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 32)._value.equals("1.2.3.4/32")); - assertTrue(UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 0, 0)._value.equals("0.0.0.0/0")); - assertTrue(UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 0, 32)._value.equals("1.2.3.4/32")); - assertTrue(UTIL.ipv4PrefixForShort(new byte[] { 1, 2, 3, 4, 5 }, 1, 32)._value.equals("2.3.4.5/32")); - - assertTrue(UTIL.splitIpv4Prefix(new IpClass("1.2.3.4/16")).getKey()._value.equals("1.2.3.4")); + assertTrue(UTIL.ipv4PrefixFor(UTIL.inetAddressFor(ipClass)).getValue().contains("/32")); + assertTrue(UTIL.ipv4PrefixFor(ipClass).getValue().contains("/32")); + assertTrue(UTIL.ipv4PrefixFor(ipClass, 16).getValue().contains("/16")); + + assertTrue(UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 0).getValue().equals("0.0.0.0/0")); + assertTrue(UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 32).getValue().equals("1.2.3.4/32")); + assertTrue(UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 0, 0).getValue().equals("0.0.0.0/0")); + assertTrue(UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 0, 32).getValue().equals("1.2.3.4/32")); + assertTrue(UTIL.ipv4PrefixForShort(new byte[] { 1, 2, 3, 4, 5 }, 1, 32).getValue().equals("2.3.4.5/32")); + assertTrue(UTIL.ipv4PrefixForShort(new byte[] { 1, 2, 3, 4, 5 }, 0, 1).getValue().equals("1.0.0.0/1")); + + assertTrue(UTIL.splitIpv4Prefix(new IpClass("1.2.3.4/16")).getKey().getValue().equals("1.2.3.4")); assertTrue(UTIL.splitIpv4Prefix(new IpClass("1.2.3.4/16")).getValue().equals(16)); assertArrayEquals(new byte[] { 1,2,3,4,16 }, UTIL.ipv4PrefixToBytes(new IpClass("1.2.3.4/16"))); } @@ -114,19 +122,19 @@ public class AbstractIetfInetUtilTest { @Test public void ipv6Tests() throws Exception { IpClass ipClass = new IpClass("::0/128"); - assertEquals("::0", UTIL.ipv6AddressFrom(ipClass)._value); + assertEquals("::0", UTIL.ipv6AddressFrom(ipClass).getValue()); ipClass = new IpClass("::0"); - assertTrue(UTIL.ipv6PrefixFor(UTIL.ipv6AddressBytes(ipClass))._value.contains("/128")); - assertTrue(UTIL.ipv6PrefixFor(UTIL.inetAddressFor(ipClass))._value.contains("/128")); - assertTrue(UTIL.ipv6PrefixFor(ipClass)._value.contains("/128")); - assertTrue(UTIL.ipv6PrefixFor(ipClass, 16)._value.contains("/16")); + assertTrue(UTIL.ipv6PrefixFor(UTIL.ipv6AddressBytes(ipClass)).getValue().contains("/128")); + assertTrue(UTIL.ipv6PrefixFor(UTIL.inetAddressFor(ipClass)).getValue().contains("/128")); + assertTrue(UTIL.ipv6PrefixFor(ipClass).getValue().contains("/128")); + assertTrue(UTIL.ipv6PrefixFor(ipClass, 16).getValue().contains("/16")); - assertTrue(UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 0)._value.equals("::0/0")); - assertTrue(UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 64)._value.equals("::/64")); - assertTrue(UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 0, 0)._value.equals("::0/0")); - assertTrue(UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 0, 32)._value.equals("::/32")); + assertTrue(UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 0).getValue().equals("::0/0")); + assertTrue(UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 64).getValue().equals("::/64")); + assertTrue(UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 0, 0).getValue().equals("::0/0")); + assertTrue(UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 0, 32).getValue().equals("::/32")); - assertTrue(UTIL.splitIpv6Prefix(new IpClass("::/32")).getKey()._value.equals("::")); + assertTrue(UTIL.splitIpv6Prefix(new IpClass("::/32")).getKey().getValue().equals("::")); assertTrue(UTIL.splitIpv6Prefix(new IpClass("::/32")).getValue().equals(32)); assertArrayEquals(new byte[] { 0, 10, 0, 0, 0, 0, 0, 0, 0, 11, 0, 12, 0, 13, 0, 14, 64 }, UTIL.ipv6PrefixToBytes(new IpClass("A::B:C:D:E/64"))); @@ -134,13 +142,13 @@ public class AbstractIetfInetUtilTest { @Test public void prefixTest() throws Exception { - assertTrue(UTIL.ipPrefixFor(UTIL.inetAddressFor(new IpClass("0.0.0.0")), 16)._value.equals("0.0.0.0/16")); + assertTrue(UTIL.ipPrefixFor(UTIL.inetAddressFor(new IpClass("0.0.0.0")), 16).getValue().equals("0.0.0.0/16")); assertTrue(UTIL.ipPrefixFor(UTIL.inetAddressFor(new IpClass("::")), 64) - ._value.equals("::/64")); + .getValue().equals("::/64")); - assertTrue(UTIL.ipPrefixFor(new byte[] { 0, 0, 0, 0 }, 16)._value.equals("0.0.0.0/16")); + assertTrue(UTIL.ipPrefixFor(new byte[] { 0, 0, 0, 0 }, 16).getValue().equals("0.0.0.0/16")); assertTrue(UTIL.ipPrefixFor(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 64) - ._value.equals("::/64")); + .getValue().equals("::/64")); } @Test @@ -148,4 +156,20 @@ public class AbstractIetfInetUtilTest { assertTrue(UTIL.inetAddressFor(new IpClass("1.2.3.4")) instanceof Inet4Address); assertTrue(UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329")) instanceof Inet6Address); } -} + + @Test(expected = IllegalArgumentException.class) + public void inet4AddressForWithExceptionTest() throws Exception { + final IpClass ipClass = mock(IpClass.class); + doReturn("testClass").when(ipClass).toString(); + doThrow(UnknownHostException.class).when(ipClass).getValue(); + UTIL.inet4AddressFor(ipClass); + } + + @Test(expected = IllegalArgumentException.class) + public void inet6AddressForWithExceptionTest() throws Exception { + final IpClass ipClass = mock(IpClass.class); + doReturn("testClass").when(ipClass).toString(); + doThrow(UnknownHostException.class).when(ipClass).getValue(); + UTIL.inet6AddressFor(ipClass); + } +} \ No newline at end of file diff --git a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtilTest.java b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtilTest.java index 7b3780aab5..fa9484e7a4 100644 --- a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtilTest.java +++ b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtilTest.java @@ -11,6 +11,7 @@ package org.opendaylight.mdsal.model.ietf.util; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.util.Arrays; import org.junit.Test; @@ -21,13 +22,13 @@ public class AbstractIetfYangUtilTest { private static final String CANON = "01:02:1e:5a:fb:88"; @Test - public void testBytesToMac() { + public void testBytesToMac() throws Exception { final MacClass mac = UTIL.macAddressFor(BYTES); - assertEquals(CANON, mac._value); + assertEquals(CANON, mac.getValue()); } @Test - public void testMacToBytes() { + public void testMacToBytes() throws Exception { final byte[] bytes1 = UTIL.bytesFor(new MacClass(CANON)); assertTrue(Arrays.equals(BYTES, bytes1)); @@ -37,7 +38,13 @@ public class AbstractIetfYangUtilTest { @Test public void canonizeMACTest() throws Exception { - assertFalse(UTIL.canonizeMacAddress(new MacClass("01:02:1E:5A:FB:88"))._value - .equals(UTIL.canonizeMacAddress(new MacClass(CANON))._value)); + assertFalse(UTIL.canonizeMacAddress(new MacClass("01:02:1E:5A:FB:88")).getValue() + .equals(UTIL.canonizeMacAddress(new MacClass(CANON)).getValue())); } -} + + @Test(expected = IllegalArgumentException.class) + public void hexValueWithExceptionTest() throws Exception { + AbstractIetfYangUtil.hexValue(Character.highSurrogate(1000)); + fail("Expected invalid character exception"); + } +} \ No newline at end of file diff --git a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpClass.java b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpClass.java index 2f588e15f4..012fb3231f 100644 --- a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpClass.java +++ b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpClass.java @@ -10,8 +10,8 @@ package org.opendaylight.mdsal.model.ietf.util; import com.google.common.base.Preconditions; -public final class IpClass { - final String _value; +public class IpClass { + private final String _value; public IpClass(final String value) { this._value = Preconditions.checkNotNull(value); @@ -20,4 +20,8 @@ public final class IpClass { public IpClass(final IpClass template) { this._value = template._value; } + + String getValue() { + return this._value; + } } \ No newline at end of file diff --git a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpUtil.java b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpUtil.java index d7286b0416..ca4d227f7d 100644 --- a/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpUtil.java +++ b/model/ietf/ietf-type-util/src/test/java/org/opendaylight/mdsal/model/ietf/util/IpUtil.java @@ -5,9 +5,11 @@ * 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.mdsal.model.ietf.util; import java.util.regex.Pattern; +import javax.annotation.Nonnull; import javax.annotation.RegEx; final class IpUtil extends AbstractIetfInetUtil { @@ -21,52 +23,60 @@ final class IpUtil extends AbstractIetfInetUtil { @Override protected String getValue(final MacClass macAddress) { - return macAddress._value; + return macAddress.getValue(); } } \ No newline at end of file -- 2.36.6