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() {
107 final YangInstanceIdentifier writePath = TestModel.TEST_PATH;
108 final NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
109 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
110 withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
111 candidate = DataTreeCandidates.fromNormalizedNode(writePath, writeData);
115 public void testCandidateSerialization() throws IOException {
116 final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
117 assertEquals("payload size", 181, payload.size());
121 public void testCandidateSerDes() throws IOException {
122 final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
123 assertCandidateEquals(candidate, payload.getCandidate().getValue());
127 public void testPayloadSerDes() throws IOException {
128 final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
129 assertCandidateEquals(candidate, SerializationUtils.clone(payload).getCandidate().getValue());
132 @SuppressWarnings({ "rawtypes", "unchecked" })
134 public void testLeafSetEntryNodeCandidate() throws Exception {
135 YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
136 YangInstanceIdentifier leafSetEntryPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET)
137 .node(entryPathArg).build();
139 NormalizedNode<?, ?> leafSetEntryNode = Builders.leafSetEntryBuilder().
140 withNodeIdentifier(entryPathArg).withValue("one").build();
142 DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafSetEntryPath, leafSetEntryNode);
143 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
144 assertCandidateEquals(candidate, payload.getCandidate().getValue());
147 @SuppressWarnings({ "rawtypes", "unchecked" })
149 public void testLeafSetNodeCandidate() throws Exception {
150 YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
151 YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
153 LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().
154 withNodeIdentifier(entryPathArg).withValue("one").build();
155 NormalizedNode<?, ?> leafSetNode = Builders.leafSetBuilder().withNodeIdentifier(
156 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
158 DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
159 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
160 assertCandidateEquals(candidate, payload.getCandidate().getValue());
163 @SuppressWarnings({ "rawtypes", "unchecked" })
165 public void testOrderedLeafSetNodeCandidate() throws Exception {
166 YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
167 YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
169 LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().
170 withNodeIdentifier(entryPathArg).withValue("one").build();
171 NormalizedNode<?, ?> leafSetNode = Builders.orderedLeafSetBuilder().withNodeIdentifier(
172 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
174 DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
175 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
176 assertCandidateEquals(candidate, payload.getCandidate().getValue());
180 public void testLeafNodeCandidate() throws Exception {
181 YangInstanceIdentifier leafPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(TestModel.DESC_QNAME).build();
182 LeafNode<Object> leafNode = Builders.leafBuilder().withNodeIdentifier(
183 new YangInstanceIdentifier.NodeIdentifier(TestModel.DESC_QNAME)).withValue("test").build();
185 DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafPath, leafNode);
186 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
187 assertCandidateEquals(candidate, payload.getCandidate().getValue());