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;
16 import akka.testkit.JavaTestKit;
17 import com.google.common.collect.ImmutableSet;
18 import java.util.concurrent.TimeUnit;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
24 import org.opendaylight.controller.cluster.datastore.Shard;
25 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
26 import org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateAdded;
27 import org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateRemoved;
28 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
34 import scala.concurrent.duration.FiniteDuration;
37 * Unit tests for CandidateListChangeListener.
39 * @author Thomas Pantelis
41 public class CandidateListChangeListenerTest extends AbstractActorTest {
42 private static final String ENTITY_TYPE = "test";
43 private static final YangInstanceIdentifier ENTITY_ID1 =
44 YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity1"));
45 private static final YangInstanceIdentifier ENTITY_ID2 =
46 YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity2"));
48 private ShardDataTree shardDataTree;
51 private Shard mockShard;
55 MockitoAnnotations.initMocks(this);
56 shardDataTree = new ShardDataTree(mockShard, SchemaContextHelper.entityOwners(), TreeType.OPERATIONAL);
60 public void testOnDataTreeChanged() throws Exception {
61 JavaTestKit kit = new JavaTestKit(getSystem());
63 new CandidateListChangeListener(kit.getRef(), "test").init(shardDataTree);
65 String memberName1 = "member-1";
66 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
68 CandidateAdded candidateAdded = kit.expectMsgClass(CandidateAdded.class);
69 assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateAdded.getEntityPath());
70 assertEquals("getNewCandidate", memberName1, candidateAdded.getNewCandidate());
71 assertEquals("getAllCandidates", ImmutableSet.of(memberName1),
72 ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
74 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
75 kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
77 String memberName2 = "member-2";
78 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName2));
80 candidateAdded = kit.expectMsgClass(CandidateAdded.class);
81 assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateAdded.getEntityPath());
82 assertEquals("getNewCandidate", memberName2, candidateAdded.getNewCandidate());
83 assertEquals("getAllCandidates", ImmutableSet.of(memberName1, memberName2),
84 ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
86 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, memberName1));
88 candidateAdded = kit.expectMsgClass(CandidateAdded.class);
89 assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID2), candidateAdded.getEntityPath());
90 assertEquals("getNewCandidate", memberName1, candidateAdded.getNewCandidate());
91 assertEquals("getAllCandidates", ImmutableSet.of(memberName1),
92 ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
94 deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName1));
96 CandidateRemoved candidateRemoved = kit.expectMsgClass(CandidateRemoved.class);
97 assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateRemoved.getEntityPath());
98 assertEquals("getRemovedCandidate", memberName1, candidateRemoved.getRemovedCandidate());
99 assertEquals("getRemainingCandidates", ImmutableSet.of(memberName2),
100 ImmutableSet.copyOf(candidateRemoved.getRemainingCandidates()));
102 deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName2));
104 candidateRemoved = kit.expectMsgClass(CandidateRemoved.class);
105 assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateRemoved.getEntityPath());
106 assertEquals("getRemovedCandidate", memberName2, candidateRemoved.getRemovedCandidate());
107 assertEquals("getRemainingCandidates", ImmutableSet.of(),
108 ImmutableSet.copyOf(candidateRemoved.getRemainingCandidates()));
111 private void writeNode(final YangInstanceIdentifier path, final NormalizedNode<?, ?> node)
112 throws DataValidationFailedException {
113 AbstractEntityOwnershipTest.writeNode(path, node, shardDataTree);
116 private void deleteNode(final YangInstanceIdentifier path) throws DataValidationFailedException {
117 AbstractEntityOwnershipTest.deleteNode(path, shardDataTree);