Bug 5947: Increasing code coverage - mdsal-dom-broker
[mdsal.git] / dom / mdsal-dom-broker / src / test / java / org / opendaylight / mdsal / dom / broker / DOMNotificationRouterTest.java
1 /*
2  * Copyright (c) 2016 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.mdsal.dom.broker;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15
16 import com.google.common.collect.Multimap;
17 import java.lang.reflect.Field;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.TimeUnit;
20 import javax.annotation.Nonnull;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.dom.api.DOMNotification;
23 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
24 import org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListener;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.util.ListenerRegistry;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28
29 public class DOMNotificationRouterTest extends TestUtils {
30
31     @Test
32     public void create() throws Exception {
33         assertNotNull(DOMNotificationRouter.create(1,1,1,TimeUnit.SECONDS));
34         assertNotNull(DOMNotificationRouter.create(1));
35     }
36
37     @Test
38     public void complexTest() throws Exception {
39         final DOMNotificationSubscriptionListener domNotificationSubscriptionListener =
40                 mock(DOMNotificationSubscriptionListener.class);
41         final DOMNotificationListener domNotificationListener = new TestListener();
42         final DOMNotificationRouter domNotificationRouter = DOMNotificationRouter.create(1);
43
44         final Field listenersField = DOMNotificationRouter.class.getDeclaredField("listeners");
45         listenersField.setAccessible(true);
46
47         final Field subscriptionListenersField = DOMNotificationRouter.class.getDeclaredField("subscriptionListeners");
48         subscriptionListenersField.setAccessible(true);
49
50         Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> listeners =
51                 (Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>>)
52                         listenersField.get(domNotificationRouter);
53
54         assertTrue(listeners.isEmpty());
55         assertNotNull(domNotificationRouter.registerNotificationListener(domNotificationListener, SchemaPath.ROOT));
56         assertNotNull(domNotificationRouter.registerNotificationListener(domNotificationListener, SchemaPath.SAME));
57
58         listeners = (Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>>)
59                         listenersField.get(domNotificationRouter);
60
61         assertFalse(listeners.isEmpty());
62
63         ListenerRegistry<DOMNotificationSubscriptionListener> subscriptionListeners =
64                 (ListenerRegistry<DOMNotificationSubscriptionListener>)
65                         subscriptionListenersField.get(domNotificationRouter);
66
67         assertFalse(subscriptionListeners.iterator().hasNext());
68         assertNotNull(domNotificationRouter.registerSubscriptionListener(domNotificationSubscriptionListener));
69
70         subscriptionListeners = (ListenerRegistry<DOMNotificationSubscriptionListener>)
71                         subscriptionListenersField.get(domNotificationRouter);
72         assertTrue(subscriptionListeners.iterator().hasNext());
73
74         final DOMNotification domNotification = mock(DOMNotification.class);
75         doReturn("test").when(domNotification).toString();
76         doReturn(SchemaPath.ROOT).when(domNotification).getType();
77         doReturn(TEST_CHILD).when(domNotification).getBody();
78
79         assertNotNull(domNotificationRouter.offerNotification(domNotification));
80
81         Exception exception = null;
82         try {
83             assertNotNull(domNotificationRouter.offerNotification(domNotification, 1, TimeUnit.SECONDS));
84             assertNotNull(domNotificationRouter.offerNotification(domNotification, 1, TimeUnit.SECONDS));
85         } catch (UnsupportedOperationException e) {
86             exception = e;
87         }
88         assertNotNull(exception);
89         assertNotNull(domNotificationRouter.putNotification(domNotification));
90     }
91
92     @Test
93     public void offerNotification() throws Exception {
94         final DOMNotificationRouter domNotificationRouter = DOMNotificationRouter.create(1);
95         final DOMNotification domNotification = mock(DOMNotification.class);
96         doReturn(SchemaPath.ROOT).when(domNotification).getType();
97         doReturn(TEST_CHILD).when(domNotification).getBody();
98         assertNotNull(domNotificationRouter.putNotification(domNotification));
99         assertNotNull(domNotificationRouter.offerNotification(domNotification));
100         assertNotNull(domNotificationRouter.offerNotification(domNotification, 1, TimeUnit.SECONDS));
101     }
102
103     @Test
104     public void close() throws Exception {
105         final DOMNotificationRouter domNotificationRouter = DOMNotificationRouter.create(1);
106
107         final Field executorField = DOMNotificationRouter.class.getDeclaredField("executor");
108         executorField.setAccessible(true);
109
110         final ExecutorService executor = (ExecutorService) executorField.get(domNotificationRouter);
111
112         assertFalse(executor.isShutdown());
113         domNotificationRouter.close();
114         assertTrue(executor.isShutdown());
115     }
116
117     private class TestListener implements DOMNotificationListener {
118         @Override
119         public void onNotification(@Nonnull DOMNotification notification) {}
120     }
121 }