Modernize codebase a bit
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / config / RibImplTest.java
index a9994a834624af443bd28fea81b8883dc4b70bad..a5a7ff44ddd0b1b86d8842ea9792ddfeb962d122 100644 (file)
@@ -5,15 +5,17 @@
  * 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.protocol.bgp.rib.impl.config;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
-import static org.mockito.Matchers.any;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 
+import com.google.common.collect.ImmutableClassToInstanceMap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.primitives.Shorts;
 import java.util.ArrayList;
@@ -22,15 +24,11 @@ import java.util.List;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeFactory;
 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
+import org.opendaylight.mdsal.dom.api.DOMDataBroker;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
-import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
-import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
-import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
 import org.opendaylight.protocol.bgp.rib.impl.RIBImpl;
 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
@@ -42,29 +40,30 @@ import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.t
 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.GlobalBuilder;
 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4UNICAST;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev160614.AfiSafi2;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev160614.AfiSafi2Builder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.GlobalAddPathsConfig;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.GlobalAddPathsConfigBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.Rib;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.BgpId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
-import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
-import org.osgi.framework.ServiceRegistration;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 
 public class RibImplTest extends AbstractConfig {
     private static final List<AfiSafi> AFISAFIS = new ArrayList<>();
     private static final Long ALL_PATHS = 0L;
-    private static final BgpTableType TABLE_TYPE =
-            new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
+    private static final BgpTableType TABLE_TYPE = new BgpTableTypeImpl(Ipv4AddressFamily.class,
+            UnicastSubsequentAddressFamily.class);
     private static final Ipv4Address BGP_ID = new BgpId(new Ipv4Address("127.0.0.1"));
 
     static {
         AFISAFIS.add(new AfiSafiBuilder().setAfiSafiName(IPV4UNICAST.class)
-                .addAugmentation(AfiSafi2.class, new AfiSafi2Builder().setReceive(true)
+                .addAugmentation(GlobalAddPathsConfig.class, new GlobalAddPathsConfigBuilder().setReceive(true)
                         .setSendMax(Shorts.checkedCast(ALL_PATHS)).build()).build());
     }
 
@@ -77,67 +76,57 @@ public class RibImplTest extends AbstractConfig {
     @Mock
     private DOMSchemaService domSchemaService;
     @Mock
-    private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
-    @Mock
     private ListenerRegistration<?> dataTreeRegistration;
     @Mock
-    private RIBSupport ribSupport;
-    @Mock
-    private ServiceRegistration<?> serviceRegistration;
-
-    private static Global createGlobal() {
-        return new GlobalBuilder()
-                .setAfiSafis(new AfiSafisBuilder().setAfiSafi(AFISAFIS).build())
-                .setConfig(new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.global.base
-                        .ConfigBuilder().setAs(AS).setRouterId(BGP_ID).build()).build();
-    }
+    private RIBSupport<?, ?, ?, ?> ribSupport;
 
     @Override
     @Before
     public void setUp() throws Exception {
         super.setUp();
-        Mockito.doAnswer(invocationOnMock -> {
-            RibImplTest.this.singletonService = (ClusterSingletonService) invocationOnMock.getArguments()[0];
-            return RibImplTest.this.singletonServiceRegistration;
-        }).when(this.clusterSingletonServiceProvider)
-                .registerClusterSingletonService(any(ClusterSingletonService.class));
 
-        Mockito.doReturn(mock(GeneratedClassLoadingStrategy.class)).when(this.extension).getClassLoadingStrategy();
-        Mockito.doReturn(this.ribSupport).when(this.extension).getRIBSupport(any(TablesKey.class));
-        final NodeIdentifier nii = new NodeIdentifier(QName.create("test").intern());
-        Mockito.doReturn(nii).when(this.ribSupport).routeAttributesIdentifier();
-        Mockito.doReturn(ImmutableSet.of()).when(this.ribSupport).cacheableAttributeObjects();
-        final ChoiceNode choiceNode = mock(ChoiceNode.class);
-        Mockito.doReturn(choiceNode).when(this.ribSupport).emptyRoutes();
-        Mockito.doReturn(nii).when(choiceNode).getIdentifier();
-        Mockito.doReturn(QName.create("test").intern()).when(choiceNode).getNodeType();
-        Mockito.doReturn(this.domTx).when(this.domDataBroker).createTransactionChain(any());
+        doReturn(mock(GeneratedClassLoadingStrategy.class)).when(this.extension).getClassLoadingStrategy();
+        doReturn(this.ribSupport).when(this.extension).getRIBSupport(any(TablesKey.class));
+        final NodeIdentifier nii = new NodeIdentifier(QName.create("", "test").intern());
+        doReturn(nii).when(this.ribSupport).routeAttributesIdentifier();
+        doReturn(ImmutableSet.of()).when(this.ribSupport).cacheableAttributeObjects();
+        final MapEntryNode emptyTable = mock(MapEntryNode.class);
+        doReturn(emptyTable).when(this.ribSupport).emptyTable();
+        final NodeIdentifierWithPredicates niie = NodeIdentifierWithPredicates.of(Rib.QNAME,
+                QName.create("", "test").intern(), "t");
+        doReturn(niie).when(emptyTable).getIdentifier();
+        doReturn(QName.create("", "test").intern()).when(emptyTable).getNodeType();
+        doReturn(this.domTx).when(this.domDataBroker).createMergingTransactionChain(any());
         final DOMDataTreeChangeService dOMDataTreeChangeService = mock(DOMDataTreeChangeService.class);
-        Mockito.doReturn(Collections.singletonMap(DOMDataTreeChangeService.class, dOMDataTreeChangeService))
-                .when(this.domDataBroker).getSupportedExtensions();
-        Mockito.doReturn(this.dataTreeRegistration).when(this.domSchemaService).registerSchemaContextListener(any());
-        Mockito.doNothing().when(this.dataTreeRegistration).close();
-        Mockito.doReturn(mock(ListenerRegistration.class)).when(dOMDataTreeChangeService)
+        doReturn(ImmutableClassToInstanceMap.of(DOMDataTreeChangeService.class, dOMDataTreeChangeService))
+                .when(this.domDataBroker).getExtensions();
+        doReturn(this.dataTreeRegistration).when(this.domSchemaService).registerSchemaContextListener(any());
+        doNothing().when(this.dataTreeRegistration).close();
+        doReturn(mock(ListenerRegistration.class)).when(dOMDataTreeChangeService)
                 .registerDataTreeChangeListener(any(), any());
-        Mockito.doNothing().when(this.serviceRegistration).unregister();
     }
 
     @Test
-    public void testRibImpl() throws Exception {
-        final RibImpl ribImpl = new RibImpl(this.clusterSingletonServiceProvider, this.extension, this.dispatcher,
-                this.bindingCodecTreeFactory, this.domDataBroker, this.domSchemaService);
-        ribImpl.setServiceRegistration(this.serviceRegistration);
+    public void testRibImpl() {
+        final RibImpl ribImpl = new RibImpl(
+                this.extension,
+                this.dispatcher,
+                this.policyProvider,
+                this.bindingCodecTreeFactory,
+                this.domDataBroker,
+                getDataBroker(),
+                this.domSchemaService);
         ribImpl.start(createGlobal(), "rib-test", this.tableTypeRegistry);
         verify(this.extension).getClassLoadingStrategy();
-        verify(this.domDataBroker).getSupportedExtensions();
-        verify(this.clusterSingletonServiceProvider).registerClusterSingletonService(any());
+        verify(this.domDataBroker).getExtensions();
         verify(this.domSchemaService).registerSchemaContextListener(any(RIBImpl.class));
-        this.singletonService.instantiateServiceInstance();
-        assertEquals("RIBImpl{}", ribImpl.toString());
-        assertEquals(ServiceGroupIdentifier.create("rib-test-service-group"), ribImpl.getRibIServiceGroupIdentifier());
+        assertEquals("RIBImpl{bgpId=Ipv4Address{_value=127.0.0.1}, localTables=[BgpTableTypeImpl ["
+                + "getAfi()=interface org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types."
+                + "rev180329.Ipv4AddressFamily, "
+                + "getSafi()=interface org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types."
+                + "rev180329.UnicastSubsequentAddressFamily]]}", ribImpl.toString());
         assertEquals(Collections.singleton(new TablesKey(Ipv4AddressFamily.class,
                 UnicastSubsequentAddressFamily.class)), ribImpl.getLocalTablesKeys());
-        assertNotNull(ribImpl.getImportPolicyPeerTracker());
         assertNotNull(ribImpl.getService());
         assertNotNull(ribImpl.getInstanceIdentifier());
         assertEquals(AS, ribImpl.getLocalAs());
@@ -151,6 +140,12 @@ public class RibImplTest extends AbstractConfig {
         ribImpl.close();
         verify(this.dataTreeRegistration).close();
         verify(this.dataTreeRegistration).close();
-        verify(this.serviceRegistration).unregister();
+    }
+
+    private static Global createGlobal() {
+        return new GlobalBuilder()
+                .setAfiSafis(new AfiSafisBuilder().setAfiSafi(AFISAFIS).build())
+                .setConfig(new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.global.base
+                        .ConfigBuilder().setAs(AS).setRouterId(BGP_ID).build()).build();
     }
 }
\ No newline at end of file