Switch to MD-SAL APIs
[openflowplugin.git] / applications / notification-supplier / src / test / java / org / opendaylight / openflowplugin / applications / notification / supplier / impl / NodeConnectorNotificationSupplierImplTest.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.FlowCapableNodeConnector;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41
42 public class NodeConnectorNotificationSupplierImplTest {
43
44     private static final String FLOW_NODE_ID = "openflow:111";
45     private static final String FLOW_CODE_CONNECTOR_ID = "test-con-111";
46     private NodeConnectorNotificationSupplierImpl notifSupplierImpl;
47     private NotificationPublishService notifProviderService;
48     private DataBroker dataBroker;
49
50     @Before
51     public void initalization() {
52         notifProviderService = mock(NotificationPublishService.class);
53         dataBroker = mock(DataBroker.class);
54         notifSupplierImpl = new NodeConnectorNotificationSupplierImpl(notifProviderService, dataBroker);
55         TestSupplierVerifyHelper.verifyDataTreeChangeListenerRegistration(dataBroker);
56     }
57
58     @Test(expected = NullPointerException.class)
59     public void testNullChangeEvent() {
60         notifSupplierImpl.onDataTreeChanged(null);
61     }
62
63     @Test(expected = NullPointerException.class)
64     public void testNullableChangeEvent() {
65         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
66     }
67
68     @Test
69     public void testEmptyChangeEvent() {
70         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
71     }
72
73     @Test
74     public void testCreate() {
75         final NodeConnectorUpdated notification = notifSupplierImpl
76                 .createNotification(createTestFlowCapableNodeConnecor(), createTestFlowCapableConnectorNodePath());
77         assertNotNull(notification);
78         assertEquals(FLOW_CODE_CONNECTOR_ID, notification.getId().getValue());
79         assertEquals(FLOW_CODE_CONNECTOR_ID, notification.getNodeConnectorRef().getValue()
80                 .firstKeyOf(NodeConnector.class).getId().getValue());
81         assertEquals(FLOW_NODE_ID,
82                      notification.getNodeConnectorRef().getValue().firstKeyOf(Node.class).getId()
83                              .getValue());
84     }
85
86     @Test
87     public void testCreateChangeEvent() throws InterruptedException {
88         final TestData<FlowCapableNodeConnector> testData = new TestData<>(createTestFlowCapableConnectorNodePath(),
89                 null, createTestFlowCapableNodeConnecor(), DataObjectModification.ModificationType.WRITE);
90         Collection<DataTreeModification<FlowCapableNodeConnector>> collection = new ArrayList<>();
91         collection.add(testData);
92         notifSupplierImpl.onDataTreeChanged(collection);
93         verify(notifProviderService, times(1)).putNotification(any(NodeConnectorUpdated.class));
94     }
95
96     @Test(expected = IllegalArgumentException.class)
97     public void testCreateFromNullNodeConnector() {
98         notifSupplierImpl.createNotification(null, createTestFlowCapableConnectorNodePath());
99     }
100
101     @Test(expected = IllegalArgumentException.class)
102     public void testCreateFromNullPath() {
103         notifSupplierImpl.createNotification(createTestFlowCapableNodeConnecor(), null);
104     }
105
106     @Test
107     public void testDelete() {
108         final NodeConnectorRemoved notification = notifSupplierImpl
109                 .deleteNotification(createTestFlowCapableConnectorNodePath());
110         assertNotNull(notification);
111         assertEquals(FLOW_CODE_CONNECTOR_ID, notification.getNodeConnectorRef().getValue()
112                 .firstKeyOf(NodeConnector.class).getId().getValue());
113         assertEquals(FLOW_NODE_ID,
114                      notification.getNodeConnectorRef().getValue().firstKeyOf(Node.class).getId()
115                              .getValue());
116     }
117
118     @Test
119     public void testDeleteChangeEvent() throws InterruptedException {
120         final TestData<FlowCapableNodeConnector> testData = new TestData<>(createTestFlowCapableConnectorNodePath(),
121                 createTestFlowCapableNodeConnecor(), null, DataObjectModification.ModificationType.DELETE);
122         Collection<DataTreeModification<FlowCapableNodeConnector>> collection = new ArrayList<>();
123         collection.add(testData);
124         notifSupplierImpl.onDataTreeChanged(collection);
125         verify(notifProviderService, times(1)).putNotification(any(NodeConnectorRemoved.class));
126     }
127
128     @Test(expected = IllegalArgumentException.class)
129     public void testDeleteFromNullPath() {
130         notifSupplierImpl.deleteNotification(null);
131     }
132
133     private static InstanceIdentifier<FlowCapableNodeConnector> createTestFlowCapableConnectorNodePath() {
134         return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
135                 .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId(FLOW_CODE_CONNECTOR_ID)))
136                 .augmentation(FlowCapableNodeConnector.class);
137     }
138
139     private static FlowCapableNodeConnector createTestFlowCapableNodeConnecor() {
140         final FlowCapableNodeConnectorBuilder builder = new FlowCapableNodeConnectorBuilder();
141         return builder.build();
142     }
143 }
144