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.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.HashMap;
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.md.sal.binding.api.DataObjectModification;
26 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
27 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
28 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestChangeEventBuildHelper;
29 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestData;
30 import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestSupplierVerifyHelper;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatistics;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
45 import org.opendaylight.yangtools.yang.binding.DataObject;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 public class FlowStatNotificationSupplierImplTest {
50 private static final String FLOW_NODE_ID = "openflow:111";
51 private static final Short FLOW_TABLE_ID = 111;
52 private static final String FLOW_ID = "test-flow-111";
53 private FlowStatNotificationSupplierImpl notifSupplierImpl;
54 private NotificationProviderService notifProviderService;
55 private DataBroker dataBroker;
58 public void initalization() {
59 notifProviderService = mock(NotificationProviderService.class);
60 dataBroker = mock(DataBroker.class);
61 notifSupplierImpl = new FlowStatNotificationSupplierImpl(notifProviderService, dataBroker);
62 TestSupplierVerifyHelper.verifyDataChangeRegistration(dataBroker);
65 @Test(expected = NullPointerException.class)
66 public void testNullChangeEvent() {
67 notifSupplierImpl.onDataTreeChanged(null);
70 @Test(expected = NullPointerException.class)
71 public void testNullableChangeEvent() {
72 notifSupplierImpl.onDataTreeChanged( TestChangeEventBuildHelper.createNullTestDataTreeEvent());
76 public void testEmptyChangeEvent() {
77 notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
81 public void testCreate() {
82 final FlowsStatisticsUpdate notification = notifSupplierImpl.createNotification(createTestFlowStat(), createTestFlowStatPath());
83 assertNotNull(notification);
84 assertEquals(FLOW_NODE_ID, notification.getId().getValue());
88 public void testCreateChangeEvent() {
89 final TestData testData = new TestData(createTestFlowStatPath(),null,createTestFlowStat(),
90 DataObjectModification.ModificationType.WRITE);
91 Collection<DataTreeModification<FlowStatistics>> collection = new ArrayList<>();
92 collection.add(testData);
93 notifSupplierImpl.onDataTreeChanged(collection);
94 verify(notifProviderService, times(1)).publish(Matchers.any(FlowsStatisticsUpdate.class));
97 @Test(expected = IllegalArgumentException.class)
98 public void testCreateFromNullNodeConnector() {
99 notifSupplierImpl.createNotification(null, createTestFlowStatPath());
102 @Test(expected = IllegalArgumentException.class)
103 public void testCreateFromNullPath() {
104 notifSupplierImpl.createNotification(createTestFlowStat(), null);
107 @Test(expected = IllegalArgumentException.class)
108 public void testUpdateFromNullNodeConnector() {
109 notifSupplierImpl.createNotification(null, createTestFlowStatPath());
112 @Test(expected = IllegalArgumentException.class)
113 public void testUpdateFromNullPath() {
114 notifSupplierImpl.createNotification(createTestFlowStat(), null);
117 private static InstanceIdentifier<FlowStatistics> createTestFlowStatPath() {
118 return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
119 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(FLOW_TABLE_ID))
120 .child(Flow.class, new FlowKey(new FlowId(FLOW_ID))).augmentation(FlowStatisticsData.class)
121 .child(FlowStatistics.class);
124 private static FlowStatistics createTestFlowStat() {
125 final FlowStatisticsBuilder builder = new FlowStatisticsBuilder();
126 return builder.build();