From 47369d590f6d19cde14aa40c5fd2600e11f29485 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 21 Jul 2021 11:50:58 +0200 Subject: [PATCH] Improve ietf-type-util assertions A number of areas are improved: - use assertThrows instead of @Test(expected=) - do not declare Exception as thrown - use assertThat(foo, instanceOf)) instead of assertTrue() - use assertEquals() instead of assertTrue() Change-Id: I0633f116d8afb2ebc46282e72f25bc4fd9c3b97a Signed-off-by: Robert Varga --- .../ietf/util/AbstractIetfInetUtilTest.java | 141 ++++++++++-------- .../ietf/util/AbstractIetfYangUtilTest.java | 17 ++- 2 files changed, 86 insertions(+), 72 deletions(-) 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 d9a422fcc3..83c1d12e2d 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 @@ -7,10 +7,12 @@ */ package org.opendaylight.mdsal.model.ietf.util; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertThrows; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -36,7 +38,7 @@ public class AbstractIetfInetUtilTest { } @Test - public void testIpToBytesAndBack() throws Exception { + public void testIpToBytesAndBack() { assertV4Equals("1.2.3.4"); assertV4Equals("12.23.34.45"); assertV4Equals("255.254.253.252"); @@ -72,69 +74,75 @@ public class AbstractIetfInetUtilTest { UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329"))).getValue().toLowerCase()); } - @Test(expected = IllegalArgumentException.class) - public void illegalArrayLengthForAddressTest() throws Exception { - UTIL.ipAddressFor(new byte[] { 0, 0, 0 }); + @Test + public void illegalArrayLengthForAddressTest() { + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, + () -> UTIL.ipAddressFor(new byte[] { 0, 0, 0 })); + assertEquals("Invalid array length 3", ex.getMessage()); } - @Test(expected = IllegalArgumentException.class) - public void unhandledAddressTest() throws Exception { + @Test + public void unhandledAddressTest() { final InetAddress adr = mock(InetAddress.class); doReturn("testAddress").when(adr).toString(); - UTIL.ipAddressFor(adr); + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> UTIL.ipAddressFor(adr)); + assertEquals("Unhandled address testAddress", ex.getMessage()); } - @Test(expected = IllegalArgumentException.class) - public void illegalArrayLengthforPrefixTest() throws Exception { - UTIL.ipPrefixFor(new byte[] { 0, 0, 0 }, 0); + @Test + public void illegalArrayLengthforPrefixTest() { + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, + () -> UTIL.ipPrefixFor(new byte[] { 0, 0, 0 }, 0)); + assertEquals("Invalid array length 3", ex.getMessage()); } - @Test(expected = IllegalArgumentException.class) - public void illegalAddressforPrefixTest() throws Exception { + @Test + public void illegalAddressforPrefixTest() { final InetAddress adr = mock(InetAddress.class); doReturn("testAddress").when(adr).toString(); - UTIL.ipPrefixFor(adr, 0); + + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, + () -> UTIL.ipPrefixFor(adr, 0)); + assertEquals("Unhandled address testAddress", ex.getMessage()); } @Test - public void ipv4Tests() throws Exception { - IpClass ipClass = new IpClass("1.2.3.4/16"); - 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)).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)); + public void ipv4Tests() { + assertEquals("1.2.3.4", UTIL.ipv4AddressFrom(new IpClass("1.2.3.4/16")).getValue()); + final IpClass ipClass = new IpClass("1.2.3.4"); + assertEquals("1.2.3.4/32", UTIL.ipv4PrefixFor(UTIL.ipv4AddressBytes(ipClass)).getValue()); + assertEquals("1.2.3.4/32", UTIL.ipv4PrefixFor(UTIL.inetAddressFor(ipClass)).getValue()); + assertEquals("1.2.3.4/32", UTIL.ipv4PrefixFor(ipClass).getValue()); + assertEquals("1.2.3.4/16", UTIL.ipv4PrefixFor(ipClass, 16).getValue()); + + assertEquals("0.0.0.0/0", UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 0).getValue()); + assertEquals("1.2.3.4/32", UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 32).getValue()); + assertEquals("0.0.0.0/0", UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 0, 0).getValue()); + assertEquals("1.2.3.4/32", UTIL.ipv4PrefixForShort(UTIL.ipv4AddressBytes(ipClass), 0, 32).getValue()); + assertEquals("2.3.4.5/32", UTIL.ipv4PrefixForShort(new byte[] { 1, 2, 3, 4, 5 }, 1, 32).getValue()); + assertEquals("1.0.0.0/1", UTIL.ipv4PrefixForShort(new byte[] { 1, 2, 3, 4, 5 }, 0, 1).getValue()); + + assertEquals("1.2.3.4", UTIL.splitIpv4Prefix(new IpClass("1.2.3.4/16")).getKey().getValue()); + assertEquals((Integer) 16, UTIL.splitIpv4Prefix(new IpClass("1.2.3.4/16")).getValue()); assertArrayEquals(new byte[] { 1,2,3,4,16 }, UTIL.ipv4PrefixToBytes(new IpClass("1.2.3.4/16"))); } @Test - public void ipv6Tests() throws Exception { - IpClass ipClass = new IpClass("::0/128"); - assertEquals("::0", UTIL.ipv6AddressFrom(ipClass).getValue()); - ipClass = new IpClass("::0"); - 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).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().getValue().equals("::")); - assertTrue(UTIL.splitIpv6Prefix(new IpClass("::/32")).getValue().equals(32)); + public void ipv6Tests() { + assertEquals("::0", UTIL.ipv6AddressFrom(new IpClass("::0/128")).getValue()); + final IpClass ipClass = new IpClass("::0"); + assertEquals("::/128", UTIL.ipv6PrefixFor(UTIL.ipv6AddressBytes(ipClass)).getValue()); + assertEquals("::/128", UTIL.ipv6PrefixFor(UTIL.inetAddressFor(ipClass)).getValue()); + assertEquals("::0/128", UTIL.ipv6PrefixFor(ipClass).getValue()); + assertEquals("::0/16", UTIL.ipv6PrefixFor(ipClass, 16).getValue()); + + assertEquals("::0/0", UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 0).getValue()); + assertEquals("::/64", UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 64).getValue()); + assertEquals("::0/0", UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 0, 0).getValue()); + assertEquals("::/32", UTIL.ipv6PrefixForShort(UTIL.ipv6AddressBytes(ipClass), 0, 32).getValue()); + + assertEquals("::", UTIL.splitIpv6Prefix(new IpClass("::/32")).getKey().getValue()); + assertEquals((Integer) 32, UTIL.splitIpv6Prefix(new IpClass("::/32")).getValue()); 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"))); @@ -145,40 +153,45 @@ public class AbstractIetfInetUtilTest { } @Test - public void prefixTest() throws Exception { - 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) - .getValue().equals("::/64")); + public void prefixTest() { + assertEquals("0.0.0.0/16", UTIL.ipPrefixFor(UTIL.inetAddressFor(new IpClass("0.0.0.0")), 16).getValue()); + assertEquals("::/64", UTIL.ipPrefixFor(UTIL.inetAddressFor(new IpClass("::")), 64).getValue()); - 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) - .getValue().equals("::/64")); + assertEquals("0.0.0.0/16", UTIL.ipPrefixFor(new byte[] { 0, 0, 0, 0 }, 16).getValue()); + assertEquals("::/64", + UTIL.ipPrefixFor(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 64).getValue()); } @Test - public void inetAddressTest() throws Exception { - assertTrue(UTIL.inetAddressFor(new IpClass("1.2.3.4")) instanceof Inet4Address); - assertTrue(UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329")) instanceof Inet6Address); + public void inetAddressTest() { + assertThat(UTIL.inetAddressFor(new IpClass("1.2.3.4")), instanceOf(Inet4Address.class)); + assertThat(UTIL.inetAddressFor(new IpClass("FE80::2002:B3FF:FE1E:8329")), instanceOf(Inet6Address.class)); } - @Test(expected = IllegalArgumentException.class) - public void inet4AddressForWithExceptionTest() throws Exception { + @Test + public void inet4AddressForWithExceptionTest() { final IpClass ipClass = mock(IpClass.class); doReturn("testClass").when(ipClass).toString(); doAnswer(inv -> { throw new UnknownHostException(); }).when(ipClass).getValue(); - UTIL.inet4AddressFor(ipClass); + + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, + () -> UTIL.inet4AddressFor(ipClass)); + assertEquals("Invalid address testClass", ex.getMessage()); } - @Test(expected = IllegalArgumentException.class) - public void inet6AddressForWithExceptionTest() throws Exception { + @Test + public void inet6AddressForWithExceptionTest() { final IpClass ipClass = mock(IpClass.class); doReturn("testClass").when(ipClass).toString(); doAnswer(inv -> { throw new UnknownHostException(); }).when(ipClass).getValue(); - UTIL.inet6AddressFor(ipClass); + + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, + () -> UTIL.inet6AddressFor(ipClass)); + assertEquals("Invalid address testClass", ex.getMessage()); } @Test 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 e45c19f2c2..4bd7a77777 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 @@ -9,7 +9,7 @@ package org.opendaylight.mdsal.model.ietf.util; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; import java.util.UUID; import org.junit.Test; @@ -20,13 +20,13 @@ public class AbstractIetfYangUtilTest { private static final String CANON = "01:02:1e:5a:fb:88"; @Test - public void testBytesToMac() throws Exception { + public void testBytesToMac() { final MacClass mac = UTIL.macAddressFor(BYTES); assertEquals(CANON, mac.getValue()); } @Test - public void testMacToBytes() throws Exception { + public void testMacToBytes() { final byte[] bytes1 = UTIL.macAddressBytes(new MacClass(CANON)); assertArrayEquals(BYTES, bytes1); @@ -91,13 +91,14 @@ public class AbstractIetfYangUtilTest { } @Test - public void canonizeMACTest() throws Exception { + public void canonizeMACTest() { assertEquals(CANON, UTIL.canonizeMacAddress(new MacClass("01:02:1E:5A:FB:88")).getValue()); } - @Test(expected = IllegalArgumentException.class) - public void hexValueWithExceptionTest() throws Exception { - AbstractIetfYangUtil.hexValue(Character.highSurrogate(1000)); - fail("Expected invalid character exception"); + @Test + public void hexValueWithExceptionTest() { + final IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, + () -> AbstractIetfYangUtil.hexValue(Character.highSurrogate(1000))); + assertEquals("Invalid character 'ퟀ' encountered", ex.getMessage()); } } -- 2.36.6