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;
14 import java.io.IOException;
15 import java.util.Collection;
16 import org.apache.commons.lang3.SerializationUtils;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.controller.cluster.datastore.AbstractTest;
20 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
24 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
30 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
31 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
34 public class CommitTransactionPayloadTest extends AbstractTest {
35 static final QName LEAF_SET = QName.create(TestModel.TEST_QNAME, "leaf-set");
37 private DataTreeCandidate candidate;
39 private static DataTreeCandidateNode findNode(final Collection<DataTreeCandidateNode> nodes, final PathArgument arg) {
40 for (DataTreeCandidateNode node : nodes) {
41 if (arg.equals(node.getIdentifier())) {
48 private static void assertChildrenEquals(final Collection<DataTreeCandidateNode> expected,
49 final Collection<DataTreeCandidateNode> actual) {
50 // Make sure all expected nodes are there
51 for (DataTreeCandidateNode exp : expected) {
52 final DataTreeCandidateNode act = findNode(actual, exp.getIdentifier());
53 assertNotNull("missing expected child", act);
54 assertCandidateNodeEquals(exp, act);
56 // Make sure no nodes are present which are not in the expected set
57 for (DataTreeCandidateNode act : actual) {
58 final DataTreeCandidateNode exp = findNode(expected, act.getIdentifier());
59 assertNull("unexpected child", exp);
63 private static void assertCandidateEquals(final DataTreeCandidate expected, final DataTreeCandidate actual) {
64 assertEquals("root path", expected.getRootPath(), actual.getRootPath());
66 final DataTreeCandidateNode expRoot = expected.getRootNode();
67 final DataTreeCandidateNode actRoot = expected.getRootNode();
68 assertEquals("root type", expRoot.getModificationType(), actRoot.getModificationType());
70 switch (actRoot.getModificationType()) {
73 assertEquals("root data", expRoot.getDataAfter(), actRoot.getDataAfter());
75 case SUBTREE_MODIFIED:
76 assertChildrenEquals(expRoot.getChildNodes(), actRoot.getChildNodes());
79 fail("Unexpect root type " + actRoot.getModificationType());
83 assertCandidateNodeEquals(expected.getRootNode(), actual.getRootNode());
86 private static void assertCandidateNodeEquals(final DataTreeCandidateNode expected, final DataTreeCandidateNode actual) {
87 assertEquals("child type", expected.getModificationType(), actual.getModificationType());
88 assertEquals("child identifier", expected.getIdentifier(), actual.getIdentifier());
90 switch (actual.getModificationType()) {
93 assertEquals("child data", expected.getDataAfter(), actual.getDataAfter());
95 case SUBTREE_MODIFIED:
96 assertChildrenEquals(expected.getChildNodes(), actual.getChildNodes());
99 fail("Unexpect root type " + actual.getModificationType());
105 public void setUp() {
106 final YangInstanceIdentifier writePath = TestModel.TEST_PATH;
107 final NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
108 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
109 withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
110 candidate = DataTreeCandidates.fromNormalizedNode(writePath, writeData);
114 public void testCandidateSerialization() throws IOException {
115 final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
116 assertEquals("payload size", 181, payload.size());
120 public void testCandidateSerDes() throws IOException {
121 final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
122 assertCandidateEquals(candidate, payload.getCandidate().getValue());
126 public void testPayloadSerDes() throws IOException {
127 final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
128 assertCandidateEquals(candidate, SerializationUtils.clone(payload).getCandidate().getValue());
131 @SuppressWarnings({ "rawtypes", "unchecked" })
133 public void testLeafSetEntryNodeCandidate() throws Exception {
134 YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
135 YangInstanceIdentifier leafSetEntryPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET)
136 .node(entryPathArg).build();
138 NormalizedNode<?, ?> leafSetEntryNode = Builders.leafSetEntryBuilder().
139 withNodeIdentifier(entryPathArg).withValue("one").build();
141 DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafSetEntryPath, leafSetEntryNode);
142 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
143 assertCandidateEquals(candidate, payload.getCandidate().getValue());
146 @SuppressWarnings({ "rawtypes", "unchecked" })
148 public void testLeafSetNodeCandidate() throws Exception {
149 YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
150 YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
152 LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().
153 withNodeIdentifier(entryPathArg).withValue("one").build();
154 NormalizedNode<?, ?> leafSetNode = Builders.leafSetBuilder().withNodeIdentifier(
155 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
157 DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
158 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
159 assertCandidateEquals(candidate, payload.getCandidate().getValue());
162 @SuppressWarnings({ "rawtypes", "unchecked" })
164 public void testOrderedLeafSetNodeCandidate() throws Exception {
165 YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
166 YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
168 LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().
169 withNodeIdentifier(entryPathArg).withValue("one").build();
170 NormalizedNode<?, ?> leafSetNode = Builders.orderedLeafSetBuilder().withNodeIdentifier(
171 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
173 DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
174 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
175 assertCandidateEquals(candidate, payload.getCandidate().getValue());
179 public void testLeafNodeCandidate() throws Exception {
180 YangInstanceIdentifier leafPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(TestModel.DESC_QNAME).build();
181 LeafNode<Object> leafNode = Builders.leafBuilder().withNodeIdentifier(
182 new YangInstanceIdentifier.NodeIdentifier(TestModel.DESC_QNAME)).withValue("test").build();
184 DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafPath, leafNode);
185 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
186 assertCandidateEquals(candidate, payload.getCandidate().getValue());