Split STATIC_CODECS into per-type caches
[mdsal.git] / binding / mdsal-binding-dom-codec / src / test / java / org / opendaylight / mdsal / binding / dom / codec / impl / InstanceIdentifierSerializeDeserializeTest.java
index 20fc24d5a886eaea09dd661a0bb62f7ab9a86b4f..e000547f16696173ace68e1b8b6967ff2bd0fba7 100644 (file)
@@ -7,13 +7,21 @@
  */
 package org.opendaylight.mdsal.binding.dom.codec.impl;
 
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
 import com.google.common.collect.Iterables;
 import org.junit.Test;
+import org.opendaylight.mdsal.binding.dom.codec.api.IncorrectNestingException;
+import org.opendaylight.yang.gen.v1.urn.odl.actions.norev.Lst;
+import org.opendaylight.yang.gen.v1.urn.odl.actions.norev.lst.Foo;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.notification.rev150205.OutOfPixieDustNotification;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.md.sal.knock.knock.rev180723.KnockKnockInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugment;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
@@ -137,7 +145,7 @@ public class InstanceIdentifierSerializeDeserializeTest extends AbstractBindingC
             InstanceIdentifier.builder(Cont.class).child(ContAug.class, GrpCont.class).build());
         assertEquals(YangInstanceIdentifier.create(NodeIdentifier.create(Cont.QNAME),
             NodeIdentifier.create(ContChoice.QNAME),
-            NodeIdentifier.create(GrpCont.QNAME.withModule(ContAug.QNAME.getModule()))), contAug);
+            NodeIdentifier.create(GrpCont.QNAME.bindTo(ContAug.QNAME.getModule()))), contAug);
 
         // Legacy: downcast the child to Class, losing type safety but still working. Faced with ambiguity, it will
         //         select the lexically-lower class
@@ -154,7 +162,7 @@ public class InstanceIdentifierSerializeDeserializeTest extends AbstractBindingC
         final YangInstanceIdentifier rootAug = codecContext.toYangInstanceIdentifier(
             InstanceIdentifier.builder(RootAug.class, GrpCont.class).build());
         assertEquals(YangInstanceIdentifier.create(NodeIdentifier.create(Root.QNAME),
-            NodeIdentifier.create(GrpCont.QNAME.withModule(RootAug.QNAME.getModule()))), rootAug);
+            NodeIdentifier.create(GrpCont.QNAME.bindTo(RootAug.QNAME.getModule()))), rootAug);
     }
 
     @Test
@@ -166,7 +174,7 @@ public class InstanceIdentifierSerializeDeserializeTest extends AbstractBindingC
 
         final InstanceIdentifier<?> contAug = codecContext.fromYangInstanceIdentifier(
             YangInstanceIdentifier.create(NodeIdentifier.create(Cont.QNAME), NodeIdentifier.create(ContChoice.QNAME),
-                NodeIdentifier.create(GrpCont.QNAME.withModule(ContAug.QNAME.getModule()))));
+                NodeIdentifier.create(GrpCont.QNAME.bindTo(ContAug.QNAME.getModule()))));
         assertEquals(InstanceIdentifier.builder(Cont.class).child(ContAug.class, GrpCont.class).build(), contAug);
 
         final InstanceIdentifier<?> rootBase = codecContext.fromYangInstanceIdentifier(
@@ -175,7 +183,43 @@ public class InstanceIdentifierSerializeDeserializeTest extends AbstractBindingC
 
         final InstanceIdentifier<?> rootAug = codecContext.fromYangInstanceIdentifier(
             YangInstanceIdentifier.create(NodeIdentifier.create(Root.QNAME),
-                NodeIdentifier.create(GrpCont.QNAME.withModule(RootAug.QNAME.getModule()))));
+                NodeIdentifier.create(GrpCont.QNAME.bindTo(RootAug.QNAME.getModule()))));
         assertEquals(InstanceIdentifier.builder(RootAug.class, GrpCont.class).build(), rootAug);
     }
+
+    @Test
+    public void testRejectNotificationQName() {
+        // A purposely-wrong YangInstanceIdentifier
+        final var yiid = YangInstanceIdentifier.create(NodeIdentifier.create(OutOfPixieDustNotification.QNAME));
+        final var ex = assertThrows(IncorrectNestingException.class,
+            () -> codecContext.fromYangInstanceIdentifier(yiid));
+        assertThat(ex.getMessage(),
+            startsWith("Argument (urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:bi:ba:notification"
+                + "?revision=2015-02-05)out-of-pixie-dust-notification is not valid data tree child of "));
+    }
+
+    @Test
+    public void testRejectRpcQName() {
+        // A purposely-wrong YangInstanceIdentifier
+        final var yiid = YangInstanceIdentifier.create(NodeIdentifier.create(
+            // TODO: use the RPC interface once we are generating it
+            QName.create(KnockKnockInput.QNAME, "knock-knock")));
+        final var ex = assertThrows(IncorrectNestingException.class,
+            () -> codecContext.fromYangInstanceIdentifier(yiid));
+        assertThat(ex.getMessage(), startsWith("Argument (urn:opendaylight:params:xml:ns:yang:md:sal:knock-knock"
+            + "?revision=2018-07-23)knock-knock is not valid child of "));
+    }
+
+    @Test
+    public void testRejectActionQName() {
+        // A purposely-wrong YangInstanceIdentifier
+        final var yiid = YangInstanceIdentifier.create(
+            NodeIdentifier.create(Lst.QNAME),
+            NodeIdentifierWithPredicates.of(Lst.QNAME, QName.create(Lst.QNAME, "key"), "foo"),
+            NodeIdentifier.create(Foo.QNAME));
+        final var ex = assertThrows(IncorrectNestingException.class,
+            () -> codecContext.fromYangInstanceIdentifier(yiid));
+        assertEquals("Argument (urn:odl:actions)foo is not valid child of "
+            + "EmptyListEffectiveStatement{argument=(urn:odl:actions)lst}", ex.getMessage());
+    }
 }