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.item;
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.FlowCapableNode;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 public class FlowNotificationSupplierImplTest {
50 private static final String FLOW_NODE_ID = "test-111";
51 private static final Short FLOW_TABLE_ID = 111;
52 private static final String FLOW_ID = "test-flow-111";
53 private FlowNotificationSupplierImpl notifSupplierImpl;
54 private NotificationProviderService notifProviderService;
55 private DataBroker dataBroker;
58 public void initalization() {
59 notifProviderService = mock(NotificationProviderService.class);
60 dataBroker = mock(DataBroker.class);
61 notifSupplierImpl = new FlowNotificationSupplierImpl(notifProviderService, dataBroker);
62 TestSupplierVerifyHelper.verifyDataChangeRegistration(dataBroker);
65 @Test(expected = IllegalArgumentException.class)
66 public void testNullChangeEvent() {
67 notifSupplierImpl.onDataChanged(null);
71 public void testNullableChangeEvent() {
72 notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
76 public void testEmptyChangeEvent() {
77 notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
81 public void testCreate() {
82 final FlowAdded notification = notifSupplierImpl.createNotification(createTestFlow(), createTestFlowPath());
83 assertNotNull(notification);
84 assertEquals(FLOW_ID, notification.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId().getValue());
85 assertEquals(FLOW_TABLE_ID, notification.getFlowRef().getValue().firstKeyOf(Table.class, TableKey.class).getId());
86 assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
90 public void testCreateChangeEvent() {
91 final Map<InstanceIdentifier<?>, DataObject> createdData = new HashMap<>();
92 createdData.put(createTestFlowPath(), createTestFlow());
93 notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(createdData, null, null));
94 verify(notifProviderService, times(1)).publish(Matchers.any(FlowAdded.class));
97 @Test(expected = IllegalArgumentException.class)
98 public void testCreateFromNullNodeConnector() {
99 notifSupplierImpl.createNotification(null, createTestFlowPath());
102 @Test(expected = IllegalArgumentException.class)
103 public void testCreateFromNullPath() {
104 notifSupplierImpl.createNotification(createTestFlow(), null);
108 public void testUpdate() {
109 final FlowUpdated notification = notifSupplierImpl.updateNotification(createTestFlow(), createTestFlowPath());
110 assertNotNull(notification);
111 assertEquals(FLOW_ID, notification.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId().getValue());
112 assertEquals(FLOW_TABLE_ID, notification.getFlowRef().getValue().firstKeyOf(Table.class, TableKey.class).getId());
113 assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
117 public void testUpdateChangeEvent() {
118 final Map<InstanceIdentifier<?>, DataObject> createdData = new HashMap<>();
119 createdData.put(createTestFlowPath(), createTestFlow());
120 notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(createdData, null, null));
121 verify(notifProviderService, times(1)).publish(Matchers.any(FlowUpdated.class));
124 @Test(expected = IllegalArgumentException.class)
125 public void testUpdateFromNullNodeConnector() {
126 notifSupplierImpl.createNotification(null, createTestFlowPath());
129 @Test(expected = IllegalArgumentException.class)
130 public void testUpdateFromNullPath() {
131 notifSupplierImpl.createNotification(createTestFlow(), null);
135 public void testDelete() {
136 final FlowRemoved notification = notifSupplierImpl.deleteNotification(createTestFlowPath());
137 assertNotNull(notification);
138 assertEquals(FLOW_ID, notification.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId().getValue());
139 assertEquals(FLOW_TABLE_ID, notification.getFlowRef().getValue().firstKeyOf(Table.class, TableKey.class).getId());
140 assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
144 public void testDeleteChangeEvent() {
145 final Set<InstanceIdentifier<?>> removeData = new HashSet<>();
146 removeData.add(createTestFlowPath());
147 notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(null, null, removeData));
148 verify(notifProviderService, times(1)).publish(Matchers.any(FlowRemoved.class));
151 @Test(expected = IllegalArgumentException.class)
152 public void testDeleteFromNullPath() {
153 notifSupplierImpl.deleteNotification(null);
156 private static InstanceIdentifier<Flow> createTestFlowPath() {
157 return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
158 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(FLOW_TABLE_ID))
159 .child(Flow.class, new FlowKey(new FlowId(FLOW_ID)));
162 private static Flow createTestFlow() {
163 final FlowBuilder builder = new FlowBuilder();
164 builder.setId(new FlowId(FLOW_ID));
165 builder.setTableId(FLOW_TABLE_ID);
166 return builder.build();