Merge "Sonar Fixes - CadiAuthFilter.java"
[music.git] / src / test / java / org / onap / music / unittests / TestMusicCore.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
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
10  * 
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  * 
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.
18  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 package org.onap.music.unittests;
24
25 import static org.junit.Assert.*;
26 import static org.onap.music.service.impl.MusicZKCore.mLockHandle;
27
28 import java.util.HashMap;
29 import java.util.Map;
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;
59
60
61 public class TestMusicCore {
62
63     
64     private Condition condition;
65
66     
67     private ResultSet rs;
68
69    
70     private PreparedQueryObject preparedQueryObject;
71     
72    
73     private Session session;
74     
75     @Mock 
76     MusicZKCore musicZKCore;
77
78     @Before
79     public void setUp() {
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);
86          
87     }
88
89     @Test
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");
95     }
96
97
98     @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");
105     }
106     
107     @Test
108     public void testIsTableOrKeySpaceLock() {
109         Boolean result = musicZKCore.isTableOrKeySpaceLock("ks1.tn1");
110         assertTrue(result);
111     }
112
113     @Test
114     public void testIsTableOrKeySpaceLockwithPrimarykey() {
115         Boolean result = musicZKCore.isTableOrKeySpaceLock("ks1.tn1.pk1");
116         assertFalse(result);
117     }
118
119     @Test
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");
126     }
127
128     @Test
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");
134     }
135
136     @Test
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");
142     }
143
144     @Test
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");
150     }
151
152     @Test
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");
161     }
162
163     @Test
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");
172     }
173     
174     @Test
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");
181     }
182     
183     @Test
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");
194     }
195     
196     @Test
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");
204     }
205
206     @Test
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");
216     }
217
218     @Test
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");
228
229     }
230
231     @Test
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");
241     }
242
243     @Test
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);
252         assertNotNull(rs1);
253     }
254
255     @Test
256     public void testGetLockNameFromId() {
257         String lockname = MusicZKCore.getLockNameFromId("lockName$id");
258         assertEquals("lockName", lockname);
259     }
260
261     @Test
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");
266     }
267
268     @Test
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");
275     }
276
277     @Test
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");
284     }
285
286     @Test
287     public void testDeleteLock() throws MusicLockingException {
288         Mockito.doNothing().when(mLockHandle).deleteLock("/" + "id1");
289         musicZKCore.deleteLock("id1");
290         Mockito.verify(mLockHandle).deleteLock("/" + "id1");
291     }
292
293     /*
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",
298      * "consistency"); }
299      */
300
301     @Test
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");
312     }
313
314     @Test
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);
327     }
328
329     @Test
330     public void testCriticalPutPreparedQuerywithValidLockId()
331                     throws Exception {
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,
343                         "id1", condition);
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");
348     }
349
350     @Test
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,
359                         "id1", condition);
360         assertEquals(expectedResult.getResult(), returnType.getResult());
361         Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1");
362     }
363
364     @Test
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,
374                         "id1", condition);
375         assertEquals(expectedResult.getResult(), returnType.getResult());
376         Mockito.verify(condition).testCondition();
377         Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1");
378     }
379
380     @Test
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");
390     }
391
392     @Test
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");
417     }
418
419     @Test
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");
431     }
432
433     @Test
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);
448         assertNotNull(rs1);
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);
455     }
456
457     @Test
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);
466         assertNull(rs1);
467         Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
468         Mockito.verify(mLockHandle).isMyTurn("id1");
469     }
470
471     @Test
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);
480         assertNotNull(rs1);
481         Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executeOneConsistencyGet(preparedQueryObject);
482
483     }
484
485     @Test
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");
497         assertNotNull(rs1);
498         Mockito.verify(mLockHandle, Mockito.atLeastOnce())
499                         .getLockState("ks1" + "." + "tn1" + "." + "pk1");
500         Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executeQuorumConsistencyGet(preparedQueryObject);
501     }
502
503     @Test
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");
511         assertNull(rs1);
512         Mockito.verify(mLockHandle, Mockito.atLeastOnce())
513                         .getLockState("ks1" + "." + "tn1" + "." + "pk1");
514     }
515
516     @Test
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);
525         assertNull(rs1);
526         Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
527         Mockito.verify(mLockHandle).isMyTurn("id1");
528     }
529     
530     @Test
531     public void testCondition() throws Exception {
532       //Condition conClass =  Mockito.mock(Condition.class);
533      // boolean ret=true;
534      //Mockito.when(conClass.testCondition().thenReturn(ret);
535       Map<String, Object> conditionsx=null;
536       PreparedQueryObject selectQueryForTheRowx=null;
537       try {
538       Condition con = new Condition(conditionsx,selectQueryForTheRowx);
539       assertTrue(con.testCondition());
540       } catch (Exception e) {
541         assertFalse(false);
542       }
543     }
544   //getLockingServiceHandl  
545     
546     @Ignore
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;
551       //mLockHandle=null;
552       System.out.println("cjc 0 locking test n");
553      // Mockito.when(MusicCore.getLockingServiceHandle()).thenReturn(mLockHandle);
554       //mLockHandle=null;
555       //System.out.println("cjc 0-1  locking test n");
556       MusicLockingService mLockHandlea = mLockHandle;
557       mLockHandle=null;
558       
559       MusicLockingService mLockHandley=null; //MusicCore.getLockingServiceHandle();
560       Mockito.when(MusicZKCore.getLockingServiceHandle()).thenReturn(mLockHandley);
561       System.out.println("cjc locking test n");
562       mLockHandle=mLockHandlea;
563       assertTrue(true);
564       
565     }
566   //add mocking 
567    @Ignore
568    @Test
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);
574       
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());
583     // } else {
584     //     MusicDataStoreHandle.mDstoreHandle = new MusicDataStore();
585     // }
586        assertTrue(true);
587    }
588    
589    @Ignore
590    @Test
591    public void testPureZkCreate() { 
592      try {
593      MusicZKCore.pureZkCreate("local");
594      } catch(NullPointerException e) {
595        System.out.println("cjc zkcreate null pointwer exception:"+ e);
596      }
597    }  
598    
599    @Ignore
600    @Test 
601    public void testPureZkRead() {   //String nodeName) {
602      byte[] data = MusicZKCore.pureZkRead("localhost");
603    }
604
605    //need fixing
606    @Ignore
607    @Test
608    public void testPureZkWrite() { //String nodeName, byte[] data) {
609      /*
610      long start = System.currentTimeMillis();
611      logger.info(EELFLoggerDelegate.applicationLogger,"Performing zookeeper write to " + nodeName);
612      try {
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);
616      }
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");
620     */
621     
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);
626        
627        byte[] data= "Testing Zoo Keeper".getBytes();
628        MusicZKCore.pureZkWrite("1.127.0.1", data);
629       // assertNotNull(rs1);
630    }
631    
632    @Test
633    public void testWhoseTurnIsIt() { //(String lockName) {
634
635      /*
636      try {
637          return getLockingServiceHandle().whoseTurnIsIt("/" + lockName) + "";
638      } catch (MusicLockingException e) {
639          logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKINGERROR+lockName ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
640      }
641      return null;
642      */
643      
644      String lockName="xxx";
645      if (musicZKCore.whoseTurnIsIt(lockName) != null) assertTrue(true);
646      
647
648
649  }
650    
651    @Test
652    public void testMarshallResults() { 
653      Map<String, HashMap<String, Object>> ret=null;
654      //ResultSet results =null;
655      rs = Mockito.mock(ResultSet.class);
656     try { 
657       ret= MusicDataStoreHandle.marshallResults(rs);
658       
659      } catch( Exception e ) {
660      
661      }
662     
663      if (ret != null) assertTrue(true);
664    }
665
666 }