Convert mdsal-binding-dom-adapter to a JPMS module
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / invoke / NotificationListenerInvokerTest.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.binding.dom.adapter.invoke;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertThrows;
15 import static org.mockito.Mockito.mock;
16
17 import com.google.common.collect.ImmutableMap;
18 import com.google.common.util.concurrent.UncheckedExecutionException;
19 import java.lang.invoke.MethodHandle;
20 import java.lang.invoke.WrongMethodTypeException;
21 import org.junit.Test;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OpendaylightTestNotificationListener;
23 import org.opendaylight.yangtools.yang.binding.Augmentation;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.opendaylight.yangtools.yang.binding.Notification;
26 import org.opendaylight.yangtools.yang.binding.NotificationListener;
27 import org.opendaylight.yangtools.yang.common.QName;
28
29 public class NotificationListenerInvokerTest {
30     @Test
31     public void fromTest() throws Exception {
32         assertNotNull(NotificationListenerInvoker.from(OpendaylightTestNotificationListener.class));
33     }
34
35     @Test
36     public void fromWithExceptionTest() {
37         final var cause = assertThrows(UncheckedExecutionException.class,
38             () -> NotificationListenerInvoker.from(TestPrivateInterface.class))
39             .getCause();
40         assertThat(cause, instanceOf(IllegalStateException.class));
41         assertThat(cause.getCause(), instanceOf(IllegalAccessException.class));
42     }
43
44     @Test
45     public void invokeNotification() {
46         final NotificationListener notificationListener = mock(NotificationListener.class);
47         final MethodHandle methodHandle = mock(MethodHandle.class);
48         final NotificationListenerInvoker notificationListenerInvoker =
49                 new NotificationListenerInvoker(ImmutableMap.of(QName.create("test", "test"), methodHandle));
50
51         final var ex = assertThrows(WrongMethodTypeException.class,
52             () -> notificationListenerInvoker.invokeNotification(notificationListener, QName.create("test", "test"),
53                 null));
54         assertEquals("expected null but found (NotificationListener,DataContainer)void", ex.getMessage());
55     }
56
57     private interface TestPrivateInterface extends NotificationListener, Augmentation {
58         QName QNAME = QName.create("test", "test");
59
60         void onTestNotificationInterface(TestNotificationInterface notif);
61     }
62
63     public interface TestNotificationInterface extends DataObject, Notification<TestNotificationInterface> {
64         QName QNAME = QName.create("test", "test");
65     }
66 }