Merge "Bug 5543 - Bo: Update JUnit tests part_6"
[openflowplugin.git] / applications / notification-supplier / src / test / java / org / opendaylight / openflowplugin / applications / notification / supplier / impl / item / MeterNotificationSupplierImplTest.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
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Matchers;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
29 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
30 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
31 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestChangeEventBuildHelper;
32 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestData;
33 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestSupplierVerifyHelper;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupUpdated;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterAdded;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterRemoved;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterUpdated;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
48 import org.opendaylight.yangtools.yang.binding.DataObject;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50
51 /**
52  *
53  */
54 public class MeterNotificationSupplierImplTest {
55
56     private static final String FLOW_NODE_ID = "openfow:111";
57     private static final Long METER_ID = 111L;
58     private static final Long UPDATED_METER_ID = 100L;
59
60     private MeterNotificationSupplierImpl 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 MeterNotificationSupplierImpl(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 MeterAdded notification = notifSupplierImpl.createNotification(createTestMeter(), createTestMeterPath());
90         assertNotNull(notification);
91         assertEquals(METER_ID, notification.getMeterId().getValue());
92         assertEquals(METER_ID, notification.getMeterRef().getValue().firstKeyOf(Meter.class, MeterKey.class).getMeterId().getValue());
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(createTestMeterPath(),null,createTestMeter(),
99                 DataObjectModification.ModificationType.WRITE);
100         Collection<DataTreeModification<Meter>> collection = new ArrayList<>();
101         collection.add(testData);
102         notifSupplierImpl.onDataTreeChanged(collection);
103         verify(notifProviderService, times(1)).publish(Matchers.any(MeterAdded.class));
104     }
105
106     @Test(expected = IllegalArgumentException.class)
107     public void testCreateFromNullNodeConnector() {
108         notifSupplierImpl.createNotification(null, createTestMeterPath());
109     }
110
111     @Test(expected = IllegalArgumentException.class)
112     public void testCreateFromNullPath() {
113         notifSupplierImpl.createNotification(createTestMeter(), null);
114     }
115
116     @Test
117     public void testUpdate() {
118         final MeterUpdated notification = notifSupplierImpl
119                 .updateNotification(createTestMeter(), createTestMeterPath());
120         assertNotNull(notification);
121         assertEquals(METER_ID, notification.getMeterId().getValue());
122         assertEquals(METER_ID, notification.getMeterRef().getValue().firstKeyOf(Meter.class, MeterKey.class).getMeterId().getValue());
123         assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
124     }
125
126     @Test
127     public void testUdateChangeEvent() {
128         final TestData testData = new TestData(createTestMeterPath(),createTestMeter(),createUpdatedTestMeter(),
129                 DataObjectModification.ModificationType.SUBTREE_MODIFIED);
130         Collection<DataTreeModification<Meter>> collection = new ArrayList<>();
131         collection.add(testData);
132         notifSupplierImpl.onDataTreeChanged(collection);
133         verify(notifProviderService, times(1)).publish(Matchers.any(MeterUpdated.class));
134     }
135
136     @Test(expected = IllegalArgumentException.class)
137     public void testUpdateFromNullNodeConnector() {
138         notifSupplierImpl.createNotification(null, createTestMeterPath());
139     }
140
141     @Test(expected = IllegalArgumentException.class)
142     public void testUpdateFromNullPath() {
143         notifSupplierImpl.createNotification(createTestMeter(), null);
144     }
145
146     @Test
147     public void testDelete() {
148         final MeterRemoved notification = notifSupplierImpl.deleteNotification(createTestMeterPath());
149         assertNotNull(notification);
150         assertEquals(METER_ID, notification.getMeterId().getValue());
151         assertEquals(METER_ID, notification.getMeterRef().getValue().firstKeyOf(Meter.class, MeterKey.class).getMeterId().getValue());
152         assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
153     }
154
155     @Test
156     public void testDeleteChangeEvent() {
157         final TestData testData = new TestData(createTestMeterPath(),createTestMeter(),null,
158                 DataObjectModification.ModificationType.DELETE);
159         Collection<DataTreeModification<Meter>> collection = new ArrayList<>();
160         collection.add(testData);
161         notifSupplierImpl.onDataTreeChanged(collection);
162         verify(notifProviderService, times(1)).publish(Matchers.any(MeterRemoved.class));
163     }
164
165     @Test(expected = IllegalArgumentException.class)
166     public void testDeleteFromNullPath() {
167         notifSupplierImpl.deleteNotification(null);
168     }
169
170     private static InstanceIdentifier<Meter> createTestMeterPath() {
171         return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
172                 .augmentation(FlowCapableNode.class).child(Meter.class, new MeterKey(new MeterId(METER_ID)));
173     }
174
175     private static Meter createTestMeter() {
176         final MeterBuilder builder = new MeterBuilder();
177         builder.setMeterId(new MeterId(METER_ID));
178         return builder.build();
179     }
180
181     private static Meter createUpdatedTestMeter(){
182         final MeterBuilder builder = new MeterBuilder();
183         builder.setMeterId(new MeterId(UPDATED_METER_ID));
184         return builder.build();
185     }
186 }
187