Bump mdsal to 5.0.2
[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 import org.opendaylight.yangtools.yang.common.Uint8;
44
45 /**
46  * Test for
47  * {@link org.opendaylight.openflowplugin.applications.notification.supplier.impl.item.FlowNotificationSupplierImpl}.
48  */
49 public class FlowNotificationSupplierImplTest {
50
51     private static final String FLOW_NODE_ID = "openflow:111";
52     private static final Uint8 FLOW_TABLE_ID = Uint8.valueOf(111);
53     private static final Uint8 UPDATED_FLOW_TABLE_ID = Uint8.valueOf(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 NotificationPublishService notifProviderService;
58     private DataBroker dataBroker;
59
60     @Before
61     public void initalization() {
62         notifProviderService = mock(NotificationPublishService.class);
63         dataBroker = mock(DataBroker.class);
64         notifSupplierImpl = new FlowNotificationSupplierImpl(notifProviderService, dataBroker);
65         TestSupplierVerifyHelper.verifyDataTreeChangeListenerRegistration(dataBroker);
66     }
67
68     @Test(expected = NullPointerException.class)
69     public void testNullChangeEvent() {
70         notifSupplierImpl.onDataTreeChanged(null);
71     }
72
73     @Test(expected = NullPointerException.class)
74     public void testNullableChangeEvent() {
75         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
76     }
77
78     @Test
79     public void testEmptyChangeEvent() {
80         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
81     }
82
83     @Test
84     public void testCreate() {
85         final FlowAdded notification = notifSupplierImpl.createNotification(createTestFlow(), createTestFlowPath());
86         assertNotNull(notification);
87         assertEquals(FLOW_ID,
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());
93     }
94
95     @Test
96     public void testCreateChangeEvent() throws InterruptedException {
97         final TestData<Flow> 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)).putNotification(any(FlowAdded.class));
103     }
104
105     @Test(expected = IllegalArgumentException.class)
106     public void testCreateFromNullNodeConnector() {
107         notifSupplierImpl.createNotification(null, createTestFlowPath());
108     }
109
110     @Test(expected = IllegalArgumentException.class)
111     public void testCreateFromNullPath() {
112         notifSupplierImpl.createNotification(createTestFlow(), null);
113     }
114
115     @Test
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());
125     }
126
127     @Test
128     public void testUpdateChangeEvent() throws InterruptedException {
129         final TestData<Flow> 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)).putNotification(any(FlowUpdated.class));
135     }
136
137     @Test(expected = IllegalArgumentException.class)
138     public void testUpdateFromNullNodeConnector() {
139         notifSupplierImpl.createNotification(null, createTestFlowPath());
140     }
141
142     @Test(expected = IllegalArgumentException.class)
143     public void testUpdateFromNullPath() {
144         notifSupplierImpl.createNotification(createTestFlow(), null);
145     }
146
147     @Test
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());
157     }
158
159     @Test
160     public void testDeleteChangeEvent() throws InterruptedException {
161         final TestData<Flow> 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)).putNotification(any(FlowRemoved.class));
167     }
168
169     @Test(expected = IllegalArgumentException.class)
170     public void testDeleteFromNullPath() {
171         notifSupplierImpl.deleteNotification(null);
172     }
173
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)));
178     }
179
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();
185     }
186
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();
192     }
193 }
194