BUG-5280: expand ShardDataTree to cover transaction mechanics
[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 import akka.testkit.JavaTestKit;
16 import com.google.common.collect.ImmutableSet;
17 import java.util.concurrent.TimeUnit;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
23 import org.opendaylight.controller.cluster.datastore.Shard;
24 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
25 import org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateAdded;
26 import org.opendaylight.controller.cluster.datastore.entityownership.messages.CandidateRemoved;
27 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
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;
33 import scala.concurrent.duration.FiniteDuration;
34
35 /**
36  * Unit tests for CandidateListChangeListener.
37  *
38  * @author Thomas Pantelis
39  */
40 public class CandidateListChangeListenerTest extends AbstractActorTest {
41     private static final String ENTITY_TYPE = "test";
42     private static final YangInstanceIdentifier ENTITY_ID1 =
43             YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity1"));
44     private static final YangInstanceIdentifier ENTITY_ID2 =
45             YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity2"));
46
47     private ShardDataTree shardDataTree;
48
49     @Mock
50     private Shard mockShard;
51
52     @Before
53     public void setup() {
54         MockitoAnnotations.initMocks(this);
55         shardDataTree = new ShardDataTree(mockShard, SchemaContextHelper.entityOwners(), TreeType.OPERATIONAL);
56     }
57
58     @Test
59     public void testOnDataTreeChanged() throws Exception {
60         JavaTestKit kit = new JavaTestKit(getSystem());
61
62         new CandidateListChangeListener(kit.getRef(), "test").init(shardDataTree);
63
64         String memberName1 = "member-1";
65         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
66
67         CandidateAdded candidateAdded = kit.expectMsgClass(CandidateAdded.class);
68         assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateAdded.getEntityPath());
69         assertEquals("getNewCandidate", memberName1, candidateAdded.getNewCandidate());
70         assertEquals("getAllCandidates", ImmutableSet.of(memberName1),
71                 ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
72
73         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName1));
74         kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
75
76         String memberName2 = "member-2";
77         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, memberName2));
78
79         candidateAdded = kit.expectMsgClass(CandidateAdded.class);
80         assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateAdded.getEntityPath());
81         assertEquals("getNewCandidate", memberName2, candidateAdded.getNewCandidate());
82         assertEquals("getAllCandidates", ImmutableSet.of(memberName1, memberName2),
83                 ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
84
85         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, memberName1));
86
87         candidateAdded = kit.expectMsgClass(CandidateAdded.class);
88         assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID2), candidateAdded.getEntityPath());
89         assertEquals("getNewCandidate", memberName1, candidateAdded.getNewCandidate());
90         assertEquals("getAllCandidates", ImmutableSet.of(memberName1),
91                 ImmutableSet.copyOf(candidateAdded.getAllCandidates()));
92
93         deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName1));
94
95         CandidateRemoved candidateRemoved = kit.expectMsgClass(CandidateRemoved.class);
96         assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateRemoved.getEntityPath());
97         assertEquals("getRemovedCandidate", memberName1, candidateRemoved.getRemovedCandidate());
98         assertEquals("getRemainingCandidates", ImmutableSet.of(memberName2),
99                 ImmutableSet.copyOf(candidateRemoved.getRemainingCandidates()));
100
101         deleteNode(candidatePath(ENTITY_TYPE, ENTITY_ID1, memberName2));
102
103         candidateRemoved = kit.expectMsgClass(CandidateRemoved.class);
104         assertEquals("getEntityId", entityPath(ENTITY_TYPE, ENTITY_ID1), candidateRemoved.getEntityPath());
105         assertEquals("getRemovedCandidate", memberName2, candidateRemoved.getRemovedCandidate());
106         assertEquals("getRemainingCandidates", ImmutableSet.of(),
107                 ImmutableSet.copyOf(candidateRemoved.getRemainingCandidates()));
108     }
109
110     private void writeNode(final YangInstanceIdentifier path, final NormalizedNode<?, ?> node) throws DataValidationFailedException {
111         AbstractEntityOwnershipTest.writeNode(path, node, shardDataTree);
112     }
113
114     private void deleteNode(final YangInstanceIdentifier path) throws DataValidationFailedException {
115         AbstractEntityOwnershipTest.deleteNode(path, shardDataTree);
116     }
117 }