aa96fc20d8579a7cc57c11a59b9c440a7b8fcd07
[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 scala.concurrent.duration.FiniteDuration;
29
30 /**
31  * Unit tests for CandidateListChangeListener.
32  *
33  * @author Thomas Pantelis
34  */
35 public class CandidateListChangeListenerTest extends AbstractActorTest {
36     private static final String ENTITY_TYPE = "test";
37     private static final YangInstanceIdentifier ENTITY_ID1 =
38             YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity1"));
39     private static final YangInstanceIdentifier ENTITY_ID2 =
40             YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity2"));
41
42     private final ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners());
43
44     @Test
45     public void testOnDataTreeChanged() throws Exception {
46         JavaTestKit kit = new JavaTestKit(getSystem());
47
48         new CandidateListChangeListener(kit.getRef()).init(shardDataTree);
49
50         String memberName1 = "member-1";
51         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
52
53         CandidateAdded candidateAdded = kit.expectMsgClass(CandidateAdded.class);
54         assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateAdded.getEntityPath());
55         assertEquals("getNewCandidate", memberName1, candidateAdded.getNewCandidate());
56         assertEquals("getAllCandidates", ImmutableSet.of(memberName1),
57                 ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
58
59         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
60         kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
61
62         String memberName2 = "member-2";
63         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName2));
64
65         candidateAdded = kit.expectMsgClass(CandidateAdded.class);
66         assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateAdded.getEntityPath());
67         assertEquals("getNewCandidate", memberName2, candidateAdded.getNewCandidate());
68         assertEquals("getAllCandidates", ImmutableSet.of(memberName1, memberName2),
69                 ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
70
71         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, memberName1));
72
73         candidateAdded = kit.expectMsgClass(CandidateAdded.class);
74         assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID2), candidateAdded.getEntityPath());
75         assertEquals("getNewCandidate", memberName1, candidateAdded.getNewCandidate());
76         assertEquals("getAllCandidates", ImmutableSet.of(memberName1),
77                 ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
78
79         deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName1));
80
81         CandidateRemoved candidateRemoved = kit.expectMsgClass(CandidateRemoved.class);
82         assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateRemoved.getEntityPath());
83         assertEquals("getRemovedCandidate", memberName1, candidateRemoved.getRemovedCandidate());
84         assertEquals("getRemainingCandidates", ImmutableSet.of(memberName2),
85                 ImmutableSet.copyOf(candidateRemoved.getRemainingCandidates()));
86
87         deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName2));
88
89         candidateRemoved = kit.expectMsgClass(CandidateRemoved.class);
90         assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateRemoved.getEntityPath());
91         assertEquals("getRemovedCandidate", memberName2, candidateRemoved.getRemovedCandidate());
92         assertEquals("getRemainingCandidates", ImmutableSet.of(),
93                 ImmutableSet.copyOf(candidateRemoved.getRemainingCandidates()));
94     }
95
96     private void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node) throws DataValidationFailedException {
97         AbstractEntityOwnershipTest.writeNode(path, node, shardDataTree);
98     }
99
100     private void deleteNode(YangInstanceIdentifier path) throws DataValidationFailedException {
101         AbstractEntityOwnershipTest.deleteNode(path, shardDataTree);
102     }
103 }