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.ArrayList;
18 import java.util.Collection;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Matchers;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
25 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
26 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
27 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestChangeEventBuildHelper;
28 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestData;
29 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestSupplierVerifyHelper;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 public class NodeConnectorNotificationSupplierImplTest {
48 private static final String FLOW_NODE_ID = "openflow:111";
49 private static final String FLOW_CODE_CONNECTOR_ID = "test-con-111";
50 private NodeConnectorNotificationSupplierImpl notifSupplierImpl;
51 private NotificationProviderService notifProviderService;
52 private DataBroker dataBroker;
55 public void initalization() {
56 notifProviderService = mock(NotificationProviderService.class);
57 dataBroker = mock(DataBroker.class);
58 notifSupplierImpl = new NodeConnectorNotificationSupplierImpl(notifProviderService, dataBroker);
59 TestSupplierVerifyHelper.verifyDataTreeChangeListenerRegistration(dataBroker);
62 @Test(expected = NullPointerException.class)
63 public void testNullChangeEvent() {
64 notifSupplierImpl.onDataTreeChanged(null);
67 @Test(expected = NullPointerException.class)
68 public void testNullableChangeEvent() {
69 notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
73 public void testEmptyChangeEvent() {
74 notifSupplierImpl.onDataTreeChanged( TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
78 public void testCreate() {
79 final NodeConnectorUpdated notification = notifSupplierImpl.createNotification(createTestFlowCapableNodeConnecor(),
80 createTestFlowCapableConnectorNodePath());
81 assertNotNull(notification);
82 assertEquals(FLOW_CODE_CONNECTOR_ID, notification.getId().getValue());
83 assertEquals(FLOW_CODE_CONNECTOR_ID, notification.getNodeConnectorRef().getValue()
84 .firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId().getValue());
85 assertEquals(FLOW_NODE_ID, notification.getNodeConnectorRef().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
89 public void testCreateChangeEvent() {
90 final TestData testData = new TestData(createTestFlowCapableConnectorNodePath(),null,
91 createTestFlowCapableNodeConnecor(),DataObjectModification.ModificationType.WRITE);
92 Collection<DataTreeModification<FlowCapableNodeConnector>> collection = new ArrayList<>();
93 collection.add(testData);
94 notifSupplierImpl.onDataTreeChanged(collection);
95 verify(notifProviderService, times(1)).publish(Matchers.any(NodeConnectorUpdated.class));
98 @Test(expected = IllegalArgumentException.class)
99 public void testCreateFromNullNodeConnector() {
100 notifSupplierImpl.createNotification(null, createTestFlowCapableConnectorNodePath());
103 @Test(expected = IllegalArgumentException.class)
104 public void testCreateFromNullPath() {
105 notifSupplierImpl.createNotification(createTestFlowCapableNodeConnecor(), null);
109 public void testDelete() {
110 final NodeConnectorRemoved notification = notifSupplierImpl.deleteNotification(createTestFlowCapableConnectorNodePath());
111 assertNotNull(notification);
112 assertEquals(FLOW_CODE_CONNECTOR_ID, notification.getNodeConnectorRef().getValue()
113 .firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId().getValue());
114 assertEquals(FLOW_NODE_ID, notification.getNodeConnectorRef().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
118 public void testDeleteChangeEvent() {
119 final TestData testData = new TestData(createTestFlowCapableConnectorNodePath(),
120 createTestFlowCapableNodeConnecor(),null,DataObjectModification.ModificationType.DELETE);
121 Collection<DataTreeModification<FlowCapableNodeConnector>> collection = new ArrayList<>();
122 collection.add(testData);
123 notifSupplierImpl.onDataTreeChanged(collection);
124 verify(notifProviderService, times(1)).publish(Matchers.any(NodeConnectorRemoved.class));
127 @Test(expected = IllegalArgumentException.class)
128 public void testDeleteFromNullPath() {
129 notifSupplierImpl.deleteNotification(null);
132 private static InstanceIdentifier<FlowCapableNodeConnector> createTestFlowCapableConnectorNodePath() {
133 return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
134 .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId(FLOW_CODE_CONNECTOR_ID))).augmentation(FlowCapableNodeConnector.class);
137 private static FlowCapableNodeConnector createTestFlowCapableNodeConnecor() {
138 final FlowCapableNodeConnectorBuilder builder = new FlowCapableNodeConnectorBuilder();
139 return builder.build();