5669580a28a84ed1e2a289ced2335ecdd4ef8b0a
[music.git] / src / test / java / org / onap / music / unittests / TstRestMusicLockAPI.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.assertEquals;
26 import static org.junit.Assert.assertTrue;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.UUID;
32 import javax.ws.rs.core.MultivaluedMap;
33 import javax.ws.rs.core.Response;
34 import javax.ws.rs.core.UriInfo;
35 import org.junit.After;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Ignore;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mindrot.jbcrypt.BCrypt;
42 import org.mockito.Mock;
43 import org.mockito.Mockito;
44 import org.mockito.junit.MockitoJUnitRunner;
45 import org.onap.music.datastore.PreparedQueryObject;
46 import org.onap.music.datastore.jsonobjects.JsonInsert;
47 import org.onap.music.datastore.jsonobjects.JsonLeasedLock;
48 import org.onap.music.datastore.jsonobjects.JsonLock;
49 import org.onap.music.datastore.jsonobjects.JsonTable;
50 import org.onap.music.datastore.jsonobjects.JsonUpdate;
51 import org.onap.music.exceptions.MusicServiceException;
52 import org.onap.music.lockingservice.cassandra.CassaLockStore;
53 import org.onap.music.lockingservice.cassandra.LockType;
54 import org.onap.music.main.MusicCore;
55 import org.onap.music.main.MusicUtil;
56 import org.onap.music.rest.RestMusicDataAPI;
57 import org.onap.music.rest.RestMusicLocksAPI;
58 import com.datastax.driver.core.DataType;
59 import com.datastax.driver.core.ResultSet;
60 import com.datastax.driver.core.Row;
61 import com.sun.jersey.core.util.Base64;
62 import com.sun.jersey.core.util.MultivaluedMapImpl;
63
64 @RunWith(MockitoJUnitRunner.class)
65 public class TstRestMusicLockAPI {
66
67
68     @Mock
69     UriInfo info;
70
71     RestMusicLocksAPI lock = new RestMusicLocksAPI();
72     RestMusicDataAPI data = new RestMusicDataAPI();
73     static PreparedQueryObject testObject;
74
75     static String appName = "TestApp";
76     static String userId = "TestUser";
77     static String password = "TestPassword";
78     static String authData = userId + ":" + password;
79     static String wrongAuthData = userId + ":" + "pass";
80     static String authorization = new String(Base64.encode(authData.getBytes()));
81     static String wrongAuthorization = new String(Base64.encode(wrongAuthData.getBytes()));
82     static boolean isAAF = false;
83     static UUID uuid = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40ce6");
84     static String keyspaceName = "testcassa";
85     static String tableName = "employees";
86     static String onboardUUID = null;
87     static String lockName = "testcassa.employees.testname";
88
89     @BeforeClass
90     public static void init() throws Exception {
91         System.out.println("Testing RestMusicLock class");
92         try {
93             createKeyspace();
94         } catch (Exception e) {
95             e.printStackTrace();
96             throw new Exception("Unable to initialize before TestRestMusicData test class. " + e.getMessage());
97         }
98     }
99     
100     @After
101     public void afterEachTest() throws MusicServiceException {
102         clearAllTablesFromKeyspace();
103     }
104
105     @AfterClass
106     public static void tearDownAfterClass() throws Exception {
107         testObject = new PreparedQueryObject();
108         testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName);
109         MusicCore.eventualPut(testObject);
110     }
111
112     @SuppressWarnings("unchecked")
113     @Test
114     public void test_createLockReference() throws Exception {
115         System.out.println("Testing create lockref");
116         createAndInsertIntoTable();
117         Response response = lock.createLockReference(lockName, "1", "1", authorization,
118                 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", null, null, appName);
119         Map<String, Object> respMap = (Map<String, Object>) response.getEntity();
120         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
121
122         assertEquals(200, response.getStatus());
123         assertTrue(respMap.containsKey("lock"));
124         assertTrue(((Map<String, String>) respMap.get("lock")).containsKey("lock"));
125     }
126     
127     @Test
128     public void test_createBadLockReference() throws Exception {
129         System.out.println("Testing create bad lockref");
130         createAndInsertIntoTable();
131         Response response = lock.createLockReference("badlock", "1", "1", authorization,
132                 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", null, null, appName);
133         Map<String, Object> respMap = (Map<String, Object>) response.getEntity();
134         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
135
136         assertEquals(400, response.getStatus());
137     }
138
139     @Test
140     public void test_createReadLock() throws Exception {
141         System.out.println("Testing create read lockref");
142         createAndInsertIntoTable();
143         JsonLock jsonLock = createJsonLock(LockType.READ);
144         Response response = lock.createLockReference(lockName, "1", "1", authorization,
145                 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", jsonLock, null, appName);
146         Map<String, Object> respMap = (Map<String, Object>) response.getEntity();
147         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
148
149         assertEquals(200, response.getStatus());
150         assertTrue(respMap.containsKey("lock"));
151         assertTrue(((Map<String, String>) respMap.get("lock")).containsKey("lock"));
152     }
153
154     @Test
155     public void test_createWriteLock() throws Exception {
156         System.out.println("Testing create read lockref");
157         createAndInsertIntoTable();
158         JsonLock jsonLock = createJsonLock(LockType.WRITE);
159         Response response = lock.createLockReference(lockName, "1", "1", authorization,
160                 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", jsonLock, null, appName);
161         Map<String, Object> respMap = (Map<String, Object>) response.getEntity();
162         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
163
164         assertEquals(200, response.getStatus());
165         assertTrue(respMap.containsKey("lock"));
166         assertTrue(((Map<String, String>) respMap.get("lock")).containsKey("lock"));
167     }
168
169     @Test
170     public void test_accquireLock() throws Exception {
171         System.out.println("Testing acquire lock");
172         createAndInsertIntoTable();
173         String lockRef = createLockReference();
174
175         Response response =
176                 lock.accquireLock(lockRef, "1", "1", authorization, "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName);
177         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
178         assertEquals(200, response.getStatus());
179     }
180
181     @Test
182     public void test_acquireReadLock() throws Exception {
183         System.out.println("Testing acquire read lock");
184         createAndInsertIntoTable();
185         String lockRef = createLockReference(LockType.READ);
186         String lockRef2 = createLockReference(LockType.READ);
187
188         Response response =
189                 lock.accquireLock(lockRef, "1", "1", authorization, "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName);
190         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
191         assertEquals(200, response.getStatus());
192         response =
193                 lock.accquireLock(lockRef2, "1", "1", authorization, "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName);
194         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
195         assertEquals(200, response.getStatus());
196     }
197     
198     @Test
199     public void test_acquireReadLockaFail() throws Exception {
200         System.out.println("Testing acquire read lock");
201         createAndInsertIntoTable();
202         String lockRef = createLockReference(LockType.WRITE);
203         String lockRef2 = createLockReference(LockType.READ);
204
205         Response response =
206                 lock.accquireLock(lockRef, "1", "1", authorization, "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName);
207         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
208         assertEquals(200, response.getStatus());
209         response =
210                 lock.accquireLock(lockRef2, "1", "1", authorization, "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName);
211         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
212         assertEquals(400, response.getStatus());
213     }
214
215     @Test
216     public void test_writeWReadLock() throws Exception {
217         System.out.println("Testing writing with a read lock");
218         createAndInsertIntoTable();
219         String lockRef = createLockReference(LockType.READ);
220
221         JsonUpdate jsonUpdate = new JsonUpdate();
222         Map<String, String> consistencyInfo = new HashMap<>();
223         Map<String, Object> values = new HashMap<>();
224         values.put("emp_salary", 2500);
225         consistencyInfo.put("type", "critical");
226         consistencyInfo.put("lockId", lockRef);
227         jsonUpdate.setConsistencyInfo(consistencyInfo);
228         jsonUpdate.setKeyspaceName(keyspaceName);
229         jsonUpdate.setTableName(tableName);
230         jsonUpdate.setValues(values);
231         MultivaluedMap<String, String> row = new MultivaluedMapImpl();
232         row.add("emp_name", "testname");
233         Mockito.when(info.getQueryParameters()).thenReturn(row);
234
235         Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
236                 authorization, jsonUpdate, keyspaceName, tableName, info);
237
238         assertEquals(400, response.getStatus());
239     }
240
241     @Test
242     public void test_writeWWriteLock() throws Exception {
243         System.out.println("Testing writing with a read lock");
244         createAndInsertIntoTable();
245         String lockRef = createLockReference(LockType.WRITE);
246
247         JsonUpdate jsonUpdate = new JsonUpdate();
248         Map<String, String> consistencyInfo = new HashMap<>();
249         Map<String, Object> values = new HashMap<>();
250         values.put("emp_salary", 2500);
251         consistencyInfo.put("type", "critical");
252         consistencyInfo.put("lockId", lockRef);
253         jsonUpdate.setConsistencyInfo(consistencyInfo);
254         jsonUpdate.setKeyspaceName(keyspaceName);
255         jsonUpdate.setTableName(tableName);
256         jsonUpdate.setValues(values);
257         MultivaluedMap<String, String> row = new MultivaluedMapImpl();
258         row.add("emp_name", "testname");
259         Mockito.when(info.getQueryParameters()).thenReturn(row);
260
261         Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
262                 authorization, jsonUpdate, keyspaceName, tableName, info);
263
264         assertEquals(200, response.getStatus());
265     }
266
267     @Test
268     public void test_accquireLockWLease() throws Exception {
269         System.out.println("Testing acquire lock with lease");
270         createAndInsertIntoTable();
271         String lockRef = createLockReference();
272
273         JsonLeasedLock jsonLock = new JsonLeasedLock();
274         jsonLock.setLeasePeriod(10000); // 10 second lease period?
275         Response response = lock.accquireLockWithLease(jsonLock, lockRef, "1", "1", authorization,
276                 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName);
277         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
278         assertEquals(200, response.getStatus());
279     }
280     
281     @Test
282     public void test_accquireBadLockWLease() throws Exception {
283         System.out.println("Testing acquire bad lock ref with lease");
284         createAndInsertIntoTable();
285         String lockRef = createLockReference();
286
287         JsonLeasedLock jsonLock = new JsonLeasedLock();
288         jsonLock.setLeasePeriod(10000); // 10 second lease period?
289         Response response = lock.accquireLockWithLease(jsonLock, "badlock", "1", "1", authorization,
290                 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName);
291         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
292         assertEquals(400, response.getStatus());
293     }
294     
295     @Test
296     public void test_accquireBadLock() throws Exception {
297         System.out.println("Testing acquire lock that is not lock-holder");
298         createAndInsertIntoTable();
299         // This is required to create an initial loc reference.
300         String lockRef1 = createLockReference();
301         // This will create the next lock reference, whcih will not be avalale yet.
302         String lockRef2 = createLockReference();
303
304         Response response = lock.accquireLock(lockRef2, "1", "1", authorization,
305                 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName);
306         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
307         assertEquals(400, response.getStatus());
308     }
309     
310     @Test
311     public void test_accquireBadLockRef() throws Exception {
312         System.out.println("Testing acquire bad lock ref");
313         createAndInsertIntoTable();
314         // This is required to create an initial loc reference.
315         String lockRef1 = createLockReference();
316
317         Response response = lock.accquireLock("badlockref", "1", "1", authorization,
318                 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName);
319         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
320         assertEquals(400, response.getStatus());
321     }
322     
323     @Test
324     public void test_currentLockHolder() throws Exception {
325         System.out.println("Testing get current lock holder");
326         createAndInsertIntoTable();
327
328         String lockRef = createLockReference();
329
330         Response response =
331                 lock.enquireLock(lockName, "1", "1", authorization, "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName);
332         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
333         assertEquals(200, response.getStatus());
334         Map<String, Object> respMap = (Map<String, Object>) response.getEntity();
335         assertEquals(lockRef, ((Map<String, String>) respMap.get("lock")).get("lock-holder"));
336     }
337     
338     @Test
339     public void test_nocurrentLockHolder() throws Exception {
340         System.out.println("Testing get current lock holder w/ bad lockref");
341         createAndInsertIntoTable();
342
343         Response response =
344                 lock.enquireLock(lockName, "1", "1", authorization, "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName);
345         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
346         assertEquals(400, response.getStatus());
347     }
348     
349     @Test
350     public void test_badcurrentLockHolder() throws Exception {
351         System.out.println("Testing get current lock holder w/ bad lockref");
352         createAndInsertIntoTable();
353
354         String lockRef = createLockReference();
355
356         Response response =
357                 lock.enquireLock("badlock", "1", "1", authorization, "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName);
358         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
359         assertEquals(400, response.getStatus());
360     }
361     
362     @Test
363     public void test_holders() throws Exception {
364         System.out.println("Testing holders api");
365         createAndInsertIntoTable();
366
367         String lockRef = createLockReference();
368         
369         Response response =
370                 lock.currentLockHolder(lockName, "1", "1", authorization, "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName);
371         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
372         assertEquals(200, response.getStatus());
373         Map<String, Object> respMap = (Map<String, Object>) response.getEntity();
374         assertEquals(lockRef, ((Map<String, List>) respMap.get("lock")).get("lock-holder").get(0));
375     }
376     
377     @Test
378     public void test_holdersbadRef() throws Exception {
379         System.out.println("Testing holders api w/ bad lockref");
380         createAndInsertIntoTable();
381
382         String lockRef = createLockReference();
383         
384         Response response =
385                 lock.currentLockHolder("badname", "1", "1", authorization, "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName);
386         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
387         assertEquals(400, response.getStatus());
388     }
389     
390     @Test
391     public void test_unLock() throws Exception {
392         System.out.println("Testing unlock");
393         createAndInsertIntoTable();
394         String lockRef = createLockReference();
395
396         Response response =
397                 lock.unLock(lockRef, "1", "1", authorization, "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName);
398         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
399         assertEquals(200, response.getStatus());
400     }
401     
402     @Test
403     public void test_unLockBadRef() throws Exception {
404         System.out.println("Testing unlock w/ bad lock ref");
405         createAndInsertIntoTable();
406         String lockRef = createLockReference();
407
408         Response response =
409                 lock.unLock("badref", "1", "1", authorization, "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName);
410         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
411         assertEquals(400, response.getStatus());
412     }
413     
414     @Test
415     public void test_getLockState() throws Exception {
416         System.out.println("Testing get lock state");
417         createAndInsertIntoTable();
418
419         String lockRef = createLockReference();
420
421         Response response = lock.currentLockState(lockName, "1", "1", authorization,
422                 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName);
423         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
424         assertEquals(200, response.getStatus());
425         Map<String,Object> respMap = (Map<String, Object>) response.getEntity();
426         assertEquals(lockRef, ((Map<String,String>) respMap.get("lock")).get("lock-holder"));
427     }
428     
429     @Test
430     public void test_getLockStateBadRef() throws Exception {
431         System.out.println("Testing get lock state w/ bad ref");
432         createAndInsertIntoTable();
433
434         String lockRef = createLockReference();
435
436         Response response = lock.currentLockState("badname", "1", "1", authorization,
437                 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName);
438         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
439         assertEquals(400, response.getStatus());
440     }
441
442     @SuppressWarnings("unchecked")
443     @Test
444     public void test_deadlock() throws Exception {
445         System.out.println("Testing deadlock");
446         createAndInsertIntoTable();
447         insertAnotherIntoTable();
448
449         // Process 1 creates and acquires a lock on row 1
450         JsonLock jsonLock = createJsonLock(LockType.WRITE);
451         Response responseCreate1 = lock.createLockReference(lockName, "1", "1", authorization,
452                 "abcde001-d857-4e90-b1e5-df98a3d40ce6", jsonLock, "process1", appName);
453         Map<String, Object> respMapCreate1 = (Map<String, Object>) responseCreate1.getEntity();
454         String lockRefCreate1 = ((Map<String, String>) respMapCreate1.get("lock")).get("lock");
455
456         Response responseAcquire1 =
457                 lock.accquireLock(lockRefCreate1, "1", "1", authorization, "abc66001-d857-4e90-b1e5-df98a3d40ce6", appName);
458
459         // Process 2 creates and acquires a lock on row 2
460         Response responseCreate2 = lock.createLockReference(lockName + "2", "1", "1", authorization,
461                 "abcde002-d857-4e90-b1e5-df98a3d40ce6", jsonLock, "process2", appName);
462         Map<String, Object> respMapCreate2 = (Map<String, Object>) responseCreate2.getEntity();
463         String lockRefCreate2 = ((Map<String, String>) respMapCreate2.get("lock")).get("lock");
464
465         Response responseAcquire2 =
466                 lock.accquireLock(lockRefCreate2, "1", "1", authorization, "abc66002-d857-4e90-b1e5-df98a3d40ce6", appName);
467
468         // Process 2 creates a lock on row 1
469         Response responseCreate3 = lock.createLockReference(lockName, "1", "1", authorization,
470                 "abcde003-d857-4e90-b1e5-df98a3d40ce6", jsonLock, "process2", appName);
471
472         // Process 1 creates a lock on row 2, causing deadlock
473         Response responseCreate4 = lock.createLockReference(lockName + "2", "1", "1", authorization,
474                 "abcde004-d857-4e90-b1e5-df98a3d40ce6", jsonLock, "process1", appName);
475         Map<String, Object> respMapCreate4 = (Map<String, Object>) responseCreate4.getEntity();
476
477         System.out.println("Status: " + responseCreate4.getStatus() + ". Entity " + responseCreate4.getEntity());
478         assertEquals(400, responseCreate4.getStatus());
479         assertTrue(respMapCreate4.containsKey("error"));
480         assertTrue( ((String)respMapCreate4.get("error")).toLowerCase().indexOf("deadlock") > -1 );
481     }
482
483
484     // Ignoring since this is now a duplicate of delete lock ref.
485     @Test
486     @Ignore
487     public void test_deleteLock() throws Exception {
488         System.out.println("Testing get lock state");
489         createAndInsertIntoTable();
490         
491         String lockRef = createLockReference();
492
493         Response response = lock.deleteLock(lockName, "1", "1",
494                 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", authorization, appName);
495         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
496         assertEquals(200, response.getStatus());
497     }
498     
499     /**
500     * Create table and lock reference
501      * 
502     * @return the lock ref created
503      * @throws Exception
504     */
505     @SuppressWarnings("unchecked")
506     private String createLockReference() throws Exception {
507         Response response = lock.createLockReference(lockName, "1", "1", authorization,
508                 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", null, null, appName);
509         Map<String, Object> respMap = (Map<String, Object>) response.getEntity();
510         return ((Map<String, String>) respMap.get("lock")).get("lock");
511     }
512
513     /**
514      * Create table and lock reference
515      * 
516      * @return the lock ref created
517      * @throws Exception
518      */
519     @SuppressWarnings("unchecked")
520     private String createLockReference(LockType lockType) throws Exception {
521         JsonLock jsonLock = createJsonLock(lockType);
522         Response response = lock.createLockReference(lockName, "1", "1", authorization,
523                 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", jsonLock, null, appName);
524         Map<String, Object> respMap = (Map<String, Object>) response.getEntity();
525         return ((Map<String, String>) respMap.get("lock")).get("lock");
526     }
527
528     private static void createKeyspace() throws Exception {
529         // shouldn't really be doing this here, but create keyspace is currently turned off
530         PreparedQueryObject query = new PreparedQueryObject();
531         query.appendQueryString(CassandraCQL.createKeySpace);
532         MusicCore.eventualPut(query);
533         
534         boolean isAAF = false;
535         String hashedpwd = BCrypt.hashpw(password, BCrypt.gensalt());
536         query = new PreparedQueryObject();
537         query.appendQueryString("INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
538                                     + "password, username, is_aaf) values (?,?,?,?,?,?,?)");
539         query.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
540         query.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspaceName));
541         query.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
542         query.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
543         query.addValue(MusicUtil.convertToActualDataType(DataType.text(), hashedpwd));
544         query.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
545         query.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
546         //CachingUtil.updateMusicCache(keyspaceName, appName);
547         //CachingUtil.updateMusicValidateCache(appName, userId, hashedpwd);
548         MusicCore.eventualPut(query);
549     }
550     
551     private void clearAllTablesFromKeyspace() throws MusicServiceException {
552         ArrayList<String> tableNames = new ArrayList<>();
553         PreparedQueryObject query = new PreparedQueryObject();
554         query.appendQueryString(
555                 "SELECT table_name FROM system_schema.tables WHERE keyspace_name = '" + keyspaceName + "';");
556         ResultSet rs = MusicCore.get(query);
557         for (Row row : rs) {
558             tableNames.add(row.getString("table_name"));
559         }
560         for (String table : tableNames) {
561             query = new PreparedQueryObject();
562             query.appendQueryString("DROP TABLE " + keyspaceName + "." + table);
563             MusicCore.eventualPut(query);
564         }
565     }
566     
567     /**
568      * Create a table {@link tableName} in {@link keyspaceName}
569      * 
570      * @throws Exception
571      */
572     private void createTable() throws Exception {
573         JsonTable jsonTable = new JsonTable();
574         Map<String, String> consistencyInfo = new HashMap<>();
575         Map<String, String> fields = new HashMap<>();
576         fields.put("uuid", "text");
577         fields.put("emp_name", "text");
578         fields.put("emp_salary", "varint");
579         fields.put("PRIMARY KEY", "(emp_name)");
580         consistencyInfo.put("type", "eventual");
581         jsonTable.setConsistencyInfo(consistencyInfo);
582         jsonTable.setKeyspaceName(keyspaceName);
583         jsonTable.setPrimaryKey("emp_name");
584         jsonTable.setTableName(tableName);
585         jsonTable.setFields(fields);
586         // Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
587         Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
588                 authorization, jsonTable, keyspaceName, tableName);
589     }
590     
591     /**
592      * Create table {@link createTable} and insert into said table
593      * 
594      * @throws Exception
595      */
596     private void createAndInsertIntoTable() throws Exception {
597         createTable();
598         
599         JsonInsert jsonInsert = new JsonInsert();
600         Map<String, String> consistencyInfo = new HashMap<>();
601         Map<String, Object> values = new HashMap<>();
602         values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
603         values.put("emp_name", "testname");
604         values.put("emp_salary", 500);
605         consistencyInfo.put("type", "eventual");
606         jsonInsert.setConsistencyInfo(consistencyInfo);
607         jsonInsert.setKeyspaceName(keyspaceName);
608         jsonInsert.setTableName(tableName);
609         jsonInsert.setValues(values);
610         Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
611                 authorization, jsonInsert, keyspaceName, tableName);
612     }
613
614     private void insertAnotherIntoTable() throws Exception {
615         createTable();
616
617         JsonInsert jsonInsert = new JsonInsert();
618         Map<String, String> consistencyInfo = new HashMap<>();
619         Map<String, Object> values = new HashMap<>();
620         values.put("uuid", "cccccccc-d857-4e90-b1e5-df98a3d40cd6");
621         values.put("emp_name", "testname2");
622         values.put("emp_salary", 700);
623         consistencyInfo.put("type", "eventual");
624         jsonInsert.setConsistencyInfo(consistencyInfo);
625         jsonInsert.setKeyspaceName(keyspaceName);
626         jsonInsert.setTableName(tableName);
627         jsonInsert.setValues(values);
628         Response response = data.insertIntoTable("1", "1", "1", "abcdef00-d857-4e90-b1e5-df98a3d40ce6", appName,
629                 authorization, jsonInsert, keyspaceName, tableName);
630     }
631
632     private JsonLock createJsonLock(LockType lockType) {
633         JsonLock jsonLock = new JsonLock();
634         jsonLock.setLockType(lockType);
635         return jsonLock;
636     }
637
638 }