2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.applications.notification.supplier.impl.item.stat;
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;
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.FlowId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatistics;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 public class FlowStatNotificationSupplierImplTest {
47 private static final String FLOW_NODE_ID = "openflow:111";
48 private static final Short FLOW_TABLE_ID = 111;
49 private static final String FLOW_ID = "test-flow-111";
50 private FlowStatNotificationSupplierImpl notifSupplierImpl;
51 private NotificationProviderService notifProviderService;
52 private DataBroker dataBroker;
55 public void initalization() {
56 notifProviderService = mock(NotificationProviderService.class);
57 dataBroker = mock(DataBroker.class);
58 notifSupplierImpl = new FlowStatNotificationSupplierImpl(notifProviderService, dataBroker);
59 TestSupplierVerifyHelper.verifyDataTreeChangeListenerRegistration(dataBroker);
62 @Test(expected = NullPointerException.class)
63 public void testNullChangeEvent() {
64 notifSupplierImpl.onDataTreeChanged(null);
67 @Test(expected = NullPointerException.class)
68 public void testNullableChangeEvent() {
69 notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
73 public void testEmptyChangeEvent() {
74 notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
78 public void testCreate() {
79 final FlowsStatisticsUpdate notification = notifSupplierImpl
80 .createNotification(createTestFlowStat(), createTestFlowStatPath());
81 assertNotNull(notification);
82 assertEquals(FLOW_NODE_ID, notification.getId().getValue());
86 public void testCreateChangeEvent() {
87 final TestData testData = new TestData(createTestFlowStatPath(), null, createTestFlowStat(),
88 DataObjectModification.ModificationType.WRITE);
89 Collection<DataTreeModification<FlowStatistics>> collection = new ArrayList<>();
90 collection.add(testData);
91 notifSupplierImpl.onDataTreeChanged(collection);
92 verify(notifProviderService, times(1)).publish(any(FlowsStatisticsUpdate.class));
95 @Test(expected = IllegalArgumentException.class)
96 public void testCreateFromNullNodeConnector() {
97 notifSupplierImpl.createNotification(null, createTestFlowStatPath());
100 @Test(expected = IllegalArgumentException.class)
101 public void testCreateFromNullPath() {
102 notifSupplierImpl.createNotification(createTestFlowStat(), null);
105 @Test(expected = IllegalArgumentException.class)
106 public void testUpdateFromNullNodeConnector() {
107 notifSupplierImpl.createNotification(null, createTestFlowStatPath());
110 @Test(expected = IllegalArgumentException.class)
111 public void testUpdateFromNullPath() {
112 notifSupplierImpl.createNotification(createTestFlowStat(), null);
115 private static InstanceIdentifier<FlowStatistics> createTestFlowStatPath() {
116 return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
117 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(FLOW_TABLE_ID))
118 .child(Flow.class, new FlowKey(new FlowId(FLOW_ID))).augmentation(FlowStatisticsData.class)
119 .child(FlowStatistics.class);
122 private static FlowStatistics createTestFlowStat() {
123 final FlowStatisticsBuilder builder = new FlowStatisticsBuilder();
124 return builder.build();