Eliminate use of deprecated mockito methods
[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.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.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
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mockito;
24 import org.opendaylight.controller.cluster.access.concepts.MemberName;
25 import org.opendaylight.controller.cluster.datastore.Shard;
26 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
27 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
28 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
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
35 /**
36  * Unit tests for EntityOwnerChangeListener.
37  *
38  * @author Thomas Pantelis
39  */
40 public class EntityOwnerChangeListenerTest {
41     private static final String LOCAL_MEMBER_NAME = "member-1";
42     private static final String REMOTE_MEMBER_NAME1 = "member-2";
43     private static final String REMOTE_MEMBER_NAME2 = "member-3";
44     private static final String ENTITY_TYPE = "test";
45     private static final YangInstanceIdentifier ENTITY_ID1 =
46             YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity1"));
47     private static final YangInstanceIdentifier ENTITY_ID2 =
48             YangInstanceIdentifier.of(QName.create("test", "2015-08-14", "entity2"));
49     private static final DOMEntity ENTITY1 = new DOMEntity(ENTITY_TYPE, ENTITY_ID1);
50     private static final DOMEntity ENTITY2 = new DOMEntity(ENTITY_TYPE, ENTITY_ID2);
51
52     private final Shard mockShard = Mockito.mock(Shard.class);
53
54     private final ShardDataTree shardDataTree = new ShardDataTree(mockShard, SchemaContextHelper.entityOwners(),
55         TreeType.OPERATIONAL);
56     private final EntityOwnershipListenerSupport mockListenerSupport = mock(EntityOwnershipListenerSupport.class);
57     private EntityOwnerChangeListener listener;
58
59     @Before
60     public void setup() {
61         listener = new EntityOwnerChangeListener(MemberName.forName(LOCAL_MEMBER_NAME), mockListenerSupport);
62         listener.init(shardDataTree);
63     }
64
65     @Test
66     public void testOnDataTreeChanged() throws Exception {
67         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, LOCAL_MEMBER_NAME));
68         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, LOCAL_MEMBER_NAME));
69         verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(DOMEntity.class), anyBoolean(),
70                 anyBoolean(), anyBoolean());
71
72         // Write local member as owner for entity 1
73
74         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
75         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, false, true, true);
76
77         // Add remote member 1 as candidate for entity 1 - listener support should not get notified
78
79         reset(mockListenerSupport);
80         writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, REMOTE_MEMBER_NAME1));
81         verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(DOMEntity.class), anyBoolean(),
82                 anyBoolean(), anyBoolean());
83
84         // Change owner to remote member 1 for entity 1
85
86         reset(mockListenerSupport);
87         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME1));
88         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, true, false, true);
89
90         // Change owner to remote member 2 for entity 1
91
92         reset(mockListenerSupport);
93         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME2));
94         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, false, false, true);
95
96         // Clear the owner for entity 1
97
98         reset(mockListenerSupport);
99         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, ""));
100         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, false, false, false);
101
102         // Change owner to the local member for entity 1
103
104         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
105         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, false, true, true);
106
107         // Change owner to remote member 2 for entity 2
108
109         reset(mockListenerSupport);
110         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, REMOTE_MEMBER_NAME1));
111         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY2, false, false, true);
112
113         // Change owner to the local member for entity 2
114
115         reset(mockListenerSupport);
116         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME));
117         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY2, false, true, true);
118
119         // Write local member owner for entity 2 again - expect no change
120
121         reset(mockListenerSupport);
122         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME));
123         verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(DOMEntity.class), anyBoolean(),
124                 anyBoolean(), anyBoolean());
125
126         // Clear the owner for entity 2
127
128         reset(mockListenerSupport);
129         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, null));
130         verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY2, true, false, false);
131
132         // Clear the owner for entity 2 again - expect no change
133
134         reset(mockListenerSupport);
135         writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, null));
136         verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(DOMEntity.class), anyBoolean(),
137                 anyBoolean(), anyBoolean());
138     }
139
140     private void writeNode(final YangInstanceIdentifier path, final NormalizedNode<?, ?> node)
141             throws DataValidationFailedException {
142         AbstractEntityOwnershipTest.writeNode(path, node, shardDataTree);
143     }
144 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.