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, 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, 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, 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, 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 // Ignoring since this is now a duplicate of delete lock ref.
445 public void test_deleteLock() throws Exception {
446 System.out.println("Testing get lock state");
447 createAndInsertIntoTable();
449 String lockRef = createLockReference();
451 Response response = lock.deleteLock(lockName, "1", "1",
452 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", authorization, appName);
453 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
454 assertEquals(200, response.getStatus());
458 * Create table and lock reference
460 * @return the lock ref created
463 @SuppressWarnings("unchecked")
464 private String createLockReference() throws Exception {
465 Response response = lock.createLockReference(lockName, "1", "1", authorization,
466 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", null, appName);
467 Map<String, Object> respMap = (Map<String, Object>) response.getEntity();
468 return ((Map<String, String>) respMap.get("lock")).get("lock");
472 * Create table and lock reference
474 * @return the lock ref created
477 @SuppressWarnings("unchecked")
478 private String createLockReference(LockType lockType) throws Exception {
479 JsonLock jsonLock = createJsonLock(lockType);
480 Response response = lock.createLockReference(lockName, "1", "1", authorization,
481 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", jsonLock, appName);
482 Map<String, Object> respMap = (Map<String, Object>) response.getEntity();
483 return ((Map<String, String>) respMap.get("lock")).get("lock");
486 private static void createKeyspace() throws Exception {
487 // shouldn't really be doing this here, but create keyspace is currently turned off
488 PreparedQueryObject query = new PreparedQueryObject();
489 query.appendQueryString(CassandraCQL.createKeySpace);
490 MusicCore.eventualPut(query);
492 boolean isAAF = false;
493 String hashedpwd = BCrypt.hashpw(password, BCrypt.gensalt());
494 query = new PreparedQueryObject();
495 query.appendQueryString("INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
496 + "password, username, is_aaf) values (?,?,?,?,?,?,?)");
497 query.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
498 query.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspaceName));
499 query.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
500 query.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
501 query.addValue(MusicUtil.convertToActualDataType(DataType.text(), hashedpwd));
502 query.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
503 query.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
504 //CachingUtil.updateMusicCache(keyspaceName, appName);
505 //CachingUtil.updateMusicValidateCache(appName, userId, hashedpwd);
506 MusicCore.eventualPut(query);
509 private void clearAllTablesFromKeyspace() throws MusicServiceException {
510 ArrayList<String> tableNames = new ArrayList<>();
511 PreparedQueryObject query = new PreparedQueryObject();
512 query.appendQueryString(
513 "SELECT table_name FROM system_schema.tables WHERE keyspace_name = '" + keyspaceName + "';");
514 ResultSet rs = MusicCore.get(query);
516 tableNames.add(row.getString("table_name"));
518 for (String table : tableNames) {
519 query = new PreparedQueryObject();
520 query.appendQueryString("DROP TABLE " + keyspaceName + "." + table);
521 MusicCore.eventualPut(query);
526 * Create a table {@link tableName} in {@link keyspaceName}
530 private void createTable() throws Exception {
531 JsonTable jsonTable = new JsonTable();
532 Map<String, String> consistencyInfo = new HashMap<>();
533 Map<String, String> fields = new HashMap<>();
534 fields.put("uuid", "text");
535 fields.put("emp_name", "text");
536 fields.put("emp_salary", "varint");
537 fields.put("PRIMARY KEY", "(emp_name)");
538 consistencyInfo.put("type", "eventual");
539 jsonTable.setConsistencyInfo(consistencyInfo);
540 jsonTable.setKeyspaceName(keyspaceName);
541 jsonTable.setPrimaryKey("emp_name");
542 jsonTable.setTableName(tableName);
543 jsonTable.setFields(fields);
544 // Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
545 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
546 authorization, jsonTable, keyspaceName, tableName);
550 * Create table {@link createTable} and insert into said table
554 private void createAndInsertIntoTable() throws Exception {
557 JsonInsert jsonInsert = new JsonInsert();
558 Map<String, String> consistencyInfo = new HashMap<>();
559 Map<String, Object> values = new HashMap<>();
560 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
561 values.put("emp_name", "testname");
562 values.put("emp_salary", 500);
563 consistencyInfo.put("type", "eventual");
564 jsonInsert.setConsistencyInfo(consistencyInfo);
565 jsonInsert.setKeyspaceName(keyspaceName);
566 jsonInsert.setTableName(tableName);
567 jsonInsert.setValues(values);
568 Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
569 authorization, jsonInsert, keyspaceName, tableName);
572 private JsonLock createJsonLock(LockType lockType) {
573 JsonLock jsonLock = new JsonLock();
574 jsonLock.setLockType(lockType);