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