Merge "Fix bad generated path for generated sources"
[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
9 package org.opendaylight.openflowplugin.applications.notification.supplier.impl.item.stat;
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.Map;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Matchers;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
24 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestChangeEventBuildHelper;
25 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestSupplierVerifyHelper;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatistics;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40
41 public class MeterStatNotificationSupplierImplTest {
42
43     private static final String FLOW_NODE_ID = "test-111";
44     private static final Long FLOW_METER_ID = 111L;
45     private MeterStatNotificationSupplierImpl notifSupplierImpl;
46     private NotificationProviderService notifProviderService;
47     private DataBroker dataBroker;
48
49     @Before
50     public void initalization() {
51         notifProviderService = mock(NotificationProviderService.class);
52         dataBroker = mock(DataBroker.class);
53         notifSupplierImpl = new MeterStatNotificationSupplierImpl(notifProviderService, dataBroker);
54         TestSupplierVerifyHelper.verifyDataChangeRegistration(dataBroker);
55     }
56
57     @Test(expected = IllegalArgumentException.class)
58     public void testNullChangeEvent() {
59         notifSupplierImpl.onDataChanged(null);
60     }
61
62     @Test
63     public void testNullableChangeEvent() {
64         notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
65     }
66
67     @Test
68     public void testEmptyChangeEvent() {
69         notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
70     }
71
72     @Test
73     public void testCreate() {
74         final MeterStatisticsUpdated notification = notifSupplierImpl.createNotification(createTestMeterStat(),
75                 createTestMeterStatPath());
76         assertNotNull(notification);
77         assertEquals(FLOW_NODE_ID, notification.getId().getValue());
78     }
79
80     @Test
81     public void testCreateChangeEvent() {
82         final Map<InstanceIdentifier<?>, DataObject> createdData = new HashMap<>();
83         createdData.put(createTestMeterStatPath(), createTestMeterStat());
84         notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(createdData, null, null));
85         verify(notifProviderService, times(1)).publish(Matchers.any(MeterStatisticsUpdated.class));
86     }
87
88     @Test(expected = IllegalArgumentException.class)
89     public void testCreateFromNullNodeConnector() {
90         notifSupplierImpl.createNotification(null, createTestMeterStatPath());
91     }
92
93     @Test(expected = IllegalArgumentException.class)
94     public void testCreateFromNullPath() {
95         notifSupplierImpl.createNotification(createTestMeterStat(), null);
96     }
97
98     @Test(expected = IllegalArgumentException.class)
99     public void testUpdateFromNullNodeConnector() {
100         notifSupplierImpl.createNotification(null, createTestMeterStatPath());
101     }
102
103     @Test(expected = IllegalArgumentException.class)
104     public void testUpdateFromNullPath() {
105         notifSupplierImpl.createNotification(createTestMeterStat(), null);
106     }
107
108     private static InstanceIdentifier<MeterStatistics> createTestMeterStatPath() {
109         return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
110                 .augmentation(FlowCapableNode.class).child(Meter.class, new MeterKey(new MeterId(FLOW_METER_ID)))
111                 .augmentation(NodeMeterStatistics.class).child(MeterStatistics.class);
112     }
113
114     private static MeterStatistics createTestMeterStat() {
115         final MeterStatisticsBuilder builder = new MeterStatisticsBuilder();
116         return builder.build();
117     }
118 }
119