Fix XML parser condition in XmlPatchBodyReader 70/103970/9
authorPeter Suna <peter.suna@pantheon.tech>
Tue, 10 Jan 2023 09:55:13 +0000 (10:55 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 24 Jan 2023 12:10:35 +0000 (13:10 +0100)
Remove redundant if condition which rejects to parse
LeafListSchemaNode or LeafSchemaNode value from XML.

JIRA: NETCONF-937
Change-Id: Ib001582b8476f4508bab7d746296cb7ea1b1619e
Signed-off-by: Peter Suna <peter.suna@pantheon.tech>
Signed-off-by: Ivan Hrasko <ivan.hrasko@pantheon.tech>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReader.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchBodyReaderTest.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReaderTest.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/test/AbstractBodyReaderTest.java

index 0059023a61d33241c3723db25ba2e5296cce93ef..2a566a8a49b59170255bc6f7828ee1b624cb30d4 100644 (file)
@@ -19,7 +19,6 @@ import java.util.Locale;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.ext.Provider;
-import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.transform.dom.DOMSource;
 import org.eclipse.jdt.annotation.NonNull;
@@ -37,15 +36,11 @@ import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
-import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
@@ -85,13 +80,12 @@ public class XmlPatchBodyReader extends AbstractPatchBodyReader {
     }
 
     private static PatchContext parse(final InstanceIdentifierContext pathContext, final Document doc)
-            throws XMLStreamException, IOException, ParserConfigurationException, SAXException, URISyntaxException {
+            throws XMLStreamException, IOException, SAXException, URISyntaxException {
         final List<PatchEntity> resultCollection = new ArrayList<>();
         final String patchId = doc.getElementsByTagName("patch-id").item(0).getFirstChild().getNodeValue();
         final NodeList editNodes = doc.getElementsByTagName("edit");
 
         for (int i = 0; i < editNodes.getLength(); i++) {
-            DataSchemaNode schemaNode = (DataSchemaNode) pathContext.getSchemaNode();
             final Element element = (Element) editNodes.item(i);
             final String operation = element.getElementsByTagName("operation").item(0).getFirstChild().getNodeValue();
             final PatchEditOperation oper = PatchEditOperation.valueOf(operation.toUpperCase(Locale.ROOT));
@@ -117,7 +111,6 @@ public class XmlPatchBodyReader extends AbstractPatchBodyReader {
                 final var lookup = DataSchemaContextTree.from(pathContext.getSchemaContext())
                     .enterPath(targetII).orElseThrow();
 
-                schemaNode = lookup.node().getDataSchemaNode();
                 final var stack = lookup.stack();
                 inference = stack.toInference();
                 if (!stack.isEmpty()) {
@@ -140,23 +133,15 @@ public class XmlPatchBodyReader extends AbstractPatchBodyReader {
             }
 
             if (oper.isWithValue()) {
-                final NormalizedNode parsed;
-                if (schemaNode instanceof  ContainerSchemaNode || schemaNode instanceof ListSchemaNode) {
-                    final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
-                    final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
-                    final XmlParserStream xmlParser = XmlParserStream.create(writer, inference);
-                    xmlParser.traverse(new DOMSource(firstValueElement));
-                    parsed = resultHolder.getResult();
-                } else {
-                    parsed = null;
-                }
-
+                final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
+                final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
+                final XmlParserStream xmlParser = XmlParserStream.create(writer, inference);
+                xmlParser.traverse(new DOMSource(firstValueElement));
                 // for lists allow to manipulate with list items through their parent
                 if (targetII.getLastPathArgument() instanceof NodeIdentifierWithPredicates) {
                     targetII = targetII.getParent();
                 }
-
-                resultCollection.add(new PatchEntity(editId, oper, targetII, parsed));
+                resultCollection.add(new PatchEntity(editId, oper, targetII, resultHolder.getResult()));
             } else {
                 resultCollection.add(new PatchEntity(editId, oper, targetII));
             }
index 664ae5e79a1c3de20d5aa7bbdcbbde898b864796..87485e60a481a29abdd1f60e4aaf099bf2ecef59 100644 (file)
@@ -168,6 +168,9 @@ public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
 
         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
         checkPatchContext(returnValue);
+        final var data = returnValue.getData().get(0).getNode();
+        assertEquals(LEAF_NAME_QNAME, data.getIdentifier().getNodeType());
+        assertEquals(ImmutableNodes.leafNode(LEAF_NAME_QNAME, "my-leaf20"), data);
     }
 
     /**
@@ -254,7 +257,7 @@ public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
         final var expectedData = Builders.leafSetBuilder()
                 .withNodeIdentifier(new NodeIdentifier(LEAF_SET_QNAME))
                 .withChild(Builders.leafSetEntryBuilder()
-                        .withNodeIdentifier(new NodeWithValue(LEAF_SET_QNAME, "data1"))
+                        .withNodeIdentifier(new NodeWithValue<>(LEAF_SET_QNAME, "data1"))
                         .withValue("data1")
                         .build())
                 .build();
index c9b33b2062a3921439a211d88f8b2d0277bc2af0..b791ed626d6d4e5b48e686c04bca4e814e465ea7 100644 (file)
@@ -15,7 +15,6 @@ import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import javax.ws.rs.core.MediaType;
 import org.junit.BeforeClass;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
@@ -176,9 +175,7 @@ public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
 
     /**
      * Test of Yang Patch on the leaf set node element.
-     * TODO: Remove ignore when NETCONF-937 will be resolved
      */
-    @Ignore
     @Test
     public void modulePatchTargetLeafSetNodeTest() throws Exception {
         mockBodyReader("", xmlToPatchBodyReader, false);
@@ -199,7 +196,7 @@ public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
         final var expectedData = Builders.leafSetBuilder()
                 .withNodeIdentifier(new NodeIdentifier(LEAF_SET_QNAME))
                 .withChild(Builders.leafSetEntryBuilder()
-                        .withNodeIdentifier(new NodeWithValue(LEAF_SET_QNAME, "data1"))
+                        .withNodeIdentifier(new NodeWithValue<>(LEAF_SET_QNAME, "data1"))
                         .withValue("data1")
                         .build())
                 .build();
@@ -280,4 +277,31 @@ public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
         assertEquals(CHOICE_CONT_QNAME, data.getIdentifier().getNodeType());
         assertEquals(expectedData, data);
     }
+
+    /**
+     * Test reading simple leaf value.
+     */
+    @Test
+    public void modulePatchSimpleLeafValueTest() throws Exception {
+        mockBodyReader("instance-identifier-patch-module:patch-cont/my-list1=leaf1", xmlToPatchBodyReader, false);
+        final var inputStream = new ByteArrayInputStream("""
+                <yang-patch xmlns="urn:ietf:params:xml:ns:yang:ietf-yang-patch">
+                    <patch-id>test-patch</patch-id>
+                    <comment>this is test patch</comment>
+                    <edit>
+                        <edit-id>edit1</edit-id>
+                        <operation>replace</operation>
+                        <target>/my-list2=my-leaf20/name</target>
+                        <value>
+                            <name xmlns="instance:identifier:patch:module">my-leaf20</name>
+                        </value>
+                    </edit>
+                </yang-patch>
+                """.getBytes(StandardCharsets.UTF_8));
+        final var returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
+        checkPatchContext(returnValue);
+        final var data = returnValue.getData().get(0).getNode();
+        assertEquals(LEAF_NAME_QNAME, data.getIdentifier().getNodeType());
+        assertEquals(ImmutableNodes.leafNode(LEAF_NAME_QNAME, "my-leaf20"), data);
+    }
 }
index 876dbfa0029176906832e60458c28f76d4d17e30..ba0e597b95dfb34d02148013f123d7532eeb9bd7 100644 (file)
@@ -52,6 +52,8 @@ public abstract class AbstractBodyReaderTest {
     protected static final QName LIST_LEAF2_QNAME = QName.create("list:ns", "leaf2").intern();
     protected static final QName CHOICE_CONT_QNAME = QName.create("choice:ns", "case-cont1").intern();
     protected static final QName CASE_LEAF1_QNAME = QName.create("choice:ns", "case-leaf1").intern();
+    protected static final QName LEAF_NAME_QNAME = QName.create("instance:identifier:patch:module",
+            "2015-11-21", "name").intern();
 
     protected final MediaType mediaType;
     protected final DatabindProvider databindProvider;