BUG-5280: add CommitTransactionPayload
[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 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;
33
34 public class CommitTransactionPayloadTest extends AbstractTest {
35     static final QName LEAF_SET = QName.create(TestModel.TEST_QNAME, "leaf-set");
36
37     private DataTreeCandidate candidate;
38
39     private static DataTreeCandidateNode findNode(final Collection<DataTreeCandidateNode> nodes, final PathArgument arg) {
40         for (DataTreeCandidateNode node : nodes) {
41             if (arg.equals(node.getIdentifier())) {
42                 return node;
43             }
44         }
45         return null;
46     }
47
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);
55         }
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);
60         }
61     }
62
63     private static void assertCandidateEquals(final DataTreeCandidate expected, final DataTreeCandidate actual) {
64         assertEquals("root path", expected.getRootPath(), actual.getRootPath());
65
66         final DataTreeCandidateNode expRoot = expected.getRootNode();
67         final DataTreeCandidateNode actRoot = expected.getRootNode();
68         assertEquals("root type", expRoot.getModificationType(), actRoot.getModificationType());
69
70         switch (actRoot.getModificationType()) {
71         case DELETE:
72         case WRITE:
73             assertEquals("root data", expRoot.getDataAfter(), actRoot.getDataAfter());
74             break;
75         case SUBTREE_MODIFIED:
76             assertChildrenEquals(expRoot.getChildNodes(), actRoot.getChildNodes());
77             break;
78         default:
79             fail("Unexpect root type " + actRoot.getModificationType());
80             break;
81         }
82
83         assertCandidateNodeEquals(expected.getRootNode(), actual.getRootNode());
84     }
85
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());
89
90         switch (actual.getModificationType()) {
91         case DELETE:
92         case WRITE:
93             assertEquals("child data", expected.getDataAfter(), actual.getDataAfter());
94             break;
95         case SUBTREE_MODIFIED:
96             assertChildrenEquals(expected.getChildNodes(), actual.getChildNodes());
97             break;
98         default:
99             fail("Unexpect root type " + actual.getModificationType());
100             break;
101         }
102     }
103
104     @Before
105     public void setUp() {
106         final YangInstanceIdentifier writePath = TestModel.TEST_PATH;
107         final NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
108                 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
109                 withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
110         candidate = DataTreeCandidates.fromNormalizedNode(writePath, writeData);
111     }
112
113     @Test
114     public void testCandidateSerialization() throws IOException {
115         final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
116         assertEquals("payload size", 181, payload.size());
117     }
118
119     @Test
120     public void testCandidateSerDes() throws IOException {
121         final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
122         assertCandidateEquals(candidate, payload.getCandidate().getValue());
123     }
124
125     @Test
126     public void testPayloadSerDes() throws IOException {
127         final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
128         assertCandidateEquals(candidate, SerializationUtils.clone(payload).getCandidate().getValue());
129     }
130
131     @SuppressWarnings({ "rawtypes", "unchecked" })
132     @Test
133     public void testLeafSetEntryNodeCandidate() throws Exception {
134         YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
135         YangInstanceIdentifier leafSetEntryPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET)
136                 .node(entryPathArg).build();
137
138         NormalizedNode<?, ?> leafSetEntryNode = Builders.leafSetEntryBuilder().
139                 withNodeIdentifier(entryPathArg).withValue("one").build();
140
141         DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafSetEntryPath, leafSetEntryNode);
142         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
143         assertCandidateEquals(candidate, payload.getCandidate().getValue());
144     }
145
146     @SuppressWarnings({ "rawtypes", "unchecked" })
147     @Test
148     public void testLeafSetNodeCandidate() throws Exception {
149         YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
150         YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
151
152         LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().
153                 withNodeIdentifier(entryPathArg).withValue("one").build();
154         NormalizedNode<?, ?> leafSetNode = Builders.leafSetBuilder().withNodeIdentifier(
155                 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
156
157         DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
158         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
159         assertCandidateEquals(candidate, payload.getCandidate().getValue());
160     }
161
162     @SuppressWarnings({ "rawtypes", "unchecked" })
163     @Test
164     public void testOrderedLeafSetNodeCandidate() throws Exception {
165         YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
166         YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
167
168         LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().
169                 withNodeIdentifier(entryPathArg).withValue("one").build();
170         NormalizedNode<?, ?> leafSetNode = Builders.orderedLeafSetBuilder().withNodeIdentifier(
171                 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
172
173         DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
174         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
175         assertCandidateEquals(candidate, payload.getCandidate().getValue());
176     }
177
178     @Test
179     public void testLeafNodeCandidate() throws Exception {
180         YangInstanceIdentifier leafPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(TestModel.DESC_QNAME).build();
181         LeafNode<Object> leafNode = Builders.leafBuilder().withNodeIdentifier(
182                 new YangInstanceIdentifier.NodeIdentifier(TestModel.DESC_QNAME)).withValue("test").build();
183
184         DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafPath, leafNode);
185         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
186         assertCandidateEquals(candidate, payload.getCandidate().getValue());
187     }
188 }