Merge "BUG-2188 :To report (TCP) port number (for the OpenFlow connection) for switch...
[openflowplugin.git] / applications / notification-supplier / src / test / java / org / opendaylight / openflowplugin / applications / notification / supplier / impl / NodeNotificationSupplierImplTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.applications.notification.supplier.impl;
10
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;
16
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.Map;
20 import java.util.Set;
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;
39
40 /**
41  *
42  */
43 public class NodeNotificationSupplierImplTest {
44
45     private static final String FLOW_NODE_ID = "test-111";
46     private NodeNotificationSupplierImpl notifSupplierImpl;
47     private NotificationProviderService notifProviderService;
48     private DataBroker dataBroker;
49
50     @Before
51     public void initalization() {
52         notifProviderService = mock(NotificationProviderService.class);
53         dataBroker = mock(DataBroker.class);
54         notifSupplierImpl = new NodeNotificationSupplierImpl(notifProviderService, dataBroker);
55         TestSupplierVerifyHelper.verifyDataChangeRegistration(dataBroker);
56     }
57
58     @Test(expected = IllegalArgumentException.class)
59     public void testNullChangeEvent() {
60         notifSupplierImpl.onDataChanged(null);
61     }
62
63     @Test
64     public void testNullableChangeEvent() {
65         notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
66     }
67
68     @Test
69     public void testEmptyChangeEvent() {
70         notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
71     }
72
73     @Test
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());
80     }
81
82     @Test
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));
88     }
89
90     @Test(expected = IllegalArgumentException.class)
91     public void testCreateFromNullNode() {
92         notifSupplierImpl.createNotification(null, createTestFlowCapableNodePath());
93     }
94
95     @Test(expected = IllegalArgumentException.class)
96     public void testCreateFromNullPath() {
97         notifSupplierImpl.createNotification(createTestFlowCapableNode(), null);
98     }
99
100     @Test
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());
105     }
106
107     @Test
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));
113     }
114
115     @Test(expected = IllegalArgumentException.class)
116     public void testDeleteFromNullPath() {
117         notifSupplierImpl.deleteNotification(null);
118     }
119
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);
123     }
124
125     private static FlowCapableNode createTestFlowCapableNode() {
126         final FlowCapableNodeBuilder builder = new FlowCapableNodeBuilder();
127         return builder.build();
128     }
129 }
130