Bug 4105: Implement UnregisterCandidateLocal in EntityOwnershipShard
[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  * Unit tests for EntityOwnerChangeListener.
32  *
33  * @author Thomas Pantelis
34  */
35 public class EntityOwnerChangeListenerTest {
36     private static final String LOCAL_MEMBER_NAME = "member-1";
37     private static final String REMOTE_MEMBER_NAME1 = "member-2";
38     private static final String REMOTE_MEMBER_NAME2 = "member-3";
39     private static final String ENTITY_TYPE = "test";
40     private static final YangInstanceIdentifier ENTITY_ID1 =
41             YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity1"));
42     private static final YangInstanceIdentifier ENTITY_ID2 =
43             YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity2"));
44     private static final Entity ENTITY1 = new Entity(ENTITY_TYPE, ENTITY_ID1);
45     private static final Entity ENTITY2 = new Entity(ENTITY_TYPE, ENTITY_ID2);
46
47     private final ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners());
48     private final EntityOwnershipListenerSupport mockListenerSupport = mock(EntityOwnershipListenerSupport.class);
49     private EntityOwnerChangeListener listener;
50
51     @Before
52     public void setup() {
53         listener = new EntityOwnerChangeListener(LOCAL_MEMBER_NAME, mockListenerSupport);
54         listener.init(shardDataTree);
55     }
56
57     @Test
58     public void testOnDataTreeChanged() throws Exception {
59         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, LOCAL_MEMBER_NAME));
60         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, LOCAL_MEMBER_NAME));
61         verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(Entity.class), anyBoolean(), anyBoolean());
62
63         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
64         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, false, true);
65
66         reset(mockListenerSupport);
67         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, REMOTE_MEMBER_NAME1));
68         verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(Entity.class), anyBoolean(), anyBoolean());
69
70         reset(mockListenerSupport);
71         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME1));
72         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, true, false);
73
74         reset(mockListenerSupport);
75         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME2));
76         verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(Entity.class), anyBoolean(), anyBoolean());
77
78         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
79         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, false, true);
80
81         reset(mockListenerSupport);
82         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, REMOTE_MEMBER_NAME1));
83         verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(Entity.class), anyBoolean(), anyBoolean());
84
85         reset(mockListenerSupport);
86         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME));
87         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY2, false, true);
88     }
89
90     private void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node) throws DataValidationFailedException {
91         AbstractEntityOwnershipTest.writeNode(path, node, shardDataTree);
92     }
93 }