From 1ca7a65e5458b997162fb7255d7cbc782ae28371 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 23 Sep 2023 01:33:03 +0200 Subject: [PATCH] Report checked exception in instance identifier writeout Both XML and JSON codecs are talking to a source of IllegalArgumentExceptions. They should convert them to XMLStreamException/IOException. JIRA: YANGTOOLS-1542 Change-Id: Ia5ac633e37ed19c29d4593e2e886501c7baa59dd Signed-off-by: Robert Varga --- .../gson/JSONInstanceIdentifierCodec.java | 8 ++++- .../yang/data/codec/gson/YT1473Test.java | 2 +- .../yang/data/codec/gson/YT1542Test.java | 30 +++++++++++++++++++ .../xml/XmlStringInstanceIdentifierCodec.java | 8 ++++- .../yang/data/codec/xml/YT1542Test.java | 29 ++++++++++++++++++ 5 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/YT1542Test.java create mode 100644 codec/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/YT1542Test.java diff --git a/codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONInstanceIdentifierCodec.java b/codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONInstanceIdentifierCodec.java index 9fbd13da9a..a181d5256b 100644 --- a/codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONInstanceIdentifierCodec.java +++ b/codec/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONInstanceIdentifierCodec.java @@ -104,6 +104,12 @@ abstract sealed class JSONInstanceIdentifierCodec extends AbstractModuleStringIn @Override public final void writeValue(final JsonWriter ctx, final YangInstanceIdentifier value) throws IOException { - ctx.value(serialize(value)); + final String str; + try { + str = serialize(value); + } catch (IllegalArgumentException e) { + throw new IOException("Failed to encode instance-identifier", e); + } + ctx.value(str); } } diff --git a/codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/YT1473Test.java b/codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/YT1473Test.java index e2f6cacf28..5e968223d3 100644 --- a/codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/YT1473Test.java +++ b/codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/YT1473Test.java @@ -7,7 +7,7 @@ */ package org.opendaylight.yangtools.yang.data.codec.gson; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doReturn; diff --git a/codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/YT1542Test.java b/codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/YT1542Test.java new file mode 100644 index 0000000000..9a5521e02b --- /dev/null +++ b/codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/YT1542Test.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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.yangtools.yang.data.codec.gson; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.IOException; +import org.junit.jupiter.api.Test; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; + +class YT1542Test { + @Test + void writeInstanceIdentifierReportsIOException() { + final var codec = JSONCodecFactorySupplier.RFC7951.createSimple(YangParserTestUtils.parseYang()) + .instanceIdentifierCodec(); + final var ex = assertThrows(IOException.class, () -> codec.writeValue(null, + YangInstanceIdentifier.of(QName.create("foo", "bar")))); + assertEquals("Failed to encode instance-identifier", ex.getMessage()); + assertInstanceOf(IllegalArgumentException.class, ex.getCause()); + } +} diff --git a/codec/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlStringInstanceIdentifierCodec.java b/codec/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlStringInstanceIdentifierCodec.java index 63e7d80fde..b83ec701a5 100644 --- a/codec/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlStringInstanceIdentifierCodec.java +++ b/codec/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlStringInstanceIdentifierCodec.java @@ -93,7 +93,13 @@ final class XmlStringInstanceIdentifierCodec extends AbstractModuleStringInstanc @Override public void writeValue(final XMLStreamWriter ctx, final YangInstanceIdentifier value) throws XMLStreamException { - ctx.writeCharacters(serialize(value)); + final String str; + try { + str = serialize(value); + } catch (IllegalArgumentException e) { + throw new XMLStreamException("Failed to encode instance-identifier", e); + } + ctx.writeCharacters(str); } private static NamespaceContext getNamespaceContext() { diff --git a/codec/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/YT1542Test.java b/codec/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/YT1542Test.java new file mode 100644 index 0000000000..8c701ed74a --- /dev/null +++ b/codec/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/YT1542Test.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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.yangtools.yang.data.codec.xml; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import javax.xml.stream.XMLStreamException; +import org.junit.jupiter.api.Test; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; + +class YT1542Test { + @Test + void writeInstanceIdentifierReportsIOException() { + final var codec = XmlCodecFactory.create(YangParserTestUtils.parseYang()).instanceIdentifierCodec(); + final var ex = assertThrows(XMLStreamException.class, () -> codec.writeValue(null, + YangInstanceIdentifier.of(QName.create("foo", "bar")))); + assertEquals("Failed to encode instance-identifier", ex.getMessage()); + assertInstanceOf(IllegalArgumentException.class, ex.getCause()); + } +} -- 2.36.6