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;
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;
39 import com.datastax.driver.core.ResultSet;
40 import com.datastax.driver.core.Row;
42 public class CassaLockStoreTest {
44 private CassaLockStore cassaLockStore;
45 private CassaLockStore.LockObject lockObject;
46 private MusicDataStore dsHandle;
50 dsHandle = Mockito.mock(MusicDataStore.class);
51 cassaLockStore = new CassaLockStore(dsHandle);
52 lockObject = cassaLockStore.new LockObject(false, null, null, null, null, null);
56 public void testLockOwner() {
57 lockObject.setIsLockOwner(true);
58 assertEquals(true, lockObject.getIsLockOwner());
60 lockObject.setIsLockOwner(false);
61 assertEquals(false, lockObject.getIsLockOwner());
65 public void testAcquireTime() {
66 lockObject.setAcquireTime("2019-11-11T15:42:12+00:00");
67 assertEquals("2019-11-11T15:42:12+00:00", lockObject.getAcquireTime());
71 public void testCreateTime() {
72 lockObject.setCreateTime("2019-11-11T15:43:44+00:00");
73 assertEquals("2019-11-11T15:43:44+00:00", lockObject.getCreateTime());
77 public void testLockRef() {
78 lockObject.setLockRef("LockReference");
79 assertEquals("LockReference", lockObject.getLockRef());
83 public void testLockType() {
84 lockObject.setLocktype(LockType.READ);
85 assertEquals(LockType.READ, lockObject.getLocktype());
89 public void testOwner() {
90 lockObject.setOwner("Owner");
91 assertEquals("Owner", lockObject.getOwner());
95 public void testCreateLockQueue() {
97 Mockito.when(dsHandle.executePut(Mockito.any(), Mockito.any())).thenReturn(true);
98 assertEquals(true, cassaLockStore.createLockQueue("keyspace1", "table1"));
99 } catch (MusicServiceException | MusicQueryException e) {
100 // TODO Auto-generated catch block
106 public void testGenLockRefandEnQueue() {
107 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
108 List<Row> latestGuardRow = Mockito.mock(List.class);
109 Mockito.when(latestGuardRow.isEmpty()).thenReturn(false);
110 Row row = Mockito.mock(Row.class);
111 Mockito.when(latestGuardRow.get(0)).thenReturn(row);
112 Mockito.when(row.getLong(0)).thenReturn((long) 4);
113 Mockito.when(resultSetMock.all()).thenReturn(latestGuardRow);
115 Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
116 Mockito.when(dsHandle.executePut(Mockito.any(), Mockito.any())).thenReturn(true);
117 assertEquals("$keyspace2.table2.lockName2$5",
118 cassaLockStore.genLockRefandEnQueue("keyspace2", "table2", "lockName2", LockType.READ, "owner2"));
119 } catch (MusicServiceException | MusicQueryException | MusicLockingException e) {
120 // TODO Auto-generated catch block
126 public void testGetLockQueue() {
127 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
128 Iterator<Row> iterator = Mockito.mock(Iterator.class);
129 Mockito.when(iterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
130 Row row = Mockito.mock(Row.class);
131 Mockito.when(row.getLong("lockReference")).thenReturn((long)1).thenReturn((long)2).thenReturn((long)3);
132 Mockito.when(row.get("lockType", LockType.class)).thenReturn(LockType.WRITE).thenReturn(LockType.WRITE).thenReturn(LockType.WRITE);
133 Mockito.when(iterator.next()).thenReturn(row).thenReturn(row).thenReturn(row);
134 Mockito.when(resultSetMock.iterator()).thenReturn(iterator);
137 Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
138 assertEquals("2", cassaLockStore.getLockQueue("keyspace2", "table2", "key2").get(1));
139 } catch (MusicServiceException | MusicQueryException e) {
140 // TODO Auto-generated catch block
146 public void testGetLockQueueSize() {
147 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
148 Row row = Mockito.mock(Row.class);
149 Mockito.when(resultSetMock.one()).thenReturn(row);
150 Mockito.when(row.getLong("count")).thenReturn((long) 6);
152 Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
153 assertEquals(6, cassaLockStore.getLockQueueSize("keyspace3", "table3", "key3"));
154 } catch (MusicServiceException | MusicQueryException e) {
155 // TODO Auto-generated catch block
161 public void testPeekLockQueue() {
162 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
163 Row row = Mockito.mock(Row.class);
164 Mockito.when(row.isNull("lockReference")).thenReturn(false);
165 Mockito.when(row.getLong("lockReference")).thenReturn((long) 6);
166 Mockito.when(row.getString("createTime")).thenReturn("2019-11-13T15:05:45+00:00");
167 Mockito.when(row.getString("acquireTime")).thenReturn("2019-11-13T15:05:45+00:00");
168 Mockito.when(row.isNull("lockReference")).thenReturn(false);
169 Mockito.when(resultSetMock.one()).thenReturn(row);
171 Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
172 assertEquals("6", cassaLockStore.peekLockQueue("keyspace4", "table4", "key4").getLockRef());
173 } catch (MusicServiceException | MusicQueryException e) {
174 // TODO Auto-generated catch block
180 public void testGetCurrentLockHolders() {
181 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
182 Iterator<Row> iterator = Mockito.mock(Iterator.class);
183 Mockito.when(iterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
184 Row row = Mockito.mock(Row.class);
185 Mockito.when(row.getLong("lockReference")).thenReturn((long) 5).thenReturn((long) 5);
186 Mockito.when(row.get("lockType", LockType.class)).thenReturn(LockType.WRITE);
187 Mockito.when(iterator.next()).thenReturn(row).thenReturn(row);
188 Mockito.when(resultSetMock.iterator()).thenReturn(iterator);
190 Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
191 assertEquals("$keyspace5.table5.key5$5", cassaLockStore.getCurrentLockHolders("keyspace5", "table5", "key5").get(1));
192 } catch (MusicServiceException | MusicQueryException e) {
193 // TODO Auto-generated catch block
199 public void testIsLockOwner() {
200 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
201 Iterator<Row> iterator = Mockito.mock(Iterator.class);
202 Mockito.when(iterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
203 Row row = Mockito.mock(Row.class);
204 Mockito.when(row.getLong("lockReference")).thenReturn((long) 5);
205 Mockito.when(row.get("lockType", LockType.class)).thenReturn(LockType.WRITE);
206 Mockito.when(iterator.next()).thenReturn(row).thenReturn(row);
207 Mockito.when(resultSetMock.iterator()).thenReturn(iterator);
209 Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
210 assertEquals(true, cassaLockStore.isLockOwner("keyspace5", "table5", "key5", "5"));
211 } catch (MusicServiceException | MusicQueryException e) {
212 // TODO Auto-generated catch block
218 public void testGetLockInfo() {
219 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
220 Row row = Mockito.mock(Row.class);
221 Mockito.when(row.isNull("lockReference")).thenReturn(false);
222 Mockito.when(row.getLong("lockReference")).thenReturn((long) 6);
223 Mockito.when(row.getString("createTime")).thenReturn("2019-11-13T15:05:45+00:00");
224 Mockito.when(row.getString("acquireTime")).thenReturn("2019-11-13T15:05:45+00:00");
225 LockType locktype = Mockito.mock(LockType.class);
226 Mockito.when(row.get("lockType", LockType.class)).thenReturn(locktype);
227 Mockito.when(row.getString("owner")).thenReturn("owner6");
228 Mockito.when(resultSetMock.one()).thenReturn(row);
231 Mockito.when(dsHandle.executeOneConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
232 CassaLockStore csLockStore = Mockito.spy(cassaLockStore);
233 Mockito.doReturn(true).when(csLockStore).isLockOwner(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
234 assertEquals("6", csLockStore.getLockInfo("keyspace6", "table6", "key6", "6").getLockRef());
235 } catch (MusicServiceException | MusicQueryException e) {
236 // TODO Auto-generated catch block
242 public void testCheckForDeadlock() {
243 DeadlockDetectionUtil ddu = Mockito.mock(DeadlockDetectionUtil.class);
244 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
245 Iterator<Row> it = Mockito.mock(Iterator.class);
246 Row row = Mockito.mock(Row.class);
247 Mockito.when(it.hasNext()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
248 Mockito.when(row.getString("key")).thenReturn("key8");
249 Mockito.when(row.getString("owner")).thenReturn("owner8");
250 Mockito.when(row.getString("acquiretime")).thenReturn("1");
251 Mockito.when(it.next()).thenReturn(row).thenReturn(row).thenReturn(row);
252 Mockito.when(resultSetMock.iterator()).thenReturn(it);
253 CassaLockStore csLockStore = Mockito.spy(cassaLockStore);
254 Mockito.doReturn(ddu).when(csLockStore).getDeadlockDetectionUtil();
255 Mockito.when(ddu.checkForDeadlock(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(true);
257 Mockito.when(dsHandle.executeLocalQuorumConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
259 cassaLockStore.checkForDeadlock("keyspace8", "table8", "lockName8", LockType.WRITE, "owner8", true));
260 } catch (MusicServiceException | MusicQueryException e) {
261 // TODO Auto-generated catch block
267 public void testGetAllLocksForOwner() {
268 ResultSet resultSetMock = Mockito.mock(ResultSet.class);
269 Iterator<Row> it = Mockito.mock(Iterator.class);
270 Mockito.when(it.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
271 Row row = Mockito.mock(Row.class);
272 Mockito.when(row.getString("key")).thenReturn("key10");
273 Mockito.when(row.getLong("lockreference")).thenReturn((long) 10);
274 Mockito.when(it.next()).thenReturn(row);
275 Mockito.when(resultSetMock.iterator()).thenReturn(it);
277 Mockito.when(dsHandle.executeQuorumConsistencyGet(Mockito.any())).thenReturn(resultSetMock);
278 assertEquals("key10$10", cassaLockStore.getAllLocksForOwner("owneer10", "keyspace10", "table10").get(1));
279 } catch (MusicServiceException | MusicQueryException e) {
280 // TODO Auto-generated catch block