Eliminate use of global service loader state 42/96742/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 30 Jun 2021 14:58:34 +0000 (16:58 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 30 Jun 2021 14:58:34 +0000 (16:58 +0200)
Use normal service loading and clean tests up a bit.

Change-Id: I9c5e09ee8e9cb0d0158ca8c1a82e2e244c7a6e60
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/AggregatorAttributeParserTest.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/AsPathAttributeParserTest.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/AtomicAggregateAttributeParserTest.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/CommunitiesAttributeParserTest.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/LocalPreferenceAttributeParserTest.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/MultiExitDiscriminatorAttributeParserTest.java
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/message/update/OriginatorIdAttributeParserTest.java

index 13c8275b4ad79faab099be12c4838967eee40671..abd40d805b15adb811441e8b0da5e02f7676f2e0 100644 (file)
@@ -12,10 +12,12 @@ import static org.junit.Assert.assertEquals;
 
 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.AsNumber;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
@@ -38,26 +40,24 @@ public class AggregatorAttributeParserTest {
                     .build())
             .build();
 
+    private final AttributeRegistry registry = ServiceLoader.load(BGPExtensionConsumerContext.class).findFirst()
+        .orElseThrow().getAttributeRegistry();
+
     @Test
     public void testAttributeParser() throws BGPParsingException, BGPDocumentedException {
         final ByteBuf actual = Unpooled.buffer();
-        ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
-                .serializeAttribute(RESULT, actual);
+        registry.serializeAttribute(RESULT, actual);
         assertArrayEquals(ATTRIBUTE_BYTES, ByteArray.getAllBytes(actual));
 
-        final Attributes attributeOut = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
-                .getAttributeRegistry().parseAttributes(actual, null).getAttributes();
+        final Attributes attributeOut = registry.parseAttributes(actual, null).getAttributes();
         assertEquals(RESULT.getAggregator(), attributeOut.getAggregator());
     }
 
     @Test
     public void testParseEmptyAttribute() {
         final ByteBuf actual = Unpooled.buffer();
-        ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
-                .serializeAttribute(new AttributesBuilder()
-                        .setAggregator(new AggregatorBuilder()
-                                .build())
-                        .build(), actual);
+        registry.serializeAttribute(new AttributesBuilder().setAggregator(new AggregatorBuilder().build()).build(),
+            actual);
         assertEquals(Unpooled.buffer(), actual);
     }
 }
index a69e9a026a8226f63832bffa59d78f616e86432f..2a7c4968e17940f611973e29e2bd6a34151fb89c 100644 (file)
@@ -12,19 +12,18 @@ import static org.junit.Assert.assertEquals;
 
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
-import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.List;
+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.AsNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.AsPathBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.as.path.Segments;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.as.path.SegmentsBuilder;
 import org.opendaylight.yangtools.yang.common.Uint32;
 
@@ -39,37 +38,33 @@ public class AsPathAttributeParserTest {
     };
     private static final byte[] EMPTY_ATTRIBUTE_BYTES = { (byte) 0x40, (byte) 0x02, (byte) 0x00 };
 
+    private final AttributeRegistry registry = ServiceLoader.load(BGPExtensionConsumerContext.class).findFirst()
+        .orElseThrow().getAttributeRegistry();
+
     @Test
     public void testAttributeParser() throws BGPParsingException, BGPDocumentedException {
-        final List<Segments> segments = new ArrayList<>();
-        segments.add(new SegmentsBuilder()
-                .setAsSet(Arrays.asList(new AsNumber(Uint32.ONE), new AsNumber(Uint32.TWO))).build());
-        segments.add(new SegmentsBuilder()
-                .setAsSequence(Arrays.asList(new AsNumber(Uint32.valueOf(3)), new AsNumber(Uint32.valueOf(4))))
-                .build());
-        final Attributes attr = new AttributesBuilder().setAsPath(new AsPathBuilder()
-                    .setSegments(segments)
-                    .build())
-                .build();
+        final Attributes attr = new AttributesBuilder()
+            .setAsPath(new AsPathBuilder()
+                .setSegments(List.of(
+                    new SegmentsBuilder().setAsSet(List.of(new AsNumber(Uint32.ONE), new AsNumber(Uint32.TWO))).build(),
+                    new SegmentsBuilder()
+                    .setAsSequence(List.of(new AsNumber(Uint32.valueOf(3)), new AsNumber(Uint32.valueOf(4))))
+                    .build()))
+                .build())
+            .build();
 
         final ByteBuf actual = Unpooled.buffer();
-        ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
-                .serializeAttribute(attr, actual);
+        registry.serializeAttribute(attr, actual);
         assertArrayEquals(ATTRIBUTE_BYTES, ByteArray.getAllBytes(actual));
 
-        final Attributes attributeOut = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
-                .getAttributeRegistry().parseAttributes(actual, null).getAttributes();
+        final Attributes attributeOut = registry.parseAttributes(actual, null).getAttributes();
         assertEquals(attr.getAsPath(), attributeOut.getAsPath());
     }
 
     @Test
     public void testParseEmptyAttribute() {
         final ByteBuf actual = Unpooled.buffer();
-        ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
-                .serializeAttribute(new AttributesBuilder()
-                        .setAsPath(new AsPathBuilder()
-                                .build())
-                        .build(), actual);
+        registry.serializeAttribute(new AttributesBuilder().setAsPath(new AsPathBuilder().build()).build(), actual);
         assertEquals(Unpooled.buffer().writeBytes(EMPTY_ATTRIBUTE_BYTES), actual);
     }
 }
