Merge "Bug 5543 - Bo: Update JUnit tests part_13"
[openflowplugin.git] / applications / notification-supplier / src / test / java / org / opendaylight / openflowplugin / applications / notification / supplier / impl / item / stat / QueueStatNotificationSupplierImplTest.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.ArrayList;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.Map;
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.FlowCapableNodeConnector;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsData;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsDataBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatistics;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatisticsBuilder;
46 import org.opendaylight.yangtools.yang.binding.DataObject;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48
49 /**
50  *
51  */
52 public class QueueStatNotificationSupplierImplTest {
53
54     private static final String FLOW_NODE_ID = "openflow:111";
55     private static final String FLOW_CODE_CONNECTOR_ID = "test-con-111";
56     private QueueStatNotificationSupplierImpl notifSupplierImpl;
57     private NotificationProviderService notifProviderService;
58     private DataBroker dataBroker;
59
60     @Before
61     public void initalization() {
62         notifProviderService = mock(NotificationProviderService.class);
63         dataBroker = mock(DataBroker.class);
64         notifSupplierImpl = new QueueStatNotificationSupplierImpl(notifProviderService, dataBroker);
65         TestSupplierVerifyHelper.verifyDataChangeRegistration(dataBroker);
66     }
67
68     @Test(expected = NullPointerException.class)
69     public void testNullChangeEvent() {
70         notifSupplierImpl.onDataTreeChanged(null);
71     }
72
73     @Test(expected = NullPointerException.class)
74     public void testNullableChangeEvent() {
75         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createNullTestDataTreeEvent());
76     }
77
78     @Test
79     public void testEmptyChangeEvent() {
80         notifSupplierImpl.onDataTreeChanged(TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
81     }
82
83     @Test
84     public void testCreate() {
85         final QueueStatisticsUpdate notification = notifSupplierImpl.createNotification(createTestQueueStat(),
86                 createTestQueueStatPath());
87         assertNotNull(notification);
88         assertEquals(FLOW_NODE_ID, notification.getId().getValue());
89         assertEquals(FLOW_CODE_CONNECTOR_ID, notification.getNodeConnector().get(0).getId().getValue());
90     }
91
92     @Test
93     public void testCreateChangeEvent() {
94         final TestData testData = new TestData(createTestQueueStatPath(),null,createTestQueueStat(),
95                 DataObjectModification.ModificationType.WRITE);
96         Collection<DataTreeModification<FlowCapableNodeConnectorQueueStatisticsData>> collection = new ArrayList<>();
97         collection.add(testData);
98         notifSupplierImpl.onDataTreeChanged(collection);
99         verify(notifProviderService, times(1)).publish(Matchers.any(QueueStatisticsUpdate.class));
100     }
101
102     @Test(expected = IllegalArgumentException.class)
103     public void testCreateFromNullNodeConnector() {
104         notifSupplierImpl.createNotification(null, createTestQueueStatPath());
105     }
106
107     @Test(expected = IllegalArgumentException.class)
108     public void testCreateFromNullPath() {
109         notifSupplierImpl.createNotification(createTestQueueStat(), null);
110     }
111
112     @Test(expected = IllegalArgumentException.class)
113     public void testUpdateFromNullNodeConnector() {
114         notifSupplierImpl.createNotification(null, createTestQueueStatPath());
115     }
116
117     @Test(expected = IllegalArgumentException.class)
118     public void testUpdateFromNullPath() {
119         notifSupplierImpl.createNotification(createTestQueueStat(), null);
120     }
121
122     private static InstanceIdentifier<FlowCapableNodeConnectorQueueStatisticsData> createTestQueueStatPath() {
123         return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
124                 .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId(FLOW_CODE_CONNECTOR_ID)))
125                 .augmentation(FlowCapableNodeConnector.class).child(Queue.class)
126                 .augmentation(FlowCapableNodeConnectorQueueStatisticsData.class);
127     }
128
129     private static FlowCapableNodeConnectorQueueStatisticsData createTestQueueStat() {
130         final FlowCapableNodeConnectorQueueStatisticsDataBuilder builder = new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
131         final FlowCapableNodeConnectorQueueStatisticsBuilder value = new FlowCapableNodeConnectorQueueStatisticsBuilder();
132         builder.setFlowCapableNodeConnectorQueueStatistics(value.build());
133         return builder.build();
134     }
135 }
136