various Updates
[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 package org.onap.music.unittests;
23
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;
28 import java.util.Map;
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;
57
58 @RunWith(MockitoJUnitRunner.class)
59 public class TestMusicCore {
60
61     @Mock
62     private Condition condition;
63
64     @Mock
65     private ResultSet rs;
66
67     @Mock
68     private PreparedQueryObject preparedQueryObject;
69     
70     @Mock
71     private Session session;
72
73     @Before
74     public void setUp() {
75         mLockHandle = Mockito.mock(MusicLockingService.class);
76
77     }
78
79     @Test
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");
85     }
86
87
88     @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");
95     }
96     
97     @Test
98     public void testIsTableOrKeySpaceLock() {
99         Boolean result = MusicCore.isTableOrKeySpaceLock("ks1.tn1");
100         assertTrue(result);
101     }
102
103     @Test
104     public void testIsTableOrKeySpaceLockwithPrimarykey() {
105         Boolean result = MusicCore.isTableOrKeySpaceLock("ks1.tn1.pk1");
106         assertFalse(result);
107     }
108
109     @Test
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");
116     }
117
118     @Test
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");
124     }
125
126     @Test
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");
132     }
133
134     @Test
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");
140     }
141
142     @Test
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");
151     }
152
153     @Test
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");
162     }
163     
164     @Test
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");
171     }
172     
173     @Test
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");
184     }
185     
186     @Test
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");
194     }
195
196     @Test
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");
206     }
207
208     @Test
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");
218
219     }
220
221     @Test
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");
231     }
232
233     @Test
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);
242         assertNotNull(rs1);
243     }
244
245     @Test
246     public void testGetLockNameFromId() {
247         String lockname = MusicCore.getLockNameFromId("lockName$id");
248         assertEquals("lockName", lockname);
249     }
250
251     @Test
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");
256     }
257
258     @Test
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");
265     }
266
267     @Test
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");
274     }
275
276     @Test
277     public void testDeleteLock() throws MusicLockingException {
278         Mockito.doNothing().when(mLockHandle).deleteLock("/" + "id1");
279         MusicCore.deleteLock("id1");
280         Mockito.verify(mLockHandle).deleteLock("/" + "id1");
281     }
282
283     /*
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",
288      * "consistency"); }
289      */
290
291     @Test
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");
302     }
303
304     @Test
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);
317     }
318
319     @Test
320     public void testCriticalPutPreparedQuerywithValidLockId()
321                     throws Exception {
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,
333                         "id1", condition);
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");
338     }
339
340     @Test
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,
349                         "id1", condition);
350         assertEquals(expectedResult.getResult(), returnType.getResult());
351         Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1");
352     }
353
354     @Test
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,
364                         "id1", condition);
365         assertEquals(expectedResult.getResult(), returnType.getResult());
366         Mockito.verify(condition).testCondition();
367         Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1");
368     }
369
370     @Test
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");
380     }
381
382     @Test
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");
407     }
408
409     @Test
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");
421     }
422
423     @Test
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);
438         assertNotNull(rs1);
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);
445     }
446
447     @Test
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);
456         assertNull(rs1);
457         Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
458         Mockito.verify(mLockHandle).isMyTurn("id1");
459     }
460
461     @Test
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);
470         assertNotNull(rs1);
471         Mockito.verify(mDstoreHandle).executeEventualGet(preparedQueryObject);
472
473     }
474
475     @Test
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");
487         assertNotNull(rs1);
488         Mockito.verify(mLockHandle, Mockito.atLeastOnce())
489                         .getLockState("ks1" + "." + "tn1" + "." + "pk1");
490         Mockito.verify(mDstoreHandle).executeCriticalGet(preparedQueryObject);
491     }
492
493     @Test
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");
501         assertNull(rs1);
502         Mockito.verify(mLockHandle, Mockito.atLeastOnce())
503                         .getLockState("ks1" + "." + "tn1" + "." + "pk1");
504     }
505
506     @Test
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);
515         assertNull(rs1);
516         Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1");
517         Mockito.verify(mLockHandle).isMyTurn("id1");
518     }
519     
520     @Test
521     public void testCondition() throws Exception {
522       //Condition conClass =  Mockito.mock(Condition.class);
523      // boolean ret=true;
524      //Mockito.when(conClass.testCondition().thenReturn(ret);
525       Map<String, Object> conditionsx=null;
526       PreparedQueryObject selectQueryForTheRowx=null;
527       try {
528       Condition con = new Condition(conditionsx,selectQueryForTheRowx);
529       assertTrue(con.testCondition());
530       } catch (Exception e) {
531         assertFalse(false);
532       }
533     }
534   //getLockingServiceHandl  
535     
536     @Ignore
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;
541       //mLockHandle=null;
542       System.out.println("cjc 0 locking test n");
543      // Mockito.when(MusicCore.getLockingServiceHandle()).thenReturn(mLockHandle);
544       //mLockHandle=null;
545       //System.out.println("cjc 0-1  locking test n");
546       MusicLockingService mLockHandlea = mLockHandle;
547       mLockHandle=null;
548       
549       MusicLockingService mLockHandley=null; //MusicCore.getLockingServiceHandle();
550       Mockito.when(MusicCore.getLockingServiceHandle()).thenReturn(mLockHandley);
551       System.out.println("cjc locking test n");
552       mLockHandle=mLockHandlea;
553       assertTrue(true);
554       
555     }
556    
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);
563       
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());
572     // } else {
573     //     mDstoreHandle = new MusicDataStore();
574     // }
575        assertTrue(true);
576    }
577    
578   //add mocking 
579    @Ignore
580    @Test
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);
586       
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());
595     // } else {
596     //     mDstoreHandle = new MusicDataStore();
597     // }
598        assertTrue(true);
599    }
600    
601    @Ignore
602    @Test
603    public void testPureZkCreate() { 
604      try {
605      MusicCore.pureZkCreate("local");
606      } catch(NullPointerException e) {
607        System.out.println("cjc zkcreate null pointwer exception:"+ e);
608      }
609    }  
610    
611    @Ignore
612    @Test 
613    public void testPureZkRead() {   //String nodeName) {
614      byte[] data = MusicCore.pureZkRead("localhost");
615    }
616
617    //need fixing
618    @Ignore
619    @Test
620    public void testPureZkWrite() { //String nodeName, byte[] data) {
621      /*
622      long start = System.currentTimeMillis();
623      logger.info(EELFLoggerDelegate.applicationLogger,"Performing zookeeper write to " + nodeName);
624      try {
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);
628      }
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");
632     */
633     
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);
638        
639        byte[] data= "Testing Zoo Keeper".getBytes();
640        MusicCore.pureZkWrite("1.127.0.1", data);
641       // assertNotNull(rs1);
642    }
643    
644    @Test
645    public void testWhoseTurnIsIt() { //(String lockName) {
646
647      /*
648      try {
649          return getLockingServiceHandle().whoseTurnIsIt("/" + lockName) + "";
650      } catch (MusicLockingException e) {
651          logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKINGERROR+lockName ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
652      }
653      return null;
654      */
655      
656      String lockName="xxx";
657      if (MusicCore.whoseTurnIsIt(lockName) != null) assertTrue(true);
658      
659
660
661  }
662    
663    @Test
664    public void testMarshallResults() { 
665      Map<String, HashMap<String, Object>> ret=null;
666      //ResultSet results =null;
667      rs = Mockito.mock(ResultSet.class);
668     try { 
669       ret= MusicCore.marshallResults(rs);
670       
671      } catch( Exception e ) {
672      
673      }
674     
675      if (ret != null) assertTrue(true);
676    }
677
678 }