2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 AT&T Intellectual Property
6 * ===================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * ============LICENSE_END=============================================
20 * ====================================================================
23 package org.onap.music.unittests;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
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;
64 @RunWith(MockitoJUnitRunner.class)
65 public class TstRestMusicLockAPI {
71 RestMusicLocksAPI lock = new RestMusicLocksAPI();
72 RestMusicDataAPI data = new RestMusicDataAPI();
73 static PreparedQueryObject testObject;
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";
90 public static void init() throws Exception {
91 System.out.println("Testing RestMusicLock class");
94 } catch (Exception e) {
96 throw new Exception("Unable to initialize before TestRestMusicData test class. " + e.getMessage());
101 public void afterEachTest() throws MusicServiceException {
102 clearAllTablesFromKeyspace();
106 public static void tearDownAfterClass() throws Exception {
107 testObject = new PreparedQueryObject();
108 testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName);
109 MusicCore.eventualPut(testObject);
112 @SuppressWarnings("unchecked")
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());
122 assertEquals(200, response.getStatus());
123 assertTrue(respMap.containsKey("lock"));
124 assertTrue(((Map<String, String>) respMap.get("lock")).containsKey("lock"));
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());
136 assertEquals(400, response.getStatus());
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());
149 assertEquals(200, response.getStatus());
150 assertTrue(respMap.containsKey("lock"));
151 assertTrue(((Map<String, String>) respMap.get("lock")).containsKey("lock"));
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());
164 assertEquals(200, response.getStatus());
165 assertTrue(respMap.containsKey("lock"));
166 assertTrue(((Map<String, String>) respMap.get("lock")).containsKey("lock"));
170 public void test_accquireLock() throws Exception {
171 System.out.println("Testing acquire lock");
172 createAndInsertIntoTable();
173 String lockRef = createLockReference();
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());
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);
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());
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());
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);
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());
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());
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);
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);
235 Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
236 authorization, jsonUpdate, keyspaceName, tableName, info);
238 assertEquals(400, response.getStatus());
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);
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);
261 Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
262 authorization, jsonUpdate, keyspaceName, tableName, info);
264 assertEquals(200, response.getStatus());
268 public void test_accquireLockWLease() throws Exception {
269 System.out.println("Testing acquire lock with lease");
270 createAndInsertIntoTable();
271 String lockRef = createLockReference();
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());
282 public void test_accquireBadLockWLease() throws Exception {
283 System.out.println("Testing acquire bad lock ref with lease");
284 createAndInsertIntoTable();
285 String lockRef = createLockReference();
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());
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();
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());
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();
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());
324 public void test_currentLockHolder() throws Exception {
325 System.out.println("Testing get current lock holder");
326 createAndInsertIntoTable();
328 String lockRef = createLockReference();
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"));
339 public void test_nocurrentLockHolder() throws Exception {
340 System.out.println("Testing get current lock holder w/ bad lockref");
341 createAndInsertIntoTable();
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());
350 public void test_badcurrentLockHolder() throws Exception {
351 System.out.println("Testing get current lock holder w/ bad lockref");
352 createAndInsertIntoTable();
354 String lockRef = createLockReference();
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());
363 public void test_holders() throws Exception {
364 System.out.println("Testing holders api");
365 createAndInsertIntoTable();
367 String lockRef = createLockReference();
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));
378 public void test_holdersbadRef() throws Exception {
379 System.out.println("Testing holders api w/ bad lockref");
380 createAndInsertIntoTable();
382 String lockRef = createLockReference();
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());
391 public void test_unLock() throws Exception {
392 System.out.println("Testing unlock");
393 createAndInsertIntoTable();
394 String lockRef = createLockReference();
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());
403 public void test_unLockBadRef() throws Exception {
404 System.out.println("Testing unlock w/ bad lock ref");
405 createAndInsertIntoTable();
406 String lockRef = createLockReference();
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());
415 public void test_getLockState() throws Exception {
416 System.out.println("Testing get lock state");
417 createAndInsertIntoTable();
419 String lockRef = createLockReference();
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"));
430 public void test_getLockStateBadRef() throws Exception {
431 System.out.println("Testing get lock state w/ bad ref");
432 createAndInsertIntoTable();
434 String lockRef = createLockReference();
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());
442 @SuppressWarnings("unchecked")
444 public void test_deadlock() throws Exception {
445 System.out.println("Testing deadlock");
446 createAndInsertIntoTable();
447 insertAnotherIntoTable();
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");
456 Response responseAcquire1 =
457 lock.accquireLock(lockRefCreate1, "1", "1", authorization, "abc66001-d857-4e90-b1e5-df98a3d40ce6", appName);
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");
465 Response responseAcquire2 =
466 lock.accquireLock(lockRefCreate2, "1", "1", authorization, "abc66002-d857-4e90-b1e5-df98a3d40ce6", appName);
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);
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();
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 );
484 @SuppressWarnings("unchecked")
486 public void test_lockPromotion() throws Exception {
487 System.out.println("Testing lock promotion");
488 createAndInsertIntoTable();
489 insertAnotherIntoTable();
492 JsonLock jsonLock = createJsonLock(LockType.READ);
493 Response responseCreate1 = lock.createLockReference(lockName, "1", "1", authorization,
494 "abcde001-d857-4e90-b1e5-df98a3d40ce6", jsonLock, "process1", appName);
495 Map<String, Object> respMapCreate1 = (Map<String, Object>) responseCreate1.getEntity();
496 String lockRefCreate1 = ((Map<String, String>) respMapCreate1.get("lock")).get("lock");
498 Response respMapPromote = lock.promoteLock(lockRefCreate1, "1", "1", authorization);
499 System.out.println("Status: " + respMapPromote.getStatus() + ". Entity " + respMapPromote.getEntity());
501 assertEquals(200, respMapPromote.getStatus());
504 @SuppressWarnings("unchecked")
506 public void test_lockPromotionReadWrite() throws Exception {
507 System.out.println("Testing lock promotion with read and writes");
508 createAndInsertIntoTable();
509 insertAnotherIntoTable();
512 JsonLock jsonLockRead = createJsonLock(LockType.READ);
513 Response responseCreate1 = lock.createLockReference(lockName, "1", "1", authorization,
514 "abcde001-d857-4e90-b1e5-df98a3d40ce6", jsonLockRead, "process1", appName);
515 Map<String, Object> respMapCreate1 = (Map<String, Object>) responseCreate1.getEntity();
516 String lockRefCreate1 = ((Map<String, String>) respMapCreate1.get("lock")).get("lock");
518 JsonLock jsonLockWrite = createJsonLock(LockType.WRITE);
519 Response responseCreate2 = lock.createLockReference(lockName, "1", "1", authorization,
520 "abcde001-d857-4e90-b1e5-df98a3d40ce6", jsonLockWrite, "process1", appName);
521 Map<String, Object> respMapCreate2 = (Map<String, Object>) responseCreate2.getEntity();
522 String lockRefCreate2 = ((Map<String, String>) respMapCreate2.get("lock")).get("lock");
524 Response respMapPromote = lock.promoteLock(lockRefCreate1, "1", "1", authorization);
525 System.out.println("Status: " + respMapPromote.getStatus() + ". Entity " + respMapPromote.getEntity());
527 assertEquals(200, respMapPromote.getStatus());
530 @SuppressWarnings("unchecked")
532 public void test_lockPromotionWriteRead() throws Exception {
533 System.out.println("Testing lock promotion with reads not at top of queue");
534 createAndInsertIntoTable();
535 insertAnotherIntoTable();
538 JsonLock jsonLockWrite = createJsonLock(LockType.WRITE);
539 Response responseCreate2 = lock.createLockReference(lockName, "1", "1", authorization,
540 "abcde001-d857-4e90-b1e5-df98a3d40ce6", jsonLockWrite, "process1", appName);
541 Map<String, Object> respMapCreate2 = (Map<String, Object>) responseCreate2.getEntity();
542 String lockRefCreate2 = ((Map<String, String>) respMapCreate2.get("lock")).get("lock");
545 JsonLock jsonLockRead = createJsonLock(LockType.READ);
546 Response responseCreate1 = lock.createLockReference(lockName, "1", "1", authorization,
547 "abcde001-d857-4e90-b1e5-df98a3d40ce6", jsonLockRead, "process1", appName);
548 Map<String, Object> respMapCreate1 = (Map<String, Object>) responseCreate1.getEntity();
549 String lockRefCreate1 = ((Map<String, String>) respMapCreate1.get("lock")).get("lock");
551 Response respMapPromote = lock.promoteLock(lockRefCreate1, "1", "1", authorization);
552 System.out.println("Status: " + respMapPromote.getStatus() + ". Entity " + respMapPromote.getEntity());
554 assertEquals(200, respMapPromote.getStatus());
557 @SuppressWarnings("unchecked")
559 public void test_lockPromotion2Reads() throws Exception {
560 System.out.println("Testing lock promotion w/ 2 ReadLocks");
561 createAndInsertIntoTable();
562 insertAnotherIntoTable();
565 JsonLock jsonLockRead = createJsonLock(LockType.READ);
566 Response responseCreate1 = lock.createLockReference(lockName, "1", "1", authorization,
567 "abcde001-d857-4e90-b1e5-df98a3d40ce6", jsonLockRead, "process1", appName);
568 Map<String, Object> respMapCreate1 = (Map<String, Object>) responseCreate1.getEntity();
569 String lockRefCreate1 = ((Map<String, String>) respMapCreate1.get("lock")).get("lock");
571 Response responseCreate2 = lock.createLockReference(lockName, "1", "1", authorization,
572 "abcde001-d857-4e90-b1e5-df98a3d40ce6", jsonLockRead, "process1", appName);
573 Map<String, Object> respMapCreate2 = (Map<String, Object>) responseCreate1.getEntity();
574 String lockRefCreate2 = ((Map<String, String>) respMapCreate1.get("lock")).get("lock");
576 Response respMapPromote = lock.promoteLock(lockRefCreate1, "1", "1", authorization);
577 System.out.println("Status: " + respMapPromote.getStatus() + ". Entity " + respMapPromote.getEntity());
579 assertEquals(400, respMapPromote.getStatus());
582 @SuppressWarnings("unchecked")
584 public void test_2lockPromotions() throws Exception {
585 System.out.println("Testing 2 lock promotions");
586 createAndInsertIntoTable();
587 insertAnotherIntoTable();
590 JsonLock jsonLockRead = createJsonLock(LockType.READ);
591 Response responseCreate1 = lock.createLockReference(lockName, "1", "1", authorization,
592 "abcde001-d857-4e90-b1e5-df98a3d40ce6", jsonLockRead, "process1", appName);
593 Map<String, Object> respMapCreate1 = (Map<String, Object>) responseCreate1.getEntity();
594 String lockRefCreate1 = ((Map<String, String>) respMapCreate1.get("lock")).get("lock");
596 Response responseCreate2 = lock.createLockReference(lockName, "1", "1", authorization,
597 "abcde001-d857-4e90-b1e5-df98a3d40ce6", jsonLockRead, "process1", appName);
598 Map<String, Object> respMapCreate2 = (Map<String, Object>) responseCreate2.getEntity();
599 String lockRefCreate2 = ((Map<String, String>) respMapCreate2.get("lock")).get("lock");
601 Response respMapPromote = lock.promoteLock(lockRefCreate1, "1", "1", authorization);
602 System.out.println("Status: " + respMapPromote.getStatus() + ". Entity " + respMapPromote.getEntity());
604 assertEquals(400, respMapPromote.getStatus());
606 Response respMap2Promote = lock.promoteLock(lockRefCreate2, "1", "1", authorization);
607 System.out.println("Status: " + respMap2Promote.getStatus() + ". Entity " + respMap2Promote.getEntity());
609 assertEquals(400, respMapPromote.getStatus());
614 // Ignoring since this is now a duplicate of delete lock ref.
617 public void test_deleteLock() throws Exception {
618 System.out.println("Testing get lock state");
619 createAndInsertIntoTable();
621 String lockRef = createLockReference();
623 Response response = lock.deleteLock(lockName, "1", "1",
624 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", authorization, appName);
625 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
626 assertEquals(200, response.getStatus());
630 * Create table and lock reference
632 * @return the lock ref created
635 @SuppressWarnings("unchecked")
636 private String createLockReference() throws Exception {
637 Response response = lock.createLockReference(lockName, "1", "1", authorization,
638 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", null, null, appName);
639 Map<String, Object> respMap = (Map<String, Object>) response.getEntity();
640 return ((Map<String, String>) respMap.get("lock")).get("lock");
644 * Create table and lock reference
646 * @return the lock ref created
649 @SuppressWarnings("unchecked")
650 private String createLockReference(LockType lockType) throws Exception {
651 JsonLock jsonLock = createJsonLock(lockType);
652 Response response = lock.createLockReference(lockName, "1", "1", authorization,
653 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", jsonLock, null, appName);
654 Map<String, Object> respMap = (Map<String, Object>) response.getEntity();
655 return ((Map<String, String>) respMap.get("lock")).get("lock");
658 private static void createKeyspace() throws Exception {
659 // shouldn't really be doing this here, but create keyspace is currently turned off
660 PreparedQueryObject query = new PreparedQueryObject();
661 query.appendQueryString(CassandraCQL.createKeySpace);
662 MusicCore.eventualPut(query);
664 boolean isAAF = false;
665 String hashedpwd = BCrypt.hashpw(password, BCrypt.gensalt());
666 query = new PreparedQueryObject();
667 query.appendQueryString("INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
668 + "password, username, is_aaf) values (?,?,?,?,?,?,?)");
669 query.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
670 query.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspaceName));
671 query.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
672 query.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
673 query.addValue(MusicUtil.convertToActualDataType(DataType.text(), hashedpwd));
674 query.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
675 query.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
676 //CachingUtil.updateMusicCache(keyspaceName, appName);
677 //CachingUtil.updateMusicValidateCache(appName, userId, hashedpwd);
678 MusicCore.eventualPut(query);
681 private void clearAllTablesFromKeyspace() throws MusicServiceException {
682 ArrayList<String> tableNames = new ArrayList<>();
683 PreparedQueryObject query = new PreparedQueryObject();
684 query.appendQueryString(
685 "SELECT table_name FROM system_schema.tables WHERE keyspace_name = '" + keyspaceName + "';");
686 ResultSet rs = MusicCore.get(query);
688 tableNames.add(row.getString("table_name"));
690 for (String table : tableNames) {
691 query = new PreparedQueryObject();
692 query.appendQueryString("DROP TABLE " + keyspaceName + "." + table);
693 MusicCore.eventualPut(query);
698 * Create a table {@link tableName} in {@link keyspaceName}
702 private void createTable() throws Exception {
703 JsonTable jsonTable = new JsonTable();
704 Map<String, String> consistencyInfo = new HashMap<>();
705 Map<String, String> fields = new HashMap<>();
706 fields.put("uuid", "text");
707 fields.put("emp_name", "text");
708 fields.put("emp_salary", "varint");
709 fields.put("PRIMARY KEY", "(emp_name)");
710 consistencyInfo.put("type", "eventual");
711 jsonTable.setConsistencyInfo(consistencyInfo);
712 jsonTable.setKeyspaceName(keyspaceName);
713 jsonTable.setPrimaryKey("emp_name");
714 jsonTable.setTableName(tableName);
715 jsonTable.setFields(fields);
716 // Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
717 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
718 authorization, jsonTable, keyspaceName, tableName);
722 * Create table {@link createTable} and insert into said table
726 private void createAndInsertIntoTable() throws Exception {
729 JsonInsert jsonInsert = new JsonInsert();
730 Map<String, String> consistencyInfo = new HashMap<>();
731 Map<String, Object> values = new HashMap<>();
732 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
733 values.put("emp_name", "testname");
734 values.put("emp_salary", 500);
735 consistencyInfo.put("type", "eventual");
736 jsonInsert.setConsistencyInfo(consistencyInfo);
737 jsonInsert.setKeyspaceName(keyspaceName);
738 jsonInsert.setTableName(tableName);
739 jsonInsert.setValues(values);
740 Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
741 authorization, jsonInsert, keyspaceName, tableName);
744 private void insertAnotherIntoTable() throws Exception {
747 JsonInsert jsonInsert = new JsonInsert();
748 Map<String, String> consistencyInfo = new HashMap<>();
749 Map<String, Object> values = new HashMap<>();
750 values.put("uuid", "cccccccc-d857-4e90-b1e5-df98a3d40cd6");
751 values.put("emp_name", "testname2");
752 values.put("emp_salary", 700);
753 consistencyInfo.put("type", "eventual");
754 jsonInsert.setConsistencyInfo(consistencyInfo);
755 jsonInsert.setKeyspaceName(keyspaceName);
756 jsonInsert.setTableName(tableName);
757 jsonInsert.setValues(values);
758 Response response = data.insertIntoTable("1", "1", "1", "abcdef00-d857-4e90-b1e5-df98a3d40ce6", appName,
759 authorization, jsonInsert, keyspaceName, tableName);
762 private JsonLock createJsonLock(LockType lockType) {
763 JsonLock jsonLock = new JsonLock();
764 jsonLock.setLockType(lockType);