Handle UNMODIFIED type in DataTreeCandidateInputOutput
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / persisted / CommitTransactionPayloadTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.persisted;
9
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
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.SchemaContextHelper;
22 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
35 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
36 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
38 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
39
40 public class CommitTransactionPayloadTest extends AbstractTest {
41     static final QName LEAF_SET = QName.create(TestModel.TEST_QNAME, "leaf-set");
42
43     private DataTreeCandidate candidate;
44
45     private static DataTreeCandidateNode findNode(final Collection<DataTreeCandidateNode> nodes,
46             final PathArgument arg) {
47         for (DataTreeCandidateNode node : nodes) {
48             if (arg.equals(node.getIdentifier())) {
49                 return node;
50             }
51         }
52         return null;
53     }
54
55     private static void assertChildrenEquals(final Collection<DataTreeCandidateNode> expected,
56             final Collection<DataTreeCandidateNode> actual) {
57         // Make sure all expected nodes are there
58         for (DataTreeCandidateNode exp : expected) {
59             final DataTreeCandidateNode act = findNode(actual, exp.getIdentifier());
60             assertNotNull("missing expected child", act);
61             assertCandidateNodeEquals(exp, act);
62         }
63         // Make sure no nodes are present which are not in the expected set
64         for (DataTreeCandidateNode act : actual) {
65             final DataTreeCandidateNode exp = findNode(expected, act.getIdentifier());
66             assertNull("unexpected child", exp);
67         }
68     }
69
70     private static void assertCandidateEquals(final DataTreeCandidate expected, final DataTreeCandidate actual) {
71         assertEquals("root path", expected.getRootPath(), actual.getRootPath());
72         assertCandidateNodeEquals(expected.getRootNode(), actual.getRootNode());
73     }
74
75     private static void assertCandidateNodeEquals(final DataTreeCandidateNode expected,
76             final DataTreeCandidateNode actual) {
77         assertEquals("child type", expected.getModificationType(), actual.getModificationType());
78
79         switch (actual.getModificationType()) {
80             case DELETE:
81             case WRITE:
82                 assertEquals("child identifier", expected.getIdentifier(), actual.getIdentifier());
83                 assertEquals("child data", expected.getDataAfter(), actual.getDataAfter());
84                 break;
85             case SUBTREE_MODIFIED:
86                 assertEquals("child identifier", expected.getIdentifier(), actual.getIdentifier());
87                 assertChildrenEquals(expected.getChildNodes(), actual.getChildNodes());
88                 break;
89             case UNMODIFIED:
90                 break;
91             default:
92                 fail("Unexpect root type " + actual.getModificationType());
93                 break;
94         }
95     }
96
97     @Before
98     public void setUp() {
99         setUpStatic();
100         final YangInstanceIdentifier writePath = TestModel.TEST_PATH;
101         final NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create()
102                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
103                 .withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
104         candidate = DataTreeCandidates.fromNormalizedNode(writePath, writeData);
105     }
106
107     @Test
108     public void testCandidateSerialization() throws IOException {
109         final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
110         assertEquals("payload size", 181, payload.size());
111     }
112
113     @Test
114     public void testCandidateSerDes() throws IOException {
115         final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
116         assertCandidateEquals(candidate, payload.getCandidate().getValue());
117     }
118
119     @Test
120     public void testPayloadSerDes() throws IOException {
121         final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
122         assertCandidateEquals(candidate, SerializationUtils.clone(payload).getCandidate().getValue());
123     }
124
125     @SuppressWarnings({ "rawtypes", "unchecked" })
126     @Test
127     public void testLeafSetEntryNodeCandidate() throws Exception {
128         YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
129         YangInstanceIdentifier leafSetEntryPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET)
130                 .node(entryPathArg).build();
131
132         NormalizedNode<?, ?> leafSetEntryNode = Builders.leafSetEntryBuilder().withNodeIdentifier(entryPathArg)
133                 .withValue("one").build();
134
135         candidate = DataTreeCandidates.fromNormalizedNode(leafSetEntryPath, leafSetEntryNode);
136         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
137         assertCandidateEquals(candidate, payload.getCandidate().getValue());
138     }
139
140     @SuppressWarnings({ "rawtypes", "unchecked" })
141     @Test
142     public void testLeafSetNodeCandidate() throws Exception {
143         YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
144         YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
145
146         LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().withNodeIdentifier(entryPathArg)
147                 .withValue("one").build();
148         NormalizedNode<?, ?> leafSetNode = Builders.leafSetBuilder().withNodeIdentifier(
149                 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
150
151         candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
152         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
153         assertCandidateEquals(candidate, payload.getCandidate().getValue());
154     }
155
156     @SuppressWarnings({ "rawtypes", "unchecked" })
157     @Test
158     public void testOrderedLeafSetNodeCandidate() throws Exception {
159         YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
160         YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
161
162         LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().withNodeIdentifier(entryPathArg)
163                 .withValue("one").build();
164         NormalizedNode<?, ?> leafSetNode = Builders.orderedLeafSetBuilder().withNodeIdentifier(
165                 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
166
167         candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
168         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
169         assertCandidateEquals(candidate, payload.getCandidate().getValue());
170     }
171
172     @Test
173     public void testLeafNodeCandidate() throws Exception {
174         YangInstanceIdentifier leafPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
175                 .node(TestModel.DESC_QNAME).build();
176         LeafNode<Object> leafNode = Builders.leafBuilder().withNodeIdentifier(
177                 new YangInstanceIdentifier.NodeIdentifier(TestModel.DESC_QNAME)).withValue("test").build();
178
179         candidate = DataTreeCandidates.fromNormalizedNode(leafPath, leafNode);
180         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
181         assertCandidateEquals(candidate, payload.getCandidate().getValue());
182     }
183
184     @Test
185     public void testUnmodifiedRootCandidate() throws Exception {
186         final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
187         dataTree.setSchemaContext(SchemaContextHelper.select(SchemaContextHelper.CARS_YANG));
188
189         DataTreeModification modification = dataTree.takeSnapshot().newModification();
190         modification.ready();
191         candidate = dataTree.prepare(modification);
192
193         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
194         assertCandidateEquals(candidate, payload.getCandidate().getValue());
195     }
196 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.