Update comments and imports after DataChangeListener changes
[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.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.mockito.Matchers;
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 org.opendaylight.openflowplugin.applications.notification.supplier.impl.item.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, notification.getGroupRef().getValue().firstKeyOf(Group.class, GroupKey.class).getGroupId().getValue());
85         assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
86     }
87
88     @Test
89     public void testCreateChangeEvent() {
90         final TestData testData = new TestData(createTestGroupPath(),null,createTestGroup(),
91                 DataObjectModification.ModificationType.WRITE);
92         Collection<DataTreeModification<Group>> collection = new ArrayList<>();
93         collection.add(testData);
94         notifSupplierImpl.onDataTreeChanged(collection);
95         verify(notifProviderService, times(1)).publish(Matchers.any(GroupAdded.class));
96     }
97
98     @Test(expected = IllegalArgumentException.class)
99     public void testCreateFromNullNodeConnector() {
100         notifSupplierImpl.createNotification(null, createTestGroupPath());
101     }
102
103     @Test(expected = IllegalArgumentException.class)
104     public void testCreateFromNullPath() {
105         notifSupplierImpl.createNotification(createTestGroup(), null);
106     }
107
108     @Test
109     public void testUpdate() {
110         final GroupUpdated notification = notifSupplierImpl.updateNotification(createTestGroup(), createTestGroupPath());
111         assertNotNull(notification);
112         assertEquals(GROUP_ID, notification.getGroupId().getValue());
113         assertEquals(GROUP_ID, notification.getGroupRef().getValue().firstKeyOf(Group.class, GroupKey.class).getGroupId().getValue());
114         assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
115     }
116
117     @Test
118     public void testUpdateChangeEvent() {
119         final TestData testData = new TestData(createTestGroupPath(),createTestGroup(),createUpdatedTestGroup(),
120                 DataObjectModification.ModificationType.SUBTREE_MODIFIED);
121         Collection<DataTreeModification<Group>> collection = new ArrayList<>();
122         collection.add(testData);
123         notifSupplierImpl.onDataTreeChanged(collection);
124         verify(notifProviderService, times(1)).publish(Matchers.any(GroupUpdated.class));
125     }
126
127     @Test(expected = IllegalArgumentException.class)
128     public void testUpdateFromNullNodeConnector() {
129         notifSupplierImpl.createNotification(null, createTestGroupPath());
130     }
131
132     @Test(expected = IllegalArgumentException.class)
133     public void testUpdateFromNullPath() {
134         notifSupplierImpl.createNotification(createTestGroup(), null);
135     }
136
137     @Test
138     public void testDelete() {
139         final GroupRemoved notification = notifSupplierImpl.deleteNotification(createTestGroupPath());
140         assertNotNull(notification);
141         assertEquals(GROUP_ID, notification.getGroupId().getValue());
142         assertEquals(GROUP_ID, notification.getGroupRef().getValue().firstKeyOf(Group.class, GroupKey.class).getGroupId().getValue());
143         assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
144     }
145
146     @Test
147     public void testDeleteChangeEvent() {
148         final TestData testData = new TestData(createTestGroupPath(),createTestGroup(),null,
149                 DataObjectModification.ModificationType.DELETE);
150         Collection<DataTreeModification<Group>> collection = new ArrayList<>();
151         collection.add(testData);
152         notifSupplierImpl.onDataTreeChanged(collection);
153         verify(notifProviderService, times(1)).publish(Matchers.any(GroupRemoved.class));
154     }
155
156     @Test(expected = IllegalArgumentException.class)
157     public void testDeleteFromNullPath() {
158         notifSupplierImpl.deleteNotification(null);
159     }
160
161     private static InstanceIdentifier<Group> createTestGroupPath() {
162         return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
163                 .augmentation(FlowCapableNode.class).child(Group.class, new GroupKey(new GroupId(GROUP_ID)));
164     }
165
166     private static Group createTestGroup() {
167         final GroupBuilder builder = new GroupBuilder();
168         builder.setGroupId(new GroupId(GROUP_ID));
169         return builder.build();
170     }
171
172     private static Group createUpdatedTestGroup() {
173         final GroupBuilder builder = new GroupBuilder();
174         builder.setGroupId(new GroupId(UPDATED_GROUP_ID));
175         return builder.build();
176     }
177
178 }
179