Migrate forwardingrules-manager to uint types
[openflowplugin.git] / applications / forwardingrules-manager / src / test / java / test / mock / MeterListenerTest.java
index 943c9c7aa3ce66f0c5c71b26fce36daa770df645..ee9f91e3e31fcbf8ed86b8f578c1e63204212eb0 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -7,6 +7,8 @@
  */
 package test.mock;
 
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.awaitility.Awaitility.await;
 import static org.junit.Assert.assertEquals;
 
 import java.util.List;
@@ -16,12 +18,12 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.Mockito;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
-import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.mdsal.binding.api.WriteTransaction;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
+import org.opendaylight.openflowplugin.api.openflow.FlowGroupCacheManager;
+import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
 import org.opendaylight.openflowplugin.applications.frm.impl.DeviceMastershipManager;
 import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
 import org.opendaylight.openflowplugin.applications.frm.recovery.OpenflowServiceRecoveryHandler;
@@ -43,6 +45,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.Rem
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint32;
 import test.mock.util.FRMTest;
 import test.mock.util.RpcProviderRegistryMock;
 import test.mock.util.SalMeterServiceMock;
@@ -52,32 +55,39 @@ public class MeterListenerTest extends FRMTest {
     private ForwardingRulesManagerImpl forwardingRulesManager;
     private static final NodeId NODE_ID = new NodeId("testnode:1");
     private static final NodeKey NODE_KEY = new NodeKey(NODE_ID);
-    RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
+    RpcProviderRegistryMock rpcProviderRegistryMock = new RpcProviderRegistryMock();
     @Mock
     ClusterSingletonServiceProvider clusterSingletonService;
     @Mock
     DeviceMastershipManager deviceMastershipManager;
     @Mock
-    private NotificationProviderService notificationService;
-    @Mock
     private ReconciliationManager reconciliationManager;
     @Mock
     private OpenflowServiceRecoveryHandler openflowServiceRecoveryHandler;
     @Mock
     private ServiceRecoveryRegistry serviceRecoveryRegistry;
+    @Mock
+    private MastershipChangeServiceManager mastershipChangeServiceManager;
+    @Mock
+    private FlowGroupCacheManager flowGroupCacheManager;
+
 
     @Before
     public void setUp() {
         forwardingRulesManager = new ForwardingRulesManagerImpl(
                 getDataBroker(),
                 rpcProviderRegistryMock,
+                rpcProviderRegistryMock,
                 getConfig(),
+                mastershipChangeServiceManager,
                 clusterSingletonService,
-                notificationService,
                 getConfigurationService(),
                 reconciliationManager,
                 openflowServiceRecoveryHandler,
-                serviceRecoveryRegistry);
+                serviceRecoveryRegistry,
+                flowGroupCacheManager,
+                getRegistrationHelper()
+                );
 
         forwardingRulesManager.start();
         // TODO consider tests rewrite (added because of complicated access)
@@ -86,30 +96,31 @@ public class MeterListenerTest extends FRMTest {
     }
 
     @Test
-    public void addTwoMetersTest() throws Exception {
+    public void addTwoMetersTest() {
         addFlowCapableNode(NODE_KEY);
 
-        MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
+        MeterKey meterKey = new MeterKey(new MeterId(Uint32.valueOf(2000)));
         InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
                 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
         Meter meter = new MeterBuilder().withKey(meterKey).setMeterName("meter_one").build();
 
         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
-        assertCommit(writeTx.submit());
+        assertCommit(writeTx.commit());
         SalMeterServiceMock salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
+        await().atMost(10, SECONDS).until(() -> salMeterService.getAddMeterCalls().size() == 1);
         List<AddMeterInput> addMeterCalls = salMeterService.getAddMeterCalls();
         assertEquals(1, addMeterCalls.size());
         assertEquals("DOM-0", addMeterCalls.get(0).getTransactionUri().getValue());
 
-        meterKey = new MeterKey(new MeterId((long) 2001));
+        meterKey = new MeterKey(new MeterId(Uint32.valueOf(2001)));
         meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
                 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
         meter = new MeterBuilder().withKey(meterKey).setMeterName("meter_two").setBarrier(true).build();
         writeTx = getDataBroker().newWriteOnlyTransaction();
         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
-        assertCommit(writeTx.submit());
-        salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
+        assertCommit(writeTx.commit());
+        await().atMost(10, SECONDS).until(() -> salMeterService.getAddMeterCalls().size() == 2);
         addMeterCalls = salMeterService.getAddMeterCalls();
         assertEquals(2, addMeterCalls.size());
         assertEquals("DOM-1", addMeterCalls.get(1).getTransactionUri().getValue());
@@ -117,18 +128,19 @@ public class MeterListenerTest extends FRMTest {
     }
 
     @Test
-    public void updateMeterTest() throws Exception {
+    public void updateMeterTest() {
         addFlowCapableNode(NODE_KEY);
 
-        MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
+        MeterKey meterKey = new MeterKey(new MeterId(Uint32.valueOf(2000)));
         InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
                 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
         Meter meter = new MeterBuilder().withKey(meterKey).setMeterName("meter_one").setBarrier(false).build();
 
         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
-        assertCommit(writeTx.submit());
+        assertCommit(writeTx.commit());
         SalMeterServiceMock salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
+        await().atMost(10, SECONDS).until(() -> salMeterService.getAddMeterCalls().size() == 1);
         List<AddMeterInput> addMeterCalls = salMeterService.getAddMeterCalls();
         assertEquals(1, addMeterCalls.size());
         assertEquals("DOM-0", addMeterCalls.get(0).getTransactionUri().getValue());
@@ -136,8 +148,8 @@ public class MeterListenerTest extends FRMTest {
         meter = new MeterBuilder().withKey(meterKey).setMeterName("meter_two").setBarrier(true).build();
         writeTx = getDataBroker().newWriteOnlyTransaction();
         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
-        assertCommit(writeTx.submit());
-        salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
+        assertCommit(writeTx.commit());
+        await().atMost(10, SECONDS).until(() -> salMeterService.getUpdateMeterCalls().size() == 1);
         List<UpdateMeterInput> updateMeterCalls = salMeterService.getUpdateMeterCalls();
         assertEquals(1, updateMeterCalls.size());
         assertEquals("DOM-1", updateMeterCalls.get(0).getTransactionUri().getValue());
@@ -145,26 +157,27 @@ public class MeterListenerTest extends FRMTest {
     }
 
     @Test
-    public void removeMeterTest() throws Exception {
+    public void removeMeterTest() {
         addFlowCapableNode(NODE_KEY);
 
-        MeterKey meterKey = new MeterKey(new MeterId((long) 2000));
+        MeterKey meterKey = new MeterKey(new MeterId(Uint32.valueOf(2000)));
         InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
                 .augmentation(FlowCapableNode.class).child(Meter.class, meterKey);
         Meter meter = new MeterBuilder().withKey(meterKey).setMeterName("meter_one").build();
 
         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
-        assertCommit(writeTx.submit());
+        assertCommit(writeTx.commit());
         SalMeterServiceMock salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
+        await().atMost(10, SECONDS).until(() -> salMeterService.getAddMeterCalls().size() == 1);
         List<AddMeterInput> addMeterCalls = salMeterService.getAddMeterCalls();
         assertEquals(1, addMeterCalls.size());
         assertEquals("DOM-0", addMeterCalls.get(0).getTransactionUri().getValue());
 
         writeTx = getDataBroker().newWriteOnlyTransaction();
         writeTx.delete(LogicalDatastoreType.CONFIGURATION, meterII);
-        assertCommit(writeTx.submit());
-        salMeterService = (SalMeterServiceMock) forwardingRulesManager.getSalMeterService();
+        assertCommit(writeTx.commit());
+        await().atMost(10, SECONDS).until(() -> salMeterService.getRemoveMeterCalls().size() == 1);
         List<RemoveMeterInput> removeMeterCalls = salMeterService.getRemoveMeterCalls();
         assertEquals(1, removeMeterCalls.size());
         assertEquals("DOM-1", removeMeterCalls.get(0).getTransactionUri().getValue());
@@ -172,17 +185,17 @@ public class MeterListenerTest extends FRMTest {
     }
 
     @Test
-    public void staleMeterCreationTest() throws Exception {
+    public void staleMeterCreationTest() {
         addFlowCapableNode(NODE_KEY);
 
-        StaleMeterKey meterKey = new StaleMeterKey(new MeterId((long) 2000));
+        StaleMeterKey meterKey = new StaleMeterKey(new MeterId(Uint32.valueOf(2000)));
         InstanceIdentifier<StaleMeter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
                 .augmentation(FlowCapableNode.class).child(StaleMeter.class, meterKey);
         StaleMeter meter = new StaleMeterBuilder().withKey(meterKey).setMeterName("stale_meter_one").build();
 
         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
         writeTx.put(LogicalDatastoreType.CONFIGURATION, meterII, meter);
-        assertCommit(writeTx.submit());
+        assertCommit(writeTx.commit());
     }
 
     @After