BUG-3674: delete of non-existing data is a no-op 71/22471/3
authorRobert Varga <rovarga@cisco.com>
Fri, 12 Jun 2015 13:59:56 +0000 (15:59 +0200)
committerRobert Varga <rovarga@cisco.com>
Fri, 12 Jun 2015 17:05:46 +0000 (19:05 +0200)
As noted in the comments, a delete on non-existing data is a no-op. That
means the user should see an UNMODIFIED modification, not a DELETE.

Change-Id: Ic9f79b99eefa352549b363c4754566da66ed0d55
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/SchemaAwareApplyOperation.java
yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/Bug3674Test.java [new file with mode: 0644]

index b771bccadf616380fe98279648c54161d9a93bcb..9ab33e3707930068462158d140c13b071a7049a5 100644 (file)
@@ -175,7 +175,7 @@ abstract class SchemaAwareApplyOperation extends ModificationApplyOperation {
 
     private static void checkDeleteApplicable(final NodeModification modification, final Optional<TreeNode> current) {
         // Delete is always applicable, we do not expose it to subclasses
-        if (current.isPresent()) {
+        if (!current.isPresent()) {
             LOG.trace("Delete operation turned to no-op on missing node {}", modification);
         }
     }
@@ -189,7 +189,8 @@ abstract class SchemaAwareApplyOperation extends ModificationApplyOperation {
     final Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> currentMeta, final Version version) {
         switch (modification.getOperation()) {
         case DELETE:
-            modification.resolveModificationType(ModificationType.DELETE);
+            // Deletion of a non-existing node is a no-op, report it as such
+            modification.resolveModificationType(currentMeta.isPresent() ? ModificationType.DELETE : ModificationType.UNMODIFIED);
             return modification.setSnapshot(Optional.<TreeNode> absent());
         case TOUCH:
             Preconditions.checkArgument(currentMeta.isPresent(), "Metadata not available for modification %s",
diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/Bug3674Test.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/Bug3674Test.java
new file mode 100644 (file)
index 0000000..31355ff
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. 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.impl.schema.tree;
+
+import static org.junit.Assert.assertEquals;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+
+/**
+ * BUG-3674: issuing a delete on a non-existent entry must be preserved in
+ *           DataTreeModification, but should appear as UNMODIFIED in the
+ *           resulting DataTreeCandidate.
+ */
+public class Bug3674Test {
+    private DataTree tree;
+
+    @Before
+    public void setUp() {
+        tree = InMemoryDataTreeFactory.getInstance().create();
+        tree.setSchemaContext(TestModel.createTestContext());
+
+        // Create the top-level container
+        final DataTreeModification mod = tree.takeSnapshot().newModification();
+        mod.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
+        mod.ready();
+        tree.commit(tree.prepare(mod));
+    }
+
+    @Test
+    public void testDeleteOfNonExistingNode() {
+        final DataTreeModification mod = tree.takeSnapshot().newModification();
+        mod.delete(TestModel.OUTER_LIST_PATH);
+        mod.ready();
+
+        final DataTreeCandidate candidate = tree.prepare(mod);
+        final DataTreeCandidateNode root = candidate.getRootNode();
+        assertEquals(ModificationType.UNMODIFIED, root.getModificationType());
+    }
+}