5f765f63d7422d7a41aa1accd69e50dee98e2f3c
[openflowplugin.git] / applications / notification-supplier / src / test / java / org / opendaylight / openflowplugin / applications / notification / supplier / impl / item / FlowNotificationSupplierImplTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.applications.notification.supplier.impl.item;
10
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;
16
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.Map;
20 import java.util.Set;
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;
44
45 /**
46  *
47  */
48 public class FlowNotificationSupplierImplTest {
49
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;
56
57     @Before
58     public void initalization() {
59         notifProviderService = mock(NotificationProviderService.class);
60         dataBroker = mock(DataBroker.class);
61         notifSupplierImpl = new FlowNotificationSupplierImpl(notifProviderService, dataBroker);
62         TestSupplierVerifyHelper.verifyDataChangeRegistration(dataBroker);
63     }
64
65     @Test(expected = IllegalArgumentException.class)
66     public void testNullChangeEvent() {
67         notifSupplierImpl.onDataChanged(null);
68     }
69
70     @Test
71     public void testNullableChangeEvent() {
72         notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
73     }
74
75     @Test
76     public void testEmptyChangeEvent() {
77         notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
78     }
79
80     @Test
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());
87     }
88
89     @Test
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));
95     }
96
97     @Test(expected = IllegalArgumentException.class)
98     public void testCreateFromNullNodeConnector() {
99         notifSupplierImpl.createNotification(null, createTestFlowPath());
100     }
101
102     @Test(expected = IllegalArgumentException.class)
103     public void testCreateFromNullPath() {
104         notifSupplierImpl.createNotification(createTestFlow(), null);
105     }
106
107     @Test
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());
114     }
115
116     @Test
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));
122     }
123
124     @Test(expected = IllegalArgumentException.class)
125     public void testUpdateFromNullNodeConnector() {
126         notifSupplierImpl.createNotification(null, createTestFlowPath());
127     }
128
129     @Test(expected = IllegalArgumentException.class)
130     public void testUpdateFromNullPath() {
131         notifSupplierImpl.createNotification(createTestFlow(), null);
132     }
133
134     @Test
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());
141     }
142
143     @Test
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));
149     }
150
151     @Test(expected = IllegalArgumentException.class)
152     public void testDeleteFromNullPath() {
153         notifSupplierImpl.deleteNotification(null);
154     }
155
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)));
160     }
161
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();
167     }
168 }
169