Modernize NotificationListenerInvokerTest
[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.yangtools.yang.binding.Augmentation;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.Notification;
25 import org.opendaylight.yangtools.yang.binding.NotificationListener;
26 import org.opendaylight.yangtools.yang.common.QName;
27
28 public class NotificationListenerInvokerTest {
29     @Test
30     public void fromTest() throws Exception {
31         assertNotNull(NotificationListenerInvoker.from(TestInterface.class));
32     }
33
34     @Test
35     public void fromWithExceptionTest() {
36         final var cause = assertThrows(UncheckedExecutionException.class,
37             () -> NotificationListenerInvoker.from(TestPrivateInterface.class))
38             .getCause();
39         assertThat(cause, instanceOf(IllegalStateException.class));
40         assertThat(cause.getCause(), instanceOf(IllegalAccessException.class));
41     }
42
43     @Test
44     public void invokeNotification() {
45         final NotificationListener notificationListener = mock(NotificationListener.class);
46         final MethodHandle methodHandle = mock(MethodHandle.class);
47         final NotificationListenerInvoker notificationListenerInvoker =
48                 new NotificationListenerInvoker(ImmutableMap.of(QName.create("test", "test"), methodHandle));
49
50         final var ex = assertThrows(WrongMethodTypeException.class,
51             () -> notificationListenerInvoker.invokeNotification(notificationListener, QName.create("test", "test"),
52                 null));
53         assertEquals("expected null but found (NotificationListener,DataContainer)void", ex.getMessage());
54     }
55
56     public interface TestInterface extends NotificationListener, Augmentation {
57         QName QNAME = QName.create("test", "test");
58
59         void onTestNotificationInterface(TestNotificationInterface notif);
60     }
61
62     private interface TestPrivateInterface extends NotificationListener, Augmentation {
63         QName QNAME = QName.create("test", "test");
64
65         void onTestNotificationInterface(TestNotificationInterface notif);
66     }
67
68     public interface TestNotificationInterface extends DataObject, Notification<TestNotificationInterface> {
69         QName QNAME = QName.create("test", "test");
70     }
71 }