Merge "Bug 5543 - Bo: Update JUnit tests part_10"
[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.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Map;
22 import java.util.Set;
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Matchers;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
29 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
30 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
31 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestChangeEventBuildHelper;
32 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestData;
33 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestSupplierVerifyHelper;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
46 import org.opendaylight.yangtools.yang.binding.DataObject;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48
49 /**
50  *
51  */
52 public class NodeConnectorNotificationSupplierImplTest {
53
54     private static final String FLOW_NODE_ID = "openflow:111";
55     private static final String FLOW_CODE_CONNECTOR_ID = "test-con-111";
56     private NodeConnectorNotificationSupplierImpl notifSupplierImpl;
57     private NotificationProviderService notifProviderService;
58     private DataBroker dataBroker;
59
60     @Before
61     public void initalization() {
62         notifProviderService = mock(NotificationProviderService.class);
63         dataBroker = mock(DataBroker.class);
64         notifSupplierImpl = new NodeConnectorNotificationSupplierImpl(notifProviderService, dataBroker);
65         TestSupplierVerifyHelper.verifyDataChangeRegistration(dataBroker);
66     }
67
68     @Test(expected = NullPointerException.class)
69     public void testNullChangeEvent() {
70         notifSupplierImpl.onDataTreeChanged(null);
71     }
72
73     @Test(expected = NullPointerException.class)
74     public void testNullableChangeEvent() {
75         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
76     }
77
78     @Test
79     public void testEmptyChangeEvent() {
80         notifSupplierImpl.onDataTreeChanged( TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
81     }
82
83     @Test
84     public void testCreate() {
85         final NodeConnectorUpdated notification = notifSupplierImpl.createNotification(createTestFlowCapableNodeConnecor(),
86                 createTestFlowCapableConnectorNodePath());
87         assertNotNull(notification);
88         assertEquals(FLOW_CODE_CONNECTOR_ID, notification.getId().getValue());
89         assertEquals(FLOW_CODE_CONNECTOR_ID, notification.getNodeConnectorRef().getValue()
90                 .firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId().getValue());
91         assertEquals(FLOW_NODE_ID, notification.getNodeConnectorRef().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
92     }
93
94     @Test
95     public void testCreateChangeEvent() {
96         final TestData testData = new TestData(createTestFlowCapableConnectorNodePath(),null,
97                 createTestFlowCapableNodeConnecor(),DataObjectModification.ModificationType.WRITE);
98         Collection<DataTreeModification<FlowCapableNodeConnector>> collection = new ArrayList<>();
99         collection.add(testData);
100         notifSupplierImpl.onDataTreeChanged(collection);
101         verify(notifProviderService, times(1)).publish(Matchers.any(NodeConnectorUpdated.class));
102     }
103
104     @Test(expected = IllegalArgumentException.class)
105     public void testCreateFromNullNodeConnector() {
106         notifSupplierImpl.createNotification(null, createTestFlowCapableConnectorNodePath());
107     }
108
109     @Test(expected = IllegalArgumentException.class)
110     public void testCreateFromNullPath() {
111         notifSupplierImpl.createNotification(createTestFlowCapableNodeConnecor(), null);
112     }
113
114     @Test
115     public void testDelete() {
116         final NodeConnectorRemoved notification = notifSupplierImpl.deleteNotification(createTestFlowCapableConnectorNodePath());
117         assertNotNull(notification);
118         assertEquals(FLOW_CODE_CONNECTOR_ID, notification.getNodeConnectorRef().getValue()
119                 .firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId().getValue());
120         assertEquals(FLOW_NODE_ID, notification.getNodeConnectorRef().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
121     }
122
123     @Test
124     public void testDeleteChangeEvent() {
125         final TestData testData = new TestData(createTestFlowCapableConnectorNodePath(),
126                 createTestFlowCapableNodeConnecor(),null,DataObjectModification.ModificationType.DELETE);
127         Collection<DataTreeModification<FlowCapableNodeConnector>> collection = new ArrayList<>();
128         collection.add(testData);
129         notifSupplierImpl.onDataTreeChanged(collection);
130         verify(notifProviderService, times(1)).publish(Matchers.any(NodeConnectorRemoved.class));
131     }
132
133     @Test(expected = IllegalArgumentException.class)
134     public void testDeleteFromNullPath() {
135         notifSupplierImpl.deleteNotification(null);
136     }
137
138     private static InstanceIdentifier<FlowCapableNodeConnector> createTestFlowCapableConnectorNodePath() {
139         return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
140                 .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId(FLOW_CODE_CONNECTOR_ID))).augmentation(FlowCapableNodeConnector.class);
141     }
142
143     private static FlowCapableNodeConnector createTestFlowCapableNodeConnecor() {
144         final FlowCapableNodeConnectorBuilder builder = new FlowCapableNodeConnectorBuilder();
145         return builder.build();
146     }
147 }
148