2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 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 * ====================================================================
22 package org.onap.music.unittests;
24 import static org.junit.Assert.*;
25 import static org.onap.music.main.MusicCore.mDstoreHandle;
26 import static org.onap.music.main.MusicCore.mLockHandle;
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.runners.MockitoJUnitRunner;
33 import org.onap.music.exceptions.MusicQueryException;
34 import org.onap.music.exceptions.MusicServiceException;
35 import org.onap.music.lockingservice.MusicLockState;
36 import org.onap.music.lockingservice.MusicLockingService;
37 import org.onap.music.lockingservice.MusicLockState.LockStatus;
38 import org.onap.music.main.MusicCore;
39 import org.onap.music.main.ResultType;
40 import org.onap.music.main.ReturnType;
41 import org.onap.music.main.MusicCore.Condition;
42 import org.onap.music.datastore.MusicDataStore;
43 import org.onap.music.datastore.PreparedQueryObject;
44 import com.datastax.driver.core.ResultSet;
46 @RunWith(MockitoJUnitRunner.class)
47 public class TestMusicCore {
50 private Condition condition;
56 private PreparedQueryObject preparedQueryObject;
60 mLockHandle = Mockito.mock(MusicLockingService.class);
65 public void testCreateLockReferenceforvalidlock() {
66 Mockito.when(mLockHandle.createLockId("/" + "test")).thenReturn("lock");
67 String lockId = MusicCore.createLockReference("test");
68 assertEquals("lock", lockId);
69 Mockito.verify(mLockHandle).createLockId("/" + "test");
73 public void testIsTableOrKeySpaceLock() {
74 Boolean result = MusicCore.isTableOrKeySpaceLock("ks1.tn1");
79 public void testIsTableOrKeySpaceLockwithPrimarykey() {
80 Boolean result = MusicCore.isTableOrKeySpaceLock("ks1.tn1.pk1");
85 public void testGetMusicLockState() {
86 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
87 Mockito.when(mLockHandle.getLockState("ks1.tb1.pk1")).thenReturn(musicLockState);
88 MusicLockState mls = MusicCore.getMusicLockState("ks1.tb1.pk1");
89 assertEquals(musicLockState, mls);
90 Mockito.verify(mLockHandle).getLockState("ks1.tb1.pk1");
94 public void testAcquireLockifisMyTurnTrue() {
95 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
96 Boolean lock = MusicCore.acquireLock("ks1.tn1", "id1");
98 Mockito.verify(mLockHandle).isMyTurn("id1");
102 public void testAcquireLockifisMyTurnFalse() {
103 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false);
104 Boolean lock = MusicCore.acquireLock("ks1.ts1", "id1");
106 Mockito.verify(mLockHandle).isMyTurn("id1");
110 public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockTrue() {
111 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
112 Boolean lock = MusicCore.acquireLock("ks1.tn1", "id1");
114 Mockito.verify(mLockHandle).isMyTurn("id1");
118 public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockFalseandHaveLock() {
119 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
120 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
121 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
122 Boolean lock = MusicCore.acquireLock("ks1.tn1.pk1", "id1");
124 Mockito.verify(mLockHandle).isMyTurn("id1");
125 Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1");
129 public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockFalseandDontHaveLock() {
130 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id2");
131 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
132 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
133 Boolean lock = MusicCore.acquireLock("ks1.tn1.pk1", "id1");
135 Mockito.verify(mLockHandle).isMyTurn("id1");
136 Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1");
140 public void testAcquireLockWithLeasewithLockStatusLOCKED() {
141 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
142 ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes");
143 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
144 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
145 ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000);
146 assertEquals(expectedResult.getResult(), actualResult.getResult());
147 Mockito.verify(mLockHandle).isMyTurn("id1");
148 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1");
152 public void testAcquireLockWithLeasewithLockStatusUNLOCKED() {
153 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
154 ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes");
155 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
156 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
157 ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000);
158 assertEquals(expectedResult.getResult(), actualResult.getResult());
159 Mockito.verify(mLockHandle).isMyTurn("id1");
160 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1");
165 public void testAcquireLockWithLeaseIfNotMyTurn() {
166 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
167 ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure");
168 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
169 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false);
170 ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000);
171 assertEquals(expectedResult.getResult(), actualResult.getResult());
172 Mockito.verify(mLockHandle).isMyTurn("id1");
173 Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1");
177 * @Test public void testQuorumGet() { mDstoreHandle = Mockito.mock(MusicDataStore.class); rs =
178 * Mockito.mock(ResultSet.class);
179 * Mockito.when(mDstoreHandle.executeCriticalGet("qu1")).thenReturn(rs); ResultSet rs1 =
180 * MusicCore.quorumGet("qu1"); assertNotNull(rs1);
181 * Mockito.verify(mDstoreHandle).executeCriticalGet("qu1");
187 public void testGetLockNameFromId() {
188 String lockname = MusicCore.getLockNameFromId("lockName$id");
189 assertEquals("lockName", lockname);
193 public void testDestroyLockRef() {
194 Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1");
195 MusicCore.destroyLockRef("id1");
196 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1");
200 public void testreleaseLockwithvoluntaryReleaseTrue() {
201 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2");
202 Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1");
203 MusicLockState musicLockState1 = MusicCore.releaseLock("id1", true);
204 assertEquals(musicLockState.getLockStatus(), musicLockState1.getLockStatus());
205 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1");
209 public void testreleaseLockwithvoluntaryReleaseFalse() {
210 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2");
211 Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1");
212 MusicLockState musicLockState1 = MusicCore.releaseLock("id1", false);
213 assertEquals(musicLockState.getLockStatus(), musicLockState1.getLockStatus());
214 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1");
218 public void testDeleteLock() {
219 Mockito.doNothing().when(mLockHandle).deleteLock("/" + "id1");
220 MusicCore.deleteLock("id1");
221 Mockito.verify(mLockHandle).deleteLock("/" + "id1");
225 * @Test public void testNonKeyRelatedPut() throws Exception { mDstoreHandle =
226 * Mockito.mock(MusicDataStore.class); Mockito.when(mDstoreHandle.executePut("qu1",
227 * "consistency")).thenReturn(true); Boolean result = MusicCore.nonKeyRelatedPut("qu1",
228 * "consistency"); assertTrue(result); Mockito.verify(mDstoreHandle).executePut("qu1",
233 public void testEventualPutPreparedQuery() throws MusicServiceException, MusicQueryException {
234 mDstoreHandle = Mockito.mock(MusicDataStore.class);
235 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
236 ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes");
237 Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "eventual")).thenReturn(true);
238 ReturnType actualResult = MusicCore.eventualPut(preparedQueryObject);
239 assertEquals(expectedResult.getResult(), actualResult.getResult());
240 Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "eventual");
244 public void testEventualPutPreparedQuerywithResultFalse()
245 throws MusicServiceException, MusicQueryException {
246 mDstoreHandle = Mockito.mock(MusicDataStore.class);
247 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
248 ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure");
249 Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "eventual")).thenReturn(false);
250 ReturnType actualResult = MusicCore.eventualPut(preparedQueryObject);
251 assertEquals(expectedResult.getResult(), actualResult.getResult());
252 Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "eventual");
256 public void testCriticalPutPreparedQuerywithValidLockId()
257 throws MusicServiceException, MusicQueryException {
258 mDstoreHandle = Mockito.mock(MusicDataStore.class);
259 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
260 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
261 Mockito.when(condition.testCondition()).thenReturn(true);
262 ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes");
263 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
264 .thenReturn(musicLockState);
265 Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "critical")).thenReturn(true);
266 ReturnType returnType = MusicCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject,
268 assertEquals(expectedResult.getResult(), returnType.getResult());
269 Mockito.verify(condition).testCondition();
270 Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1");
271 Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "critical");
275 public void testCriticalPutPreparedQuerywithInvalidLockId() {
276 mDstoreHandle = Mockito.mock(MusicDataStore.class);
277 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
278 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2");
279 ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure");
280 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
281 .thenReturn(musicLockState);
282 ReturnType returnType = MusicCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject,
284 assertEquals(expectedResult.getResult(), returnType.getResult());
285 Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1");
289 public void testCriticalPutPreparedQuerywithvalidLockIdandTestConditionFalse() {
290 mDstoreHandle = Mockito.mock(MusicDataStore.class);
291 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
292 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
293 Mockito.when(condition.testCondition()).thenReturn(false);
294 ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure");
295 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
296 .thenReturn(musicLockState);
297 ReturnType returnType = MusicCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject,
299 assertEquals(expectedResult.getResult(), returnType.getResult());
300 Mockito.verify(condition).testCondition();
301 Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1");
305 public void testNonKeyRelatedPutPreparedQuery() throws Exception {
306 mDstoreHandle = Mockito.mock(MusicDataStore.class);
307 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
308 Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "consistency")).thenReturn(true);
309 Boolean result = MusicCore.nonKeyRelatedPut(preparedQueryObject, "consistency");
311 Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "consistency");
315 public void testAtomicPutPreparedQuery() throws MusicServiceException, MusicQueryException {
316 mDstoreHandle = Mockito.mock(MusicDataStore.class);
317 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
318 Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1");
319 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
320 ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes");
321 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
322 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
323 Mockito.when(condition.testCondition()).thenReturn(true);
324 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
325 .thenReturn(musicLockState);
326 Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "critical")).thenReturn(true);
327 ReturnType returnType =
328 MusicCore.atomicPut("ks1", "tn1", "pk1", preparedQueryObject, condition);
329 assertEquals(expectedResult.getResult(), returnType.getResult());
330 Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
331 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1");
332 Mockito.verify(mLockHandle).isMyTurn("id1");
333 Mockito.verify(condition).testCondition();
334 Mockito.verify(mLockHandle, Mockito.atLeastOnce())
335 .getLockState("ks1" + "." + "tn1" + "." + "pk1");
336 Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "critical");
340 public void testAtomicPutPreparedQuerywithAcquireLockWithLeaseFalse() {
341 mDstoreHandle = Mockito.mock(MusicDataStore.class);
342 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
343 Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1");
344 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
345 ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure");
346 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
347 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false);
348 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
349 .thenReturn(musicLockState);
350 ReturnType returnType =
351 MusicCore.atomicPut("ks1", "tn1", "pk1", preparedQueryObject, condition);
352 assertEquals(expectedResult.getResult(), returnType.getResult());
353 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1");
354 Mockito.verify(mLockHandle).isMyTurn("id1");
355 Mockito.verify(mLockHandle, Mockito.atLeastOnce())
356 .getLockState("ks1" + "." + "tn1" + "." + "pk1");
357 Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
361 public void testAtomicGetPreparedQuery() throws MusicServiceException, MusicQueryException {
362 mDstoreHandle = Mockito.mock(MusicDataStore.class);
363 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
364 rs = Mockito.mock(ResultSet.class);
365 Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1");
366 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
367 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
368 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
369 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
370 .thenReturn(musicLockState);
371 Mockito.when(mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs);
372 ResultSet rs1 = MusicCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject);
374 Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
375 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1");
376 Mockito.verify(mLockHandle).isMyTurn("id1");
377 Mockito.verify(mLockHandle, Mockito.atLeastOnce())
378 .getLockState("ks1" + "." + "tn1" + "." + "pk1");
379 Mockito.verify(mDstoreHandle).executeCriticalGet(preparedQueryObject);
383 public void testAtomicGetPreparedQuerywithAcquireLockWithLeaseFalse()
384 throws MusicServiceException {
385 mDstoreHandle = Mockito.mock(MusicDataStore.class);
386 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
387 rs = Mockito.mock(ResultSet.class);
388 Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1");
389 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
390 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
391 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false);
392 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
393 .thenReturn(musicLockState);
394 ResultSet rs1 = MusicCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject);
396 Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
397 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1");
398 Mockito.verify(mLockHandle).isMyTurn("id1");
399 Mockito.verify(mLockHandle, Mockito.atLeastOnce())
400 .getLockState("ks1" + "." + "tn1" + "." + "pk1");
405 public void testGetPreparedQuery() throws MusicServiceException, MusicQueryException {
406 mDstoreHandle = Mockito.mock(MusicDataStore.class);
407 rs = Mockito.mock(ResultSet.class);
408 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
409 Mockito.when(mDstoreHandle.executeEventualGet(preparedQueryObject)).thenReturn(rs);
410 ResultSet rs1 = MusicCore.get(preparedQueryObject);
412 Mockito.verify(mDstoreHandle).executeEventualGet(preparedQueryObject);
417 public void testcriticalGetPreparedQuery() throws MusicServiceException, MusicQueryException {
418 mDstoreHandle = Mockito.mock(MusicDataStore.class);
419 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
420 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
421 rs = Mockito.mock(ResultSet.class);
422 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
423 .thenReturn(musicLockState);
424 Mockito.when(mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs);
425 ResultSet rs1 = MusicCore.criticalGet("ks1", "tn1", "pk1", preparedQueryObject, "id1");
427 Mockito.verify(mLockHandle, Mockito.atLeastOnce())
428 .getLockState("ks1" + "." + "tn1" + "." + "pk1");
429 Mockito.verify(mDstoreHandle).executeCriticalGet(preparedQueryObject);
433 public void testcriticalGetPreparedQuerywithInvalidLockId() throws MusicServiceException {
434 mDstoreHandle = Mockito.mock(MusicDataStore.class);
435 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
436 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2");
437 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
438 .thenReturn(musicLockState);
439 ResultSet rs1 = MusicCore.criticalGet("ks1", "tn1", "pk1", preparedQueryObject, "id1");
441 Mockito.verify(mLockHandle, Mockito.atLeastOnce())
442 .getLockState("ks1" + "." + "tn1" + "." + "pk1");