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