Switch to MD-SAL APIs
[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 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.group.service.rev130918.GroupAdded;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupRemoved;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupUpdated;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41
42 /**
43  * Test for {@link GroupNotificationSupplierImpl}.
44  */
45 public class GroupNotificationSupplierImplTest {
46
47     private static final String FLOW_NODE_ID = "openflow:111";
48     private static final Long GROUP_ID = 111L;
49     private static final Long UPDATED_GROUP_ID = 100L;
50
51     private GroupNotificationSupplierImpl notifSupplierImpl;
52     private NotificationPublishService notifProviderService;
53     private DataBroker dataBroker;
54
55     @Before
56     public void initalization() {
57         notifProviderService = mock(NotificationPublishService.class);
58         dataBroker = mock(DataBroker.class);
59         notifSupplierImpl = new GroupNotificationSupplierImpl(notifProviderService, dataBroker);
60         TestSupplierVerifyHelper.verifyDataTreeChangeListenerRegistration(dataBroker);
61     }
62
63     @Test(expected = NullPointerException.class)
64     public void testNullChangeEvent() {
65         notifSupplierImpl.onDataTreeChanged(null);
66     }
67
68     @Test(expected = NullPointerException.class)
69     public void testNullableChangeEvent() {
70         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
71     }
72
73     @Test
74     public void testEmptyChangeEvent() {
75         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
76     }
77
78     @Test
79     public void testCreate() {
80         final GroupAdded notification = notifSupplierImpl.createNotification(createTestGroup(), createTestGroupPath());
81         assertNotNull(notification);
82         assertEquals(GROUP_ID, notification.getGroupId().getValue());
83         assertEquals(GROUP_ID,
84                      notification.getGroupRef().getValue().firstKeyOf(Group.class).getGroupId()
85                              .getValue());
86         assertEquals(FLOW_NODE_ID,
87                      notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
88     }
89
90     @Test
91     public void testCreateChangeEvent() throws InterruptedException {
92         final TestData<Group> testData = new TestData<>(createTestGroupPath(), null, createTestGroup(),
93                                                         DataObjectModification.ModificationType.WRITE);
94         Collection<DataTreeModification<Group>> collection = new ArrayList<>();
95         collection.add(testData);
96         notifSupplierImpl.onDataTreeChanged(collection);
97         verify(notifProviderService, times(1)).putNotification(any(GroupAdded.class));
98     }
99
100     @Test(expected = IllegalArgumentException.class)
101     public void testCreateFromNullNodeConnector() {
102         notifSupplierImpl.createNotification(null, createTestGroupPath());
103     }
104
105     @Test(expected = IllegalArgumentException.class)
106     public void testCreateFromNullPath() {
107         notifSupplierImpl.createNotification(createTestGroup(), null);
108     }
109
110     @Test
111     public void testUpdate() {
112         final GroupUpdated notification = notifSupplierImpl
113                 .updateNotification(createTestGroup(), createTestGroupPath());
114         assertNotNull(notification);
115         assertEquals(GROUP_ID, notification.getGroupId().getValue());
116         assertEquals(GROUP_ID,
117                      notification.getGroupRef().getValue().firstKeyOf(Group.class).getGroupId()
118                              .getValue());
119         assertEquals(FLOW_NODE_ID,
120                      notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
121     }
122
123     @Test
124     public void testUpdateChangeEvent() throws InterruptedException {
125         final TestData<Group> testData = new TestData<>(createTestGroupPath(), createTestGroup(),
126                                                         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)).putNotification(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() throws InterruptedException {
158         final TestData<Group> 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)).putNotification(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