2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.applications.notification.supplier.impl;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
17 import java.util.HashMap;
18 import java.util.HashSet;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Matchers;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
26 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestChangeEventBuildHelper;
27 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestSupplierVerifyHelper;
28 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestSupplierVerifyHelper;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
37 import org.opendaylight.yangtools.yang.binding.DataObject;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 public class NodeNotificationSupplierImplTest {
45 private static final String FLOW_NODE_ID = "test-111";
46 private NodeNotificationSupplierImpl notifSupplierImpl;
47 private NotificationProviderService notifProviderService;
48 private DataBroker dataBroker;
51 public void initalization() {
52 notifProviderService = mock(NotificationProviderService.class);
53 dataBroker = mock(DataBroker.class);
54 notifSupplierImpl = new NodeNotificationSupplierImpl(notifProviderService, dataBroker);
55 TestSupplierVerifyHelper.verifyDataChangeRegistration(dataBroker);
58 @Test(expected = IllegalArgumentException.class)
59 public void testNullChangeEvent() {
60 notifSupplierImpl.onDataChanged(null);
64 public void testNullableChangeEvent() {
65 notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
69 public void testEmptyChangeEvent() {
70 notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
74 public void testCreate() {
75 final NodeUpdated notification = notifSupplierImpl.createNotification(createTestFlowCapableNode(),
76 createTestFlowCapableNodePath());
77 assertNotNull(notification);
78 assertEquals(FLOW_NODE_ID, notification.getId().getValue());
79 assertEquals(FLOW_NODE_ID, notification.getNodeRef().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
83 public void testCreateChangeEvent() {
84 final Map<InstanceIdentifier<?>, DataObject> createdData = new HashMap<>();
85 createdData.put(createTestFlowCapableNodePath(), createTestFlowCapableNode());
86 notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(createdData, null, null));
87 verify(notifProviderService, times(1)).publish(Matchers.any(NodeUpdated.class));
90 @Test(expected = IllegalArgumentException.class)
91 public void testCreateFromNullNode() {
92 notifSupplierImpl.createNotification(null, createTestFlowCapableNodePath());
95 @Test(expected = IllegalArgumentException.class)
96 public void testCreateFromNullPath() {
97 notifSupplierImpl.createNotification(createTestFlowCapableNode(), null);
101 public void testDelete() {
102 final NodeRemoved notification = notifSupplierImpl.deleteNotification(createTestFlowCapableNodePath());
103 assertNotNull(notification);
104 assertEquals(FLOW_NODE_ID, notification.getNodeRef().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
108 public void testDeleteChangeEvent() {
109 final Set<InstanceIdentifier<?>> removeData = new HashSet<>();
110 removeData.add(createTestFlowCapableNodePath());
111 notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(null, null, removeData));
112 verify(notifProviderService, times(1)).publish(Matchers.any(NodeRemoved.class));
115 @Test(expected = IllegalArgumentException.class)
116 public void testDeleteFromNullPath() {
117 notifSupplierImpl.deleteNotification(null);
120 private static InstanceIdentifier<FlowCapableNode> createTestFlowCapableNodePath() {
121 return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
122 .augmentation(FlowCapableNode.class);
125 private static FlowCapableNode createTestFlowCapableNode() {
126 final FlowCapableNodeBuilder builder = new FlowCapableNodeBuilder();
127 return builder.build();