2 * Copyright (c) 2015 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;
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.md.cluster.datastore.model.TestModel;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
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.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
33 public class DataTreeCandidatePayloadTest {
34 static final QName LEAF_SET = QName.create(TestModel.TEST_QNAME, "leaf-set");
36 private DataTreeCandidate candidate;
38 private static DataTreeCandidateNode findNode(final Collection<DataTreeCandidateNode> nodes, final PathArgument arg) {
39 for (DataTreeCandidateNode node : nodes) {
40 if (arg.equals(node.getIdentifier())) {
47 private static void assertChildrenEquals(final Collection<DataTreeCandidateNode> expected,
48 final Collection<DataTreeCandidateNode> actual) {
49 // Make sure all expected nodes are there
50 for (DataTreeCandidateNode exp : expected) {
51 final DataTreeCandidateNode act = findNode(actual, exp.getIdentifier());
52 assertNotNull("missing expected child", act);
53 assertCandidateNodeEquals(exp, act);
55 // Make sure no nodes are present which are not in the expected set
56 for (DataTreeCandidateNode act : actual) {
57 final DataTreeCandidateNode exp = findNode(expected, act.getIdentifier());
58 assertNull("unexpected child", exp);
62 private static void assertCandidateEquals(final DataTreeCandidate expected, final DataTreeCandidate actual) {
63 assertEquals("root path", expected.getRootPath(), actual.getRootPath());
65 final DataTreeCandidateNode expRoot = expected.getRootNode();
66 final DataTreeCandidateNode actRoot = expected.getRootNode();
67 assertEquals("root type", expRoot.getModificationType(), actRoot.getModificationType());
69 switch (actRoot.getModificationType()) {
72 assertEquals("root data", expRoot.getDataAfter(), actRoot.getDataAfter());
74 case SUBTREE_MODIFIED:
75 assertChildrenEquals(expRoot.getChildNodes(), actRoot.getChildNodes());
78 fail("Unexpect root type " + actRoot.getModificationType());
82 assertCandidateNodeEquals(expected.getRootNode(), actual.getRootNode());
85 private static void assertCandidateNodeEquals(final DataTreeCandidateNode expected, final DataTreeCandidateNode actual) {
86 assertEquals("child type", expected.getModificationType(), actual.getModificationType());
87 assertEquals("child identifier", expected.getIdentifier(), actual.getIdentifier());
89 switch (actual.getModificationType()) {
92 assertEquals("child data", expected.getDataAfter(), actual.getDataAfter());
94 case SUBTREE_MODIFIED:
95 assertChildrenEquals(expected.getChildNodes(), actual.getChildNodes());
98 fail("Unexpect root type " + actual.getModificationType());
104 public void setUp() {
105 final YangInstanceIdentifier writePath = TestModel.TEST_PATH;
106 final NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
107 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
108 withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
109 candidate = DataTreeCandidates.fromNormalizedNode(writePath, writeData);
113 public void testCandidateSerialization() throws IOException {
114 final DataTreeCandidatePayload payload = DataTreeCandidatePayload.create(candidate);
115 assertEquals("payload size", 141, payload.size());
119 public void testCandidateSerDes() throws IOException {
120 final DataTreeCandidatePayload payload = DataTreeCandidatePayload.create(candidate);
121 assertCandidateEquals(candidate, payload.getCandidate());
125 public void testPayloadSerDes() throws IOException {
126 final DataTreeCandidatePayload payload = DataTreeCandidatePayload.create(candidate);
127 assertCandidateEquals(candidate, SerializationUtils.clone(payload).getCandidate());
130 @SuppressWarnings({ "rawtypes", "unchecked" })
132 public void testLeafSetEntryNodeCandidate() throws Exception {
133 YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
134 YangInstanceIdentifier leafSetEntryPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET)
135 .node(entryPathArg).build();
137 NormalizedNode<?, ?> leafSetEntryNode = Builders.leafSetEntryBuilder().
138 withNodeIdentifier(entryPathArg).withValue("one").build();
140 DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafSetEntryPath, leafSetEntryNode);
141 DataTreeCandidatePayload payload = DataTreeCandidatePayload.create(candidate);
142 assertCandidateEquals(candidate, payload.getCandidate());
145 @SuppressWarnings({ "rawtypes", "unchecked" })
147 public void testLeafSetNodeCandidate() throws Exception {
148 YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
149 YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
151 LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().
152 withNodeIdentifier(entryPathArg).withValue("one").build();
153 NormalizedNode<?, ?> leafSetNode = Builders.leafSetBuilder().withNodeIdentifier(
154 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
156 DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
157 DataTreeCandidatePayload payload = DataTreeCandidatePayload.create(candidate);
158 assertCandidateEquals(candidate, payload.getCandidate());
161 @SuppressWarnings({ "rawtypes", "unchecked" })
163 public void testOrderedLeafSetNodeCandidate() throws Exception {
164 YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
165 YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
167 LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().
168 withNodeIdentifier(entryPathArg).withValue("one").build();
169 NormalizedNode<?, ?> leafSetNode = Builders.orderedLeafSetBuilder().withNodeIdentifier(
170 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
172 DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
173 DataTreeCandidatePayload payload = DataTreeCandidatePayload.create(candidate);
174 assertCandidateEquals(candidate, payload.getCandidate());
178 public void testLeafNodeCandidate() throws Exception {
179 YangInstanceIdentifier leafPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(TestModel.DESC_QNAME).build();
180 LeafNode<Object> leafNode = Builders.leafBuilder().withNodeIdentifier(
181 new YangInstanceIdentifier.NodeIdentifier(TestModel.DESC_QNAME)).withValue("test").build();
183 DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafPath, leafNode);
184 DataTreeCandidatePayload payload = DataTreeCandidatePayload.create(candidate);
185 assertCandidateEquals(candidate, payload.getCandidate());