Switch to MD-SAL APIs
[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.ArgumentMatchers.any;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.binding.api.DataObjectModification;
24 import org.opendaylight.mdsal.binding.api.DataTreeModification;
25 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
26 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestChangeEventBuildHelper;
27 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestData;
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.InstanceIdentifier;
38
39 public class NodeNotificationSupplierImplTest {
40
41     private static final String FLOW_NODE_ID = "openflow:111";
42     private NodeNotificationSupplierImpl notifSupplierImpl;
43     private NotificationPublishService notifProviderService;
44     private DataBroker dataBroker;
45
46     @Before
47     public void initalization() {
48         notifProviderService = mock(NotificationPublishService.class);
49         dataBroker = mock(DataBroker.class);
50         notifSupplierImpl = new NodeNotificationSupplierImpl(notifProviderService, dataBroker);
51         TestSupplierVerifyHelper.verifyDataTreeChangeListenerRegistration(dataBroker);
52     }
53
54     @Test(expected = NullPointerException.class)
55     public void testNullChangeEvent() {
56         notifSupplierImpl.onDataTreeChanged(null);
57     }
58
59     @Test(expected = NullPointerException.class)
60     public void testNullableChangeEvent() {
61         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
62     }
63
64     @Test
65     public void testEmptyChangeEvent() {
66         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
67     }
68
69     @Test
70     public void testCreate() {
71         final NodeUpdated notification = notifSupplierImpl
72                 .createNotification(createTestFlowCapableNode(), createTestFlowCapableNodePath());
73         assertNotNull(notification);
74         assertEquals(FLOW_NODE_ID, notification.getId().getValue());
75         assertEquals(FLOW_NODE_ID,
76                      notification.getNodeRef().getValue().firstKeyOf(Node.class).getId().getValue());
77     }
78
79     @Test
80     public void testCreateChangeEvent() throws InterruptedException {
81         final TestData<FlowCapableNode> testData = new TestData<>(createTestFlowCapableNodePath(), null,
82                 createTestFlowCapableNode(), DataObjectModification.ModificationType.WRITE);
83         Collection<DataTreeModification<FlowCapableNode>> collection = new ArrayList<>();
84         collection.add(testData);
85         notifSupplierImpl.onDataTreeChanged(collection);
86         verify(notifProviderService, times(1)).putNotification(any(NodeUpdated.class));
87     }
88
89     @Test(expected = IllegalArgumentException.class)
90     public void testCreateFromNullNode() {
91         notifSupplierImpl.createNotification(null, createTestFlowCapableNodePath());
92     }
93
94     @Test(expected = IllegalArgumentException.class)
95     public void testCreateFromNullPath() {
96         notifSupplierImpl.createNotification(createTestFlowCapableNode(), null);
97     }
98
99     @Test
100     public void testDelete() {
101         final NodeRemoved notification = notifSupplierImpl.deleteNotification(createTestFlowCapableNodePath());
102         assertNotNull(notification);
103         assertEquals(FLOW_NODE_ID,
104                      notification.getNodeRef().getValue().firstKeyOf(Node.class).getId().getValue());
105     }
106
107     @Test
108     public void testDeleteChangeEvent() throws InterruptedException {
109         final TestData<FlowCapableNode> testData = new TestData<>(createTestFlowCapableNodePath(),
110                 createTestFlowCapableNode(), null, DataObjectModification.ModificationType.DELETE);
111         Collection<DataTreeModification<FlowCapableNode>> collection = new ArrayList<>();
112         collection.add(testData);
113         notifSupplierImpl.onDataTreeChanged(collection);
114         verify(notifProviderService, times(1)).putNotification(any(NodeRemoved.class));
115     }
116
117     @Test(expected = IllegalArgumentException.class)
118     public void testDeleteFromNullPath() {
119         notifSupplierImpl.deleteNotification(null);
120     }
121
122     private static InstanceIdentifier<FlowCapableNode> createTestFlowCapableNodePath() {
123         return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
124                 .augmentation(FlowCapableNode.class);
125     }
126
127     private static FlowCapableNode createTestFlowCapableNode() {
128         final FlowCapableNodeBuilder builder = new FlowCapableNodeBuilder();
129         return builder.build();
130     }
131 }
132