2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2019 AT&T Intellectual Property
6 * ===================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * ============LICENSE_END=============================================
20 * ====================================================================
23 package org.onap.music.lockingservice.cassandra;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
27 import java.util.Iterator;
28 import java.util.List;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mockito;
33 import org.onap.music.datastore.MusicDataStore;
34 import org.onap.music.exceptions.MusicLockingException;
35 import org.onap.music.exceptions.MusicQueryException;
36 import org.onap.music.exceptions.MusicServiceException;
37 import org.onap.music.main.DeadlockDetectionUtil;
38 import com.datastax.driver.core.ConsistencyLevel;
39 import com.datastax.driver.core.ResultSet;
40 import com.datastax.driver.core.Row;
41 import com.datastax.driver.core.WriteType;
42 import com.datastax.driver.core.exceptions.WriteTimeoutException;
44 public class CassaLockStoreTest {
46 private CassaLockStore cassaLockStore;
47 private CassaLockStore.LockObject lockObject;
48 private MusicDataStore dsHandle;
52 dsHandle = Mockito.mock(MusicDataStore.class);
53 cassaLockStore = new CassaLockStore(dsHandle);
54 lockObject = cassaLockStore.new LockObject(false, null, null, null, null, null);
58 public void testLockOwner() {
59 lockObject.setIsLockOwner(true);
60 assertEquals(true, lockObject.getIsLockOwner());
62 lockObject.setIsLockOwner(false);
63 assertEquals(false, lockObject.getIsLockOwner());
67 public void testAcquireTime() {
68 lockObject.setAcquireTime("2019-11-11T15:42:12+00:00");
69 assertEquals("2019-11-11T15:42:12+00:00", lockObject.getAcquireTime());
73 public void testCreateTime() {
74 lockObject.setCreateTime("2019-11-11T15:43:44+00:00");
75 assertEquals("2019-11-11T15:43:44+00:00", lockObject.getCreateTime());
79 public void testLockRef() {
80 lockObject.setLockRef("LockReference");
81 assertEquals("LockReference", lockObject.getLockRef());
85 public void testLockType() {
86 lockObject.setLocktype(LockType.READ);
87 assertEquals(LockType.READ, lockObject.getLocktype());
91 public void testOwner() {
92 lockObject.setOwner("Owner");
93 assertEquals("Owner", lockObject.getOwner());
97 public void testCreateLockQueue() {
99 Mockito.when(dsHandle.executePut(Mockito.any(), Mockito.any())).thenReturn(true);
100 assertEquals(true, cassaLockStore.createLockQueue("keyspace1", "table1"));
101 } catch (MusicServiceException | MusicQueryException e) {
102 // TODO Auto-generated catch block
108 public void testGenLockRefandEnQueue() {
109 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
110 List<Row> latestGuardRow = Mockito.mock(List.class);
111 Mockito.when(latestGuardRow.isEmpty()).thenReturn(false);
112 Row row = Mockito.mock(Row.class);
113 Mockito.when(latestGuardRow.get(0)).thenReturn(row);
114 Mockito.when(row.getLong(0)).thenReturn((long) 4);
115 Mockito.when(resultSetMock.all()).thenReturn(latestGuardRow);
117 Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
118 Mockito.when(dsHandle.executePut(Mockito.any(), Mockito.any())).thenReturn(true);
119 assertEquals("$keyspace2.table2.lockName2$5",
120 cassaLockStore.genLockRefandEnQueue("keyspace2", "table2", "lockName2", LockType.READ, "owner2"));
121 } catch (MusicServiceException | MusicQueryException | MusicLockingException e) {
122 // TODO Auto-generated catch block
128 public void testGetLockQueue() {
129 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
130 Iterator<Row> iterator = Mockito.mock(Iterator.class);
131 Mockito.when(iterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
132 Row row = Mockito.mock(Row.class);
133 Mockito.when(row.getLong("lockReference")).thenReturn((long)1).thenReturn((long)2).thenReturn((long)3);
134 Mockito.when(row.get("lockType", LockType.class)).thenReturn(LockType.WRITE).thenReturn(LockType.WRITE).thenReturn(LockType.WRITE);
135 Mockito.when(iterator.next()).thenReturn(row).thenReturn(row).thenReturn(row);
136 Mockito.when(resultSetMock.iterator()).thenReturn(iterator);
139 Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
140 assertEquals("2", cassaLockStore.getLockQueue("keyspace2", "table2", "key2").get(1));
141 } catch (MusicServiceException | MusicQueryException e) {
142 // TODO Auto-generated catch block
148 public void testGetLockQueueSize() {
149 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
150 Row row = Mockito.mock(Row.class);
151 Mockito.when(resultSetMock.one()).thenReturn(row);
152 Mockito.when(row.getLong("count")).thenReturn((long) 6);
154 Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
155 assertEquals(6, cassaLockStore.getLockQueueSize("keyspace3", "table3", "key3"));
156 } catch (MusicServiceException | MusicQueryException e) {
157 // TODO Auto-generated catch block
163 public void testPeekLockQueue() {
164 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
165 Row row = Mockito.mock(Row.class);
166 Mockito.when(row.isNull("lockReference")).thenReturn(false);
167 Mockito.when(row.getLong("lockReference")).thenReturn((long) 6);
168 Mockito.when(row.getString("createTime")).thenReturn("2019-11-13T15:05:45+00:00");
169 Mockito.when(row.getString("acquireTime")).thenReturn("2019-11-13T15:05:45+00:00");
170 Mockito.when(row.isNull("lockReference")).thenReturn(false);
171 Mockito.when(resultSetMock.one()).thenReturn(row);
173 Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
174 assertEquals("6", cassaLockStore.peekLockQueue("keyspace4", "table4", "key4").getLockRef());
175 } catch (MusicServiceException | MusicQueryException e) {
176 // TODO Auto-generated catch block
182 public void testGetCurrentLockHolders() {
183 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
184 Iterator<Row> iterator = Mockito.mock(Iterator.class);
185 Mockito.when(iterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
186 Row row = Mockito.mock(Row.class);
187 Mockito.when(row.getLong("lockReference")).thenReturn((long) 5).thenReturn((long) 5);
188 Mockito.when(row.get("lockType", LockType.class)).thenReturn(LockType.WRITE);
189 Mockito.when(iterator.next()).thenReturn(row).thenReturn(row);
190 Mockito.when(resultSetMock.iterator()).thenReturn(iterator);
192 Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
193 assertEquals("$keyspace5.table5.key5$5", cassaLockStore.getCurrentLockHolders("keyspace5", "table5", "key5").get(1));
194 } catch (MusicServiceException | MusicQueryException e) {
195 // TODO Auto-generated catch block
201 public void testIsLockOwner() {
202 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
203 Iterator<Row> iterator = Mockito.mock(Iterator.class);
204 Mockito.when(iterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
205 Row row = Mockito.mock(Row.class);
206 Mockito.when(row.getLong("lockReference")).thenReturn((long) 5);
207 Mockito.when(row.get("lockType", LockType.class)).thenReturn(LockType.WRITE);
208 Mockito.when(iterator.next()).thenReturn(row).thenReturn(row);
209 Mockito.when(resultSetMock.iterator()).thenReturn(iterator);
211 Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
212 assertEquals(true, cassaLockStore.isLockOwner("keyspace5", "table5", "key5", "5"));
213 } catch (MusicServiceException | MusicQueryException e) {
214 // TODO Auto-generated catch block
220 public void testGetLockInfo() {
221 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
222 Row row = Mockito.mock(Row.class);
223 Mockito.when(row.isNull("lockReference")).thenReturn(false);
224 Mockito.when(row.getLong("lockReference")).thenReturn((long) 6);
225 Mockito.when(row.getString("createTime")).thenReturn("2019-11-13T15:05:45+00:00");
226 Mockito.when(row.getString("acquireTime")).thenReturn("2019-11-13T15:05:45+00:00");
227 LockType locktype = Mockito.mock(LockType.class);
228 Mockito.when(row.get("lockType", LockType.class)).thenReturn(locktype);
229 Mockito.when(row.getString("owner")).thenReturn("owner6");
230 Mockito.when(resultSetMock.one()).thenReturn(row);
233 Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
234 CassaLockStore csLockStore = Mockito.spy(cassaLockStore);
235 Mockito.doReturn(true).when(csLockStore).isLockOwner(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
236 assertEquals("6", csLockStore.getLockInfo("keyspace6", "table6", "key6", "6").getLockRef());
237 } catch (MusicServiceException | MusicQueryException e) {
238 // TODO Auto-generated catch block
244 public void testCheckForDeadlock() {
245 DeadlockDetectionUtil ddu = Mockito.mock(DeadlockDetectionUtil.class);
246 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
247 Iterator<Row> it = Mockito.mock(Iterator.class);
248 Row row = Mockito.mock(Row.class);
249 Mockito.when(it.hasNext()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
250 Mockito.when(row.getString("key")).thenReturn("key8");
251 Mockito.when(row.getString("owner")).thenReturn("owner8");
252 Mockito.when(row.getString("acquiretime")).thenReturn("1");
253 Mockito.when(it.next()).thenReturn(row).thenReturn(row).thenReturn(row);
254 Mockito.when(resultSetMock.iterator()).thenReturn(it);
255 CassaLockStore csLockStore = Mockito.spy(cassaLockStore);
256 Mockito.doReturn(ddu).when(csLockStore).getDeadlockDetectionUtil();
257 Mockito.when(ddu.checkForDeadlock(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(true);
259 Mockito.when(dsHandle.executeLocalQuorumConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
261 cassaLockStore.checkForDeadlock("keyspace8", "table8", "lockName8", LockType.WRITE, "owner8", true));
262 } catch (MusicServiceException | MusicQueryException e) {
263 // TODO Auto-generated catch block
269 public void testGetAllLocksForOwner() {
270 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
271 Iterator<Row> it = Mockito.mock(Iterator.class);
272 Mockito.when(it.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
273 Row row = Mockito.mock(Row.class);
274 Mockito.when(row.getString("key")).thenReturn("key10");
275 Mockito.when(row.getLong("lockreference")).thenReturn((long) 10);
276 Mockito.when(it.next()).thenReturn(row);
277 Mockito.when(resultSetMock.iterator()).thenReturn(it);
279 Mockito.when(dsHandle.executeQuorumConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
280 assertEquals("key10$10", cassaLockStore.getAllLocksForOwner("owneer10", "keyspace10", "table10").get(1));
281 } catch (MusicServiceException | MusicQueryException e) {
282 // TODO Auto-generated catch block
288 public void testDequeueLockRef() throws Exception {
289 cassaLockStore.deQueueLockRef("keyspace1", "table1", "key6", "6", 2);
291 // note only expecting 1 call to this instance, expecting it to succeed
292 Mockito.verify(dsHandle, Mockito.times(1)).executePut(Mockito.any(), Mockito.anyString());
296 public void testDequeueLockRefWriteTimeout() throws Exception {
299 Mockito.when(dsHandle.executePut(Mockito.any(), Mockito.anyString()))
300 .thenThrow(new MusicServiceException("Cassandra timeout during..."));
301 cassaLockStore.deQueueLockRef("keyspace1", "table1", "key6", "6", retryCount);
303 // Should never reach here
305 } catch (MusicServiceException | MusicQueryException | MusicLockingException e) {
306 // should throw an error
309 Mockito.verify(dsHandle, Mockito.times(retryCount)).executePut(Mockito.any(), Mockito.anyString());