OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / applications / notification-supplier / src / test / java / org / opendaylight / openflowplugin / applications / notification / supplier / impl / item / FlowNotificationSupplierImplTest.java
index 5f765f63d7422d7a41aa1accd69e50dee98e2f3c..103d13f20088bd2f0f883174662f327a2c02684a 100644 (file)
@@ -10,20 +10,21 @@ package org.opendaylight.openflowplugin.applications.notification.supplier.impl.
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
+import java.util.ArrayList;
+import java.util.Collection;
 import org.junit.Before;
 import org.junit.Test;
-import org.mockito.Matchers;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestChangeEventBuildHelper;
+import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestData;
 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestSupplierVerifyHelper;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
@@ -39,17 +40,19 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
-import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
 /**
- *
+ * Test for
+ * {@link org.opendaylight.openflowplugin.applications.notification.supplier.impl.item.FlowNotificationSupplierImpl}.
  */
 public class FlowNotificationSupplierImplTest {
 
-    private static final String FLOW_NODE_ID = "test-111";
+    private static final String FLOW_NODE_ID = "openflow:111";
     private static final Short FLOW_TABLE_ID = 111;
+    private static final Short UPDATED_FLOW_TABLE_ID = 100;
     private static final String FLOW_ID = "test-flow-111";
+    private static final String UPDATED_FLOW_ID = "test-flow-100";
     private FlowNotificationSupplierImpl notifSupplierImpl;
     private NotificationProviderService notifProviderService;
     private DataBroker dataBroker;
@@ -59,39 +62,44 @@ public class FlowNotificationSupplierImplTest {
         notifProviderService = mock(NotificationProviderService.class);
         dataBroker = mock(DataBroker.class);
         notifSupplierImpl = new FlowNotificationSupplierImpl(notifProviderService, dataBroker);
-        TestSupplierVerifyHelper.verifyDataChangeRegistration(dataBroker);
+        TestSupplierVerifyHelper.verifyDataTreeChangeListenerRegistration(dataBroker);
     }
 
-    @Test(expected = IllegalArgumentException.class)
+    @Test(expected = NullPointerException.class)
     public void testNullChangeEvent() {
-        notifSupplierImpl.onDataChanged(null);
+        notifSupplierImpl.onDataTreeChanged(null);
     }
 
-    @Test
+    @Test(expected = NullPointerException.class)
     public void testNullableChangeEvent() {
-        notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
+        notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
     }
 
     @Test
     public void testEmptyChangeEvent() {
-        notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
+        notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
     }
 
     @Test
     public void testCreate() {
         final FlowAdded notification = notifSupplierImpl.createNotification(createTestFlow(), createTestFlowPath());
         assertNotNull(notification);
-        assertEquals(FLOW_ID, notification.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId().getValue());
-        assertEquals(FLOW_TABLE_ID, notification.getFlowRef().getValue().firstKeyOf(Table.class, TableKey.class).getId());
-        assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
+        assertEquals(FLOW_ID,
+                     notification.getFlowRef().getValue().firstKeyOf(Flow.class).getId().getValue());
+        assertEquals(FLOW_TABLE_ID,
+                     notification.getFlowRef().getValue().firstKeyOf(Table.class).getId());
+        assertEquals(FLOW_NODE_ID,
+                     notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
     }
 
     @Test
     public void testCreateChangeEvent() {
-        final Map<InstanceIdentifier<?>, DataObject> createdData = new HashMap<>();
-        createdData.put(createTestFlowPath(), createTestFlow());
-        notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(createdData, null, null));
-        verify(notifProviderService, times(1)).publish(Matchers.any(FlowAdded.class));
+        final TestData testData = new TestData(createTestFlowPath(), null, createTestFlow(),
+                                               DataObjectModification.ModificationType.WRITE);
+        Collection<DataTreeModification<Flow>> collection = new ArrayList<>();
+        collection.add(testData);
+        notifSupplierImpl.onDataTreeChanged(collection);
+        verify(notifProviderService, times(1)).publish(any(FlowAdded.class));
     }
 
     @Test(expected = IllegalArgumentException.class)
@@ -108,17 +116,22 @@ public class FlowNotificationSupplierImplTest {
     public void testUpdate() {
         final FlowUpdated notification = notifSupplierImpl.updateNotification(createTestFlow(), createTestFlowPath());
         assertNotNull(notification);
-        assertEquals(FLOW_ID, notification.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId().getValue());
-        assertEquals(FLOW_TABLE_ID, notification.getFlowRef().getValue().firstKeyOf(Table.class, TableKey.class).getId());
-        assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
+        assertEquals(FLOW_ID,
+                     notification.getFlowRef().getValue().firstKeyOf(Flow.class).getId().getValue());
+        assertEquals(FLOW_TABLE_ID,
+                     notification.getFlowRef().getValue().firstKeyOf(Table.class).getId());
+        assertEquals(FLOW_NODE_ID,
+                     notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
     }
 
     @Test
     public void testUpdateChangeEvent() {
-        final Map<InstanceIdentifier<?>, DataObject> createdData = new HashMap<>();
-        createdData.put(createTestFlowPath(), createTestFlow());
-        notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(createdData, null, null));
-        verify(notifProviderService, times(1)).publish(Matchers.any(FlowUpdated.class));
+        final TestData testData = new TestData(createTestFlowPath(), createTestFlow(), createUpdatedTestFlow(),
+                                               DataObjectModification.ModificationType.SUBTREE_MODIFIED);
+        Collection<DataTreeModification<Flow>> collection = new ArrayList<>();
+        collection.add(testData);
+        notifSupplierImpl.onDataTreeChanged(collection);
+        verify(notifProviderService, times(1)).publish(any(FlowUpdated.class));
     }
 
     @Test(expected = IllegalArgumentException.class)
@@ -135,17 +148,22 @@ public class FlowNotificationSupplierImplTest {
     public void testDelete() {
         final FlowRemoved notification = notifSupplierImpl.deleteNotification(createTestFlowPath());
         assertNotNull(notification);
-        assertEquals(FLOW_ID, notification.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId().getValue());
-        assertEquals(FLOW_TABLE_ID, notification.getFlowRef().getValue().firstKeyOf(Table.class, TableKey.class).getId());
-        assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
+        assertEquals(FLOW_ID,
+                     notification.getFlowRef().getValue().firstKeyOf(Flow.class).getId().getValue());
+        assertEquals(FLOW_TABLE_ID,
+                     notification.getFlowRef().getValue().firstKeyOf(Table.class).getId());
+        assertEquals(FLOW_NODE_ID,
+                     notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
     }
 
     @Test
     public void testDeleteChangeEvent() {
-        final Set<InstanceIdentifier<?>> removeData = new HashSet<>();
-        removeData.add(createTestFlowPath());
-        notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(null, null, removeData));
-        verify(notifProviderService, times(1)).publish(Matchers.any(FlowRemoved.class));
+        final TestData testData = new TestData(createTestFlowPath(), createTestFlow(), null,
+                                               DataObjectModification.ModificationType.DELETE);
+        Collection<DataTreeModification<Flow>> collection = new ArrayList<>();
+        collection.add(testData);
+        notifSupplierImpl.onDataTreeChanged(collection);
+        verify(notifProviderService, times(1)).publish(any(FlowRemoved.class));
     }
 
     @Test(expected = IllegalArgumentException.class)
@@ -165,5 +183,12 @@ public class FlowNotificationSupplierImplTest {
         builder.setTableId(FLOW_TABLE_ID);
         return builder.build();
     }
+
+    private static Flow createUpdatedTestFlow() {
+        final FlowBuilder builder = new FlowBuilder();
+        builder.setId(new FlowId(UPDATED_FLOW_ID));
+        builder.setTableId(UPDATED_FLOW_TABLE_ID);
+        return builder.build();
+    }
 }