Bug 5947: yet another tests for binding-dom-adapter
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / DirectGetterRouteContextExtractorTest.java
diff --git a/binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/DirectGetterRouteContextExtractorTest.java b/binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/DirectGetterRouteContextExtractorTest.java
new file mode 100644 (file)
index 0000000..f3d6d96
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.dom.adapter;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+
+import java.lang.reflect.Method;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+public class DirectGetterRouteContextExtractorTest {
+
+    private static final InstanceIdentifier INSTANCE_IDENTIFIER = InstanceIdentifier.create(DataObject.class);
+    private static final String EXCEPTION_TEXT = "testException";
+
+    @Test
+    public void basicTest() throws Exception {
+        final Method testMthd = this.getClass().getDeclaredMethod("testMethod", DataObject.class);
+        testMthd.setAccessible(true);
+        final ContextReferenceExtractor referenceExtractor = DirectGetterRouteContextExtractor.create(testMthd);
+        assertEquals(testMethod(mock(DataObject.class)), referenceExtractor.extract(mock(DataObject.class)));
+
+        try {
+            referenceExtractor.extract(null);
+            fail("Expected exception");
+        } catch (NullPointerException e) {
+            assertTrue(e.getMessage().equals(EXCEPTION_TEXT));
+        }
+    }
+
+    private static InstanceIdentifier testMethod(DataObject data) {
+        if (data == null) {
+            throw new NullPointerException(EXCEPTION_TEXT);
+        }
+        return INSTANCE_IDENTIFIER;
+    }
+}
\ No newline at end of file