Switch current {ABI,DataStore,Payload}Version to Sodium SR1
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / persisted / CommitTransactionPayloadTest.java
index 0b440c668f435837f2a05f858b44155e45286a92..e0518a791bc0051f379cafc0b908c62c95518e05 100644 (file)
@@ -11,12 +11,14 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.fail;
+
 import java.io.IOException;
 import java.util.Collection;
 import org.apache.commons.lang3.SerializationUtils;
 import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.controller.cluster.datastore.AbstractTest;
+import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -24,19 +26,24 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgum
 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+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.DataTreeCandidates;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
 
 public class CommitTransactionPayloadTest extends AbstractTest {
     static final QName LEAF_SET = QName.create(TestModel.TEST_QNAME, "leaf-set");
 
     private DataTreeCandidate candidate;
 
-    private static DataTreeCandidateNode findNode(final Collection<DataTreeCandidateNode> nodes, final PathArgument arg) {
+    private static DataTreeCandidateNode findNode(final Collection<DataTreeCandidateNode> nodes,
+            final PathArgument arg) {
         for (DataTreeCandidateNode node : nodes) {
             if (arg.equals(node.getIdentifier())) {
                 return node;
@@ -62,42 +69,28 @@ public class CommitTransactionPayloadTest extends AbstractTest {
 
     private static void assertCandidateEquals(final DataTreeCandidate expected, final DataTreeCandidate actual) {
         assertEquals("root path", expected.getRootPath(), actual.getRootPath());
-
-        final DataTreeCandidateNode expRoot = expected.getRootNode();
-        final DataTreeCandidateNode actRoot = expected.getRootNode();
-        assertEquals("root type", expRoot.getModificationType(), actRoot.getModificationType());
-
-        switch (actRoot.getModificationType()) {
-        case DELETE:
-        case WRITE:
-            assertEquals("root data", expRoot.getDataAfter(), actRoot.getDataAfter());
-            break;
-        case SUBTREE_MODIFIED:
-            assertChildrenEquals(expRoot.getChildNodes(), actRoot.getChildNodes());
-            break;
-        default:
-            fail("Unexpect root type " + actRoot.getModificationType());
-            break;
-        }
-
         assertCandidateNodeEquals(expected.getRootNode(), actual.getRootNode());
     }
 
-    private static void assertCandidateNodeEquals(final DataTreeCandidateNode expected, final DataTreeCandidateNode actual) {
+    private static void assertCandidateNodeEquals(final DataTreeCandidateNode expected,
+            final DataTreeCandidateNode actual) {
         assertEquals("child type", expected.getModificationType(), actual.getModificationType());
-        assertEquals("child identifier", expected.getIdentifier(), actual.getIdentifier());
 
         switch (actual.getModificationType()) {
-        case DELETE:
-        case WRITE:
-            assertEquals("child data", expected.getDataAfter(), actual.getDataAfter());
-            break;
-        case SUBTREE_MODIFIED:
-            assertChildrenEquals(expected.getChildNodes(), actual.getChildNodes());
-            break;
-        default:
-            fail("Unexpect root type " + actual.getModificationType());
-            break;
+            case DELETE:
+            case WRITE:
+                assertEquals("child identifier", expected.getIdentifier(), actual.getIdentifier());
+                assertEquals("child data", expected.getDataAfter(), actual.getDataAfter());
+                break;
+            case SUBTREE_MODIFIED:
+                assertEquals("child identifier", expected.getIdentifier(), actual.getIdentifier());
+                assertChildrenEquals(expected.getChildNodes(), actual.getChildNodes());
+                break;
+            case UNMODIFIED:
+                break;
+            default:
+                fail("Unexpect root type " + actual.getModificationType());
+                break;
         }
     }
 
@@ -105,16 +98,16 @@ public class CommitTransactionPayloadTest extends AbstractTest {
     public void setUp() {
         setUpStatic();
         final YangInstanceIdentifier writePath = TestModel.TEST_PATH;
-        final NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
-                new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
-                withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
+        final NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
+                .withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
         candidate = DataTreeCandidates.fromNormalizedNode(writePath, writeData);
     }
 
     @Test
     public void testCandidateSerialization() throws IOException {
         final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
-        assertEquals("payload size", 181, payload.size());
+        assertEquals("payload size", 156, payload.size());
     }
 
     @Test
@@ -136,10 +129,10 @@ public class CommitTransactionPayloadTest extends AbstractTest {
         YangInstanceIdentifier leafSetEntryPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET)
                 .node(entryPathArg).build();
 
-        NormalizedNode<?, ?> leafSetEntryNode = Builders.leafSetEntryBuilder().
-                withNodeIdentifier(entryPathArg).withValue("one").build();
+        NormalizedNode<?, ?> leafSetEntryNode = Builders.leafSetEntryBuilder().withNodeIdentifier(entryPathArg)
+                .withValue("one").build();
 
-        DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafSetEntryPath, leafSetEntryNode);
+        candidate = DataTreeCandidates.fromNormalizedNode(leafSetEntryPath, leafSetEntryNode);
         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
         assertCandidateEquals(candidate, payload.getCandidate().getValue());
     }
@@ -150,12 +143,12 @@ public class CommitTransactionPayloadTest extends AbstractTest {
         YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
         YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
 
-        LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().
-                withNodeIdentifier(entryPathArg).withValue("one").build();
+        LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().withNodeIdentifier(entryPathArg)
+                .withValue("one").build();
         NormalizedNode<?, ?> leafSetNode = Builders.leafSetBuilder().withNodeIdentifier(
                 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
 
-        DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
+        candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
         assertCandidateEquals(candidate, payload.getCandidate().getValue());
     }
@@ -166,23 +159,37 @@ public class CommitTransactionPayloadTest extends AbstractTest {
         YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
         YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
 
-        LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().
-                withNodeIdentifier(entryPathArg).withValue("one").build();
+        LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().withNodeIdentifier(entryPathArg)
+                .withValue("one").build();
         NormalizedNode<?, ?> leafSetNode = Builders.orderedLeafSetBuilder().withNodeIdentifier(
                 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
 
-        DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
+        candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
         assertCandidateEquals(candidate, payload.getCandidate().getValue());
     }
 
     @Test
     public void testLeafNodeCandidate() throws Exception {
-        YangInstanceIdentifier leafPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(TestModel.DESC_QNAME).build();
+        YangInstanceIdentifier leafPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
+                .node(TestModel.DESC_QNAME).build();
         LeafNode<Object> leafNode = Builders.leafBuilder().withNodeIdentifier(
                 new YangInstanceIdentifier.NodeIdentifier(TestModel.DESC_QNAME)).withValue("test").build();
 
-        DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafPath, leafNode);
+        candidate = DataTreeCandidates.fromNormalizedNode(leafPath, leafNode);
+        CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
+        assertCandidateEquals(candidate, payload.getCandidate().getValue());
+    }
+
+    @Test
+    public void testUnmodifiedRootCandidate() throws Exception {
+        final DataTree dataTree = new InMemoryDataTreeFactory().create(
+            DataTreeConfiguration.DEFAULT_CONFIGURATION, SchemaContextHelper.select(SchemaContextHelper.CARS_YANG));
+
+        DataTreeModification modification = dataTree.takeSnapshot().newModification();
+        modification.ready();
+        candidate = dataTree.prepare(modification);
+
         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
         assertCandidateEquals(candidate, payload.getCandidate().getValue());
     }