Merge "IMDS: trim down commit overhead"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DataTreeCandidatePayloadTest.java
1 /*
2  * Copyright (c) 2015 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;
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.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
25 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
27 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
28
29 public class DataTreeCandidatePayloadTest {
30     private DataTreeCandidate candidate;
31
32     private static DataTreeCandidateNode findNode(final Collection<DataTreeCandidateNode> nodes, final PathArgument arg) {
33         for (DataTreeCandidateNode node : nodes) {
34             if (arg.equals(node.getIdentifier())) {
35                 return node;
36             }
37         }
38         return null;
39     }
40
41     private static void assertChildrenEquals(final Collection<DataTreeCandidateNode> expected,
42             final Collection<DataTreeCandidateNode> actual) {
43         // Make sure all expected nodes are there
44         for (DataTreeCandidateNode exp : expected) {
45             final DataTreeCandidateNode act = findNode(actual, exp.getIdentifier());
46             assertNotNull("missing expected child", act);
47             assertCandidateNodeEquals(exp, act);
48         }
49         // Make sure no nodes are present which are not in the expected set
50         for (DataTreeCandidateNode act : actual) {
51             final DataTreeCandidateNode exp = findNode(expected, act.getIdentifier());
52             assertNull("unexpected child", exp);
53         }
54     }
55
56     private static void assertCandidateEquals(final DataTreeCandidate expected, final DataTreeCandidate actual) {
57         assertEquals("root path", expected.getRootPath(), actual.getRootPath());
58
59         final DataTreeCandidateNode expRoot = expected.getRootNode();
60         final DataTreeCandidateNode actRoot = expected.getRootNode();
61         assertEquals("root type", expRoot.getModificationType(), actRoot.getModificationType());
62
63         switch (actRoot.getModificationType()) {
64         case DELETE:
65         case WRITE:
66             assertEquals("root data", expRoot.getDataAfter(), actRoot.getDataAfter());
67             break;
68         case SUBTREE_MODIFIED:
69             assertChildrenEquals(expRoot.getChildNodes(), actRoot.getChildNodes());
70             break;
71         default:
72             fail("Unexpect root type " + actRoot.getModificationType());
73             break;
74         }
75
76         assertCandidateNodeEquals(expected.getRootNode(), actual.getRootNode());
77     }
78
79     private static void assertCandidateNodeEquals(final DataTreeCandidateNode expected, final DataTreeCandidateNode actual) {
80         assertEquals("child type", expected.getModificationType(), actual.getModificationType());
81         assertEquals("child identifier", expected.getIdentifier(), actual.getIdentifier());
82
83         switch (actual.getModificationType()) {
84         case DELETE:
85         case WRITE:
86             assertEquals("child data", expected.getDataAfter(), actual.getDataAfter());
87             break;
88         case SUBTREE_MODIFIED:
89             assertChildrenEquals(expected.getChildNodes(), actual.getChildNodes());
90             break;
91         default:
92             fail("Unexpect root type " + actual.getModificationType());
93             break;
94         }
95     }
96
97     @Before
98     public void setUp() {
99         final YangInstanceIdentifier writePath = TestModel.TEST_PATH;
100         final NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
101                 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
102                 withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
103         candidate = DataTreeCandidates.fromNormalizedNode(writePath, writeData);
104     }
105
106     @Test
107     public void testCandidateSerialization() throws IOException {
108         final DataTreeCandidatePayload payload = DataTreeCandidatePayload.create(candidate);
109         assertEquals("payload size", 141, payload.size());
110     }
111
112     @Test
113     public void testCandidateSerDes() throws IOException {
114         final DataTreeCandidatePayload payload = DataTreeCandidatePayload.create(candidate);
115         assertCandidateEquals(candidate, payload.getCandidate());
116     }
117
118     @Test
119     public void testPayloadSerDes() throws IOException {
120         final DataTreeCandidatePayload payload = DataTreeCandidatePayload.create(candidate);
121         assertCandidateEquals(candidate, SerializationUtils.clone(payload).getCandidate());
122     }
123 }