index 53459cb2ab3fd88711628912b4f7cfb4f38e39f7..c78da8472340ff16c43091d18ff62a0155a3e7da 100644 (file)
@@ -12,10 +12,12 @@ import static org.junit.Assert.assertEquals;
 
 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.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
@@ -26,17 +28,19 @@ public class AtomicAggregateAttributeParserTest {
     private static final byte[] ATTRIBUTE_BYTES = {(byte) 0x40, (byte) 0x06, (byte) 0x00};
 
     private static final Attributes RESULT = new AttributesBuilder()
-            .setAtomicAggregate(new AtomicAggregateBuilder().build()).build();
+            .setAtomicAggregate(new AtomicAggregateBuilder().build())
+            .build();
+
+    private final AttributeRegistry registry = ServiceLoader.load(BGPExtensionConsumerContext.class).findFirst()
+        .orElseThrow().getAttributeRegistry();
 
     @Test
     public void testAttributeParser() throws BGPParsingException, BGPDocumentedException {
         final ByteBuf actual = Unpooled.buffer();
-        ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
-                .serializeAttribute(RESULT, actual);
+        registry.serializeAttribute(RESULT, actual);
         assertArrayEquals(ATTRIBUTE_BYTES, ByteArray.getAllBytes(actual));
 
-        final Attributes attributeOut = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
-                .getAttributeRegistry().parseAttributes(actual, null).getAttributes();
+        final Attributes attributeOut = registry.parseAttributes(actual, null).getAttributes();
         assertEquals(RESULT.getAtomicAggregate(), attributeOut.getAtomicAggregate());
     }
 }
index 421ecfc4aebfb04e000d839223762cf2557e3ee4..09fc596e90d19913613ef035ed70d8f9117949ea 100644 (file)
@@ -14,8 +14,10 @@ import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.ServiceLoader;
 import org.junit.Test;
-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.protocol.util.NoopReferenceCache;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
@@ -33,6 +35,9 @@ public class CommunitiesAttributeParserTest {
         (byte) 0xFF, (byte) 0xFF, (byte) 0x00, (byte) 0x07
     };
 
