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.HashMap;
18 import java.util.HashSet;
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.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
39 import org.opendaylight.yangtools.yang.binding.DataObject;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 public class NodeConnectorNotificationSupplierImplTest {
47 private static final String FLOW_NODE_ID = "test-111";
48 private static final String FLOW_CODE_CONNECTOR_ID = "test-con-111";
49 private NodeConnectorNotificationSupplierImpl notifSupplierImpl;
50 private NotificationProviderService notifProviderService;
51 private DataBroker dataBroker;
54 public void initalization() {
55 notifProviderService = mock(NotificationProviderService.class);
56 dataBroker = mock(DataBroker.class);
57 notifSupplierImpl = new NodeConnectorNotificationSupplierImpl(notifProviderService, dataBroker);
58 TestSupplierVerifyHelper.verifyDataChangeRegistration(dataBroker);
61 @Test(expected = IllegalArgumentException.class)
62 public void testNullChangeEvent() {
63 notifSupplierImpl.onDataChanged(null);
67 public void testNullableChangeEvent() {
68 notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
72 public void testEmptyChangeEvent() {
73 notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
77 public void testCreate() {
78 final NodeConnectorUpdated notification = notifSupplierImpl.createNotification(createTestFlowCapableNodeConnecor(),
79 createTestFlowCapableConnectorNodePath());
80 assertNotNull(notification);
81 assertEquals(FLOW_CODE_CONNECTOR_ID, notification.getId().getValue());
82 assertEquals(FLOW_CODE_CONNECTOR_ID, notification.getNodeConnectorRef().getValue()
83 .firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId().getValue());
84 assertEquals(FLOW_NODE_ID, notification.getNodeConnectorRef().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
88 public void testCreateChangeEvent() {
89 final Map<InstanceIdentifier<?>, DataObject> createdData = new HashMap<>();
90 createdData.put(createTestFlowCapableConnectorNodePath(), createTestFlowCapableNodeConnecor());
91 notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(createdData, null, null));
92 verify(notifProviderService, times(1)).publish(Matchers.any(NodeConnectorUpdated.class));
95 @Test(expected = IllegalArgumentException.class)
96 public void testCreateFromNullNodeConnector() {
97 notifSupplierImpl.createNotification(null, createTestFlowCapableConnectorNodePath());
100 @Test(expected = IllegalArgumentException.class)
101 public void testCreateFromNullPath() {
102 notifSupplierImpl.createNotification(createTestFlowCapableNodeConnecor(), null);
106 public void testDelete() {
107 final NodeConnectorRemoved notification = notifSupplierImpl.deleteNotification(createTestFlowCapableConnectorNodePath());
108 assertNotNull(notification);
109 assertEquals(FLOW_CODE_CONNECTOR_ID, notification.getNodeConnectorRef().getValue()
110 .firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId().getValue());
111 assertEquals(FLOW_NODE_ID, notification.getNodeConnectorRef().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
115 public void testDeleteChangeEvent() {
116 final Set<InstanceIdentifier<?>> removeData = new HashSet<>();
117 removeData.add(createTestFlowCapableConnectorNodePath());
118 notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(null, null, removeData));
119 verify(notifProviderService, times(1)).publish(Matchers.any(NodeConnectorRemoved.class));
122 @Test(expected = IllegalArgumentException.class)
123 public void testDeleteFromNullPath() {
124 notifSupplierImpl.deleteNotification(null);
127 private static InstanceIdentifier<FlowCapableNodeConnector> createTestFlowCapableConnectorNodePath() {
128 return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
129 .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId(FLOW_CODE_CONNECTOR_ID))).augmentation(FlowCapableNodeConnector.class);
132 private static FlowCapableNodeConnector createTestFlowCapableNodeConnecor() {
133 final FlowCapableNodeConnectorBuilder builder = new FlowCapableNodeConnectorBuilder();
134 return builder.build();