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