Switch to MD-SAL APIs
[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 package org.opendaylight.openflowplugin.applications.notification.supplier.impl.item;
9
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;
16
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;
41
42 /**
43  * Test for
44  * {@link org.opendaylight.openflowplugin.applications.notification.supplier.impl.item.MeterNotificationSupplierImpl}.
45  */
46 public class MeterNotificationSupplierImplTest {
47
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;
51
52     private MeterNotificationSupplierImpl notifSupplierImpl;
53     private NotificationPublishService notifProviderService;
54     private DataBroker dataBroker;
55
56     @Before
57     public void initalization() {
58         notifProviderService = mock(NotificationPublishService.class);
59         dataBroker = mock(DataBroker.class);
60         notifSupplierImpl = new MeterNotificationSupplierImpl(notifProviderService, dataBroker);
61         TestSupplierVerifyHelper.verifyDataTreeChangeListenerRegistration(dataBroker);
62     }
63
64     @Test(expected = NullPointerException.class)
65     public void testNullChangeEvent() {
66         notifSupplierImpl.onDataTreeChanged(null);
67     }
68
69     @Test(expected = NullPointerException.class)
70     public void testNullableChangeEvent() {
71         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
72     }
73
74     @Test
75     public void testEmptyChangeEvent() {
76         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
77     }
78
79     @Test
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()
86                              .getValue());
87         assertEquals(FLOW_NODE_ID,
88                      notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
89     }
90
91     @Test
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));
99     }
100
101     @Test(expected = IllegalArgumentException.class)
102     public void testCreateFromNullNodeConnector() {
103         notifSupplierImpl.createNotification(null, createTestMeterPath());
104     }
105
106     @Test(expected = IllegalArgumentException.class)
107     public void testCreateFromNullPath() {
108         notifSupplierImpl.createNotification(createTestMeter(), null);
109     }
110
111     @Test
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()
119                              .getValue());
120         assertEquals(FLOW_NODE_ID,
121                      notification.getNode().getValue().firstKeyOf(Node.class).getId().getValue());
122     }
123
124     @Test
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));
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() 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));
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