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