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 java.util.HashMap;
29 import org.apache.zookeeper.KeeperException.NoNodeException;
30 import org.junit.Before;
31 import org.junit.Ignore;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.runners.MockitoJUnitRunner;
37 import org.onap.music.exceptions.MusicLockingException;
38 import org.onap.music.exceptions.MusicQueryException;
39 import org.onap.music.exceptions.MusicServiceException;
40 import org.onap.music.lockingservice.MusicLockState;
41 import org.onap.music.lockingservice.MusicLockingService;
42 import org.onap.music.lockingservice.MusicLockState.LockStatus;
43 import org.onap.music.main.MusicCore;
44 import org.onap.music.main.MusicUtil;
45 import org.onap.music.main.ResultType;
46 import org.onap.music.main.ReturnType;
47 import org.onap.music.main.MusicCore.Condition;
48 import org.onap.music.datastore.MusicDataStore;
49 import org.onap.music.datastore.PreparedQueryObject;
50 import org.onap.music.eelf.logging.EELFLoggerDelegate;
51 import org.onap.music.eelf.logging.format.AppMessages;
52 import org.onap.music.eelf.logging.format.ErrorSeverity;
53 import org.onap.music.eelf.logging.format.ErrorTypes;
54 import com.att.eelf.exception.EELFException;
55 import com.datastax.driver.core.ResultSet;
56 import com.datastax.driver.core.Session;
58 @RunWith(MockitoJUnitRunner.class)
59 public class TestMusicCore {
62 private Condition condition;
68 private PreparedQueryObject preparedQueryObject;
71 private Session session;
75 mLockHandle = Mockito.mock(MusicLockingService.class);
80 public void testCreateLockReferenceforvalidlock() {
81 Mockito.when(mLockHandle.createLockId("/" + "test")).thenReturn("lock");
82 String lockId = MusicCore.createLockReference("test");
83 assertEquals("lock", lockId);
84 Mockito.verify(mLockHandle).createLockId("/" + "test");
89 public void testCreateLockReferencefornullname() {
90 Mockito.when(mLockHandle.createLockId("/" + "test")).thenReturn("lock");
91 String lockId = MusicCore.createLockReference("x"); //test");
92 //System.out.println("cjc exception lockhandle=" + mLockHandle+"lockid="+lockId );
93 assertNotEquals("lock", lockId);
94 //Mockito.verify(mLockHandle).createLockId("/" + "test");
98 public void testIsTableOrKeySpaceLock() {
99 Boolean result = MusicCore.isTableOrKeySpaceLock("ks1.tn1");
104 public void testIsTableOrKeySpaceLockwithPrimarykey() {
105 Boolean result = MusicCore.isTableOrKeySpaceLock("ks1.tn1.pk1");
110 public void testGetMusicLockState() throws MusicLockingException {
111 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
112 Mockito.when(mLockHandle.getLockState("ks1.tb1.pk1")).thenReturn(musicLockState);
113 MusicLockState mls = MusicCore.getMusicLockState("ks1.tb1.pk1");
114 assertEquals(musicLockState, mls);
115 Mockito.verify(mLockHandle).getLockState("ks1.tb1.pk1");
119 public void testAcquireLockifisMyTurnTrue() throws MusicLockingException {
120 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
121 ReturnType lock = MusicCore.acquireLock("ks1.tn1", "id1");
122 assertEquals(lock.getResult(), ResultType.SUCCESS);
123 Mockito.verify(mLockHandle).isMyTurn("id1");
127 public void testAcquireLockifisMyTurnFalse() throws MusicLockingException {
128 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false);
129 ReturnType lock = MusicCore.acquireLock("ks1.ts1", "id1");
130 assertEquals(lock.getResult(), ResultType.FAILURE);
131 Mockito.verify(mLockHandle).isMyTurn("id1");
135 public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockTrue() throws MusicLockingException {
136 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
137 ReturnType lock = MusicCore.acquireLock("ks1.tn1", "id1");
138 assertEquals(lock.getResult(), ResultType.SUCCESS);
139 Mockito.verify(mLockHandle).isMyTurn("id1");
143 public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockFalseandHaveLock() throws MusicLockingException {
144 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
145 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
146 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
147 ReturnType lock = MusicCore.acquireLock("ks1.tn1.pk1", "id1");
148 assertEquals(lock.getResult(), ResultType.SUCCESS);
149 Mockito.verify(mLockHandle).isMyTurn("id1");
150 Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1");
154 public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockFalseandDontHaveLock() throws MusicLockingException {
155 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id2");
156 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
157 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
158 ReturnType lock = MusicCore.acquireLock("ks1.tn1.pk1", "id1");
159 assertEquals(lock.getResult(), ResultType.SUCCESS);
160 Mockito.verify(mLockHandle).isMyTurn("id1");
161 Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1");
165 public void testAcquireLockifLockRefDoesntExist() throws MusicLockingException {
166 Mockito.when(mLockHandle.lockIdExists("bs1")).thenReturn(false);
167 ReturnType lock = MusicCore.acquireLock("ks1.ts1", "bs1");
168 assertEquals(lock.getResult(), ResultType.FAILURE);
169 assertEquals(lock.getMessage(), "Lockid doesn't exist");
170 Mockito.verify(mLockHandle).lockIdExists("bs1");
174 public void testAcquireLockWithLeasewithLease() throws MusicLockingException {
175 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
176 musicLockState.setLeasePeriod(0);
177 ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes");
178 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
179 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
180 ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000);
181 assertEquals(expectedResult.getResult(), actualResult.getResult());
182 Mockito.verify(mLockHandle).isMyTurn("id1");
183 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1");
187 public void testAcquireLockWithLeasewithException() throws MusicLockingException {
188 ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "failure");
189 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenThrow(new MusicLockingException());
190 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
191 ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000);
192 assertEquals(expectedResult.getResult(), actualResult.getResult());
193 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1");
197 public void testAcquireLockWithLeasewithLockStatusLOCKED() throws MusicLockingException {
198 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
199 ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes");
200 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
201 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
202 ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000);
203 assertEquals(expectedResult.getResult(), actualResult.getResult());
204 Mockito.verify(mLockHandle).isMyTurn("id1");
205 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1");
209 public void testAcquireLockWithLeasewithLockStatusUNLOCKED() throws MusicLockingException {
210 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
211 ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes");
212 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
213 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
214 ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000);
215 assertEquals(expectedResult.getResult(), actualResult.getResult());
216 Mockito.verify(mLockHandle).isMyTurn("id1");
217 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1");
222 public void testAcquireLockWithLeaseIfNotMyTurn() throws MusicLockingException {
223 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
224 ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure");
225 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
226 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false);
227 ReturnType actualResult = MusicCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000);
228 assertEquals(expectedResult.getResult(), actualResult.getResult());
229 Mockito.verify(mLockHandle).isMyTurn("id1");
230 Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1");
234 public void testQuorumGet() throws MusicServiceException, MusicQueryException {
235 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
236 mDstoreHandle = Mockito.mock(MusicDataStore.class);
237 rs = Mockito.mock(ResultSet.class);
238 session = Mockito.mock(Session.class);
239 Mockito.when(mDstoreHandle.getSession()).thenReturn(session);
240 Mockito.when(mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs);
241 ResultSet rs1 = MusicCore.quorumGet(preparedQueryObject);
246 public void testGetLockNameFromId() {
247 String lockname = MusicCore.getLockNameFromId("lockName$id");
248 assertEquals("lockName", lockname);
252 public void testDestroyLockRef() throws NoNodeException {
253 Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1");
254 MusicCore.destroyLockRef("id1");
255 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1");
259 public void testreleaseLockwithvoluntaryReleaseTrue() throws NoNodeException {
260 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2");
261 Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1");
262 MusicLockState musicLockState1 = MusicCore.releaseLock("id1", true);
263 assertEquals(musicLockState.getLockStatus(), musicLockState1.getLockStatus());
264 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1");
268 public void testreleaseLockwithvoluntaryReleaseFalse() throws NoNodeException {
269 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2");
270 Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1");
271 MusicLockState musicLockState1 = MusicCore.releaseLock("id1", false);
272 assertEquals(musicLockState.getLockStatus(), musicLockState1.getLockStatus());
273 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1");
277 public void testDeleteLock() throws MusicLockingException {
278 Mockito.doNothing().when(mLockHandle).deleteLock("/" + "id1");
279 MusicCore.deleteLock("id1");
280 Mockito.verify(mLockHandle).deleteLock("/" + "id1");
284 * @Test public void testNonKeyRelatedPut() throws Exception { mDstoreHandle =
285 * Mockito.mock(MusicDataStore.class); Mockito.when(mDstoreHandle.executePut("qu1",
286 * "consistency")).thenReturn(true); Boolean result = MusicCore.nonKeyRelatedPut("qu1",
287 * "consistency"); assertTrue(result); Mockito.verify(mDstoreHandle).executePut("qu1",
292 public void testEventualPutPreparedQuery() throws MusicServiceException, MusicQueryException {
293 mDstoreHandle = Mockito.mock(MusicDataStore.class);
294 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
295 ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes");
296 session = Mockito.mock(Session.class);
297 Mockito.when(mDstoreHandle.getSession()).thenReturn(session);
298 Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "eventual")).thenReturn(true);
299 ReturnType actualResult = MusicCore.eventualPut(preparedQueryObject);
300 assertEquals(expectedResult.getResult(), actualResult.getResult());
301 Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "eventual");
305 public void testEventualPutPreparedQuerywithResultFalse()
306 throws MusicServiceException, MusicQueryException {
307 mDstoreHandle = Mockito.mock(MusicDataStore.class);
308 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
309 ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure");
310 session = Mockito.mock(Session.class);
311 Mockito.when(mDstoreHandle.getSession()).thenReturn(session);
312 Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "eventual")).thenReturn(false);
313 ReturnType actualResult = MusicCore.eventualPut(preparedQueryObject);
314 assertEquals(expectedResult.getResult(), actualResult.getResult());
315 Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "eventual");
316 //Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, MusicUtil.EVENTUAL);
320 public void testCriticalPutPreparedQuerywithValidLockId()
322 mDstoreHandle = Mockito.mock(MusicDataStore.class);
323 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
324 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
325 Mockito.when(condition.testCondition()).thenReturn(true);
326 session = Mockito.mock(Session.class);
327 Mockito.when(mDstoreHandle.getSession()).thenReturn(session);
328 ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes");
329 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
330 .thenReturn(musicLockState);
331 Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "critical")).thenReturn(true);
332 ReturnType returnType = MusicCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject,
334 assertEquals(expectedResult.getResult(), returnType.getResult());
335 Mockito.verify(condition).testCondition();
336 Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1");
337 Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "critical");
341 public void testCriticalPutPreparedQuerywithInvalidLockId() throws MusicLockingException {
342 mDstoreHandle = Mockito.mock(MusicDataStore.class);
343 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
344 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2");
345 ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure");
346 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
347 .thenReturn(musicLockState);
348 ReturnType returnType = MusicCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject,
350 assertEquals(expectedResult.getResult(), returnType.getResult());
351 Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1");
355 public void testCriticalPutPreparedQuerywithvalidLockIdandTestConditionFalse() throws Exception {
356 mDstoreHandle = Mockito.mock(MusicDataStore.class);
357 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
358 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
359 Mockito.when(condition.testCondition()).thenReturn(false);
360 ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure");
361 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
362 .thenReturn(musicLockState);
363 ReturnType returnType = MusicCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject,
365 assertEquals(expectedResult.getResult(), returnType.getResult());
366 Mockito.verify(condition).testCondition();
367 Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1");
371 public void testNonKeyRelatedPutPreparedQuery() throws Exception {
372 mDstoreHandle = Mockito.mock(MusicDataStore.class);
373 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
374 session = Mockito.mock(Session.class);
375 Mockito.when(mDstoreHandle.getSession()).thenReturn(session);
376 Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "consistency")).thenReturn(true);
377 ResultType result = MusicCore.nonKeyRelatedPut(preparedQueryObject, "consistency");
378 assertEquals(ResultType.SUCCESS, result);
379 Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "consistency");
383 public void testAtomicPutPreparedQuery() throws Exception {
384 mDstoreHandle = Mockito.mock(MusicDataStore.class);
385 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
386 Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1");
387 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
388 ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes");
389 session = Mockito.mock(Session.class);
390 Mockito.when(mDstoreHandle.getSession()).thenReturn(session);
391 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
392 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
393 Mockito.when(condition.testCondition()).thenReturn(true);
394 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
395 .thenReturn(musicLockState);
396 Mockito.when(mDstoreHandle.executePut(preparedQueryObject, "critical")).thenReturn(true);
397 ReturnType returnType =
398 MusicCore.atomicPut("ks1", "tn1", "pk1", preparedQueryObject, condition);
399 assertEquals(expectedResult.getResult(), returnType.getResult());
400 Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
401 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1");
402 Mockito.verify(mLockHandle).isMyTurn("id1");
403 Mockito.verify(condition).testCondition();
404 Mockito.verify(mLockHandle, Mockito.atLeastOnce())
405 .getLockState("ks1" + "." + "tn1" + "." + "pk1");
406 Mockito.verify(mDstoreHandle).executePut(preparedQueryObject, "critical");
410 public void testAtomicPutPreparedQuerywithAcquireLockWithLeaseFalse() throws MusicLockingException {
411 mDstoreHandle = Mockito.mock(MusicDataStore.class);
412 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
413 Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1");
414 ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure");
415 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false);
416 ReturnType returnType =
417 MusicCore.atomicPut("ks1", "tn1", "pk1", preparedQueryObject, condition);
418 assertEquals(expectedResult.getResult(), returnType.getResult());
419 Mockito.verify(mLockHandle).isMyTurn("id1");
420 Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
424 public void testAtomicGetPreparedQuery() throws MusicServiceException, MusicQueryException, MusicLockingException {
425 mDstoreHandle = Mockito.mock(MusicDataStore.class);
426 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
427 rs = Mockito.mock(ResultSet.class);
428 session = Mockito.mock(Session.class);
429 Mockito.when(mDstoreHandle.getSession()).thenReturn(session);
430 Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1");
431 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
432 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
433 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
434 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
435 .thenReturn(musicLockState);
436 Mockito.when(mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs);
437 ResultSet rs1 = MusicCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject);
439 Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
440 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1");
441 Mockito.verify(mLockHandle).isMyTurn("id1");
442 Mockito.verify(mLockHandle, Mockito.atLeastOnce())
443 .getLockState("ks1" + "." + "tn1" + "." + "pk1");
444 Mockito.verify(mDstoreHandle).executeCriticalGet(preparedQueryObject);
448 public void testAtomicGetPreparedQuerywithAcquireLockWithLeaseFalse()
449 throws MusicServiceException, MusicLockingException {
450 mDstoreHandle = Mockito.mock(MusicDataStore.class);
451 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
452 rs = Mockito.mock(ResultSet.class);
453 Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1");
454 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false);
455 ResultSet rs1 = MusicCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject);
457 Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
458 Mockito.verify(mLockHandle).isMyTurn("id1");
462 public void testGetPreparedQuery() throws MusicServiceException, MusicQueryException {
463 mDstoreHandle = Mockito.mock(MusicDataStore.class);
464 rs = Mockito.mock(ResultSet.class);
465 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
466 session = Mockito.mock(Session.class);
467 Mockito.when(mDstoreHandle.getSession()).thenReturn(session);
468 Mockito.when(mDstoreHandle.executeEventualGet(preparedQueryObject)).thenReturn(rs);
469 ResultSet rs1 = MusicCore.get(preparedQueryObject);
471 Mockito.verify(mDstoreHandle).executeEventualGet(preparedQueryObject);
476 public void testcriticalGetPreparedQuery() throws MusicServiceException, MusicQueryException, MusicLockingException {
477 mDstoreHandle = Mockito.mock(MusicDataStore.class);
478 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
479 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
480 rs = Mockito.mock(ResultSet.class);
481 session = Mockito.mock(Session.class);
482 Mockito.when(mDstoreHandle.getSession()).thenReturn(session);
483 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
484 .thenReturn(musicLockState);
485 Mockito.when(mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs);
486 ResultSet rs1 = MusicCore.criticalGet("ks1", "tn1", "pk1", preparedQueryObject, "id1");
488 Mockito.verify(mLockHandle, Mockito.atLeastOnce())
489 .getLockState("ks1" + "." + "tn1" + "." + "pk1");
490 Mockito.verify(mDstoreHandle).executeCriticalGet(preparedQueryObject);
494 public void testcriticalGetPreparedQuerywithInvalidLockId() throws MusicServiceException, MusicLockingException {
495 mDstoreHandle = Mockito.mock(MusicDataStore.class);
496 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
497 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2");
498 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
499 .thenReturn(musicLockState);
500 ResultSet rs1 = MusicCore.criticalGet("ks1", "tn1", "pk1", preparedQueryObject, "id1");
502 Mockito.verify(mLockHandle, Mockito.atLeastOnce())
503 .getLockState("ks1" + "." + "tn1" + "." + "pk1");
507 public void testAtomicGetPreparedQuerywithDeleteLockWithLeaseFalse()
508 throws MusicServiceException, MusicLockingException {
509 mDstoreHandle = Mockito.mock(MusicDataStore.class);
510 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
511 rs = Mockito.mock(ResultSet.class);
512 Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1");
513 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false);
514 ResultSet rs1 = MusicCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject);
516 Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
517 Mockito.verify(mLockHandle).isMyTurn("id1");
521 public void testCondition() throws Exception {
522 //Condition conClass = Mockito.mock(Condition.class);
524 //Mockito.when(conClass.testCondition().thenReturn(ret);
525 Map<String, Object> conditionsx=null;
526 PreparedQueryObject selectQueryForTheRowx=null;
528 Condition con = new Condition(conditionsx,selectQueryForTheRowx);
529 assertTrue(con.testCondition());
530 } catch (Exception e) {
534 //getLockingServiceHandl
537 @Test(expected = MusicLockingException.class) //("Failed to aquire Locl store handle " + e))
538 public void testgetLockingServiceHandle() throws Exception {
539 // MusicLockingService mLockHandlex = Mockito.mock(MusicLockingService.class);
540 //MusicLockingService mLockHandlea = mLockHandle;
542 System.out.println("cjc 0 locking test n");
543 // Mockito.when(MusicCore.getLockingServiceHandle()).thenReturn(mLockHandle);
545 //System.out.println("cjc 0-1 locking test n");
546 MusicLockingService mLockHandlea = mLockHandle;
549 MusicLockingService mLockHandley=null; //MusicCore.getLockingServiceHandle();
550 Mockito.when(MusicCore.getLockingServiceHandle()).thenReturn(mLockHandley);
551 System.out.println("cjc locking test n");
552 mLockHandle=mLockHandlea;
557 @Test(expected = MusicServiceException.class)
558 public void testGetDSHandle() throws MusicServiceException, MusicQueryException {
559 // rs = Mockito.mock(ResultSet.class);
560 // session = Mockito.mock(Session.class);
561 //Mockito.when(mDstoreHandle.getSession()).thenReturn(session);
562 //Mockito.when(mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs);
564 MusicDataStore mDstoreHandlea = Mockito.mock(MusicDataStore.class);
565 //MusicUtil mMusicUtil=Mockito.mock(MusicUtil.class);
566 //System.out.println("cjc 0 getDsHandle");
567 Mockito.when(MusicCore.getDSHandle()).thenReturn(mDstoreHandlea);
568 // System.out.println("cjc localhost");
569 // Mockito.when(mMusicUtil.getMyCassaHost().equals("localhost")).thenReturn(null);
570 //System.out.println("cjc 1 localhost");
571 // mDstoreHandle = new MusicDataStore(MusicUtil.getMyCassaHost());
573 // mDstoreHandle = new MusicDataStore();
581 public void testGetDSHandleIp() throws MusicServiceException, MusicQueryException {
582 // rs = Mockito.mock(ResultSet.class);
583 // session = Mockito.mock(Session.class);
584 //Mockito.when(mDstoreHandle.getSession()).thenReturn(session);
585 //Mockito.when(mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs);
587 //mDstoreHandle = Mockito.mock(MusicDataStore.class);
588 //MusicUtil mMusicUtil=Mockito.mock(MusicUtil.class);
589 System.out.println("cjc 0 getDsHandleIP");
590 Mockito.when(MusicCore.getDSHandle("1.127.0.1")).thenReturn(mDstoreHandle);
591 System.out.println("cjc localhost");
592 // Mockito.when(mMusicUtil.getMyCassaHost().equals("localhost")).thenReturn(null);
593 System.out.println("cjc 1 localhost IP");
594 // mDstoreHandle = new MusicDataStore(MusicUtil.getMyCassaHost());
596 // mDstoreHandle = new MusicDataStore();
603 public void testPureZkCreate() {
605 MusicCore.pureZkCreate("local");
606 } catch(NullPointerException e) {
607 System.out.println("cjc zkcreate null pointwer exception:"+ e);
613 public void testPureZkRead() { //String nodeName) {
614 byte[] data = MusicCore.pureZkRead("localhost");
620 public void testPureZkWrite() { //String nodeName, byte[] data) {
622 long start = System.currentTimeMillis();
623 logger.info(EELFLoggerDelegate.applicationLogger,"Performing zookeeper write to " + nodeName);
625 getLockingServiceHandle().getzkLockHandle().setNodeData(nodeName, data);
626 } catch (MusicLockingException e) {
627 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), "[ERR512E] Failed to get ZK Lock Handle " ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
629 logger.info(EELFLoggerDelegate.applicationLogger,"Performed zookeeper write to " + nodeName);
630 long end = System.currentTimeMillis();
631 logger.info(EELFLoggerDelegate.applicationLogger,"Time taken for the actual zk put:" + (end - start) + " ms");
634 // mDstoreHandle = Mockito.mock(MusicDataStore.class);
635 // rs = Mockito.mock(ResultSet.class);
636 // session = Mockito.mock(Session.class);
637 //Mockito.when(mDstoreHandle.getSession()).thenReturn(session);
639 byte[] data= "Testing Zoo Keeper".getBytes();
640 MusicCore.pureZkWrite("1.127.0.1", data);
641 // assertNotNull(rs1);
645 public void testWhoseTurnIsIt() { //(String lockName) {
649 return getLockingServiceHandle().whoseTurnIsIt("/" + lockName) + "";
650 } catch (MusicLockingException e) {
651 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKINGERROR+lockName ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
656 String lockName="xxx";
657 if (MusicCore.whoseTurnIsIt(lockName) != null) assertTrue(true);
664 public void testMarshallResults() {
665 Map<String, HashMap<String, Object>> ret=null;
666 //ResultSet results =null;
667 rs = Mockito.mock(ResultSet.class);
669 ret= MusicCore.marshallResults(rs);
671 } catch( Exception e ) {
675 if (ret != null) assertTrue(true);