Do not pass null to codec 18/108018/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 28 Sep 2023 17:06:20 +0000 (19:06 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 28 Sep 2023 17:16:46 +0000 (19:16 +0200)
We are expecting a checked exception and are passing a null argument for
something which we are supposed to interact with. Use a proper mock for
argument. This will allow us to further extend verification should the
implementation change.

JIRA: YANGTOOLS-1543
Change-Id: I0f1defd442ae60ee9e9b1b7a43949eed320ac148
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
codec/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/YT1542Test.java

index 8c701ed74a295377b59864cdd80223253778ee26..6150d0e96f58aa3607061bd25993e141db052179 100644 (file)
@@ -12,18 +12,28 @@ import static org.junit.jupiter.api.Assertions.assertInstanceOf;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
+@ExtendWith(MockitoExtension.class)
 class YT1542Test {
+    @Mock
+    private XMLStreamWriter writer;
+
     @Test
     void writeInstanceIdentifierReportsIOException() {
         final var codec = XmlCodecFactory.create(YangParserTestUtils.parseYang()).instanceIdentifierCodec();
-        final var ex = assertThrows(XMLStreamException.class, () -> codec.writeValue(null,
+        final var ex = assertThrows(XMLStreamException.class, () -> codec.writeValue(writer,
             YangInstanceIdentifier.of(QName.create("foo", "bar"))));
         assertEquals("Failed to encode instance-identifier", ex.getMessage());
-        assertInstanceOf(IllegalArgumentException.class, ex.getCause());
+        final var cause = assertInstanceOf(IllegalArgumentException.class, ex.getCause());
+        assertEquals("Invalid input /(foo)bar: schema for argument (foo)bar (after \"\") not found",
+            cause.getMessage());
     }
 }