Add negative tests for instance identifier codec 30/99830/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 21 Feb 2022 13:03:13 +0000 (14:03 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 22 Feb 2022 09:47:58 +0000 (10:47 +0100)
We are about to refactor a few aspects of how things are being looked
up, make sure we cover cases which should be rejected.

JIRA: MDSAL-696
Change-Id: I8b0eddf05eadd4178951176480fa35b9753bc751
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-dom-codec/src/test/java/org/opendaylight/mdsal/binding/dom/codec/impl/InstanceIdentifierSerializeDeserializeTest.java

index a837d961279e32b65303ec1a2a63a5455513a010..c80f1e72f48656ab47ba68c00a0900f15f309406 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;
@@ -178,4 +186,39 @@ public class InstanceIdentifierSerializeDeserializeTest extends AbstractBindingC
                 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 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 list lst", ex.getMessage());
+    }
 }