2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore.persisted;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.fail;
15 import java.io.IOException;
16 import java.util.Collection;
17 import org.apache.commons.lang3.SerializationUtils;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.controller.cluster.datastore.AbstractTest;
21 import org.opendaylight.controller.cluster.datastore.persisted.DataTreeCandidateInputOutput.DataTreeCandidateWithVersion;
22 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
23 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
30 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
31 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
32 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
33 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode;
34 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
35 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
36 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
37 import org.opendaylight.yangtools.yang.data.tree.spi.DataTreeCandidates;
39 public class CommitTransactionPayloadTest extends AbstractTest {
40 static final QName LEAF_SET = QName.create(TestModel.TEST_QNAME, "leaf-set");
42 private DataTreeCandidate candidate;
44 private static DataTreeCandidateNode findNode(final Collection<DataTreeCandidateNode> nodes,
45 final PathArgument arg) {
46 for (DataTreeCandidateNode node : nodes) {
47 if (arg.equals(node.getIdentifier())) {
54 private static void assertChildrenEquals(final Collection<DataTreeCandidateNode> expected,
55 final Collection<DataTreeCandidateNode> actual) {
56 // Make sure all expected nodes are there
57 for (DataTreeCandidateNode exp : expected) {
58 final DataTreeCandidateNode act = findNode(actual, exp.getIdentifier());
59 assertNotNull("missing expected child", act);
60 assertCandidateNodeEquals(exp, act);
62 // Make sure no nodes are present which are not in the expected set
63 for (DataTreeCandidateNode act : actual) {
64 final DataTreeCandidateNode exp = findNode(expected, act.getIdentifier());
65 assertNull("unexpected child", exp);
69 private static void assertCandidateEquals(final DataTreeCandidate expected,
70 final DataTreeCandidateWithVersion actual) {
71 final DataTreeCandidate candidate = actual.getCandidate();
72 assertEquals("root path", expected.getRootPath(), candidate.getRootPath());
73 assertCandidateNodeEquals(expected.getRootNode(), candidate.getRootNode());
76 private static void assertCandidateNodeEquals(final DataTreeCandidateNode expected,
77 final DataTreeCandidateNode actual) {
78 assertEquals("child type", expected.getModificationType(), actual.getModificationType());
80 switch (actual.getModificationType()) {
83 assertEquals("child identifier", expected.getIdentifier(), actual.getIdentifier());
84 assertEquals("child data", expected.getDataAfter(), actual.getDataAfter());
86 case SUBTREE_MODIFIED:
87 assertEquals("child identifier", expected.getIdentifier(), actual.getIdentifier());
88 assertChildrenEquals(expected.getChildNodes(), actual.getChildNodes());
93 fail("Unexpect root type " + actual.getModificationType());
101 candidate = DataTreeCandidates.fromNormalizedNode(TestModel.TEST_PATH, Builders.containerBuilder()
102 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
103 .withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo"))
108 public void testCandidateSerialization() throws IOException {
109 final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
110 assertEquals("payload size", 156, payload.size());
114 public void testCandidateSerDes() throws IOException {
115 final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
116 assertCandidateEquals(candidate, payload.getCandidate().getValue());
120 public void testPayloadSerDes() throws IOException {
121 final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
122 assertCandidateEquals(candidate, SerializationUtils.clone(payload).getCandidate().getValue());
126 public void testLeafSetEntryNodeCandidate() throws Exception {
127 NodeWithValue<String> entryPathArg = new NodeWithValue<>(LEAF_SET, "one");
128 YangInstanceIdentifier leafSetEntryPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET)
129 .node(entryPathArg).build();
131 candidate = DataTreeCandidates.fromNormalizedNode(leafSetEntryPath, Builders.leafSetEntryBuilder()
132 .withNodeIdentifier(entryPathArg)
135 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
136 assertCandidateEquals(candidate, payload.getCandidate().getValue());
140 public void testLeafSetNodeCandidate() throws Exception {
141 YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
143 candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, Builders.leafSetBuilder()
144 .withNodeIdentifier(new NodeIdentifier(LEAF_SET))
145 .withChild(Builders.leafSetEntryBuilder()
146 .withNodeIdentifier(new NodeWithValue<>(LEAF_SET, "one"))
150 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
151 assertCandidateEquals(candidate, payload.getCandidate().getValue());
155 public void testOrderedLeafSetNodeCandidate() throws Exception {
156 YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
158 candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, Builders.orderedLeafSetBuilder()
159 .withNodeIdentifier(new NodeIdentifier(LEAF_SET))
160 .withChild(Builders.leafSetEntryBuilder()
161 .withNodeIdentifier(new NodeWithValue<>(LEAF_SET, "one"))
165 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
166 assertCandidateEquals(candidate, payload.getCandidate().getValue());
170 public void testLeafNodeCandidate() throws Exception {
171 YangInstanceIdentifier leafPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
172 .node(TestModel.DESC_QNAME).build();
174 candidate = DataTreeCandidates.fromNormalizedNode(leafPath,
175 ImmutableNodes.leafNode(TestModel.DESC_QNAME, "test"));
176 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
177 assertCandidateEquals(candidate, payload.getCandidate().getValue());
181 public void testUnmodifiedRootCandidate() throws Exception {
182 final DataTree dataTree = new InMemoryDataTreeFactory().create(
183 DataTreeConfiguration.DEFAULT_CONFIGURATION, SchemaContextHelper.select(SchemaContextHelper.CARS_YANG));
185 DataTreeModification modification = dataTree.takeSnapshot().newModification();
186 modification.ready();
187 candidate = dataTree.prepare(modification);
189 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
190 assertCandidateEquals(candidate, payload.getCandidate().getValue());