Switch to MD-SAL APIs
[openflowplugin.git] / applications / notification-supplier / src / test / java / org / opendaylight / openflowplugin / applications / notification / supplier / impl / item / stat / MeterStatNotificationSupplierImplTest.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.stat;
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.MeterKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatistics;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41
42 public class MeterStatNotificationSupplierImplTest {
43
44     private static final String FLOW_NODE_ID = "openflow:111";
45     private static final Long FLOW_METER_ID = 111L;
46     private MeterStatNotificationSupplierImpl notifSupplierImpl;
47     private NotificationPublishService notifProviderService;
48     private DataBroker dataBroker;
49
50     @Before
51     public void initalization() {
52         notifProviderService = mock(NotificationPublishService.class);
53         dataBroker = mock(DataBroker.class);
54         notifSupplierImpl = new MeterStatNotificationSupplierImpl(notifProviderService, dataBroker);
55         TestSupplierVerifyHelper.verifyDataTreeChangeListenerRegistration(dataBroker);
56     }
57
58     @Test(expected = NullPointerException.class)
59     public void testNullChangeEvent() {
60         notifSupplierImpl.onDataTreeChanged(null);
61     }
62
63     @Test(expected = NullPointerException.class)
64     public void testNullableChangeEvent() {
65         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
66     }
67
68     @Test
69     public void testEmptyChangeEvent() {
70         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
71     }
72
73     @Test
74     public void testCreate() {
75         final MeterStatisticsUpdated notification = notifSupplierImpl.createNotification(createTestMeterStat(),
76                 createTestMeterStatPath());
77         assertNotNull(notification);
78         assertEquals(FLOW_NODE_ID, notification.getId().getValue());
79     }
80
81     @Test
82     public void testCreateChangeEvent() throws InterruptedException {
83         final TestData<MeterStatistics> testData = new TestData<>(createTestMeterStatPath(),null,createTestMeterStat(),
84                 DataObjectModification.ModificationType.WRITE);
85         Collection<DataTreeModification<MeterStatistics>> collection = new ArrayList<>();
86         collection.add(testData);
87         notifSupplierImpl.onDataTreeChanged(collection);
88         verify(notifProviderService, times(1)).putNotification(any(MeterStatisticsUpdated.class));
89     }
90
91     @Test(expected = IllegalArgumentException.class)
92     public void testCreateFromNullNodeConnector() {
93         notifSupplierImpl.createNotification(null, createTestMeterStatPath());
94     }
95
96     @Test(expected = IllegalArgumentException.class)
97     public void testCreateFromNullPath() {
98         notifSupplierImpl.createNotification(createTestMeterStat(), null);
99     }
100
101     @Test(expected = IllegalArgumentException.class)
102     public void testUpdateFromNullNodeConnector() {
103         notifSupplierImpl.createNotification(null, createTestMeterStatPath());
104     }
105
106     @Test(expected = IllegalArgumentException.class)
107     public void testUpdateFromNullPath() {
108         notifSupplierImpl.createNotification(createTestMeterStat(), null);
109     }
110
111     private static InstanceIdentifier<MeterStatistics> createTestMeterStatPath() {
112         return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
113                 .augmentation(FlowCapableNode.class).child(Meter.class, new MeterKey(new MeterId(FLOW_METER_ID)))
114                 .augmentation(NodeMeterStatistics.class).child(MeterStatistics.class);
115     }
116
117     private static MeterStatistics createTestMeterStat() {
118         final MeterStatisticsBuilder builder = new MeterStatisticsBuilder();
119         return builder.build();
120     }
121 }
122