BUG-4117: add support for meter and group old Notif
[openflowplugin.git] / applications / old-notification-supplier / src / test / java / org / opendaylight / openflowplugin / applications / old / 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.old.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.HashMap;
18 import java.util.HashSet;
19 import java.util.Map;
20 import java.util.Set;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Matchers;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
26 import org.opendaylight.openflowplugin.applications.old.notification.supplier.impl.helper.TestChangeEventBuildHelper;
27 import org.opendaylight.openflowplugin.applications.old.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.DataObject;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42
43 /**
44  *
45  */
46 public class MeterNotificationSupplierImplTest {
47
48     private static final String FLOW_NODE_ID = "test-111";
49     private static final Long METER_ID = 111L;
50     private MeterNotificationSupplierImpl notifSupplierImpl;
51     private NotificationProviderService notifProviderService;
52     private DataBroker dataBroker;
53
54     @Before
55     public void initalization() {
56         notifProviderService = mock(NotificationProviderService.class);
57         dataBroker = mock(DataBroker.class);
58         notifSupplierImpl = new MeterNotificationSupplierImpl(notifProviderService, dataBroker);
59         TestSupplierVerifyHelper.verifyDataChangeRegistration(dataBroker);
60     }
61
62     @Test(expected = IllegalArgumentException.class)
63     public void testNullChangeEvent() {
64         notifSupplierImpl.onDataChanged(null);
65     }
66
67     @Test
68     public void testNullableChangeEvent() {
69         notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
70     }
71
72     @Test
73     public void testEmptyChangeEvent() {
74         notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
75     }
76
77     @Test
78     public void testCreate() {
79         final MeterAdded notification = notifSupplierImpl.createNotification(createTestMeter(), createTestMeterPath());
80         assertNotNull(notification);
81         assertEquals(METER_ID, notification.getMeterId().getValue());
82         assertEquals(METER_ID, notification.getMeterRef().getValue().firstKeyOf(Meter.class, MeterKey.class).getMeterId().getValue());
83         assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
84     }
85
86     @Test
87     public void testCreateChangeEvent() {
88         final Map<InstanceIdentifier<?>, DataObject> createdData = new HashMap<>();
89         createdData.put(createTestMeterPath(), createTestMeter());
90         notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(createdData, null, null));
91         verify(notifProviderService, times(1)).publish(Matchers.any(MeterAdded.class));
92     }
93
94     @Test(expected = IllegalArgumentException.class)
95     public void testCreateFromNullNodeConnector() {
96         notifSupplierImpl.createNotification(null, createTestMeterPath());
97     }
98
99     @Test(expected = IllegalArgumentException.class)
100     public void testCreateFromNullPath() {
101         notifSupplierImpl.createNotification(createTestMeter(), null);
102     }
103
104     @Test
105     public void testUpdate() {
106         final MeterUpdated notification = notifSupplierImpl
107                 .updateNotification(createTestMeter(), createTestMeterPath());
108         assertNotNull(notification);
109         assertEquals(METER_ID, notification.getMeterId().getValue());
110         assertEquals(METER_ID, notification.getMeterRef().getValue().firstKeyOf(Meter.class, MeterKey.class).getMeterId().getValue());
111         assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
112     }
113
114     @Test
115     public void testUdateChangeEvent() {
116         final Map<InstanceIdentifier<?>, DataObject> createdData = new HashMap<>();
117         createdData.put(createTestMeterPath(), createTestMeter());
118         notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(createdData, null, null));
119         verify(notifProviderService, times(1)).publish(Matchers.any(MeterUpdated.class));
120     }
121
122     @Test(expected = IllegalArgumentException.class)
123     public void testUpdateFromNullNodeConnector() {
124         notifSupplierImpl.createNotification(null, createTestMeterPath());
125     }
126
127     @Test(expected = IllegalArgumentException.class)
128     public void testUpdateFromNullPath() {
129         notifSupplierImpl.createNotification(createTestMeter(), null);
130     }
131
132     @Test
133     public void testDelete() {
134         final MeterRemoved notification = notifSupplierImpl.deleteNotification(createTestMeterPath());
135         assertNotNull(notification);
136         assertEquals(METER_ID, notification.getMeterId().getValue());
137         assertEquals(METER_ID, notification.getMeterRef().getValue().firstKeyOf(Meter.class, MeterKey.class).getMeterId().getValue());
138         assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
139     }
140
141     @Test
142     public void testDeleteChangeEvent() {
143         final Set<InstanceIdentifier<?>> removeData = new HashSet<>();
144         removeData.add(createTestMeterPath());
145         notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(null, null, removeData));
146         verify(notifProviderService, times(1)).publish(Matchers.any(MeterRemoved.class));
147     }
148
149     @Test(expected = IllegalArgumentException.class)
150     public void testDeleteFromNullPath() {
151         notifSupplierImpl.deleteNotification(null);
152     }
153
154     private static InstanceIdentifier<Meter> createTestMeterPath() {
155         return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
156                 .augmentation(FlowCapableNode.class).child(Meter.class, new MeterKey(new MeterId(METER_ID)));
157     }
158
159     private static Meter createTestMeter() {
160         final MeterBuilder builder = new MeterBuilder();
161         builder.setMeterId(new MeterId(METER_ID));
162         return builder.build();
163     }
164 }
165