OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / applications / notification-supplier / src / test / java / org / opendaylight / openflowplugin / applications / notification / supplier / impl / item / GroupNotificationSupplierImplTest.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.ArgumentMatchers.any;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
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.TestData;
28 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestSupplierVerifyHelper;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupAdded;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupRemoved;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupUpdated;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42
43 /**
44  * Test for {@link GroupNotificationSupplierImpl}.
45  */
46 public class GroupNotificationSupplierImplTest {
47
48     private static final String FLOW_NODE_ID = "openflow:111";
49     private static final Long GROUP_ID = 111L;
50     private static final Long UPDATED_GROUP_ID = 100L;
51
52     private GroupNotificationSupplierImpl notifSupplierImpl;
53     private NotificationProviderService notifProviderService;
54     private DataBroker dataBroker;
55
56     @Before
57     public void initalization() {
58         notifProviderService = mock(NotificationProviderService.class);
59         dataBroker = mock(DataBroker.class);
60         notifSupplierImpl = new GroupNotificationSupplierImpl(notifProviderService, dataBroker);
61         TestSupplierVerifyHelper.verifyDataTreeChangeListenerRegistration(dataBroker);
62     }
63
64     @Test(expected = NullPointerException.class)
65     public void testNullChangeEvent() {
66         notifSupplierImpl.onDataTreeChanged(null);
67     }
68
69     @Test(expected = NullPointerException.class)
70     public void testNullableChangeEvent() {
71         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
72     }
73
74     @Test
75     public void testEmptyChangeEvent() {
76         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
77     }
78
79     @Test
80     public void testCreate() {
81         final GroupAdded notification = notifSupplierImpl.createNotification(createTestGroup(), createTestGroupPath());
82         assertNotNull(notification);
83         assertEquals(GROUP_ID, notification.getGroupId().getValue());
84         assertEquals(GROUP_ID,
85                      notification.getGroupRef().getValue().firstKeyOf(Group.class).getGroupId()
86                              .getValue());
87         assertEquals(FLOW_NODE_ID,
88                      notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
89     }
90
91     @Test
92     public void testCreateChangeEvent() {
93         final TestData testData = new TestData(createTestGroupPath(), null, createTestGroup(),
94                                                DataObjectModification.ModificationType.WRITE);
95         Collection<DataTreeModification<Group>> collection = new ArrayList<>();
96         collection.add(testData);
97         notifSupplierImpl.onDataTreeChanged(collection);
98         verify(notifProviderService, times(1)).publish(any(GroupAdded.class));
99     }
100
101     @Test(expected = IllegalArgumentException.class)
102     public void testCreateFromNullNodeConnector() {
103         notifSupplierImpl.createNotification(null, createTestGroupPath());
104     }
105
106     @Test(expected = IllegalArgumentException.class)
107     public void testCreateFromNullPath() {
108         notifSupplierImpl.createNotification(createTestGroup(), null);
109     }
110
111     @Test
112     public void testUpdate() {
113         final GroupUpdated notification = notifSupplierImpl
114                 .updateNotification(createTestGroup(), createTestGroupPath());
115         assertNotNull(notification);
116         assertEquals(GROUP_ID, notification.getGroupId().getValue());
117         assertEquals(GROUP_ID,
118                      notification.getGroupRef().getValue().firstKeyOf(Group.class).getGroupId()
119                              .getValue());
120         assertEquals(FLOW_NODE_ID,
121                      notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
122     }
123
124     @Test
125     public void testUpdateChangeEvent() {
126         final TestData testData = new TestData(createTestGroupPath(), createTestGroup(), createUpdatedTestGroup(),
127                                                DataObjectModification.ModificationType.SUBTREE_MODIFIED);
128         Collection<DataTreeModification<Group>> collection = new ArrayList<>();
129         collection.add(testData);
130         notifSupplierImpl.onDataTreeChanged(collection);
131         verify(notifProviderService, times(1)).publish(any(GroupUpdated.class));
132     }
133
134     @Test(expected = IllegalArgumentException.class)
135     public void testUpdateFromNullNodeConnector() {
136         notifSupplierImpl.createNotification(null, createTestGroupPath());
137     }
138
139     @Test(expected = IllegalArgumentException.class)
140     public void testUpdateFromNullPath() {
141         notifSupplierImpl.createNotification(createTestGroup(), null);
142     }
143
144     @Test
145     public void testDelete() {
146         final GroupRemoved notification = notifSupplierImpl.deleteNotification(createTestGroupPath());
147         assertNotNull(notification);
148         assertEquals(GROUP_ID, notification.getGroupId().getValue());
149         assertEquals(GROUP_ID,
150                      notification.getGroupRef().getValue().firstKeyOf(Group.class).getGroupId()
151                              .getValue());
152         assertEquals(FLOW_NODE_ID,
153                      notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
154     }
155
156     @Test
157     public void testDeleteChangeEvent() {
158         final TestData testData = new TestData(createTestGroupPath(), createTestGroup(), null,
159                                                DataObjectModification.ModificationType.DELETE);
160         Collection<DataTreeModification<Group>> collection = new ArrayList<>();
161         collection.add(testData);
162         notifSupplierImpl.onDataTreeChanged(collection);
163         verify(notifProviderService, times(1)).publish(any(GroupRemoved.class));
164     }
165
166     @Test(expected = IllegalArgumentException.class)
167     public void testDeleteFromNullPath() {
168         notifSupplierImpl.deleteNotification(null);
169     }
170
171     private static InstanceIdentifier<Group> createTestGroupPath() {
172         return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
173                 .augmentation(FlowCapableNode.class).child(Group.class, new GroupKey(new GroupId(GROUP_ID)));
174     }
175
176     private static Group createTestGroup() {
177         final GroupBuilder builder = new GroupBuilder();
178         builder.setGroupId(new GroupId(GROUP_ID));
179         return builder.build();
180     }
181
182     private static Group createUpdatedTestGroup() {
183         final GroupBuilder builder = new GroupBuilder();
184         builder.setGroupId(new GroupId(UPDATED_GROUP_ID));
185         return builder.build();
186     }
187
188 }
189