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 / entityownership / CandidateListChangeListenerTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.entityownership;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
12 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.candidatePath;
13 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithCandidate;
14 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
15 import akka.testkit.JavaTestKit;
16 import com.google.common.collect.ImmutableSet;
17 import java.util.concurrent.TimeUnit;
18 import org.junit.Test;
19 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
20 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
21 import org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateAdded;
22 import org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateRemoved;
23 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
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.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
29 import scala.concurrent.duration.FiniteDuration;
30
31 /**
32  * Unit tests for CandidateListChangeListener.
33  *
34  * @author Thomas Pantelis
35  */
36 public class CandidateListChangeListenerTest extends AbstractActorTest {
37     private static final String ENTITY_TYPE = "test";
38     private static final YangInstanceIdentifier ENTITY_ID1 =
39             YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity1"));
40     private static final YangInstanceIdentifier ENTITY_ID2 =
41             YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity2"));
42
43     private final ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners(),
44         TreeType.OPERATIONAL);
45
46     @Test
47     public void testOnDataTreeChanged() throws Exception {
48         JavaTestKit kit = new JavaTestKit(getSystem());
49
50         new CandidateListChangeListener(kit.getRef(), "test").init(shardDataTree);
51
52         String memberName1 = "member-1";
53         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
54
55         CandidateAdded candidateAdded = kit.expectMsgClass(CandidateAdded.class);
56         assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateAdded.getEntityPath());
57         assertEquals("getNewCandidate", memberName1, candidateAdded.getNewCandidate());
58         assertEquals("getAllCandidates", ImmutableSet.of(memberName1),
59                 ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
60
61         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
62         kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
63
64         String memberName2 = "member-2";
65         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName2));
66
67         candidateAdded = kit.expectMsgClass(CandidateAdded.class);
68         assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateAdded.getEntityPath());
69         assertEquals("getNewCandidate", memberName2, candidateAdded.getNewCandidate());
70         assertEquals("getAllCandidates", ImmutableSet.of(memberName1, memberName2),
71                 ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
72
73         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, memberName1));
74
75         candidateAdded = kit.expectMsgClass(CandidateAdded.class);
76         assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID2), candidateAdded.getEntityPath());
77         assertEquals("getNewCandidate", memberName1, candidateAdded.getNewCandidate());
78         assertEquals("getAllCandidates", ImmutableSet.of(memberName1),
79                 ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
80
81         deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName1));
82
83         CandidateRemoved candidateRemoved = kit.expectMsgClass(CandidateRemoved.class);
84         assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateRemoved.getEntityPath());
85         assertEquals("getRemovedCandidate", memberName1, candidateRemoved.getRemovedCandidate());
86         assertEquals("getRemainingCandidates", ImmutableSet.of(memberName2),
87                 ImmutableSet.copyOf(candidateRemoved.getRemainingCandidates()));
88
89         deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName2));
90
91         candidateRemoved = kit.expectMsgClass(CandidateRemoved.class);
92         assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateRemoved.getEntityPath());
93         assertEquals("getRemovedCandidate", memberName2, candidateRemoved.getRemovedCandidate());
94         assertEquals("getRemainingCandidates", ImmutableSet.of(),
95                 ImmutableSet.copyOf(candidateRemoved.getRemainingCandidates()));
96     }
97
98     private void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node) throws DataValidationFailedException {
99         AbstractEntityOwnershipTest.writeNode(path, node, shardDataTree);
100     }
101
102     private void deleteNode(YangInstanceIdentifier path) throws DataValidationFailedException {
103         AbstractEntityOwnershipTest.deleteNode(path, shardDataTree);
104     }
105 }