2a9efeb6b63b7efa9dca97517651c0c5d3f2e965
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / DirectGetterRouteContextExtractorTest.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;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13 import static org.mockito.Mockito.mock;
14
15 import java.lang.reflect.Method;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19
20 public class DirectGetterRouteContextExtractorTest {
21
22     private static final InstanceIdentifier<?> INSTANCE_IDENTIFIER = InstanceIdentifier.create(DataObject.class);
23     private static final String EXCEPTION_TEXT = "testException";
24
25     @Test
26     public void basicTest() throws Exception {
27         final Method testMthd = this.getClass().getDeclaredMethod("testMethod", DataObject.class);
28         testMthd.setAccessible(true);
29         final ContextReferenceExtractor referenceExtractor = DirectGetterRouteContextExtractor.create(testMthd);
30         assertEquals(testMethod(mock(DataObject.class)), referenceExtractor.extract(mock(DataObject.class)));
31
32         try {
33             referenceExtractor.extract(null);
34             fail("Expected exception");
35         } catch (NullPointerException e) {
36             assertTrue(e.getMessage().equals(EXCEPTION_TEXT));
37         }
38     }
39
40     private static InstanceIdentifier<?> testMethod(final DataObject data) {
41         if (data == null) {
42             throw new NullPointerException(EXCEPTION_TEXT);
43         }
44         return INSTANCE_IDENTIFIER;
45     }
46 }