2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.applications.notification.supplier.impl.item;
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;
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.flow.inventory.rev130819.meters.Meter;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterAdded;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterRemoved;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterUpdated;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 * {@link org.opendaylight.openflowplugin.applications.notification.supplier.impl.item.MeterNotificationSupplierImpl}.
47 public class MeterNotificationSupplierImplTest {
49 private static final String FLOW_NODE_ID = "openfow:111";
50 private static final Long METER_ID = 111L;
51 private static final Long UPDATED_METER_ID = 100L;
53 private MeterNotificationSupplierImpl notifSupplierImpl;
54 private NotificationProviderService notifProviderService;
55 private DataBroker dataBroker;
58 public void initalization() {
59 notifProviderService = mock(NotificationProviderService.class);
60 dataBroker = mock(DataBroker.class);
61 notifSupplierImpl = new MeterNotificationSupplierImpl(notifProviderService, dataBroker);
62 TestSupplierVerifyHelper.verifyDataTreeChangeListenerRegistration(dataBroker);
65 @Test(expected = NullPointerException.class)
66 public void testNullChangeEvent() {
67 notifSupplierImpl.onDataTreeChanged(null);
70 @Test(expected = NullPointerException.class)
71 public void testNullableChangeEvent() {
72 notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
76 public void testEmptyChangeEvent() {
77 notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
81 public void testCreate() {
82 final MeterAdded notification = notifSupplierImpl.createNotification(createTestMeter(), createTestMeterPath());
83 assertNotNull(notification);
84 assertEquals(METER_ID, notification.getMeterId().getValue());
85 assertEquals(METER_ID,
86 notification.getMeterRef().getValue().firstKeyOf(Meter.class).getMeterId()
88 assertEquals(FLOW_NODE_ID,
89 notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
93 public void testCreateChangeEvent() {
94 final TestData testData = new TestData(createTestMeterPath(), null, createTestMeter(),
95 DataObjectModification.ModificationType.WRITE);
96 Collection<DataTreeModification<Meter>> collection = new ArrayList<>();
97 collection.add(testData);
98 notifSupplierImpl.onDataTreeChanged(collection);
99 verify(notifProviderService, times(1)).publish(any(MeterAdded.class));
102 @Test(expected = IllegalArgumentException.class)
103 public void testCreateFromNullNodeConnector() {
104 notifSupplierImpl.createNotification(null, createTestMeterPath());
107 @Test(expected = IllegalArgumentException.class)
108 public void testCreateFromNullPath() {
109 notifSupplierImpl.createNotification(createTestMeter(), null);
113 public void testUpdate() {
114 final MeterUpdated notification = notifSupplierImpl
115 .updateNotification(createTestMeter(), createTestMeterPath());
116 assertNotNull(notification);
117 assertEquals(METER_ID, notification.getMeterId().getValue());
118 assertEquals(METER_ID,
119 notification.getMeterRef().getValue().firstKeyOf(Meter.class).getMeterId()
121 assertEquals(FLOW_NODE_ID,
122 notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
126 public void testUdateChangeEvent() {
127 final TestData testData = new TestData(createTestMeterPath(), createTestMeter(), createUpdatedTestMeter(),
128 DataObjectModification.ModificationType.SUBTREE_MODIFIED);
129 Collection<DataTreeModification<Meter>> collection = new ArrayList<>();
130 collection.add(testData);
131 notifSupplierImpl.onDataTreeChanged(collection);
132 verify(notifProviderService, times(1)).publish(any(MeterUpdated.class));
135 @Test(expected = IllegalArgumentException.class)
136 public void testUpdateFromNullNodeConnector() {
137 notifSupplierImpl.createNotification(null, createTestMeterPath());
140 @Test(expected = IllegalArgumentException.class)
141 public void testUpdateFromNullPath() {
142 notifSupplierImpl.createNotification(createTestMeter(), null);
146 public void testDelete() {
147 final MeterRemoved notification = notifSupplierImpl.deleteNotification(createTestMeterPath());
148 assertNotNull(notification);
149 assertEquals(METER_ID, notification.getMeterId().getValue());
150 assertEquals(METER_ID,
151 notification.getMeterRef().getValue().firstKeyOf(Meter.class).getMeterId()
153 assertEquals(FLOW_NODE_ID,
154 notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
158 public void testDeleteChangeEvent() {
159 final TestData testData = new TestData(createTestMeterPath(), createTestMeter(), null,
160 DataObjectModification.ModificationType.DELETE);
161 Collection<DataTreeModification<Meter>> collection = new ArrayList<>();
162 collection.add(testData);
163 notifSupplierImpl.onDataTreeChanged(collection);
164 verify(notifProviderService, times(1)).publish(any(MeterRemoved.class));
167 @Test(expected = IllegalArgumentException.class)
168 public void testDeleteFromNullPath() {
169 notifSupplierImpl.deleteNotification(null);
172 private static InstanceIdentifier<Meter> createTestMeterPath() {
173 return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
174 .augmentation(FlowCapableNode.class).child(Meter.class, new MeterKey(new MeterId(METER_ID)));
177 private static Meter createTestMeter() {
178 final MeterBuilder builder = new MeterBuilder();
179 builder.setMeterId(new MeterId(METER_ID));
180 return builder.build();
183 private static Meter createUpdatedTestMeter() {
184 final MeterBuilder builder = new MeterBuilder();
185 builder.setMeterId(new MeterId(UPDATED_METER_ID));
186 return builder.build();