c5bdd1a41de0a86b82bbd06690ef440f6dd176a5
[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.ArgumentMatchers.any;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17
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;
42
43 /**
44  * Test for
45  * {@link org.opendaylight.openflowplugin.applications.notification.supplier.impl.item.MeterNotificationSupplierImpl}.
46  */
47 public class MeterNotificationSupplierImplTest {
48
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;
52
53     private MeterNotificationSupplierImpl notifSupplierImpl;
54     private NotificationProviderService notifProviderService;
55     private DataBroker dataBroker;
56
57     @Before
58     public void initalization() {
59         notifProviderService = mock(NotificationProviderService.class);
60         dataBroker = mock(DataBroker.class);
61         notifSupplierImpl = new MeterNotificationSupplierImpl(notifProviderService, dataBroker);
62         TestSupplierVerifyHelper.verifyDataTreeChangeListenerRegistration(dataBroker);
63     }
64
65     @Test(expected = NullPointerException.class)
66     public void testNullChangeEvent() {
67         notifSupplierImpl.onDataTreeChanged(null);
68     }
69
70     @Test(expected = NullPointerException.class)
71     public void testNullableChangeEvent() {
72         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
73     }
74
75     @Test
76     public void testEmptyChangeEvent() {
77         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
78     }
79
80     @Test
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()
87                              .getValue());
88         assertEquals(FLOW_NODE_ID,
89                      notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
90     }
91
92     @Test
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));
100     }
101
102     @Test(expected = IllegalArgumentException.class)
103     public void testCreateFromNullNodeConnector() {
104         notifSupplierImpl.createNotification(null, createTestMeterPath());
105     }
106
107     @Test(expected = IllegalArgumentException.class)
108     public void testCreateFromNullPath() {
109         notifSupplierImpl.createNotification(createTestMeter(), null);
110     }
111
112     @Test
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()
120                              .getValue());
121         assertEquals(FLOW_NODE_ID,
122                      notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
123     }
124
125     @Test
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));
133     }
134
135     @Test(expected = IllegalArgumentException.class)
136     public void testUpdateFromNullNodeConnector() {
137         notifSupplierImpl.createNotification(null, createTestMeterPath());
138     }
139
140     @Test(expected = IllegalArgumentException.class)
141     public void testUpdateFromNullPath() {
142         notifSupplierImpl.createNotification(createTestMeter(), null);
143     }
144
145     @Test
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()
152                              .getValue());
153         assertEquals(FLOW_NODE_ID,
154                      notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
155     }
156
157     @Test
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));
165     }
166
167     @Test(expected = IllegalArgumentException.class)
168     public void testDeleteFromNullPath() {
169         notifSupplierImpl.deleteNotification(null);
170     }
171
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)));
175     }
176
177     private static Meter createTestMeter() {
178         final MeterBuilder builder = new MeterBuilder();
179         builder.setMeterId(new MeterId(METER_ID));
180         return builder.build();
181     }
182
183     private static Meter createUpdatedTestMeter() {
184         final MeterBuilder builder = new MeterBuilder();
185         builder.setMeterId(new MeterId(UPDATED_METER_ID));
186         return builder.build();
187     }
188 }
189