Migrate from JavaTestKit to javadsl.TestKit
[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
16 import akka.testkit.javadsl.TestKit;
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;
35
36 /**
37  * Unit tests for CandidateListChangeListener.
38  *
39  * @author Thomas Pantelis
40  */
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"));
47
48     private ShardDataTree shardDataTree;
49
50     @Mock
51     private Shard mockShard;
52
53     @Before
54     public void setup() {
55         MockitoAnnotations.initMocks(this);
56         shardDataTree = new ShardDataTree(mockShard, SchemaContextHelper.entityOwners(), TreeType.OPERATIONAL);
57     }
58
59     @Test
60     public void testOnDataTreeChanged() throws Exception {
61         TestKit kit = new TestKit(getSystem());
62
63         new CandidateListChangeListener(kit.getRef(), "test").init(shardDataTree);
64
65         String memberName1 = "member-1";
66         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
67
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()));
73
74         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
75         kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
76
77         String memberName2 = "member-2";
78         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName2));
79
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()));
85
86         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, memberName1));
87
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()));
93
94         deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName1));
95
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()));
101
102         deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName2));
103
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()));
109     }
110
111     private void writeNode(final YangInstanceIdentifier path, final NormalizedNode<?, ?> node)
112             throws DataValidationFailedException {
113         AbstractEntityOwnershipTest.writeNode(path, node, shardDataTree);
114     }
115
116     private void deleteNode(final YangInstanceIdentifier path) throws DataValidationFailedException {
117         AbstractEntityOwnershipTest.deleteNode(path, shardDataTree);
118     }
119 }