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.md.cluster.datastore.model.TestModel;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
31 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
32 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
35 public class CommitTransactionPayloadTest extends AbstractTest {
36 static final QName LEAF_SET = QName.create(TestModel.TEST_QNAME, "leaf-set");
38 private DataTreeCandidate candidate;
40 private static DataTreeCandidateNode findNode(final Collection<DataTreeCandidateNode> nodes,
41 final PathArgument arg) {
42 for (DataTreeCandidateNode node : nodes) {
43 if (arg.equals(node.getIdentifier())) {
50 private static void assertChildrenEquals(final Collection<DataTreeCandidateNode> expected,
51 final Collection<DataTreeCandidateNode> actual) {
52 // Make sure all expected nodes are there
53 for (DataTreeCandidateNode exp : expected) {
54 final DataTreeCandidateNode act = findNode(actual, exp.getIdentifier());
55 assertNotNull("missing expected child", act);
56 assertCandidateNodeEquals(exp, act);
58 // Make sure no nodes are present which are not in the expected set
59 for (DataTreeCandidateNode act : actual) {
60 final DataTreeCandidateNode exp = findNode(expected, act.getIdentifier());
61 assertNull("unexpected child", exp);
65 private static void assertCandidateEquals(final DataTreeCandidate expected, final DataTreeCandidate actual) {
66 assertEquals("root path", expected.getRootPath(), actual.getRootPath());
68 final DataTreeCandidateNode expRoot = expected.getRootNode();
69 final DataTreeCandidateNode actRoot = expected.getRootNode();
70 assertEquals("root type", expRoot.getModificationType(), actRoot.getModificationType());
72 switch (actRoot.getModificationType()) {
75 assertEquals("root data", expRoot.getDataAfter(), actRoot.getDataAfter());
77 case SUBTREE_MODIFIED:
78 assertChildrenEquals(expRoot.getChildNodes(), actRoot.getChildNodes());
81 fail("Unexpect root type " + actRoot.getModificationType());
85 assertCandidateNodeEquals(expected.getRootNode(), actual.getRootNode());
88 private static void assertCandidateNodeEquals(final DataTreeCandidateNode expected,
89 final DataTreeCandidateNode actual) {
90 assertEquals("child type", expected.getModificationType(), actual.getModificationType());
91 assertEquals("child identifier", expected.getIdentifier(), actual.getIdentifier());
93 switch (actual.getModificationType()) {
96 assertEquals("child data", expected.getDataAfter(), actual.getDataAfter());
98 case SUBTREE_MODIFIED:
99 assertChildrenEquals(expected.getChildNodes(), actual.getChildNodes());
102 fail("Unexpect root type " + actual.getModificationType());
108 public void setUp() {
110 final YangInstanceIdentifier writePath = TestModel.TEST_PATH;
111 final NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create()
112 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
113 .withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
114 candidate = DataTreeCandidates.fromNormalizedNode(writePath, writeData);
118 public void testCandidateSerialization() throws IOException {
119 final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
120 assertEquals("payload size", 181, payload.size());
124 public void testCandidateSerDes() throws IOException {
125 final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
126 assertCandidateEquals(candidate, payload.getCandidate().getValue());
130 public void testPayloadSerDes() throws IOException {
131 final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
132 assertCandidateEquals(candidate, SerializationUtils.clone(payload).getCandidate().getValue());
135 @SuppressWarnings({ "rawtypes", "unchecked" })
137 public void testLeafSetEntryNodeCandidate() throws Exception {
138 YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
139 YangInstanceIdentifier leafSetEntryPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET)
140 .node(entryPathArg).build();
142 NormalizedNode<?, ?> leafSetEntryNode = Builders.leafSetEntryBuilder().withNodeIdentifier(entryPathArg)
143 .withValue("one").build();
145 candidate = DataTreeCandidates.fromNormalizedNode(leafSetEntryPath, leafSetEntryNode);
146 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
147 assertCandidateEquals(candidate, payload.getCandidate().getValue());
150 @SuppressWarnings({ "rawtypes", "unchecked" })
152 public void testLeafSetNodeCandidate() throws Exception {
153 YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
154 YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
156 LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().withNodeIdentifier(entryPathArg)
157 .withValue("one").build();
158 NormalizedNode<?, ?> leafSetNode = Builders.leafSetBuilder().withNodeIdentifier(
159 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
161 candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
162 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
163 assertCandidateEquals(candidate, payload.getCandidate().getValue());
166 @SuppressWarnings({ "rawtypes", "unchecked" })
168 public void testOrderedLeafSetNodeCandidate() throws Exception {
169 YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
170 YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
172 LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().withNodeIdentifier(entryPathArg)
173 .withValue("one").build();
174 NormalizedNode<?, ?> leafSetNode = Builders.orderedLeafSetBuilder().withNodeIdentifier(
175 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
177 candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
178 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
179 assertCandidateEquals(candidate, payload.getCandidate().getValue());
183 public void testLeafNodeCandidate() throws Exception {
184 YangInstanceIdentifier leafPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
185 .node(TestModel.DESC_QNAME).build();
186 LeafNode<Object> leafNode = Builders.leafBuilder().withNodeIdentifier(
187 new YangInstanceIdentifier.NodeIdentifier(TestModel.DESC_QNAME)).withValue("test").build();
189 candidate = DataTreeCandidates.fromNormalizedNode(leafPath, leafNode);
190 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
191 assertCandidateEquals(candidate, payload.getCandidate().getValue());