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
8 package org.opendaylight.openflowplugin.applications.notification.supplier.impl.item;
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;
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.flow.inventory.rev130819.meters.Meter;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterAdded;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterRemoved;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterUpdated;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 * {@link org.opendaylight.openflowplugin.applications.notification.supplier.impl.item.MeterNotificationSupplierImpl}.
46 public class MeterNotificationSupplierImplTest {
48 private static final String FLOW_NODE_ID = "openfow:111";
49 private static final Long METER_ID = 111L;
50 private static final Long UPDATED_METER_ID = 100L;
52 private MeterNotificationSupplierImpl notifSupplierImpl;
53 private NotificationPublishService notifProviderService;
54 private DataBroker dataBroker;
57 public void initalization() {
58 notifProviderService = mock(NotificationPublishService.class);
59 dataBroker = mock(DataBroker.class);
60 notifSupplierImpl = new MeterNotificationSupplierImpl(notifProviderService, dataBroker);
61 TestSupplierVerifyHelper.verifyDataTreeChangeListenerRegistration(dataBroker);
64 @Test(expected = NullPointerException.class)
65 public void testNullChangeEvent() {
66 notifSupplierImpl.onDataTreeChanged(null);
69 @Test(expected = NullPointerException.class)
70 public void testNullableChangeEvent() {
71 notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
75 public void testEmptyChangeEvent() {
76 notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
80 public void testCreate() {
81 final MeterAdded notification = notifSupplierImpl.createNotification(createTestMeter(), createTestMeterPath());
82 assertNotNull(notification);
83 assertEquals(METER_ID, notification.getMeterId().getValue());
84 assertEquals(METER_ID,
85 notification.getMeterRef().getValue().firstKeyOf(Meter.class).getMeterId()
87 assertEquals(FLOW_NODE_ID,
88 notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
92 public void testCreateChangeEvent() throws InterruptedException {
93 final TestData<Meter> testData = new TestData<>(createTestMeterPath(), null, createTestMeter(),
94 DataObjectModification.ModificationType.WRITE);
95 Collection<DataTreeModification<Meter>> collection = new ArrayList<>();
96 collection.add(testData);
97 notifSupplierImpl.onDataTreeChanged(collection);
98 verify(notifProviderService, times(1)).putNotification(any(MeterAdded.class));
101 @Test(expected = IllegalArgumentException.class)
102 public void testCreateFromNullNodeConnector() {
103 notifSupplierImpl.createNotification(null, createTestMeterPath());
106 @Test(expected = IllegalArgumentException.class)
107 public void testCreateFromNullPath() {
108 notifSupplierImpl.createNotification(createTestMeter(), null);
112 public void testUpdate() {
113 final MeterUpdated notification = notifSupplierImpl
114 .updateNotification(createTestMeter(), createTestMeterPath());
115 assertNotNull(notification);
116 assertEquals(METER_ID, notification.getMeterId().getValue());
117 assertEquals(METER_ID,
118 notification.getMeterRef().getValue().firstKeyOf(Meter.class).getMeterId()
120 assertEquals(FLOW_NODE_ID,
121 notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
125 public void testUdateChangeEvent() throws InterruptedException {
126 final TestData<Meter> testData = new TestData<>(createTestMeterPath(), createTestMeter(),
127 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)).putNotification(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() throws InterruptedException {
159 final TestData<Meter> 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)).putNotification(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();