Bug 4105: Add EntityOwnerDataChangeListener
[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.DataTreeCandidateTip;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
31
32 /**
33  * Unit tests for EntityOwnerChangeListener.
34  *
35  * @author Thomas Pantelis
36  */
37 public class EntityOwnerChangeListenerTest {
38     private static final String LOCAL_MEMBER_NAME = "member-1";
39     private static final String REMOTE_MEMBER_NAME1 = "member-2";
40     private static final String REMOTE_MEMBER_NAME2 = "member-3";
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     private static final Entity ENTITY1 = new Entity(ENTITY_TYPE, ENTITY_ID1);
47     private static final Entity ENTITY2 = new Entity(ENTITY_TYPE, ENTITY_ID2);
48
49     private final ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners());
50     private final EntityOwnershipListenerSupport mockListenerSupport = mock(EntityOwnershipListenerSupport.class);
51     private EntityOwnerChangeListener listener;
52
53     @Before
54     public void setup() {
55         listener = new EntityOwnerChangeListener(LOCAL_MEMBER_NAME, mockListenerSupport);
56         listener.init(shardDataTree);
57     }
58
59     @Test
60     public void testOnDataChanged() throws Exception {
61         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, LOCAL_MEMBER_NAME));
62         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, LOCAL_MEMBER_NAME));
63         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, REMOTE_MEMBER_NAME1));
64
65         verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(Entity.class), anyBoolean(), anyBoolean());
66
67         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
68
69         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, false, true);
70
71         reset(mockListenerSupport);
72         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME1));
73
74         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, true, false);
75
76         reset(mockListenerSupport);
77         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME2));
78
79         verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(Entity.class), anyBoolean(), anyBoolean());
80
81         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
82
83         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, false, true);
84
85         reset(mockListenerSupport);
86         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, REMOTE_MEMBER_NAME1));
87
88         verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(Entity.class), anyBoolean(), anyBoolean());
89
90         reset(mockListenerSupport);
91         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME));
92
93         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY2, false, true);
94     }
95
96     private void writeNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node) throws DataValidationFailedException {
97         DataTreeModification modification = shardDataTree.getDataTree().takeSnapshot().newModification();
98         modification.merge(path, node);
99         modification.ready();
100
101         shardDataTree.getDataTree().validate(modification);
102         DataTreeCandidateTip candidate = shardDataTree.getDataTree().prepare(modification);
103         shardDataTree.getDataTree().commit(candidate);
104         shardDataTree.notifyListeners(candidate);
105     }
106 }