Bug 4105: Remove candidates on PeerDown
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / entityownership / EntityOwnerChangeListenerTest.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.mockito.Matchers.any;
11 import static org.mockito.Matchers.anyBoolean;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.never;
14 import static org.mockito.Mockito.reset;
15 import static org.mockito.Mockito.verify;
16 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
17 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityEntryWithOwner;
18 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityOwnersWithCandidate;
19 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
23 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
24 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
29 ;
30
31 /**
32  * Unit tests for EntityOwnerChangeListener.
33  *
34  * @author Thomas Pantelis
35  */
36 public class EntityOwnerChangeListenerTest {
37     private static final String LOCAL_MEMBER_NAME = "member-1";
38     private static final String REMOTE_MEMBER_NAME1 = "member-2";
39     private static final String REMOTE_MEMBER_NAME2 = "member-3";
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"));
45     private static final Entity ENTITY1 = new Entity(ENTITY_TYPE, ENTITY_ID1);
46     private static final Entity ENTITY2 = new Entity(ENTITY_TYPE, ENTITY_ID2);
47
48     private final ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners());
49     private final EntityOwnershipListenerSupport mockListenerSupport = mock(EntityOwnershipListenerSupport.class);
50     private EntityOwnerChangeListener listener;
51
52     @Before
53     public void setup() {
54         listener = new EntityOwnerChangeListener(LOCAL_MEMBER_NAME, mockListenerSupport);
55         listener.init(shardDataTree);
56     }
57
58     @Test
59     public void testOnDataTreeChanged() throws Exception {
60         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, LOCAL_MEMBER_NAME));
61         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, LOCAL_MEMBER_NAME));
62         verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(Entity.class), anyBoolean(), anyBoolean());
63
64         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
65         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, false, true);
66
67         reset(mockListenerSupport);
68         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, REMOTE_MEMBER_NAME1));
69         verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(Entity.class), anyBoolean(), anyBoolean());
70
71         reset(mockListenerSupport);
72         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME1));
73         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, true, false);
74
75         reset(mockListenerSupport);
76         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME2));
77         verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(Entity.class), anyBoolean(), anyBoolean());
78
79         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
80         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, false, true);
81
82         reset(mockListenerSupport);
83         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, REMOTE_MEMBER_NAME1));
84         verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(Entity.class), anyBoolean(), anyBoolean());
85
86         reset(mockListenerSupport);
87         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME));
88         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY2, false, true);
89
90         reset(mockListenerSupport);
91         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME));
92         verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(Entity.class), anyBoolean(), anyBoolean());
93
94         reset(mockListenerSupport);
95         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, null));
96         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY2, true, false);
97     }
98
99     private void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node) throws DataValidationFailedException {
100         AbstractEntityOwnershipTest.writeNode(path, node, shardDataTree);
101     }
102
103     private void deleteNode(YangInstanceIdentifier path) throws DataValidationFailedException {
104         AbstractEntityOwnershipTest.deleteNode(path, shardDataTree);
105     }
106 }