Switch to MD-SAL APIs
[openflowplugin.git] / applications / notification-supplier / src / test / java / org / opendaylight / openflowplugin / applications / notification / supplier / impl / item / GroupNotificationSupplierImplTest.java
index fc890c754fc355c656d706720bc498265d2511fa..154c1302b23f52983c5ee9f4683312cd80bfb243 100644 (file)
@@ -5,25 +5,25 @@
  * 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.openflowplugin.applications.notification.supplier.impl.item;
 
 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.sal.binding.api.NotificationProviderService;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.DataObjectModification;
+import org.opendaylight.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.mdsal.binding.api.NotificationPublishService;
 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.group.service.rev130918.GroupAdded;
@@ -37,41 +37,42 @@ 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 GroupNotificationSupplierImpl}.
  */
 public class GroupNotificationSupplierImplTest {
 
-    private static final String FLOW_NODE_ID = "test-111";
+    private static final String FLOW_NODE_ID = "openflow:111";
     private static final Long GROUP_ID = 111L;
+    private static final Long UPDATED_GROUP_ID = 100L;
+
     private GroupNotificationSupplierImpl notifSupplierImpl;
-    private NotificationProviderService notifProviderService;
+    private NotificationPublishService notifProviderService;
     private DataBroker dataBroker;
 
     @Before
     public void initalization() {
-        notifProviderService = mock(NotificationProviderService.class);
+        notifProviderService = mock(NotificationPublishService.class);
         dataBroker = mock(DataBroker.class);
         notifSupplierImpl = new GroupNotificationSupplierImpl(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
@@ -79,16 +80,21 @@ public class GroupNotificationSupplierImplTest {
         final GroupAdded notification = notifSupplierImpl.createNotification(createTestGroup(), createTestGroupPath());
         assertNotNull(notification);
         assertEquals(GROUP_ID, notification.getGroupId().getValue());
-        assertEquals(GROUP_ID, notification.getGroupRef().getValue().firstKeyOf(Group.class, GroupKey.class).getGroupId().getValue());
-        assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
+        assertEquals(GROUP_ID,
+                     notification.getGroupRef().getValue().firstKeyOf(Group.class).getGroupId()
+                             .getValue());
+        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(createTestGroupPath(), createTestGroup());
-        notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(createdData, null, null));
-        verify(notifProviderService, times(1)).publish(Matchers.any(GroupAdded.class));
+    public void testCreateChangeEvent() throws InterruptedException {
+        final TestData<Group> testData = new TestData<>(createTestGroupPath(), null, createTestGroup(),
+                                                        DataObjectModification.ModificationType.WRITE);
+        Collection<DataTreeModification<Group>> collection = new ArrayList<>();
+        collection.add(testData);
+        notifSupplierImpl.onDataTreeChanged(collection);
+        verify(notifProviderService, times(1)).putNotification(any(GroupAdded.class));
     }
 
     @Test(expected = IllegalArgumentException.class)
@@ -103,19 +109,26 @@ public class GroupNotificationSupplierImplTest {
 
     @Test
     public void testUpdate() {
-        final GroupUpdated notification = notifSupplierImpl.updateNotification(createTestGroup(), createTestGroupPath());
+        final GroupUpdated notification = notifSupplierImpl
+                .updateNotification(createTestGroup(), createTestGroupPath());
         assertNotNull(notification);
         assertEquals(GROUP_ID, notification.getGroupId().getValue());
-        assertEquals(GROUP_ID, notification.getGroupRef().getValue().firstKeyOf(Group.class, GroupKey.class).getGroupId().getValue());
-        assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
+        assertEquals(GROUP_ID,
+                     notification.getGroupRef().getValue().firstKeyOf(Group.class).getGroupId()
+                             .getValue());
+        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(createTestGroupPath(), createTestGroup());
-        notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(createdData, null, null));
-        verify(notifProviderService, times(1)).publish(Matchers.any(GroupUpdated.class));
+    public void testUpdateChangeEvent() throws InterruptedException {
+        final TestData<Group> testData = new TestData<>(createTestGroupPath(), createTestGroup(),
+                                                        createUpdatedTestGroup(),
+                                                        DataObjectModification.ModificationType.SUBTREE_MODIFIED);
+        Collection<DataTreeModification<Group>> collection = new ArrayList<>();
+        collection.add(testData);
+        notifSupplierImpl.onDataTreeChanged(collection);
+        verify(notifProviderService, times(1)).putNotification(any(GroupUpdated.class));
     }
 
     @Test(expected = IllegalArgumentException.class)
@@ -133,16 +146,21 @@ public class GroupNotificationSupplierImplTest {
         final GroupRemoved notification = notifSupplierImpl.deleteNotification(createTestGroupPath());
         assertNotNull(notification);
         assertEquals(GROUP_ID, notification.getGroupId().getValue());
-        assertEquals(GROUP_ID, notification.getGroupRef().getValue().firstKeyOf(Group.class, GroupKey.class).getGroupId().getValue());
-        assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
+        assertEquals(GROUP_ID,
+                     notification.getGroupRef().getValue().firstKeyOf(Group.class).getGroupId()
+                             .getValue());
+        assertEquals(FLOW_NODE_ID,
+                     notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
     }
 
     @Test
-    public void testDeleteChangeEvent() {
-        final Set<InstanceIdentifier<?>> removeData = new HashSet<>();
-        removeData.add(createTestGroupPath());
-        notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(null, null, removeData));
-        verify(notifProviderService, times(1)).publish(Matchers.any(GroupRemoved.class));
+    public void testDeleteChangeEvent() throws InterruptedException {
+        final TestData<Group> testData = new TestData<>(createTestGroupPath(), createTestGroup(), null,
+                                                        DataObjectModification.ModificationType.DELETE);
+        Collection<DataTreeModification<Group>> collection = new ArrayList<>();
+        collection.add(testData);
+        notifSupplierImpl.onDataTreeChanged(collection);
+        verify(notifProviderService, times(1)).putNotification(any(GroupRemoved.class));
     }
 
     @Test(expected = IllegalArgumentException.class)
@@ -160,5 +178,12 @@ public class GroupNotificationSupplierImplTest {
         builder.setGroupId(new GroupId(GROUP_ID));
         return builder.build();
     }
+
+    private static Group createUpdatedTestGroup() {
+        final GroupBuilder builder = new GroupBuilder();
+        builder.setGroupId(new GroupId(UPDATED_GROUP_ID));
+        return builder.build();
+    }
+
 }