5e262136469c3f920755be611b81b8cb3d8e8987
[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 org.junit.Before;
11 import org.junit.BeforeClass;
12 import org.junit.Test;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
14 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern;
15 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceService;
16 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicInput;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.any;
30
31 public class EventSourceTopicTest {
32
33     EventSourceTopic eventSourceTopic;
34     org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node dataObjectMock;
35     NodeId nodeIdMock;
36     EventSourceService eventSourceServiceMock;
37
38     @BeforeClass
39     public static void initTestClass() throws IllegalAccessException, InstantiationException {
40     }
41
42     @Before
43     public void setUp() throws Exception {
44         NotificationPattern notificationPattern = new NotificationPattern("value1");
45         eventSourceServiceMock = mock(EventSourceService.class);
46         eventSourceTopic = new EventSourceTopic(notificationPattern, "nodeIdPattern1", eventSourceServiceMock);
47     }
48
49     @Test
50     public void createModuleTest() {
51         assertNotNull("Instance has not been created correctly.", eventSourceTopic);
52     }
53
54     @Test
55     public void getTopicIdTest() {
56         assertNotNull("Topic has not been created correctly.", eventSourceTopic.getTopicId());
57     }
58
59     @Test
60     public void onDataChangedTest() {
61         AsyncDataChangeEvent asyncDataChangeEventMock = mock(AsyncDataChangeEvent.class);
62         onDataChangedTestHelper(asyncDataChangeEventMock);
63         eventSourceTopic.onDataChanged(asyncDataChangeEventMock);
64         verify(dataObjectMock, times(1)).getId();
65         verify(nodeIdMock, times(1)).getValue();
66     }
67
68     private void onDataChangedTestHelper(AsyncDataChangeEvent asyncDataChangeEventMock){
69         Map<InstanceIdentifier<?>, DataObject> map = new HashMap<>();
70         InstanceIdentifier instanceIdentifierMock = mock(InstanceIdentifier.class);
71         dataObjectMock = mock(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class);
72         map.put(instanceIdentifierMock, dataObjectMock);
73         doReturn(map).when(asyncDataChangeEventMock).getUpdatedData();
74
75         nodeIdMock = mock(NodeId.class);
76         doReturn(nodeIdMock).when(dataObjectMock).getId();
77         doReturn("0").when(nodeIdMock).getValue();
78     }
79
80     @Test
81     public void notifyNodeTest() {
82         InstanceIdentifier instanceIdentifierMock = mock(InstanceIdentifier.class);
83         eventSourceTopic.notifyNode(instanceIdentifierMock);
84         verify(eventSourceServiceMock, times(1)).joinTopic(any(JoinTopicInput.class));
85     }
86
87 }