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.entityownership;
10 import static org.junit.Assert.assertEquals;
11 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
12 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.candidatePath;
13 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.entityOwnersWithCandidate;
14 import static org.opendaylight.controller.cluster.entityownership.EntityOwnersModel.entityPath;
16 import akka.testkit.javadsl.TestKit;
17 import com.google.common.collect.ImmutableSet;
18 import java.time.Duration;
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.entityownership.messages.CandidateAdded;
27 import org.opendaylight.controller.cluster.entityownership.messages.CandidateRemoved;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
35 * Unit tests for CandidateListChangeListener.
37 * @author Thomas Pantelis
39 public class CandidateListChangeListenerTest extends AbstractActorTest {
40 private static final String ENTITY_TYPE = "test";
41 private static final YangInstanceIdentifier ENTITY_ID1 =
42 YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity1"));
43 private static final YangInstanceIdentifier ENTITY_ID2 =
44 YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity2"));
46 private ShardDataTree shardDataTree;
49 private Shard mockShard;
53 MockitoAnnotations.initMocks(this);
54 shardDataTree = new ShardDataTree(mockShard, EOSTestUtils.SCHEMA_CONTEXT, TreeType.OPERATIONAL);
58 public void testOnDataTreeChanged() throws Exception {
59 TestKit kit = new TestKit(getSystem());
61 new CandidateListChangeListener(kit.getRef(), "test").init(shardDataTree);
63 String memberName1 = "member-1";
64 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
66 CandidateAdded candidateAdded = kit.expectMsgClass(CandidateAdded.class);
67 assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateAdded.getEntityPath());
68 assertEquals("getNewCandidate", memberName1, candidateAdded.getNewCandidate());
69 assertEquals("getAllCandidates", ImmutableSet.of(memberName1),
70 ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
72 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
73 kit.expectNoMessage(Duration.ofMillis(500));
75 String memberName2 = "member-2";
76 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName2));
78 candidateAdded = kit.expectMsgClass(CandidateAdded.class);
79 assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateAdded.getEntityPath());
80 assertEquals("getNewCandidate", memberName2, candidateAdded.getNewCandidate());
81 assertEquals("getAllCandidates", ImmutableSet.of(memberName1, memberName2),
82 ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
84 writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, memberName1));
86 candidateAdded = kit.expectMsgClass(CandidateAdded.class);
87 assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID2), candidateAdded.getEntityPath());
88 assertEquals("getNewCandidate", memberName1, candidateAdded.getNewCandidate());
89 assertEquals("getAllCandidates", ImmutableSet.of(memberName1),
90 ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
92 deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName1));
94 CandidateRemoved candidateRemoved = kit.expectMsgClass(CandidateRemoved.class);
95 assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateRemoved.getEntityPath());
96 assertEquals("getRemovedCandidate", memberName1, candidateRemoved.getRemovedCandidate());
97 assertEquals("getRemainingCandidates", ImmutableSet.of(memberName2),
98 ImmutableSet.copyOf(candidateRemoved.getRemainingCandidates()));
100 deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName2));
102 candidateRemoved = kit.expectMsgClass(CandidateRemoved.class);
103 assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateRemoved.getEntityPath());
104 assertEquals("getRemovedCandidate", memberName2, candidateRemoved.getRemovedCandidate());
105 assertEquals("getRemainingCandidates", ImmutableSet.of(),
106 ImmutableSet.copyOf(candidateRemoved.getRemainingCandidates()));
109 private void writeNode(final YangInstanceIdentifier path, final NormalizedNode node)
110 throws DataValidationFailedException {
111 AbstractEntityOwnershipTest.writeNode(path, node, shardDataTree);
114 private void deleteNode(final YangInstanceIdentifier path) throws DataValidationFailedException {
115 AbstractEntityOwnershipTest.deleteNode(path, shardDataTree);