Bug 6186 - fix testCandidateSerialization()
[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         setUpStatic();
107         final YangInstanceIdentifier writePath = TestModel.TEST_PATH;
108         final NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
109                 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
110                 withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
111         candidate = DataTreeCandidates.fromNormalizedNode(writePath, writeData);
112     }
113
114     @Test
115     public void testCandidateSerialization() throws IOException {
116         final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
117         assertEquals("payload size", 181, payload.size());
118     }
119
120     @Test
121     public void testCandidateSerDes() throws IOException {
122         final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
123         assertCandidateEquals(candidate, payload.getCandidate().getValue());
124     }
125
126     @Test
127     public void testPayloadSerDes() throws IOException {
128         final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
129         assertCandidateEquals(candidate, SerializationUtils.clone(payload).getCandidate().getValue());
130     }
131
132     @SuppressWarnings({ "rawtypes", "unchecked" })
133     @Test
134     public void testLeafSetEntryNodeCandidate() throws Exception {
135         YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
136         YangInstanceIdentifier leafSetEntryPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET)
137                 .node(entryPathArg).build();
138
139         NormalizedNode<?, ?> leafSetEntryNode = Builders.leafSetEntryBuilder().
140                 withNodeIdentifier(entryPathArg).withValue("one").build();
141
142         DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafSetEntryPath, leafSetEntryNode);
143         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
144         assertCandidateEquals(candidate, payload.getCandidate().getValue());
145     }
146
147     @SuppressWarnings({ "rawtypes", "unchecked" })
148     @Test
149     public void testLeafSetNodeCandidate() throws Exception {
150         YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
151         YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
152
153         LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().
154                 withNodeIdentifier(entryPathArg).withValue("one").build();
155         NormalizedNode<?, ?> leafSetNode = Builders.leafSetBuilder().withNodeIdentifier(
156                 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
157
158         DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
159         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
160         assertCandidateEquals(candidate, payload.getCandidate().getValue());
161     }
162
163     @SuppressWarnings({ "rawtypes", "unchecked" })
164     @Test
165     public void testOrderedLeafSetNodeCandidate() throws Exception {
166         YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
167         YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
168
169         LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().
170                 withNodeIdentifier(entryPathArg).withValue("one").build();
171         NormalizedNode<?, ?> leafSetNode = Builders.orderedLeafSetBuilder().withNodeIdentifier(
172                 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
173
174         DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
175         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
176         assertCandidateEquals(candidate, payload.getCandidate().getValue());
177     }
178
179     @Test
180     public void testLeafNodeCandidate() throws Exception {
181         YangInstanceIdentifier leafPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(TestModel.DESC_QNAME).build();
182         LeafNode<Object> leafNode = Builders.leafBuilder().withNodeIdentifier(
183                 new YangInstanceIdentifier.NodeIdentifier(TestModel.DESC_QNAME)).withValue("test").build();
184
185         DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(leafPath, leafNode);
186         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
187         assertCandidateEquals(candidate, payload.getCandidate().getValue());
188     }
189 }