0c32cb51e32239ce66aa982955b1ffa9637ce2e1
[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 java.util.HashMap;
20 import java.util.Map;
21
22 import org.junit.Before;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern;
31 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceService;
32 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicInput;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38
39 import com.google.common.util.concurrent.CheckedFuture;
40
41 public class EventSourceTopicTest {
42
43     EventSourceTopic eventSourceTopic;
44     Node dataObjectNodeMock;
45     NodeId nodeIdMock;
46     DataBroker dataBrokerMock;
47     EventSourceService eventSourceServiceMock;
48     EventSourceTopology eventSourceTopologyMock;
49
50     @BeforeClass
51     public static void initTestClass() throws IllegalAccessException, InstantiationException {
52     }
53
54     @Before
55     public void setUp() throws Exception {
56         NotificationPattern notificationPattern = new NotificationPattern("value1");
57         eventSourceServiceMock = mock(EventSourceService.class);
58         eventSourceTopologyMock = mock(EventSourceTopology.class);
59         dataBrokerMock = mock(DataBroker.class);
60         doReturn(eventSourceServiceMock).when(eventSourceTopologyMock).getEventSourceService();
61         doReturn(dataBrokerMock).when(eventSourceTopologyMock).getDataBroker();
62
63         WriteTransaction writeTransactionMock = mock(WriteTransaction.class);
64         doReturn(writeTransactionMock).when(dataBrokerMock).newWriteOnlyTransaction();
65         doNothing().when(writeTransactionMock).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(DataObject.class),eq(true));
66         CheckedFuture checkedFutureWriteMock = mock(CheckedFuture.class);
67         doReturn(checkedFutureWriteMock).when(writeTransactionMock).submit();
68
69         ReadOnlyTransaction readOnlyTransactionMock = mock(ReadOnlyTransaction.class);
70         doReturn(readOnlyTransactionMock).when(dataBrokerMock).newReadOnlyTransaction();
71         CheckedFuture checkedFutureReadMock = mock(CheckedFuture.class);
72         doReturn(checkedFutureReadMock).when(readOnlyTransactionMock).read(LogicalDatastoreType.OPERATIONAL, EventSourceTopology.EVENT_SOURCE_TOPOLOGY_PATH);
73         eventSourceTopic = EventSourceTopic.create(notificationPattern, "nodeIdPattern1", eventSourceTopologyMock);
74     }
75
76     @Test
77     public void createModuleTest() {
78         assertNotNull("Instance has not been created correctly.", eventSourceTopic);
79     }
80
81     @Test
82     public void getTopicIdTest() {
83         assertNotNull("Topic has not been created correctly.", eventSourceTopic.getTopicId());
84     }
85
86     @Test
87     public void onDataChangedTest() {
88         AsyncDataChangeEvent asyncDataChangeEventMock = mock(AsyncDataChangeEvent.class);
89         onDataChangedTestHelper(asyncDataChangeEventMock);
90         eventSourceTopic.onDataChanged(asyncDataChangeEventMock);
91         verify(dataObjectNodeMock, times(2)).getNodeId();
92         verify(nodeIdMock, times(2)).getValue();
93     }
94
95     private void onDataChangedTestHelper(AsyncDataChangeEvent asyncDataChangeEventMock){
96         Map<InstanceIdentifier<?>, DataObject> map = new HashMap<>();
97         InstanceIdentifier instanceIdentifierMock = mock(InstanceIdentifier.class);
98         dataObjectNodeMock = mock(Node.class);
99         doReturn(getNodeKey("testNodeId01")).when(dataObjectNodeMock).getKey();
100         map.put(instanceIdentifierMock, dataObjectNodeMock);
101         doReturn(map).when(asyncDataChangeEventMock).getUpdatedData();
102         doReturn(map).when(asyncDataChangeEventMock).getCreatedData();
103         nodeIdMock = mock(NodeId.class);
104         doReturn(nodeIdMock).when(dataObjectNodeMock).getNodeId();
105         doReturn("nodeIdPattern1").when(nodeIdMock).getValue();
106     }
107
108     @Test
109     public void notifyNodeTest() {
110         InstanceIdentifier instanceIdentifierMock = mock(InstanceIdentifier.class);
111         eventSourceTopic.notifyNode(instanceIdentifierMock);
112         verify(eventSourceServiceMock, times(1)).joinTopic(any(JoinTopicInput.class));
113     }
114
115     public NodeKey getNodeKey(String nodeId){
116         return new NodeKey(new NodeId(nodeId));
117     }
118 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.