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.ArrayList;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.HashSet;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Matchers;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
28 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
29 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
30 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestChangeEventBuildHelper;
31 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestData;
32 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestSupplierVerifyHelper;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
47 import org.opendaylight.yangtools.yang.binding.DataObject;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 public class FlowNotificationSupplierImplTest {
55 private static final String FLOW_NODE_ID = "openflow:111";
56 private static final Short FLOW_TABLE_ID = 111;
57 private static final Short UPDATED_FLOW_TABLE_ID = 100;
58 private static final String FLOW_ID = "test-flow-111";
59 private static final String UPDATED_FLOW_ID = "test-flow-100";
60 private FlowNotificationSupplierImpl notifSupplierImpl;
61 private NotificationProviderService notifProviderService;
62 private DataBroker dataBroker;
65 public void initalization() {
66 notifProviderService = mock(NotificationProviderService.class);
67 dataBroker = mock(DataBroker.class);
68 notifSupplierImpl = new FlowNotificationSupplierImpl(notifProviderService, dataBroker);
69 TestSupplierVerifyHelper.verifyDataChangeRegistration(dataBroker);
72 @Test(expected = NullPointerException.class)
73 public void testNullChangeEvent() {
74 notifSupplierImpl.onDataTreeChanged(null);
77 @Test(expected = NullPointerException.class)
78 public void testNullableChangeEvent() {
79 notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
83 public void testEmptyChangeEvent() {
84 notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
88 public void testCreate() {
89 final FlowAdded notification = notifSupplierImpl.createNotification(createTestFlow(), createTestFlowPath());
90 assertNotNull(notification);
91 assertEquals(FLOW_ID, notification.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId().getValue());
92 assertEquals(FLOW_TABLE_ID, notification.getFlowRef().getValue().firstKeyOf(Table.class, TableKey.class).getId());
93 assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
97 public void testCreateChangeEvent() {
98 final TestData testData = new TestData(createTestFlowPath(),null,createTestFlow(),
99 DataObjectModification.ModificationType.WRITE);
100 Collection<DataTreeModification<Flow>> collection = new ArrayList<>();
101 collection.add(testData);
102 notifSupplierImpl.onDataTreeChanged(collection);
103 verify(notifProviderService, times(1)).publish(Matchers.any(FlowAdded.class));
106 @Test(expected = IllegalArgumentException.class)
107 public void testCreateFromNullNodeConnector() {
108 notifSupplierImpl.createNotification(null, createTestFlowPath());
111 @Test(expected = IllegalArgumentException.class)
112 public void testCreateFromNullPath() {
113 notifSupplierImpl.createNotification(createTestFlow(), null);
117 public void testUpdate() {
118 final FlowUpdated notification = notifSupplierImpl.updateNotification(createTestFlow(), createTestFlowPath());
119 assertNotNull(notification);
120 assertEquals(FLOW_ID, notification.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId().getValue());
121 assertEquals(FLOW_TABLE_ID, notification.getFlowRef().getValue().firstKeyOf(Table.class, TableKey.class).getId());
122 assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
126 public void testUpdateChangeEvent() {
127 final TestData testData = new TestData(createTestFlowPath(),createTestFlow(),createUpdatedTestFlow(),
128 DataObjectModification.ModificationType.SUBTREE_MODIFIED);
129 Collection<DataTreeModification<Flow>> collection = new ArrayList<>();
130 collection.add(testData);
131 notifSupplierImpl.onDataTreeChanged(collection);
132 verify(notifProviderService, times(1)).publish(Matchers.any(FlowUpdated.class));
135 @Test(expected = IllegalArgumentException.class)
136 public void testUpdateFromNullNodeConnector() {
137 notifSupplierImpl.createNotification(null, createTestFlowPath());
140 @Test(expected = IllegalArgumentException.class)
141 public void testUpdateFromNullPath() {
142 notifSupplierImpl.createNotification(createTestFlow(), null);
146 public void testDelete() {
147 final FlowRemoved notification = notifSupplierImpl.deleteNotification(createTestFlowPath());
148 assertNotNull(notification);
149 assertEquals(FLOW_ID, notification.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId().getValue());
150 assertEquals(FLOW_TABLE_ID, notification.getFlowRef().getValue().firstKeyOf(Table.class, TableKey.class).getId());
151 assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
155 public void testDeleteChangeEvent() {
156 final TestData testData = new TestData(createTestFlowPath(),createTestFlow(),null,
157 DataObjectModification.ModificationType.DELETE);
158 Collection<DataTreeModification<Flow>> collection = new ArrayList<>();
159 collection.add(testData);
160 notifSupplierImpl.onDataTreeChanged(collection);
161 verify(notifProviderService, times(1)).publish(Matchers.any(FlowRemoved.class));
164 @Test(expected = IllegalArgumentException.class)
165 public void testDeleteFromNullPath() {
166 notifSupplierImpl.deleteNotification(null);
169 private static InstanceIdentifier<Flow> createTestFlowPath() {
170 return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
171 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(FLOW_TABLE_ID))
172 .child(Flow.class, new FlowKey(new FlowId(FLOW_ID)));
175 private static Flow createTestFlow() {
176 final FlowBuilder builder = new FlowBuilder();
177 builder.setId(new FlowId(FLOW_ID));
178 builder.setTableId(FLOW_TABLE_ID);
179 return builder.build();
182 private static Flow createUpdatedTestFlow(){
183 final FlowBuilder builder = new FlowBuilder();
184 builder.setId(new FlowId(UPDATED_FLOW_ID));
185 builder.setTableId(UPDATED_FLOW_TABLE_ID);
186 return builder.build();