+    private final AttributeRegistry registry = ServiceLoader.load(BGPExtensionConsumerContext.class).findFirst()
+        .orElseThrow().getAttributeRegistry();
+
     @Test
     public void testCommunitiesAttributeParser() throws Exception {
         final List<Communities> comms = new ArrayList<>();
@@ -47,11 +52,9 @@ public class CommunitiesAttributeParserTest {
         paBuilder.setCommunities(comms);
 
         final ByteBuf actual = Unpooled.buffer();
-        ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
-            .serializeAttribute(paBuilder.build(), actual);
+        registry.serializeAttribute(paBuilder.build(), actual);
         assertArrayEquals(COMMUNITIES_BYTES, ByteArray.getAllBytes(actual));
-        final Attributes attributeOut = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
-            .getAttributeRegistry().parseAttributes(actual, null).getAttributes();
+        final Attributes attributeOut = registry.parseAttributes(actual, null).getAttributes();
         assertEquals(comms, attributeOut.getCommunities());
     }
 
@@ -59,16 +62,14 @@ public class CommunitiesAttributeParserTest {
     public void testParseEmptyListAttribute() {
         final List<Communities> comms = new ArrayList<>();
         final ByteBuf actual = Unpooled.buffer();
-        ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
-            .serializeAttribute(new AttributesBuilder().setCommunities(comms).build(), actual);
+        registry.serializeAttribute(new AttributesBuilder().setCommunities(comms).build(), actual);
         assertEquals(Unpooled.buffer(), actual);
     }
 
     @Test
     public void testParseEmptyAttribute() {
         final ByteBuf actual = Unpooled.buffer();
-        ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
-            .serializeAttribute(new AttributesBuilder().build(), actual);
+        registry.serializeAttribute(new AttributesBuilder().build(), actual);
         assertEquals(Unpooled.buffer(), actual);
     }
 }
\ No newline at end of file
index bc953803f7913e98b07a8fa22747f7acc5a56739..995c0b756d4edbca85745175779f7375e1699eb0 100644 (file)
@@ -12,10 +12,12 @@ import static org.junit.Assert.assertEquals;
 
 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.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
@@ -31,25 +33,24 @@ public class LocalPreferenceAttributeParserTest {
             .setLocalPref(new LocalPrefBuilder().setPref(Uint32.ONE).build())
             .build();
 
+    private final AttributeRegistry registry = ServiceLoader.load(BGPExtensionConsumerContext.class).findFirst()
+        .orElseThrow().getAttributeRegistry();
+
     @Test
     public void testAttributeParser() throws BGPParsingException, BGPDocumentedException {
         final ByteBuf actual = Unpooled.buffer();
-        ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
-                .serializeAttribute(RESULT, actual);
+        registry.serializeAttribute(RESULT, actual);
         assertArrayEquals(ATTRIBUTE_BYTES, ByteArray.getAllBytes(actual));
 
-        final Attributes attributeOut = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
-                .getAttributeRegistry().parseAttributes(actual, null).getAttributes();
+        final Attributes attributeOut = registry.parseAttributes(actual, null).getAttributes();
         assertEquals(RESULT.getLocalPref(), attributeOut.getLocalPref());
     }
 
     @Test
     public void testParseEmptyAttribute() {
         final ByteBuf actual = Unpooled.buffer();
-        ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
-                .serializeAttribute(new AttributesBuilder()
-                        .setLocalPref(new LocalPrefBuilder().build())
-                        .build(), actual);
+        registry.serializeAttribute(new AttributesBuilder().setLocalPref(new LocalPrefBuilder().build()).build(),
+            actual);
         assertEquals(Unpooled.buffer(), actual);
     }
 }
index 2619267fbf64101bb8a123887c8123896185b306..34dc9c3413d126981f2c17f76eb1780635514be6 100644 (file)
@@ -12,10 +12,12 @@ import static org.junit.Assert.assertEquals;
 
 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.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
@@ -28,27 +30,28 @@ public class MultiExitDiscriminatorAttributeParserTest {
     };
 
     private static final Attributes RESULT = new AttributesBuilder()
-            .setMultiExitDisc(new MultiExitDiscBuilder().setMed(Uint32.ONE).build()).build();
+            .setMultiExitDisc(new MultiExitDiscBuilder().setMed(Uint32.ONE).build())
+            .build();
+
+    private final AttributeRegistry registry = ServiceLoader.load(BGPExtensionConsumerContext.class).findFirst()
+        .orElseThrow().getAttributeRegistry();
 
     @Test
     public void testAttributeParser() throws BGPParsingException, BGPDocumentedException {
         final ByteBuf actual = Unpooled.buffer();
-        ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
-                .serializeAttribute(RESULT, actual);
+        registry.serializeAttribute(RESULT, actual);
         assertArrayEquals(ATTRIBUTE_BYTES, ByteArray.getAllBytes(actual));
 
-        final Attributes attributeOut = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
-                .getAttributeRegistry().parseAttributes(actual, null).getAttributes();
+        final Attributes attributeOut = registry.parseAttributes(actual, null).getAttributes();
         assertEquals(RESULT.getMultiExitDisc(), attributeOut.getMultiExitDisc());
     }
 
     @Test
     public void testParseEmptyAttribute() {
         final ByteBuf actual = Unpooled.buffer();
-        ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
-                .serializeAttribute(new AttributesBuilder()
-                        .setMultiExitDisc(new MultiExitDiscBuilder().build())
-                        .build(), actual);
+        registry.serializeAttribute(new AttributesBuilder()
+            .setMultiExitDisc(new MultiExitDiscBuilder().build())
+            .build(), actual);
         assertEquals(Unpooled.buffer(), actual);
     }
 }
index b79db2c463499e70aa8050c8c50ef430abdcee52..f3d2c6cdb5f1da6c00fffe819c17ad34f2a45369 100644 (file)
@@ -12,10 +12,12 @@ import static org.junit.Assert.assertEquals;
 
 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.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
@@ -34,25 +36,24 @@ public class OriginatorIdAttributeParserTest {
                     .build())
             .build();
 
+    private final AttributeRegistry registry = ServiceLoader.load(BGPExtensionConsumerContext.class).findFirst()
+        .orElseThrow().getAttributeRegistry();
+
     @Test
     public void testAttributeParser() throws BGPParsingException, BGPDocumentedException {
         final ByteBuf actual = Unpooled.buffer();
-        ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
-                .serializeAttribute(RESULT, actual);
+        registry.serializeAttribute(RESULT, actual);
         assertArrayEquals(ATTRIBUTE_BYTES, ByteArray.getAllBytes(actual));
 
-        final Attributes attributeOut = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
-                .getAttributeRegistry().parseAttributes(actual, null).getAttributes();
+        final Attributes attributeOut = registry.parseAttributes(actual, null).getAttributes();
         assertEquals(RESULT.getOriginatorId(), attributeOut.getOriginatorId());
     }
 
     @Test
     public void testParseEmptyAttribute() {
         final ByteBuf actual = Unpooled.buffer();
-        ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
-                .serializeAttribute(new AttributesBuilder()
-                        .setOriginatorId(new OriginatorIdBuilder().build())
-                        .build(), actual);
+        registry.serializeAttribute(new AttributesBuilder().setOriginatorId(new OriginatorIdBuilder().build()).build(),
+            actual);
         assertEquals(Unpooled.buffer(), actual);
     }
 }