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