Fix unit test CS warnings in sal-distributed-datastore
[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.md.cluster.datastore.model.TestModel;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
31 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
32 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
34
35 public class CommitTransactionPayloadTest extends AbstractTest {
36     static final QName LEAF_SET = QName.create(TestModel.TEST_QNAME, "leaf-set");
37
38     private DataTreeCandidate candidate;
39
40     private static DataTreeCandidateNode findNode(final Collection<DataTreeCandidateNode> nodes,
41             final PathArgument arg) {
42         for (DataTreeCandidateNode node : nodes) {
43             if (arg.equals(node.getIdentifier())) {
44                 return node;
45             }
46         }
47         return null;
48     }
49
50     private static void assertChildrenEquals(final Collection<DataTreeCandidateNode> expected,
51             final Collection<DataTreeCandidateNode> actual) {
52         // Make sure all expected nodes are there
53         for (DataTreeCandidateNode exp : expected) {
54             final DataTreeCandidateNode act = findNode(actual, exp.getIdentifier());
55             assertNotNull("missing expected child", act);
56             assertCandidateNodeEquals(exp, act);
57         }
58         // Make sure no nodes are present which are not in the expected set
59         for (DataTreeCandidateNode act : actual) {
60             final DataTreeCandidateNode exp = findNode(expected, act.getIdentifier());
61             assertNull("unexpected child", exp);
62         }
63     }
64
65     private static void assertCandidateEquals(final DataTreeCandidate expected, final DataTreeCandidate actual) {
66         assertEquals("root path", expected.getRootPath(), actual.getRootPath());
67
68         final DataTreeCandidateNode expRoot = expected.getRootNode();
69         final DataTreeCandidateNode actRoot = expected.getRootNode();
70         assertEquals("root type", expRoot.getModificationType(), actRoot.getModificationType());
71
72         switch (actRoot.getModificationType()) {
73             case DELETE:
74             case WRITE:
75                 assertEquals("root data", expRoot.getDataAfter(), actRoot.getDataAfter());
76                 break;
77             case SUBTREE_MODIFIED:
78                 assertChildrenEquals(expRoot.getChildNodes(), actRoot.getChildNodes());
79                 break;
80             default:
81                 fail("Unexpect root type " + actRoot.getModificationType());
82                 break;
83         }
84
85         assertCandidateNodeEquals(expected.getRootNode(), actual.getRootNode());
86     }
87
88     private static void assertCandidateNodeEquals(final DataTreeCandidateNode expected,
89             final DataTreeCandidateNode actual) {
90         assertEquals("child type", expected.getModificationType(), actual.getModificationType());
91         assertEquals("child identifier", expected.getIdentifier(), actual.getIdentifier());
92
93         switch (actual.getModificationType()) {
94             case DELETE:
95             case WRITE:
96                 assertEquals("child data", expected.getDataAfter(), actual.getDataAfter());
97                 break;
98             case SUBTREE_MODIFIED:
99                 assertChildrenEquals(expected.getChildNodes(), actual.getChildNodes());
100                 break;
101             default:
102                 fail("Unexpect root type " + actual.getModificationType());
103                 break;
104         }
105     }
106
107     @Before
108     public void setUp() {
109         setUpStatic();
110         final YangInstanceIdentifier writePath = TestModel.TEST_PATH;
111         final NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create()
112                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
113                 .withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
114         candidate = DataTreeCandidates.fromNormalizedNode(writePath, writeData);
115     }
116
117     @Test
118     public void testCandidateSerialization() throws IOException {
119         final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
120         assertEquals("payload size", 181, payload.size());
121     }
122
123     @Test
124     public void testCandidateSerDes() throws IOException {
125         final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
126         assertCandidateEquals(candidate, payload.getCandidate().getValue());
127     }
128
129     @Test
130     public void testPayloadSerDes() throws IOException {
131         final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
132         assertCandidateEquals(candidate, SerializationUtils.clone(payload).getCandidate().getValue());
133     }
134
135     @SuppressWarnings({ "rawtypes", "unchecked" })
136     @Test
137     public void testLeafSetEntryNodeCandidate() throws Exception {
138         YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
139         YangInstanceIdentifier leafSetEntryPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET)
140                 .node(entryPathArg).build();
141
142         NormalizedNode<?, ?> leafSetEntryNode = Builders.leafSetEntryBuilder().withNodeIdentifier(entryPathArg)
143                 .withValue("one").build();
144
145         candidate = DataTreeCandidates.fromNormalizedNode(leafSetEntryPath, leafSetEntryNode);
146         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
147         assertCandidateEquals(candidate, payload.getCandidate().getValue());
148     }
149
150     @SuppressWarnings({ "rawtypes", "unchecked" })
151     @Test
152     public void testLeafSetNodeCandidate() throws Exception {
153         YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
154         YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
155
156         LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().withNodeIdentifier(entryPathArg)
157                 .withValue("one").build();
158         NormalizedNode<?, ?> leafSetNode = Builders.leafSetBuilder().withNodeIdentifier(
159                 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
160
161         candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
162         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
163         assertCandidateEquals(candidate, payload.getCandidate().getValue());
164     }
165
166     @SuppressWarnings({ "rawtypes", "unchecked" })
167     @Test
168     public void testOrderedLeafSetNodeCandidate() throws Exception {
169         YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
170         YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
171
172         LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().withNodeIdentifier(entryPathArg)
173                 .withValue("one").build();
174         NormalizedNode<?, ?> leafSetNode = Builders.orderedLeafSetBuilder().withNodeIdentifier(
175                 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
176
177         candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
178         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
179         assertCandidateEquals(candidate, payload.getCandidate().getValue());
180     }
181
182     @Test
183     public void testLeafNodeCandidate() throws Exception {
184         YangInstanceIdentifier leafPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
185                 .node(TestModel.DESC_QNAME).build();
186         LeafNode<Object> leafNode = Builders.leafBuilder().withNodeIdentifier(
187                 new YangInstanceIdentifier.NodeIdentifier(TestModel.DESC_QNAME)).withValue("test").build();
188
189         candidate = DataTreeCandidates.fromNormalizedNode(leafPath, leafNode);
190         CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
191         assertCandidateEquals(candidate, payload.getCandidate().getValue());
192     }
193 }