Switch to MD-SAL APIs
[openflowplugin.git] / applications / notification-supplier / src / test / java / org / opendaylight / openflowplugin / applications / notification / supplier / impl / item / stat / FlowStatNotificationSupplierImplTest.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.ArgumentMatchers.any;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.binding.api.DataObjectModification;
24 import org.opendaylight.mdsal.binding.api.DataTreeModification;
25 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
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;
44
45 public class FlowStatNotificationSupplierImplTest {
46
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 NotificationPublishService notifProviderService;
52     private DataBroker dataBroker;
53
54     @Before
55     public void initalization() {
56         notifProviderService = mock(NotificationPublishService.class);
57         dataBroker = mock(DataBroker.class);
58         notifSupplierImpl = new FlowStatNotificationSupplierImpl(notifProviderService, dataBroker);
59         TestSupplierVerifyHelper.verifyDataTreeChangeListenerRegistration(dataBroker);
60     }
61
62     @Test(expected = NullPointerException.class)
63     public void testNullChangeEvent() {
64         notifSupplierImpl.onDataTreeChanged(null);
65     }
66
67     @Test(expected = NullPointerException.class)
68     public void testNullableChangeEvent() {
69         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
70     }
71
72     @Test
73     public void testEmptyChangeEvent() {
74         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
75     }
76
77     @Test
78     public void testCreate() {
79         final FlowsStatisticsUpdate notification = notifSupplierImpl
80                 .createNotification(createTestFlowStat(), createTestFlowStatPath());
81         assertNotNull(notification);
82         assertEquals(FLOW_NODE_ID, notification.getId().getValue());
83     }
84
85     @Test
86     public void testCreateChangeEvent() throws InterruptedException {
87         final TestData<FlowStatistics> 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)).putNotification(any(FlowsStatisticsUpdate.class));
93     }
94
95     @Test(expected = IllegalArgumentException.class)
96     public void testCreateFromNullNodeConnector() {
97         notifSupplierImpl.createNotification(null, createTestFlowStatPath());
98     }
99
100     @Test(expected = IllegalArgumentException.class)
101     public void testCreateFromNullPath() {
102         notifSupplierImpl.createNotification(createTestFlowStat(), null);
103     }
104
105     @Test(expected = IllegalArgumentException.class)
106     public void testUpdateFromNullNodeConnector() {
107         notifSupplierImpl.createNotification(null, createTestFlowStatPath());
108     }
109
110     @Test(expected = IllegalArgumentException.class)
111     public void testUpdateFromNullPath() {
112         notifSupplierImpl.createNotification(createTestFlowStat(), null);
113     }
114
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);
120     }
121
122     private static FlowStatistics createTestFlowStat() {
123         final FlowStatisticsBuilder builder = new FlowStatisticsBuilder();
124         return builder.build();
125     }
126 }
127