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