Refactor NextHopAttributeParserTest 37/96737/2
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 30 Jun 2021 14:09:56 +0000 (16:09 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 30 Jun 2021 14:20:40 +0000 (16:20 +0200)
Use ServiceLoader and assertThrows() to make the suite more reliable.

Change-Id: I02392e48767933e43e6d2efaf756fb7d5267db4c
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

index 082ab385d54295a550b8c7e01a293f1101c490d2..90ad242f027a54b494d56b23f10bc48ba7a66086 100644 (file)
@@ -9,13 +9,17 @@ 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;
 import io.netty.buffer.Unpooled;
+import java.util.ServiceLoader;
 import org.junit.Test;
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
-import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
+import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
+import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionConsumerContext;
 import org.opendaylight.protocol.util.ByteArray;
 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;
@@ -49,43 +53,44 @@ public class NextHopAttributeParserTest {
                     .setLinkLocal(new Ipv6AddressNoZone("ffff::2"))
                     .build()).build()).build();
 
+    private final AttributeRegistry registry = ServiceLoader.load(BGPExtensionConsumerContext.class)
+        .findFirst().orElseThrow().getAttributeRegistry();
+
     @Test
     public void testIpv4AttributeParser() throws BGPParsingException, BGPDocumentedException {
         final ByteBuf actual = Unpooled.buffer();
-        ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
-                .serializeAttribute(IPV4_RESULT, actual);
+        registry.serializeAttribute(IPV4_RESULT, actual);
         assertArrayEquals(IPV4_NEXT_HOP_BYTES, ByteArray.getAllBytes(actual));
 
-        final Attributes attributeOut = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
-                .getAttributeRegistry().parseAttributes(actual, null).getAttributes();
+        final Attributes attributeOut = registry.parseAttributes(actual, null).getAttributes();
         assertEquals(IPV4_RESULT.getCNextHop(), attributeOut.getCNextHop());
     }
 
     @Test
     public void testIpv6AttributeParser() throws BGPParsingException, BGPDocumentedException {
         final ByteBuf actual = Unpooled.buffer();
-        ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
-                .serializeAttribute(IPV6_RESULT, actual);
+        registry.serializeAttribute(IPV6_RESULT, actual);
         assertArrayEquals(IPV6_NEXT_HOP_BYTES, ByteArray.getAllBytes(actual));
 
-        final Attributes attributeOut = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
-                .getAttributeRegistry().parseAttributes(actual, null).getAttributes();
+        final Attributes attributeOut = registry.parseAttributes(actual, null).getAttributes();
         assertEquals(IPV6_RESULT.getCNextHop(), attributeOut.getCNextHop());
     }
 
-    @Test (expected = NullPointerException.class)
+    @Test
     public void testParseEmptyIpv4Attribute() {
-        ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
-                .serializeAttribute(new AttributesBuilder()
-                        .setCNextHop(new Ipv4NextHopCaseBuilder().build())
-                        .build(), Unpooled.buffer());
+        final NullPointerException ex = assertThrows(NullPointerException.class,
+            () -> registry.serializeAttribute(new AttributesBuilder()
+                .setCNextHop(new Ipv4NextHopCaseBuilder().build())
+                .build(), Unpooled.buffer()));
+        assertNull(ex.getMessage());
     }
 
-    @Test (expected = NullPointerException.class)
+    @Test
     public void testParseEmptyIpv6Attribute() {
-        ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
-                .serializeAttribute(new AttributesBuilder()
-                        .setCNextHop(new Ipv6NextHopCaseBuilder().build())
-                        .build(), Unpooled.buffer());
+        final NullPointerException ex = assertThrows(NullPointerException.class,
+            () -> registry.serializeAttribute(new AttributesBuilder()
+                .setCNextHop(new Ipv6NextHopCaseBuilder().build())
+                .build(), Unpooled.buffer()));
+        assertNull(ex.getMessage());
     }
 }