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