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 * ====================================================================
23 package org.onap.music.unittests;
25 import static org.junit.Assert.*;
26 import static org.onap.music.service.impl.MusicZKCore.mLockHandle;
28 import java.util.HashMap;
30 import org.apache.zookeeper.KeeperException.NoNodeException;
31 import org.junit.Before;
32 import org.junit.Ignore;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.runners.MockitoJUnitRunner;
38 import org.onap.music.exceptions.MusicLockingException;
39 import org.onap.music.exceptions.MusicQueryException;
40 import org.onap.music.exceptions.MusicServiceException;
41 import org.onap.music.lockingservice.cassandra.MusicLockState;
42 import org.onap.music.lockingservice.cassandra.MusicLockState.LockStatus;
43 import org.onap.music.lockingservice.zookeeper.MusicLockingService;
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.service.impl.MusicZKCore;
48 import org.onap.music.datastore.Condition;
49 import org.onap.music.datastore.MusicDataStore;
50 import org.onap.music.datastore.MusicDataStoreHandle;
51 import org.onap.music.datastore.PreparedQueryObject;
52 import org.onap.music.eelf.logging.EELFLoggerDelegate;
53 import org.onap.music.eelf.logging.format.AppMessages;
54 import org.onap.music.eelf.logging.format.ErrorSeverity;
55 import org.onap.music.eelf.logging.format.ErrorTypes;
56 import com.att.eelf.exception.EELFException;
57 import com.datastax.driver.core.ResultSet;
58 import com.datastax.driver.core.Session;
61 public class TestMusicCore {
64 private Condition condition;
70 private PreparedQueryObject preparedQueryObject;
73 private Session session;
76 MusicZKCore musicZKCore;
80 mLockHandle = Mockito.mock(MusicLockingService.class);
81 musicZKCore = MusicZKCore.getInstance();
82 condition=Mockito.mock(Condition.class);
83 rs =Mockito.mock(ResultSet.class);
84 preparedQueryObject =Mockito.mock(PreparedQueryObject.class);
85 session =Mockito.mock(Session.class);
90 public void testCreateLockReferenceforvalidlock() {
91 Mockito.when(mLockHandle.createLockId("/" + "test")).thenReturn("lock");
92 String lockId = musicZKCore.createLockReference("test");
93 assertEquals("lock", lockId);
94 Mockito.verify(mLockHandle).createLockId("/" + "test");
99 public void testCreateLockReferencefornullname() {
100 //Mockito.when(mLockHandle.createLockId("/" + "test")).thenReturn("lock");
101 String lockId = musicZKCore.createLockReference("x"); //test");
102 //System.out.println("cjc exception lockhandle=" + mLockHandle+"lockid="+lockId );
103 assertNotEquals("lock", lockId);
104 //Mockito.verify(mLockHandle).createLockId("/" + "test");
108 public void testIsTableOrKeySpaceLock() {
109 Boolean result = musicZKCore.isTableOrKeySpaceLock("ks1.tn1");
114 public void testIsTableOrKeySpaceLockwithPrimarykey() {
115 Boolean result = musicZKCore.isTableOrKeySpaceLock("ks1.tn1.pk1");
120 public void testGetMusicLockState() throws MusicLockingException {
121 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
122 Mockito.when(mLockHandle.getLockState("ks1.tb1.pk1")).thenReturn(musicLockState);
123 MusicLockState mls = MusicZKCore.getMusicLockState("ks1.tb1.pk1");
124 assertEquals(musicLockState, mls);
125 Mockito.verify(mLockHandle).getLockState("ks1.tb1.pk1");
129 public void testAcquireLockifisMyTurnTrue() throws MusicLockingException {
130 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
131 ReturnType lock = musicZKCore.acquireLock("ks1.tn1", "id1");
132 assertEquals(lock.getResult(), ResultType.SUCCESS);
133 Mockito.verify(mLockHandle).isMyTurn("id1");
137 public void testAcquireLockifisMyTurnFalse() throws MusicLockingException {
138 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false);
139 ReturnType lock = musicZKCore.acquireLock("ks1.ts1", "id1");
140 assertEquals(lock.getResult(), ResultType.FAILURE);
141 Mockito.verify(mLockHandle).isMyTurn("id1");
145 public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockTrue() throws MusicLockingException {
146 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
147 ReturnType lock = musicZKCore.acquireLock("ks1.tn1", "id1");
148 assertEquals(lock.getResult(), ResultType.SUCCESS);
149 Mockito.verify(mLockHandle).isMyTurn("id1");
153 public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockFalseandHaveLock() throws MusicLockingException {
154 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
155 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
156 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
157 ReturnType lock = musicZKCore.acquireLock("ks1.tn1.pk1", "id1");
158 assertEquals(lock.getResult(), ResultType.SUCCESS);
159 Mockito.verify(mLockHandle).isMyTurn("id1");
160 Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1");
164 public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockFalseandDontHaveLock() throws MusicLockingException {
165 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id2");
166 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
167 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
168 ReturnType lock = musicZKCore.acquireLock("ks1.tn1.pk1", "id1");
169 assertEquals(lock.getResult(), ResultType.SUCCESS);
170 Mockito.verify(mLockHandle).isMyTurn("id1");
171 Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1");
175 public void testAcquireLockifLockRefDoesntExist() throws MusicLockingException {
176 Mockito.when(mLockHandle.lockIdExists("bs1")).thenReturn(false);
177 ReturnType lock = musicZKCore.acquireLock("ks1.ts1", "bs1");
178 assertEquals(lock.getResult(), ResultType.FAILURE);
179 assertEquals(lock.getMessage(), "Lockid doesn't exist");
180 Mockito.verify(mLockHandle).lockIdExists("bs1");
184 public void testAcquireLockWithLeasewithLease() throws MusicLockingException {
185 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
186 musicLockState.setLeasePeriod(0);
187 ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes");
188 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
189 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
190 ReturnType actualResult = musicZKCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000);
191 assertEquals(expectedResult.getResult(), actualResult.getResult());
192 Mockito.verify(mLockHandle).isMyTurn("id1");
193 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1");
197 public void testAcquireLockWithLeasewithException() throws MusicLockingException {
198 ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "failure");
199 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenThrow(new MusicLockingException());
200 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
201 ReturnType actualResult = musicZKCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000);
202 assertEquals(expectedResult.getResult(), actualResult.getResult());
203 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1");
207 public void testAcquireLockWithLeasewithLockStatusLOCKED() throws MusicLockingException {
208 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
209 ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes");
210 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
211 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
212 ReturnType actualResult = musicZKCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000);
213 assertEquals(expectedResult.getResult(), actualResult.getResult());
214 Mockito.verify(mLockHandle).isMyTurn("id1");
215 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1");
219 public void testAcquireLockWithLeasewithLockStatusUNLOCKED() throws MusicLockingException {
220 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
221 ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes");
222 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
223 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
224 ReturnType actualResult = musicZKCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000);
225 assertEquals(expectedResult.getResult(), actualResult.getResult());
226 Mockito.verify(mLockHandle).isMyTurn("id1");
227 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1");
232 public void testAcquireLockWithLeaseIfNotMyTurn() throws MusicLockingException {
233 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
234 ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure");
235 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
236 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false);
237 ReturnType actualResult = musicZKCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000);
238 assertEquals(expectedResult.getResult(), actualResult.getResult());
239 Mockito.verify(mLockHandle).isMyTurn("id1");
240 Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1");
244 public void testQuorumGet() throws MusicServiceException, MusicQueryException {
245 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
246 MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class);
247 rs = Mockito.mock(ResultSet.class);
248 session = Mockito.mock(Session.class);
249 Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session);
250 Mockito.when(MusicDataStoreHandle.mDstoreHandle.executeQuorumConsistencyGet(preparedQueryObject)).thenReturn(rs);
251 ResultSet rs1 = musicZKCore.quorumGet(preparedQueryObject);
256 public void testGetLockNameFromId() {
257 String lockname = MusicZKCore.getLockNameFromId("lockName$id");
258 assertEquals("lockName", lockname);
262 public void testDestroyLockRef() throws NoNodeException {
263 Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1");
264 musicZKCore.destroyLockRef("id1");
265 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1");
269 public void testreleaseLockwithvoluntaryReleaseTrue() throws NoNodeException {
270 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2");
271 Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1");
272 MusicLockState musicLockState1 = musicZKCore.releaseLock("id1", true);
273 assertEquals(musicLockState.getLockStatus(), musicLockState1.getLockStatus());
274 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1");
278 public void testreleaseLockwithvoluntaryReleaseFalse() throws NoNodeException {
279 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2");
280 Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1");
281 MusicLockState musicLockState1 = musicZKCore.releaseLock("id1", false);
282 assertEquals(musicLockState.getLockStatus(), musicLockState1.getLockStatus());
283 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1");
287 public void testDeleteLock() throws MusicLockingException {
288 Mockito.doNothing().when(mLockHandle).deleteLock("/" + "id1");
289 musicZKCore.deleteLock("id1");
290 Mockito.verify(mLockHandle).deleteLock("/" + "id1");
294 * @Test public void testNonKeyRelatedPut() throws Exception { MusicDataStoreHandle.mDstoreHandle =
295 * Mockito.mock(MusicDataStore.class); Mockito.when(MusicDataStoreHandle.mDstoreHandle.executePut("qu1",
296 * "consistency")).thenReturn(true); Boolean result = MusicCore.nonKeyRelatedPut("qu1",
297 * "consistency"); assertTrue(result); Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut("qu1",
302 public void testEventualPutPreparedQuery() throws MusicServiceException, MusicQueryException {
303 MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class);
304 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
305 ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes");
306 session = Mockito.mock(Session.class);
307 Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session);
308 Mockito.when(MusicDataStoreHandle.mDstoreHandle.executePut(preparedQueryObject, "eventual")).thenReturn(true);
309 ReturnType actualResult = musicZKCore.eventualPut(preparedQueryObject);
310 assertEquals(expectedResult.getResult(), actualResult.getResult());
311 Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut(preparedQueryObject, "eventual");
315 public void testEventualPutPreparedQuerywithResultFalse()
316 throws MusicServiceException, MusicQueryException {
317 MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class);
318 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
319 ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure");
320 session = Mockito.mock(Session.class);
321 Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session);
322 Mockito.when(MusicDataStoreHandle.mDstoreHandle.executePut(preparedQueryObject, "eventual")).thenReturn(false);
323 ReturnType actualResult = musicZKCore.eventualPut(preparedQueryObject);
324 assertEquals(expectedResult.getResult(), actualResult.getResult());
325 Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut(preparedQueryObject, "eventual");
326 //Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut(preparedQueryObject, MusicUtil.EVENTUAL);
330 public void testCriticalPutPreparedQuerywithValidLockId()
332 MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class);
333 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
334 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
335 Mockito.when(condition.testCondition()).thenReturn(true);
336 session = Mockito.mock(Session.class);
337 Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session);
338 ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes");
339 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
340 .thenReturn(musicLockState);
341 Mockito.when(MusicDataStoreHandle.mDstoreHandle.executePut(preparedQueryObject, "critical")).thenReturn(true);
342 ReturnType returnType = musicZKCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject,
344 assertEquals(expectedResult.getResult(), returnType.getResult());
345 Mockito.verify(condition).testCondition();
346 Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1");
347 Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut(preparedQueryObject, "critical");
351 public void testCriticalPutPreparedQuerywithInvalidLockId() throws MusicLockingException {
352 MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class);
353 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
354 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2");
355 ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure");
356 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
357 .thenReturn(musicLockState);
358 ReturnType returnType = musicZKCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject,
360 assertEquals(expectedResult.getResult(), returnType.getResult());
361 Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1");
365 public void testCriticalPutPreparedQuerywithvalidLockIdandTestConditionFalse() throws Exception {
366 MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class);
367 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
368 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
369 Mockito.when(condition.testCondition()).thenReturn(false);
370 ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure");
371 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
372 .thenReturn(musicLockState);
373 ReturnType returnType = musicZKCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject,
375 assertEquals(expectedResult.getResult(), returnType.getResult());
376 Mockito.verify(condition).testCondition();
377 Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1");
381 public void testNonKeyRelatedPutPreparedQuery() throws Exception {
382 MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class);
383 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
384 session = Mockito.mock(Session.class);
385 Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session);
386 Mockito.when(MusicDataStoreHandle.mDstoreHandle.executePut(preparedQueryObject, "consistency")).thenReturn(true);
387 ResultType result = musicZKCore.nonKeyRelatedPut(preparedQueryObject, "consistency");
388 assertEquals(ResultType.SUCCESS, result);
389 Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut(preparedQueryObject, "consistency");
393 public void testAtomicPutPreparedQuery() throws Exception {
394 MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class);
395 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
396 Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1");
397 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
398 ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes");
399 session = Mockito.mock(Session.class);
400 Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session);
401 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
402 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
403 Mockito.when(condition.testCondition()).thenReturn(true);
404 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
405 .thenReturn(musicLockState);
406 Mockito.when(MusicDataStoreHandle.mDstoreHandle.executePut(preparedQueryObject, "critical")).thenReturn(true);
407 ReturnType returnType =
408 musicZKCore.atomicPut("ks1", "tn1", "pk1", preparedQueryObject, condition);
409 assertEquals(expectedResult.getResult(), returnType.getResult());
410 Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
411 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1");
412 Mockito.verify(mLockHandle).isMyTurn("id1");
413 Mockito.verify(condition).testCondition();
414 Mockito.verify(mLockHandle, Mockito.atLeastOnce())
415 .getLockState("ks1" + "." + "tn1" + "." + "pk1");
416 Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut(preparedQueryObject, "critical");
420 public void testAtomicPutPreparedQuerywithAcquireLockWithLeaseFalse() throws MusicLockingException {
421 MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class);
422 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
423 Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1");
424 ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure");
425 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false);
426 ReturnType returnType =
427 musicZKCore.atomicPut("ks1", "tn1", "pk1", preparedQueryObject, condition);
428 assertEquals(expectedResult.getResult(), returnType.getResult());
429 Mockito.verify(mLockHandle).isMyTurn("id1");
430 Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
434 public void testAtomicGetPreparedQuery() throws MusicServiceException, MusicQueryException, MusicLockingException {
435 MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class);
436 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
437 rs = Mockito.mock(ResultSet.class);
438 session = Mockito.mock(Session.class);
439 Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session);
440 Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1");
441 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
442 Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState);
443 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true);
444 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
445 .thenReturn(musicLockState);
446 Mockito.when(MusicDataStoreHandle.mDstoreHandle.executeQuorumConsistencyGet(preparedQueryObject)).thenReturn(rs);
447 ResultSet rs1 = musicZKCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject);
449 Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
450 Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1");
451 Mockito.verify(mLockHandle).isMyTurn("id1");
452 Mockito.verify(mLockHandle, Mockito.atLeastOnce())
453 .getLockState("ks1" + "." + "tn1" + "." + "pk1");
454 Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executeQuorumConsistencyGet(preparedQueryObject);
458 public void testAtomicGetPreparedQuerywithAcquireLockWithLeaseFalse()
459 throws MusicServiceException, MusicLockingException {
460 MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class);
461 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
462 rs = Mockito.mock(ResultSet.class);
463 Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1");
464 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false);
465 ResultSet rs1 = musicZKCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject);
467 Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
468 Mockito.verify(mLockHandle).isMyTurn("id1");
472 public void testGetPreparedQuery() throws MusicServiceException, MusicQueryException {
473 MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class);
474 rs = Mockito.mock(ResultSet.class);
475 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
476 session = Mockito.mock(Session.class);
477 Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session);
478 Mockito.when(MusicDataStoreHandle.mDstoreHandle.executeOneConsistencyGet(preparedQueryObject)).thenReturn(rs);
479 ResultSet rs1 = musicZKCore.get(preparedQueryObject);
481 Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executeOneConsistencyGet(preparedQueryObject);
486 public void testcriticalGetPreparedQuery() throws MusicServiceException, MusicQueryException, MusicLockingException {
487 MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class);
488 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
489 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
490 rs = Mockito.mock(ResultSet.class);
491 session = Mockito.mock(Session.class);
492 Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session);
493 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
494 .thenReturn(musicLockState);
495 Mockito.when(MusicDataStoreHandle.mDstoreHandle.executeQuorumConsistencyGet(preparedQueryObject)).thenReturn(rs);
496 ResultSet rs1 = musicZKCore.criticalGet("ks1", "tn1", "pk1", preparedQueryObject, "id1");
498 Mockito.verify(mLockHandle, Mockito.atLeastOnce())
499 .getLockState("ks1" + "." + "tn1" + "." + "pk1");
500 Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executeQuorumConsistencyGet(preparedQueryObject);
504 public void testcriticalGetPreparedQuerywithInvalidLockId() throws MusicServiceException, MusicLockingException {
505 MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class);
506 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
507 MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2");
508 Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1"))
509 .thenReturn(musicLockState);
510 ResultSet rs1 = musicZKCore.criticalGet("ks1", "tn1", "pk1", preparedQueryObject, "id1");
512 Mockito.verify(mLockHandle, Mockito.atLeastOnce())
513 .getLockState("ks1" + "." + "tn1" + "." + "pk1");
517 public void testAtomicGetPreparedQuerywithDeleteLockWithLeaseFalse()
518 throws MusicServiceException, MusicLockingException {
519 MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class);
520 preparedQueryObject = Mockito.mock(PreparedQueryObject.class);
521 rs = Mockito.mock(ResultSet.class);
522 Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1");
523 Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false);
524 ResultSet rs1 = musicZKCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject);
526 Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
527 Mockito.verify(mLockHandle).isMyTurn("id1");
531 public void testCondition() throws Exception {
532 //Condition conClass = Mockito.mock(Condition.class);
534 //Mockito.when(conClass.testCondition().thenReturn(ret);
535 Map<String, Object> conditionsx=null;
536 PreparedQueryObject selectQueryForTheRowx=null;
538 Condition con = new Condition(conditionsx,selectQueryForTheRowx);
539 assertTrue(con.testCondition());
540 } catch (Exception e) {
544 //getLockingServiceHandl
547 @Test(expected = MusicLockingException.class) //("Failed to aquire Locl store handle " + e))
548 public void testgetLockingServiceHandle() throws Exception {
549 // MusicLockingService mLockHandlex = Mockito.mock(MusicLockingService.class);
550 //MusicLockingService mLockHandlea = mLockHandle;
552 System.out.println("cjc 0 locking test n");
553 // Mockito.when(MusicCore.getLockingServiceHandle()).thenReturn(mLockHandle);
555 //System.out.println("cjc 0-1 locking test n");
556 MusicLockingService mLockHandlea = mLockHandle;
559 MusicLockingService mLockHandley=null; //MusicCore.getLockingServiceHandle();
560 Mockito.when(MusicZKCore.getLockingServiceHandle()).thenReturn(mLockHandley);
561 System.out.println("cjc locking test n");
562 mLockHandle=mLockHandlea;
569 public void testGetDSHandleIp() throws MusicServiceException, MusicQueryException {
570 // rs = Mockito.mock(ResultSet.class);
571 // session = Mockito.mock(Session.class);
572 //Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session);
573 //Mockito.when(MusicDataStoreHandle.mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs);
575 //MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class);
576 //MusicUtil mMusicUtil=Mockito.mock(MusicUtil.class);
577 System.out.println("cjc 0 getDsHandleIP");
578 Mockito.when(MusicDataStoreHandle.getDSHandle("1.127.0.1")).thenReturn(MusicDataStoreHandle.mDstoreHandle);
579 System.out.println("cjc localhost");
580 // Mockito.when(mMusicUtil.getMyCassaHost().equals("localhost")).thenReturn(null);
581 System.out.println("cjc 1 localhost IP");
582 // MusicDataStoreHandle.mDstoreHandle = new MusicDataStore(MusicUtil.getMyCassaHost());
584 // MusicDataStoreHandle.mDstoreHandle = new MusicDataStore();
591 public void testPureZkCreate() {
593 MusicZKCore.pureZkCreate("local");
594 } catch(NullPointerException e) {
595 System.out.println("cjc zkcreate null pointwer exception:"+ e);
601 public void testPureZkRead() { //String nodeName) {
602 byte[] data = MusicZKCore.pureZkRead("localhost");
608 public void testPureZkWrite() { //String nodeName, byte[] data) {
610 long start = System.currentTimeMillis();
611 logger.info(EELFLoggerDelegate.applicationLogger,"Performing zookeeper write to " + nodeName);
613 getLockingServiceHandle().getzkLockHandle().setNodeData(nodeName, data);
614 } catch (MusicLockingException e) {
615 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), "[ERR512E] Failed to get ZK Lock Handle " ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
617 logger.info(EELFLoggerDelegate.applicationLogger,"Performed zookeeper write to " + nodeName);
618 long end = System.currentTimeMillis();
619 logger.info(EELFLoggerDelegate.applicationLogger,"Time taken for the actual zk put:" + (end - start) + " ms");
622 // MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class);
623 // rs = Mockito.mock(ResultSet.class);
624 // session = Mockito.mock(Session.class);
625 //Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session);
627 byte[] data= "Testing Zoo Keeper".getBytes();
628 MusicZKCore.pureZkWrite("1.127.0.1", data);
629 // assertNotNull(rs1);
633 public void testWhoseTurnIsIt() { //(String lockName) {
637 return getLockingServiceHandle().whoseTurnIsIt("/" + lockName) + "";
638 } catch (MusicLockingException e) {
639 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKINGERROR+lockName ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
644 String lockName="xxx";
645 if (musicZKCore.whoseTurnIsIt(lockName) != null) assertTrue(true);
652 public void testMarshallResults() {
653 Map<String, HashMap<String, Object>> ret=null;
654 //ResultSet results =null;
655 rs = Mockito.mock(ResultSet.class);
657 ret= MusicDataStoreHandle.marshallResults(rs);
659 } catch( Exception e ) {
663 if (ret != null) assertTrue(true);