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 scala.concurrent.duration.FiniteDuration;
31 * Unit tests for CandidateListChangeListener.
33 * @author Thomas Pantelis
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"));
42 private final ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners());
45 public void testOnDataTreeChanged() throws Exception {
46 JavaTestKit kit = new JavaTestKit(getSystem());
48 new CandidateListChangeListener(kit.getRef()).init(shardDataTree);
50 String memberName1 = "member-1";
51 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
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()));
59 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
60 kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
62 String memberName2 = "member-2";
63 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName2));
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()));
71 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, memberName1));
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()));
79 deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName1));
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()));
87 deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName2));
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()));
96 private void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node) throws DataValidationFailedException {
97 AbstractEntityOwnershipTest.writeNode(path, node, shardDataTree);
100 private void deleteNode(YangInstanceIdentifier path) throws DataValidationFailedException {
101 AbstractEntityOwnershipTest.deleteNode(path, shardDataTree);