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