1 /*******************************************************************************
2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2018 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 * ====================================================================
21 *******************************************************************************/
22 package org.onap.music.service.impl;
24 import static org.junit.Assert.*;
25 import java.util.ArrayList;
26 import java.util.List;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.junit.MockitoJUnitRunner;
33 import org.onap.music.datastore.PreparedQueryObject;
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.lockingservice.cassandra.CassaLockStore;
38 import org.onap.music.lockingservice.cassandra.CassaLockStore.LockObject;
39 import org.onap.music.lockingservice.cassandra.LockType;
40 import org.onap.music.main.MusicUtil;
41 import org.onap.music.main.ResultType;
42 import org.onap.music.main.ReturnType;
44 @RunWith(MockitoJUnitRunner.class)
45 public class MusicCassaCoreTest {
48 private CassaLockStore mLockHandle;
53 public void before() {
54 core = MusicCassaCore.getInstance();
55 MusicCassaCore.setmLockHandle(mLockHandle);
59 public void testGetmLockHandle() {
60 assertEquals(mLockHandle, MusicCassaCore.getmLockHandle());
64 public void testSetmLockHandle() {
65 CassaLockStore m2 = Mockito.mock(CassaLockStore.class);
66 MusicCassaCore.setmLockHandle(m2);
67 assertEquals(m2, MusicCassaCore.getmLockHandle());
68 //revert back to original handle
69 MusicCassaCore.setmLockHandle(mLockHandle);
73 public void testGetInstance() {
74 assertEquals(core, MusicCassaCore.getInstance());
78 public void testGetLockingServiceHandle() {
79 assertEquals(mLockHandle, MusicCassaCore.getmLockHandle());
83 public void testCreateLockReferenceAtomicString() throws Exception {
84 String fullyQualifiedKey = "keyspace.table.lockName";
85 Mockito.when(mLockHandle.genLockRefandEnQueue("keyspace", "table", "lockName", LockType.WRITE, null))
86 .thenReturn("lockReturned");
88 String lockRef = core.createLockReferenceAtomic(fullyQualifiedKey);
90 Mockito.verify(mLockHandle).genLockRefandEnQueue("keyspace", "table", "lockName", LockType.WRITE, null);
91 assertEquals("lockReturned", lockRef);
95 public void testCreateLockReferenceStringString() throws Exception {
96 String fullyQualifiedKey = "keyspace.table.lockName";
97 String owner = "owner1";
98 Mockito.when(mLockHandle.genLockRefandEnQueue("keyspace", "table", "lockName", LockType.WRITE, owner))
99 .thenReturn("lockReturned");
101 String lockRef = core.createLockReference(fullyQualifiedKey, owner);
103 Mockito.verify(mLockHandle).genLockRefandEnQueue("keyspace", "table", "lockName", LockType.WRITE, owner);
104 assertEquals("lockReturned", lockRef);
108 public void testCreateLockReferenceAtomicStringLockType() throws Exception {
109 String fullyQualifiedKey = "keyspace.table.lockName";
110 Mockito.when(mLockHandle.genLockRefandEnQueue("keyspace", "table", "lockName", LockType.READ, null))
111 .thenReturn("lockReturned");
113 String lockRef = core.createLockReferenceAtomic(fullyQualifiedKey, LockType.READ);
115 Mockito.verify(mLockHandle).genLockRefandEnQueue("keyspace", "table", "lockName", LockType.READ, null);
116 assertEquals("lockReturned", lockRef);
120 public void testCreateLockReferenceStringLockTypeString() throws Exception {
121 String fullyQualifiedKey = "keyspace.table.lockName";
122 String owner = "owner1";
123 Mockito.when(mLockHandle.genLockRefandEnQueue("keyspace", "table", "lockName", LockType.READ, owner))
124 .thenReturn("lockReturned");
126 String lockRef = core.createLockReference(fullyQualifiedKey, LockType.READ, owner);
128 Mockito.verify(mLockHandle).genLockRefandEnQueue("keyspace", "table", "lockName", LockType.READ, owner);
129 assertEquals("lockReturned", lockRef);
133 public void testPromoteLock() throws Exception {
134 String lockId = "$keyspace.table.lockName$1";
135 Mockito.when(mLockHandle.promoteLock("keyspace", "table", "lockName", "1"))
136 .thenReturn(new ReturnType(ResultType.SUCCESS, "Lock Promoted"));
138 ReturnType rt = core.promoteLock(lockId);
139 assertEquals(ResultType.SUCCESS, rt.getResult());
140 assertEquals("Lock Promoted", rt.getMessage());
144 public void testAcquireLockWithLease() throws Exception {
145 String fullyQualifiedKey = "keyspace.table.lockName";
146 String lockId = "$keyspace.table.lockName$1";
147 long leasePeriod = 1000;
148 String currTime = String.valueOf(System.currentTimeMillis());
149 Mockito.when(mLockHandle.peekLockQueue("keyspace", "table", "lockName"))
150 .thenReturn(mLockHandle.new LockObject(true, lockId, currTime, currTime, LockType.WRITE, null));
151 Mockito.when(mLockHandle.getLockInfo("keyspace", "table", "lockName", "1"))
152 .thenReturn(mLockHandle.new LockObject(false, lockId, null, null, LockType.WRITE, null));
154 ReturnType rt = core.acquireLockWithLease(fullyQualifiedKey, lockId, leasePeriod);
155 assertEquals(ResultType.FAILURE, rt.getResult());
159 public void testAcquireLock() throws Exception {
160 String fullyQualifiedKey = "keyspace.table.lockName";
161 String lockId = "$keyspace.table.lockName$1";
162 Mockito.when(mLockHandle.getLockInfo("keyspace", "table", "lockName", "1"))
163 .thenReturn(mLockHandle.new LockObject(false, lockId, null, null, LockType.WRITE, null));
165 ReturnType rt = core.acquireLock(fullyQualifiedKey, lockId);
167 assertEquals(ResultType.FAILURE, rt.getResult());
168 Mockito.verify(mLockHandle).getLockInfo("keyspace", "table", "lockName", "1");
169 /*TODO: if we successfully acquire the lock we hit an error by trying to read MusicDatastoreHandle */
173 public void testWhoseTurnIsIt() throws Exception {
174 String fullyQualifiedKey = "keyspace.table.lockName";
175 Mockito.when(mLockHandle.peekLockQueue("keyspace", "table", "lockName"))
176 .thenReturn(mLockHandle.new LockObject(true, "1", "", "", LockType.WRITE, null));
178 String topOfQ = core.whoseTurnIsIt(fullyQualifiedKey);
179 System.out.println(topOfQ);
181 assertEquals("$"+fullyQualifiedKey+"$1", topOfQ);
185 public void testGetCurrentLockHolders() throws Exception {
186 String fullyQualifiedKey = "keyspace.table.lockName";
187 List<String> currentHolders = new ArrayList<>();
188 currentHolders.add("$"+fullyQualifiedKey+"$1");
189 currentHolders.add("$"+fullyQualifiedKey+"$2");
190 Mockito.when(mLockHandle.getCurrentLockHolders("keyspace", "table", "lockName"))
191 .thenReturn(currentHolders);
193 List<String> holders = core.getCurrentLockHolders(fullyQualifiedKey);
195 assertTrue(currentHolders.containsAll(holders) && holders.containsAll(currentHolders));
199 public void testGetLockNameFromId() {
200 String lockId = "$keyspace.table.lockName$1";
201 assertEquals("keyspace.table.lockName", core.getLockNameFromId(lockId));
205 public void testDestroyLockRefString() throws Exception {
206 String lockId = "$keyspace.table.lockName$1";
208 core.destroyLockRef(lockId);
209 Mockito.verify(mLockHandle).deQueueLockRef("keyspace", "table", "lockName", "1", MusicUtil.getRetryCount());
213 public void testDestroyLockRefStringString() throws Exception {
214 String fullyQualifiedKey = "keyspace.table.lockName";
215 String lockReference = "1";
217 core.destroyLockRef(fullyQualifiedKey, lockReference);
218 Mockito.verify(mLockHandle).deQueueLockRef("keyspace", "table", "lockName", "1", MusicUtil.getRetryCount());
222 public void testReleaseLock() throws Exception {
223 String lockId = "$keyspace.table.lockName$1";
225 core.releaseLock(lockId, true);
227 Mockito.verify(mLockHandle).deQueueLockRef("keyspace", "table", "lockName", "1", MusicUtil.getRetryCount());
231 public void testVoluntaryReleaseLock() throws Exception {
232 String fullyQualifiedKey = "keyspace.table.lockName";
233 String lockReference = "1";
235 core.voluntaryReleaseLock(fullyQualifiedKey, lockReference);
237 Mockito.verify(mLockHandle).deQueueLockRef("keyspace", "table", "lockName", "1", MusicUtil.getRetryCount());
241 public void testReleaseAllLocksForOwner() throws Exception {
242 List<String> ownersLocks = new ArrayList<>();
243 ownersLocks.add("lockName$1");
244 ownersLocks.add("lockName$2");
245 Mockito.when(mLockHandle.getAllLocksForOwner("ownerId", "keyspace", "table"))
246 .thenReturn(ownersLocks);
248 List<String> locksReleased = core.releaseAllLocksForOwner("ownerId", "keyspace", "table");
250 Mockito.verify(mLockHandle).deQueueLockRef("keyspace", "table", "lockName", "1", MusicUtil.getRetryCount());
251 Mockito.verify(mLockHandle).deQueueLockRef("keyspace", "table", "lockName", "2", MusicUtil.getRetryCount());
252 assertTrue(ownersLocks.containsAll(locksReleased) && locksReleased.containsAll(ownersLocks));
257 public void testValidateLock() {
258 String lockId = "$keyspace.table.lockName$1";
260 assertFalse(core.validateLock(lockId).containsKey("Error"));
264 public void testGetLockQueue() throws Exception {
265 String fullyQualifiedKey = "keyspace.table.lockName";
266 List<String> myList = new ArrayList<>();
267 Mockito.when(mLockHandle.getLockQueue("keyspace", "table", "lockName"))
269 List<String> theirList = core.getLockQueue(fullyQualifiedKey);
271 assertEquals(myList, theirList);
275 public void testGetLockQueueSize() throws Exception {
276 String fullyQualifiedKey = "keyspace.table.lockName";
277 Mockito.when(mLockHandle.getLockQueueSize("keyspace", "table", "lockName"))
278 .thenReturn((long) 23);
279 long theirSize = core.getLockQueueSize(fullyQualifiedKey);
281 assertEquals(23, theirSize);