2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.entityownership;
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;
32 * Unit tests for CandidateListChangeListener.
34 * @author Thomas Pantelis
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"));
43 private final ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners(),
44 TreeType.OPERATIONAL);
47 public void testOnDataTreeChanged() throws Exception {
48 JavaTestKit kit = new JavaTestKit(getSystem());
50 new CandidateListChangeListener(kit.getRef(), "test").init(shardDataTree);
52 String memberName1 = "member-1";
53 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
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()));
61 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
62 kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
64 String memberName2 = "member-2";
65 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName2));
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()));
73 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, memberName1));
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()));
81 deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName1));
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()));
89 deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName2));
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()));
98 private void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node) throws DataValidationFailedException {
99 AbstractEntityOwnershipTest.writeNode(path, node, shardDataTree);
102 private void deleteNode(YangInstanceIdentifier path) throws DataValidationFailedException {
103 AbstractEntityOwnershipTest.deleteNode(path, shardDataTree);