Fix NPE message asserts of JDK15+ 40/100440/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 6 Apr 2022 18:55:38 +0000 (20:55 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 6 Apr 2022 18:57:48 +0000 (20:57 +0200)
When we are running on JDK15+, the NPE encountered will have a
meaningful message. Adjust asserts accordingly.

JIRA: BGPCEP-1003
Change-Id: Id92070a75fa906de21a2de4d12970664e872ea7b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/NextHopAttributeParserTest.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/OriginAttributeParserTest.java

index 90ad242f027a54b494d56b23f10bc48ba7a66086..56bc3aa12607a31e97dde70c14c057c553ac1f9a 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThrows;
 
 import io.netty.buffer.ByteBuf;
@@ -78,19 +77,31 @@ public class NextHopAttributeParserTest {
 
     @Test
     public void testParseEmptyIpv4Attribute() {
-        final NullPointerException ex = assertThrows(NullPointerException.class,
+        final String message = assertThrows(NullPointerException.class,
             () -> registry.serializeAttribute(new AttributesBuilder()
                 .setCNextHop(new Ipv4NextHopCaseBuilder().build())
-                .build(), Unpooled.buffer()));
-        assertNull(ex.getMessage());
+                .build(), Unpooled.buffer()))
+            .getMessage();
+        assertEquals(npeString("Cannot invoke \"org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp"
+            + ".types.rev200120.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHop.getGlobal()\" because the return "
+            + "value of \"org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop"
+            + ".c.next.hop.Ipv4NextHopCase.getIpv4NextHop()\" is null"), message);
     }
 
     @Test
     public void testParseEmptyIpv6Attribute() {
-        final NullPointerException ex = assertThrows(NullPointerException.class,
+        final String message = assertThrows(NullPointerException.class,
             () -> registry.serializeAttribute(new AttributesBuilder()
                 .setCNextHop(new Ipv6NextHopCaseBuilder().build())
-                .build(), Unpooled.buffer()));
-        assertNull(ex.getMessage());
+                .build(), Unpooled.buffer()))
+            .getMessage();
+        assertEquals(npeString("Cannot invoke \"org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp"
+            + ".types.rev200120.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHop.getGlobal()\" because \"nextHop\" "
+            + "is null"), message);
+    }
+
+    // FIXME: remove this method once we require JDK17+
+    private static String npeString(final String helpfulString) {
+        return Runtime.getRuntime().version().feature() >= 15 ? helpfulString : null;
     }
 }
index f2291d9881513193fa873d0412b9ff3454c59c72..5e0072d7f00b7b2b34dbe65b6480151662057a61 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThrows;
 
 import io.netty.buffer.ByteBuf;
@@ -83,10 +82,19 @@ public class OriginAttributeParserTest {
 
     @Test
     public void testParseEmptyAttribute() {
-        final NullPointerException ex = assertThrows(NullPointerException.class,
+        final String message = assertThrows(NullPointerException.class,
             () -> attributeRegistry.serializeAttribute(new AttributesBuilder()
                 .setOrigin(new OriginBuilder().build())
-                .build(), Unpooled.buffer()));
-        assertNull(ex.getMessage());
+                .build(), Unpooled.buffer()))
+            .getMessage();
+        assertEquals(npeString("Cannot invoke \"org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp"
+            + ".types.rev200120.BgpOrigin.getIntValue()\" because the return value of \"org.opendaylight.yang.gen.v1"
+            + ".urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.Origin"
+            + ".getValue()\" is null"), message);
+    }
+
+    // FIXME: remove this method once we require JDK17+
+    private static String npeString(final String helpfulString) {
+        return Runtime.getRuntime().version().feature() >= 15 ? helpfulString : null;
     }
 }