Merge "Bug 5543 - Bo: Update JUnit tests part_9"
[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.ArrayList;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Map;
22 import java.util.Set;
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;
49
50 /**
51  *
52  */
53 public class FlowNotificationSupplierImplTest {
54
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;
63
64     @Before
65     public void initalization() {
66         notifProviderService = mock(NotificationProviderService.class);
67         dataBroker = mock(DataBroker.class);
68         notifSupplierImpl = new FlowNotificationSupplierImpl(notifProviderService, dataBroker);
69         TestSupplierVerifyHelper.verifyDataChangeRegistration(dataBroker);
70     }
71
72     @Test(expected = NullPointerException.class)
73     public void testNullChangeEvent() {
74         notifSupplierImpl.onDataTreeChanged(null);
75     }
76
77     @Test(expected = NullPointerException.class)
78     public void testNullableChangeEvent() {
79         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
80     }
81
82     @Test
83     public void testEmptyChangeEvent() {
84         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
85     }
86
87     @Test
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());
94     }
95
96     @Test
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));
104     }
105
106     @Test(expected = IllegalArgumentException.class)
107     public void testCreateFromNullNodeConnector() {
108         notifSupplierImpl.createNotification(null, createTestFlowPath());
109     }
110
111     @Test(expected = IllegalArgumentException.class)
112     public void testCreateFromNullPath() {
113         notifSupplierImpl.createNotification(createTestFlow(), null);
114     }
115
116     @Test
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());
123     }
124
125     @Test
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));
133     }
134
135     @Test(expected = IllegalArgumentException.class)
136     public void testUpdateFromNullNodeConnector() {
137         notifSupplierImpl.createNotification(null, createTestFlowPath());
138     }
139
140     @Test(expected = IllegalArgumentException.class)
141     public void testUpdateFromNullPath() {
142         notifSupplierImpl.createNotification(createTestFlow(), null);
143     }
144
145     @Test
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());
152     }
153
154     @Test
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));
162     }
163
164     @Test(expected = IllegalArgumentException.class)
165     public void testDeleteFromNullPath() {
166         notifSupplierImpl.deleteNotification(null);
167     }
168
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)));
173     }
174
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();
180     }
181
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();
187     }
188 }
189