Switch to MD-SAL APIs
[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 package org.opendaylight.openflowplugin.applications.notification.supplier.impl.item;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.ArgumentMatchers.any;
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.ArrayList;
18 import java.util.Collection;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.mdsal.binding.api.DataBroker;
22 import org.opendaylight.mdsal.binding.api.DataObjectModification;
23 import org.opendaylight.mdsal.binding.api.DataTreeModification;
24 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
25 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestChangeEventBuildHelper;
26 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestData;
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.InstanceIdentifier;
43
44 /**
45  * Test for
46  * {@link org.opendaylight.openflowplugin.applications.notification.supplier.impl.item.FlowNotificationSupplierImpl}.
47  */
48 public class FlowNotificationSupplierImplTest {
49
50     private static final String FLOW_NODE_ID = "openflow:111";
51     private static final Short FLOW_TABLE_ID = 111;
52     private static final Short UPDATED_FLOW_TABLE_ID = 100;
53     private static final String FLOW_ID = "test-flow-111";
54     private static final String UPDATED_FLOW_ID = "test-flow-100";
55     private FlowNotificationSupplierImpl notifSupplierImpl;
56     private NotificationPublishService notifProviderService;
57     private DataBroker dataBroker;
58
59     @Before
60     public void initalization() {
61         notifProviderService = mock(NotificationPublishService.class);
62         dataBroker = mock(DataBroker.class);
63         notifSupplierImpl = new FlowNotificationSupplierImpl(notifProviderService, dataBroker);
64         TestSupplierVerifyHelper.verifyDataTreeChangeListenerRegistration(dataBroker);
65     }
66
67     @Test(expected = NullPointerException.class)
68     public void testNullChangeEvent() {
69         notifSupplierImpl.onDataTreeChanged(null);
70     }
71
72     @Test(expected = NullPointerException.class)
73     public void testNullableChangeEvent() {
74         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
75     }
76
77     @Test
78     public void testEmptyChangeEvent() {
79         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
80     }
81
82     @Test
83     public void testCreate() {
84         final FlowAdded notification = notifSupplierImpl.createNotification(createTestFlow(), createTestFlowPath());
85         assertNotNull(notification);
86         assertEquals(FLOW_ID,
87                      notification.getFlowRef().getValue().firstKeyOf(Flow.class).getId().getValue());
88         assertEquals(FLOW_TABLE_ID,
89                      notification.getFlowRef().getValue().firstKeyOf(Table.class).getId());
90         assertEquals(FLOW_NODE_ID,
91                      notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
92     }
93
94     @Test
95     public void testCreateChangeEvent() throws InterruptedException {
96         final TestData<Flow> testData = new TestData<>(createTestFlowPath(), null, createTestFlow(),
97                                                        DataObjectModification.ModificationType.WRITE);
98         Collection<DataTreeModification<Flow>> collection = new ArrayList<>();
99         collection.add(testData);
100         notifSupplierImpl.onDataTreeChanged(collection);
101         verify(notifProviderService, times(1)).putNotification(any(FlowAdded.class));
102     }
103
104     @Test(expected = IllegalArgumentException.class)
105     public void testCreateFromNullNodeConnector() {
106         notifSupplierImpl.createNotification(null, createTestFlowPath());
107     }
108
109     @Test(expected = IllegalArgumentException.class)
110     public void testCreateFromNullPath() {
111         notifSupplierImpl.createNotification(createTestFlow(), null);
112     }
113
114     @Test
115     public void testUpdate() {
116         final FlowUpdated notification = notifSupplierImpl.updateNotification(createTestFlow(), createTestFlowPath());
117         assertNotNull(notification);
118         assertEquals(FLOW_ID,
119                      notification.getFlowRef().getValue().firstKeyOf(Flow.class).getId().getValue());
120         assertEquals(FLOW_TABLE_ID,
121                      notification.getFlowRef().getValue().firstKeyOf(Table.class).getId());
122         assertEquals(FLOW_NODE_ID,
123                      notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
124     }
125
126     @Test
127     public void testUpdateChangeEvent() throws InterruptedException {
128         final TestData<Flow> testData = new TestData<>(createTestFlowPath(), createTestFlow(), createUpdatedTestFlow(),
129                                                        DataObjectModification.ModificationType.SUBTREE_MODIFIED);
130         Collection<DataTreeModification<Flow>> collection = new ArrayList<>();
131         collection.add(testData);
132         notifSupplierImpl.onDataTreeChanged(collection);
133         verify(notifProviderService, times(1)).putNotification(any(FlowUpdated.class));
134     }
135
136     @Test(expected = IllegalArgumentException.class)
137     public void testUpdateFromNullNodeConnector() {
138         notifSupplierImpl.createNotification(null, createTestFlowPath());
139     }
140
141     @Test(expected = IllegalArgumentException.class)
142     public void testUpdateFromNullPath() {
143         notifSupplierImpl.createNotification(createTestFlow(), null);
144     }
145
146     @Test
147     public void testDelete() {
148         final FlowRemoved notification = notifSupplierImpl.deleteNotification(createTestFlowPath());
149         assertNotNull(notification);
150         assertEquals(FLOW_ID,
151                      notification.getFlowRef().getValue().firstKeyOf(Flow.class).getId().getValue());
152         assertEquals(FLOW_TABLE_ID,
153                      notification.getFlowRef().getValue().firstKeyOf(Table.class).getId());
154         assertEquals(FLOW_NODE_ID,
155                      notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
156     }
157
158     @Test
159     public void testDeleteChangeEvent() throws InterruptedException {
160         final TestData<Flow> testData = new TestData<>(createTestFlowPath(), createTestFlow(), null,
161                                                        DataObjectModification.ModificationType.DELETE);
162         Collection<DataTreeModification<Flow>> collection = new ArrayList<>();
163         collection.add(testData);
164         notifSupplierImpl.onDataTreeChanged(collection);
165         verify(notifProviderService, times(1)).putNotification(any(FlowRemoved.class));
166     }
167
168     @Test(expected = IllegalArgumentException.class)
169     public void testDeleteFromNullPath() {
170         notifSupplierImpl.deleteNotification(null);
171     }
172
173     private static InstanceIdentifier<Flow> createTestFlowPath() {
174         return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
175                 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(FLOW_TABLE_ID))
176                 .child(Flow.class, new FlowKey(new FlowId(FLOW_ID)));
177     }
178
179     private static Flow createTestFlow() {
180         final FlowBuilder builder = new FlowBuilder();
181         builder.setId(new FlowId(FLOW_ID));
182         builder.setTableId(FLOW_TABLE_ID);
183         return builder.build();
184     }
185
186     private static Flow createUpdatedTestFlow() {
187         final FlowBuilder builder = new FlowBuilder();
188         builder.setId(new FlowId(UPDATED_FLOW_ID));
189         builder.setTableId(UPDATED_FLOW_TABLE_ID);
190         return builder.build();
191     }
192 }
193