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.ArgumentMatchers.any;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
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.TestData;
28 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestSupplierVerifyHelper;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 * {@link org.opendaylight.openflowplugin.applications.notification.supplier.impl.item.FlowNotificationSupplierImpl}.
49 public class FlowNotificationSupplierImplTest {
51 private static final String FLOW_NODE_ID = "openflow:111";
52 private static final Short FLOW_TABLE_ID = 111;
53 private static final Short UPDATED_FLOW_TABLE_ID = 100;
54 private static final String FLOW_ID = "test-flow-111";
55 private static final String UPDATED_FLOW_ID = "test-flow-100";
56 private FlowNotificationSupplierImpl notifSupplierImpl;
57 private NotificationProviderService notifProviderService;
58 private DataBroker dataBroker;
61 public void initalization() {
62 notifProviderService = mock(NotificationProviderService.class);
63 dataBroker = mock(DataBroker.class);
64 notifSupplierImpl = new FlowNotificationSupplierImpl(notifProviderService, dataBroker);
65 TestSupplierVerifyHelper.verifyDataTreeChangeListenerRegistration(dataBroker);
68 @Test(expected = NullPointerException.class)
69 public void testNullChangeEvent() {
70 notifSupplierImpl.onDataTreeChanged(null);
73 @Test(expected = NullPointerException.class)
74 public void testNullableChangeEvent() {
75 notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
79 public void testEmptyChangeEvent() {
80 notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
84 public void testCreate() {
85 final FlowAdded notification = notifSupplierImpl.createNotification(createTestFlow(), createTestFlowPath());
86 assertNotNull(notification);
88 notification.getFlowRef().getValue().firstKeyOf(Flow.class).getId().getValue());
89 assertEquals(FLOW_TABLE_ID,
90 notification.getFlowRef().getValue().firstKeyOf(Table.class).getId());
91 assertEquals(FLOW_NODE_ID,
92 notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
96 public void testCreateChangeEvent() {
97 final TestData testData = new TestData(createTestFlowPath(), null, createTestFlow(),
98 DataObjectModification.ModificationType.WRITE);
99 Collection<DataTreeModification<Flow>> collection = new ArrayList<>();
100 collection.add(testData);
101 notifSupplierImpl.onDataTreeChanged(collection);
102 verify(notifProviderService, times(1)).publish(any(FlowAdded.class));
105 @Test(expected = IllegalArgumentException.class)
106 public void testCreateFromNullNodeConnector() {
107 notifSupplierImpl.createNotification(null, createTestFlowPath());
110 @Test(expected = IllegalArgumentException.class)
111 public void testCreateFromNullPath() {
112 notifSupplierImpl.createNotification(createTestFlow(), null);
116 public void testUpdate() {
117 final FlowUpdated notification = notifSupplierImpl.updateNotification(createTestFlow(), createTestFlowPath());
118 assertNotNull(notification);
119 assertEquals(FLOW_ID,
120 notification.getFlowRef().getValue().firstKeyOf(Flow.class).getId().getValue());
121 assertEquals(FLOW_TABLE_ID,
122 notification.getFlowRef().getValue().firstKeyOf(Table.class).getId());
123 assertEquals(FLOW_NODE_ID,
124 notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
128 public void testUpdateChangeEvent() {
129 final TestData testData = new TestData(createTestFlowPath(), createTestFlow(), createUpdatedTestFlow(),
130 DataObjectModification.ModificationType.SUBTREE_MODIFIED);
131 Collection<DataTreeModification<Flow>> collection = new ArrayList<>();
132 collection.add(testData);
133 notifSupplierImpl.onDataTreeChanged(collection);
134 verify(notifProviderService, times(1)).publish(any(FlowUpdated.class));
137 @Test(expected = IllegalArgumentException.class)
138 public void testUpdateFromNullNodeConnector() {
139 notifSupplierImpl.createNotification(null, createTestFlowPath());
142 @Test(expected = IllegalArgumentException.class)
143 public void testUpdateFromNullPath() {
144 notifSupplierImpl.createNotification(createTestFlow(), null);
148 public void testDelete() {
149 final FlowRemoved notification = notifSupplierImpl.deleteNotification(createTestFlowPath());
150 assertNotNull(notification);
151 assertEquals(FLOW_ID,
152 notification.getFlowRef().getValue().firstKeyOf(Flow.class).getId().getValue());
153 assertEquals(FLOW_TABLE_ID,
154 notification.getFlowRef().getValue().firstKeyOf(Table.class).getId());
155 assertEquals(FLOW_NODE_ID,
156 notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
160 public void testDeleteChangeEvent() {
161 final TestData testData = new TestData(createTestFlowPath(), createTestFlow(), null,
162 DataObjectModification.ModificationType.DELETE);
163 Collection<DataTreeModification<Flow>> collection = new ArrayList<>();
164 collection.add(testData);
165 notifSupplierImpl.onDataTreeChanged(collection);
166 verify(notifProviderService, times(1)).publish(any(FlowRemoved.class));
169 @Test(expected = IllegalArgumentException.class)
170 public void testDeleteFromNullPath() {
171 notifSupplierImpl.deleteNotification(null);
174 private static InstanceIdentifier<Flow> createTestFlowPath() {
175 return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
176 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(FLOW_TABLE_ID))
177 .child(Flow.class, new FlowKey(new FlowId(FLOW_ID)));
180 private static Flow createTestFlow() {
181 final FlowBuilder builder = new FlowBuilder();
182 builder.setId(new FlowId(FLOW_ID));
183 builder.setTableId(FLOW_TABLE_ID);
184 return builder.build();
187 private static Flow createUpdatedTestFlow() {
188 final FlowBuilder builder = new FlowBuilder();
189 builder.setId(new FlowId(UPDATED_FLOW_ID));
190 builder.setTableId(UPDATED_FLOW_TABLE_ID);
191 return builder.build();