DataChangeListener cleanup
[controller.git] / opendaylight / md-sal / messagebus-impl / src / test / java / org / opendaylight / controller / messagebus / app / impl / EventSourceTopicTest.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 package org.opendaylight.controller.messagebus.app.impl;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18
19 import com.google.common.util.concurrent.CheckedFuture;
20 import java.util.Collections;
21 import org.junit.Before;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
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.DataTreeIdentifier;
27 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
28 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
29 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern;
32 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceService;
33 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicInput;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
37 import org.opendaylight.yangtools.yang.binding.DataObject;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39
40 public class EventSourceTopicTest {
41
42     EventSourceTopic eventSourceTopic;
43     DataBroker dataBrokerMock;
44     EventSourceService eventSourceServiceMock;
45     EventSourceTopology eventSourceTopologyMock;
46
47     @BeforeClass
48     public static void initTestClass() throws IllegalAccessException, InstantiationException {
49     }
50
51     @Before
52     public void setUp() throws Exception {
53         NotificationPattern notificationPattern = new NotificationPattern("value1");
54         eventSourceServiceMock = mock(EventSourceService.class);
55         eventSourceTopologyMock = mock(EventSourceTopology.class);
56         dataBrokerMock = mock(DataBroker.class);
57         doReturn(eventSourceServiceMock).when(eventSourceTopologyMock).getEventSourceService();
58         doReturn(dataBrokerMock).when(eventSourceTopologyMock).getDataBroker();
59
60         WriteTransaction writeTransactionMock = mock(WriteTransaction.class);
61         doReturn(writeTransactionMock).when(dataBrokerMock).newWriteOnlyTransaction();
62         doNothing().when(writeTransactionMock).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(DataObject.class),eq(true));
63         CheckedFuture checkedFutureWriteMock = mock(CheckedFuture.class);
64         doReturn(checkedFutureWriteMock).when(writeTransactionMock).submit();
65
66         ReadOnlyTransaction readOnlyTransactionMock = mock(ReadOnlyTransaction.class);
67         doReturn(readOnlyTransactionMock).when(dataBrokerMock).newReadOnlyTransaction();
68         CheckedFuture checkedFutureReadMock = mock(CheckedFuture.class);
69         doReturn(checkedFutureReadMock).when(readOnlyTransactionMock).read(LogicalDatastoreType.OPERATIONAL, EventSourceTopology.EVENT_SOURCE_TOPOLOGY_PATH);
70         eventSourceTopic = EventSourceTopic.create(notificationPattern, "nodeIdPattern1", eventSourceTopologyMock);
71     }
72
73     @Test
74     public void createModuleTest() {
75         assertNotNull("Instance has not been created correctly.", eventSourceTopic);
76     }
77
78     @Test
79     public void getTopicIdTest() {
80         assertNotNull("Topic has not been created correctly.", eventSourceTopic.getTopicId());
81     }
82
83     @SuppressWarnings("unchecked")
84     @Test
85     public void onDataTreeChangedTest() {
86         InstanceIdentifier<Node> instanceIdentifierMock = mock(InstanceIdentifier.class);
87         DataTreeModification<Node> mockDataTreeModification = mock(DataTreeModification.class);
88         DataObjectModification<Node> mockModification = mock(DataObjectModification.class);
89         doReturn(mockModification).when(mockDataTreeModification).getRootNode();
90         doReturn(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, instanceIdentifierMock))
91                 .when(mockDataTreeModification).getRootPath();
92         doReturn(DataObjectModification.ModificationType.WRITE).when(mockModification).getModificationType();
93
94         Node dataObjectNodeMock = mock(Node.class);
95         doReturn(getNodeKey("testNodeId01")).when(dataObjectNodeMock).getKey();
96         NodeId nodeIdMock = mock(NodeId.class);
97         doReturn(nodeIdMock).when(dataObjectNodeMock).getNodeId();
98         doReturn("nodeIdPattern1").when(nodeIdMock).getValue();
99
100         doReturn(dataObjectNodeMock).when(mockModification).getDataAfter();
101
102         eventSourceTopic.onDataTreeChanged(Collections.singletonList(mockDataTreeModification));
103         verify(dataObjectNodeMock).getNodeId();
104         verify(nodeIdMock).getValue();
105     }
106
107     @Test
108     public void notifyNodeTest() {
109         InstanceIdentifier instanceIdentifierMock = mock(InstanceIdentifier.class);
110         eventSourceTopic.notifyNode(instanceIdentifierMock);
111         verify(eventSourceServiceMock, times(1)).joinTopic(any(JoinTopicInput.class));
112     }
113
114     public NodeKey getNodeKey(String nodeId){
115         return new NodeKey(new NodeId(nodeId));
116     }
117 }