Fix NotificationListenerInvokerTest on Java 19+
[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.endsWith;
11 import static org.hamcrest.CoreMatchers.instanceOf;
12 import static org.hamcrest.MatcherAssert.assertThat;
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.MethodHandles;
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() throws Exception {
46         final var notificationListener = mock(NotificationListener.class);
47         final var methodHandle = MethodHandles.publicLookup().unreflect(String.class.getDeclaredMethod("toString"));
48
49         final var notificationListenerInvoker = new NotificationListenerInvoker(
50             ImmutableMap.of(QName.create("test", "test"), methodHandle));
51
52         final var ex = assertThrows(WrongMethodTypeException.class,
53             () -> notificationListenerInvoker.invokeNotification(notificationListener, QName.create("test", "test"),
54                 null));
55         assertThat(ex.getMessage(), endsWith(" (String)String but found (NotificationListener,DataContainer)void"));
56     }
57
58     private interface TestPrivateInterface extends NotificationListener, Augmentation {
59         QName QNAME = QName.create("test", "test");
60
61         void onTestNotificationInterface(TestNotificationInterface notif);
62     }
63
64     public interface TestNotificationInterface extends DataObject, Notification<TestNotificationInterface> {
65         QName QNAME = QName.create("test", "test");
66     }
67 }