2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2019 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 java.math.BigInteger;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
31 import java.util.UUID;
32 import javax.servlet.http.HttpServletResponse;
33 import javax.ws.rs.core.MultivaluedHashMap;
34 import javax.ws.rs.core.MultivaluedMap;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.UriInfo;
37 import org.junit.After;
38 import org.junit.AfterClass;
39 import org.junit.BeforeClass;
40 import org.junit.Ignore;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mindrot.jbcrypt.BCrypt;
44 import org.mockito.Mock;
45 import org.mockito.Mockito;
46 import org.mockito.junit.MockitoJUnitRunner;
47 import org.onap.music.authentication.CachingUtil;
48 import org.onap.music.datastore.MusicDataStoreHandle;
49 import org.onap.music.datastore.PreparedQueryObject;
50 import org.onap.music.datastore.jsonobjects.JsonDelete;
51 import org.onap.music.datastore.jsonobjects.JsonInsert;
52 import org.onap.music.datastore.jsonobjects.JsonKeySpace;
53 import org.onap.music.datastore.jsonobjects.JsonSelect;
54 import org.onap.music.datastore.jsonobjects.JsonTable;
55 import org.onap.music.datastore.jsonobjects.JsonUpdate;
56 import org.onap.music.exceptions.MusicServiceException;
57 import org.onap.music.main.MusicCore;
58 import org.onap.music.main.MusicUtil;
59 import org.onap.music.main.ResultType;
60 import org.onap.music.rest.RestMusicDataAPI;
61 import org.onap.music.rest.RestMusicLocksAPI;
62 import com.datastax.driver.core.DataType;
63 import com.datastax.driver.core.ResultSet;
64 import com.datastax.driver.core.Row;
65 import com.sun.jersey.core.util.Base64;
66 import com.sun.jersey.core.util.MultivaluedMapImpl;
68 @RunWith(MockitoJUnitRunner.class)
69 public class TstRestMusicDataAPI {
71 RestMusicDataAPI data = new RestMusicDataAPI();
72 RestMusicLocksAPI lock = new RestMusicLocksAPI();
73 static PreparedQueryObject testObject;
76 HttpServletResponse http;
81 static String appName = "TestApp";
82 static String userId = "TestUser";
83 static String password = "TestPassword";
84 static String authData = userId + ":" + password;
85 static String wrongAuthData = userId + ":" + "pass";
86 static String authorization = new String(Base64.encode(authData.getBytes()));
87 static String wrongAuthorization = new String(Base64.encode(wrongAuthData.getBytes()));
88 static boolean isAAF = false;
89 static UUID uuid = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40ce6");
90 static String keyspaceName = "testcassa";
91 static String tableName = "employees";
92 static String xLatestVersion = "X-latestVersion";
93 static String onboardUUID = null;
94 static String aid = TestsUsingCassandra.aid;
97 public static void init() throws Exception {
98 System.out.println("Testing RestMusicData class");
101 } catch (Exception e) {
103 throw new Exception("Unable to initialize before TestRestMusicData test class. " + e.getMessage());
108 public void afterEachTest() throws MusicServiceException {
109 clearAllTablesFromKeyspace();
113 public static void tearDownAfterClass() throws Exception {
114 testObject = new PreparedQueryObject();
115 testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName);
116 MusicCore.eventualPut(testObject);
120 public void test1_createKeyspace() throws Exception {
121 System.out.println("Testing create keyspace");
122 JsonKeySpace jsonKeyspace = new JsonKeySpace();
123 Map<String, String> consistencyInfo = new HashMap<>();
124 Map<String, Object> replicationInfo = new HashMap<>();
125 consistencyInfo.put("type", "eventual");
126 replicationInfo.put("class", "SimpleStrategy");
127 replicationInfo.put("replication_factor", 1);
128 jsonKeyspace.setConsistencyInfo(consistencyInfo);
129 jsonKeyspace.setDurabilityOfWrites("true");
130 jsonKeyspace.setKeyspaceName(keyspaceName);
131 jsonKeyspace.setReplicationInfo(replicationInfo);
132 // Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
134 data.createKeySpace("1", "1", "1", null, authorization, appName, jsonKeyspace, keyspaceName);
135 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
136 assertEquals(400, response.getStatus());
137 Map<String, String> respMap = (Map<String, String>) response.getEntity();
138 assertEquals(ResultType.FAILURE, respMap.get("status"));
142 public void test3_createTable() throws Exception {
143 System.out.println("Testing create table");
144 JsonTable jsonTable = new JsonTable();
145 Map<String, String> consistencyInfo = new HashMap<>();
146 Map<String, String> fields = new HashMap<>();
147 fields.put("uuid", "text");
148 fields.put("emp_name", "text");
149 fields.put("emp_salary", "varint");
150 fields.put("PRIMARY KEY", "(emp_name)");
151 consistencyInfo.put("type", "eventual");
152 jsonTable.setConsistencyInfo(consistencyInfo);
153 jsonTable.setKeyspaceName(keyspaceName);
154 jsonTable.setPrimaryKey("emp_name");
155 jsonTable.setTableName(tableName);
156 jsonTable.setFields(fields);
157 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
158 authorization, jsonTable, keyspaceName, tableName);
159 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
160 assertEquals(200, response.getStatus());
164 public void test3_createTableNoName() throws Exception {
165 System.out.println("Testing create table without name");
166 JsonTable jsonTable = new JsonTable();
167 Map<String, String> consistencyInfo = new HashMap<>();
168 Map<String, String> fields = new HashMap<>();
169 fields.put("uuid", "text");
170 fields.put("emp_name", "text");
171 fields.put("emp_salary", "varint");
172 fields.put("PRIMARY KEY", "(emp_name)");
173 consistencyInfo.put("type", "eventual");
174 jsonTable.setConsistencyInfo(consistencyInfo);
175 jsonTable.setKeyspaceName(keyspaceName);
176 jsonTable.setPrimaryKey("emp_name");
177 jsonTable.setTableName("");
178 jsonTable.setFields(fields);
179 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
180 authorization, jsonTable, keyspaceName, "");
181 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
182 assertEquals(400, response.getStatus());
187 public void test3_createTableClusterOrderBad() throws Exception {
188 System.out.println("Testing create table bad clustering");
189 JsonTable jsonTable = new JsonTable();
190 Map<String, String> consistencyInfo = new HashMap<>();
191 Map<String, String> fields = new HashMap<>();
192 fields.put("uuid", "text");
193 fields.put("emp_name", "text");
194 fields.put("emp_salary", "varint");
195 fields.put("PRIMARY KEY", "(emp_name,emp_salary)");
196 consistencyInfo.put("type", "eventual");
197 jsonTable.setConsistencyInfo(consistencyInfo);
198 jsonTable.setKeyspaceName(keyspaceName);
199 jsonTable.setPrimaryKey("emp_name,emp_salary");
200 jsonTable.setClusteringOrder("ASC");
201 jsonTable.setTableName(tableName);
202 jsonTable.setFields(fields);
203 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
204 authorization, jsonTable, keyspaceName, tableName);
205 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
206 assertEquals(400, response.getStatus());
210 public void test3_createTable_withPropertiesNotNull() throws Exception {
211 System.out.println("Testing create table with properties");
212 JsonTable jsonTable = new JsonTable();
213 Map<String, String> consistencyInfo = new HashMap<>();
214 Map<String, String> fields = new HashMap<>();
215 fields.put("uuid", "text");
216 fields.put("emp_name", "text");
217 fields.put("emp_salary", "varint");
218 fields.put("PRIMARY KEY", "(emp_name)");
219 consistencyInfo.put("type", "eventual");
220 Map<String, Object> properties = new HashMap<>();
221 properties.put("comment", "Testing prperties not null");
222 jsonTable.setConsistencyInfo(consistencyInfo);
223 jsonTable.setKeyspaceName(keyspaceName);
224 jsonTable.setPrimaryKey("emp_name");
225 String tableName_prop = tableName + "_Prop";
226 jsonTable.setTableName(tableName_prop);
227 jsonTable.setFields(fields);
228 jsonTable.setProperties(properties);
230 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
231 authorization, jsonTable, keyspaceName, tableName_prop);
232 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
233 assertEquals(200, response.getStatus());
237 public void test3_createTable_duplicateTable() throws Exception {
238 System.out.println("Testing creating duplicate tables");
239 JsonTable jsonTable = new JsonTable();
240 Map<String, String> consistencyInfo = new HashMap<>();
241 Map<String, String> fields = new HashMap<>();
242 fields.put("uuid", "text");
243 fields.put("emp_name", "text");
244 fields.put("emp_salary", "varint");
245 fields.put("PRIMARY KEY", "(emp_name)");
246 consistencyInfo.put("type", "eventual");
247 jsonTable.setConsistencyInfo(consistencyInfo);
248 jsonTable.setKeyspaceName(keyspaceName);
249 jsonTable.setPrimaryKey("emp_name");
250 String tableNameDup = tableName + "x";
251 jsonTable.setTableName(tableNameDup);
252 jsonTable.setFields(fields);
253 // Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
254 Response response1 = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
255 authorization, jsonTable, keyspaceName, tableNameDup);
257 Response response2 = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
258 authorization, jsonTable, keyspaceName, tableNameDup);
259 System.out.println("Status: " + response2.getStatus() + ". Entity " + response2.getEntity());
261 assertEquals(400, response2.getStatus());
262 Map<String, String> respMap = (Map<String, String>) response2.getEntity();
263 assertEquals(ResultType.FAILURE, respMap.get("status"));
264 assertEquals("Table " + keyspaceName + "." + tableNameDup + " already exists", respMap.get("error"));
269 public void test3_createTable1() throws Exception {
270 System.out.println("Testing create table w/ improper authentication");
271 JsonTable jsonTable = new JsonTable();
272 Map<String, String> consistencyInfo = new HashMap<>();
273 Map<String, String> fields = new HashMap<>();
274 fields.put("uuid", "text");
275 fields.put("emp_name", "text");
276 fields.put("emp_salary", "varint");
277 fields.put("PRIMARY KEY", "(emp_name)");
278 consistencyInfo.put("type", "eventual");
279 jsonTable.setConsistencyInfo(consistencyInfo);
280 jsonTable.setKeyspaceName(keyspaceName);
281 jsonTable.setPrimaryKey("emp_name");
282 jsonTable.setTableName(tableName);
283 jsonTable.setFields(fields);
284 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
285 wrongAuthorization, jsonTable, keyspaceName, tableName);
286 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
287 assertEquals(401, response.getStatus());
292 public void test3_createTable3() throws Exception {
293 System.out.println("Testing create table for wrong keyspace");
294 JsonTable jsonTable = new JsonTable();
295 Map<String, String> consistencyInfo = new HashMap<>();
296 Map<String, String> fields = new HashMap<>();
297 fields.put("uuid", "text");
298 fields.put("emp_name", "text");
299 fields.put("emp_salary", "varint");
300 fields.put("PRIMARY KEY", "(emp_name)");
301 consistencyInfo.put("type", "eventual");
302 jsonTable.setConsistencyInfo(consistencyInfo);
303 jsonTable.setKeyspaceName(keyspaceName);
304 jsonTable.setPrimaryKey("emp_name");
305 jsonTable.setTableName(tableName);
306 jsonTable.setFields(fields);
307 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
308 authorization, jsonTable, "wrong", tableName);
309 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
310 assertEquals(401, response.getStatus());
313 // Improper parenthesis in key field
315 public void test3_createTable_badParantesis() throws Exception {
316 System.out.println("Testing malformed create table request");
317 String tableNameC = "testTable0";
318 JsonTable jsonTable = new JsonTable();
319 Map<String, String> consistencyInfo = new HashMap<>();
320 Map<String, String> fields = new HashMap<>();
321 fields.put("uuid", "text");
322 fields.put("emp_name", "text");
323 fields.put("emp_salary", "varint");
324 fields.put("PRIMARY KEY", "(emp_name),emp_id)");
325 fields.put("emp_id", "varint");
326 consistencyInfo.put("type", "eventual");
327 jsonTable.setConsistencyInfo(consistencyInfo);
328 jsonTable.setKeyspaceName(keyspaceName);
329 jsonTable.setPrimaryKey("emp_name");
330 jsonTable.setTableName(tableNameC);
331 jsonTable.setClusteringOrder("emp_id Desc");
332 jsonTable.setFields(fields);
333 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
334 authorization, jsonTable, keyspaceName, tableNameC);
335 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
337 assertEquals(400, response.getStatus());
341 // good clustering key
343 public void test3_createTable_1_clusterKey_good() throws Exception {
344 System.out.println("Testing create w/ clusterKey");
346 String tableNameC = "testTableC1";
347 JsonTable jsonTable = new JsonTable();
348 Map<String, String> consistencyInfo = new HashMap<>();
349 Map<String, String> fields = new HashMap<>();
350 fields.put("uuid", "text");
351 fields.put("emp_name", "text");
352 fields.put("emp_salary", "varint");
353 fields.put("PRIMARY KEY", "((emp_name),emp_salary)");
354 consistencyInfo.put("type", "eventual");
355 jsonTable.setConsistencyInfo(consistencyInfo);
356 jsonTable.setKeyspaceName(keyspaceName);
357 // jsonTable.setPrimaryKey("emp_name");
358 jsonTable.setTableName(tableNameC);
359 jsonTable.setClusteringOrder("emp_salary ASC");
360 jsonTable.setFields(fields);
361 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
362 authorization, jsonTable, keyspaceName, tableNameC);
363 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
365 assertEquals(200, response.getStatus());
368 // bad partition key=clustering key
370 public void test3_createTable_2_clusterKey_bad() throws Exception {
371 System.out.println("Testing create w/ bad clusterKey");
372 String tableNameC = "testTableC2";
373 JsonTable jsonTable = new JsonTable();
374 Map<String, String> consistencyInfo = new HashMap<>();
375 Map<String, String> fields = new HashMap<>();
376 fields.put("uuid", "text");
377 fields.put("emp_name", "text");
378 fields.put("emp_salary", "varint");
379 fields.put("PRIMARY KEY", "((emp_name),emp_name)");
380 consistencyInfo.put("type", "eventual");
381 jsonTable.setConsistencyInfo(consistencyInfo);
382 jsonTable.setKeyspaceName(keyspaceName);
383 jsonTable.setPrimaryKey("emp_name"); // "PRIMARY KEY" overrides if primaryKey present
384 jsonTable.setTableName(tableNameC);
385 jsonTable.setClusteringOrder("emp_salary ASC");
386 jsonTable.setFields(fields);
387 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
388 authorization, jsonTable, keyspaceName, tableNameC);
389 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
391 assertEquals(400, response.getStatus());
394 // good composite partition key,clustering key
396 public void test3_createTable_3_pfartition_clusterKey_good() throws Exception {
397 System.out.println("Testing create w/ composite partition key, clusterKey");
399 String tableNameC = "testTableC3";
400 JsonTable jsonTable = new JsonTable();
401 Map<String, String> consistencyInfo = new HashMap<>();
402 Map<String, String> fields = new HashMap<>();
403 fields.put("uuid", "text");
404 fields.put("emp_name", "text");
405 fields.put("emp_id", "varint");
406 fields.put("emp_salary", "varint");
407 fields.put("PRIMARY KEY", "((emp_name,emp_id),emp_salary)");
408 consistencyInfo.put("type", "eventual");
409 jsonTable.setConsistencyInfo(consistencyInfo);
410 jsonTable.setKeyspaceName(keyspaceName);
411 jsonTable.setPrimaryKey("emp_name");
412 jsonTable.setTableName(tableNameC);
413 jsonTable.setClusteringOrder("emp_salary ASC");
414 jsonTable.setFields(fields);
415 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
416 authorization, jsonTable, keyspaceName, tableNameC);
417 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
419 assertEquals(200, response.getStatus());
422 // bad - wrong cols in order by of composite partition key,clustering key
424 public void test3_createTable_5_clusteringOrder_bad() throws Exception {
425 System.out.println("Testing create table bad request with clustering & composite keys");
426 String tableNameC = "testTableC5";
427 JsonTable jsonTable = new JsonTable();
428 Map<String, String> consistencyInfo = new HashMap<>();
429 Map<String, String> fields = new HashMap<>();
430 fields.put("uuid", "text");
431 fields.put("emp_name", "text");
432 fields.put("emp_id", "varint");
433 fields.put("emp_salary", "varint");
434 fields.put("PRIMARY KEY", "((uuid,emp_name),emp_id,emp_salary)");
435 consistencyInfo.put("type", "eventual");
436 jsonTable.setConsistencyInfo(consistencyInfo);
437 jsonTable.setKeyspaceName(keyspaceName);
438 jsonTable.setPrimaryKey("emp_name");
439 jsonTable.setTableName(tableNameC);
440 jsonTable.setClusteringOrder("emp_idx desc, emp_salary ASC");
441 jsonTable.setFields(fields);
442 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
443 authorization, jsonTable, keyspaceName, tableNameC);
444 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
446 assertEquals(400, response.getStatus());
450 // good clustering key, need to pass queryparameter
452 public void test3_createTableIndex_1() throws Exception {
453 System.out.println("Testing index in create table");
454 String tableNameC = "testTableCinx";
455 JsonTable jsonTable = new JsonTable();
456 Map<String, String> consistencyInfo = new HashMap<>();
457 Map<String, String> fields = new HashMap<>();
458 fields.put("uuid", "text");
459 fields.put("emp_name", "text");
460 fields.put("emp_salary", "varint");
461 fields.put("PRIMARY KEY", "((emp_name),emp_salary)");
462 consistencyInfo.put("type", "eventual");
463 jsonTable.setConsistencyInfo(consistencyInfo);
464 jsonTable.setKeyspaceName(keyspaceName);
465 jsonTable.setTableName(tableNameC);
466 jsonTable.setClusteringOrder("emp_salary ASC");
467 jsonTable.setFields(fields);
468 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
469 authorization, jsonTable, keyspaceName, tableNameC);
470 // if 200 print to log otherwise fail assertEquals(200, response.getStatus());
471 // info.setQueryParameters("index_name=inx_uuid");
472 Map<String, String> queryParametersMap = new HashMap<String, String>();
474 queryParametersMap.put("index_name", "inxuuid");
475 Mockito.when(info.getQueryParameters()).thenReturn(new MultivaluedHashMap<String, String>(queryParametersMap));
476 response = data.createIndex("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization,
477 keyspaceName, tableNameC, "uuid", info);
478 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
480 assertEquals(200, response.getStatus());
483 // create index without table name
485 public void test3_createTableIndexNoName() throws Exception {
486 System.out.println("Testing index in create table w/o tablename");
487 String tableNameC = "testTableCinx";
488 JsonTable jsonTable = new JsonTable();
489 Map<String, String> consistencyInfo = new HashMap<>();
490 Map<String, String> fields = new HashMap<>();
491 fields.put("uuid", "text");
492 fields.put("emp_name", "text");
493 fields.put("emp_salary", "varint");
494 fields.put("PRIMARY KEY", "((emp_name),emp_salary)");
495 consistencyInfo.put("type", "eventual");
496 jsonTable.setConsistencyInfo(consistencyInfo);
497 jsonTable.setKeyspaceName(keyspaceName);
498 jsonTable.setTableName(tableNameC);
499 jsonTable.setClusteringOrder("emp_salary ASC");
500 jsonTable.setFields(fields);
501 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
502 authorization, jsonTable, keyspaceName, tableNameC);
503 // if 200 print to log otherwise fail assertEquals(200, response.getStatus());
504 // info.setQueryParameters("index_name=inx_uuid");
505 Map<String, String> queryParametersMap = new HashMap<String, String>();
507 queryParametersMap.put("index_name", "inxuuid");
508 response = data.createIndex("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, "",
510 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
512 assertEquals(400, response.getStatus());
516 public void test4_insertIntoTable() throws Exception {
517 System.out.println("Testing insert into table");
519 JsonInsert jsonInsert = new JsonInsert();
520 Map<String, String> consistencyInfo = new HashMap<>();
521 Map<String, Object> values = new HashMap<>();
522 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
523 values.put("emp_name", "testname");
524 values.put("emp_salary", 500);
525 consistencyInfo.put("type", "eventual");
526 jsonInsert.setConsistencyInfo(consistencyInfo);
527 jsonInsert.setKeyspaceName(keyspaceName);
528 jsonInsert.setTableName(tableName);
529 jsonInsert.setValues(values);
530 Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
531 authorization, jsonInsert, keyspaceName, tableName);
532 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
534 assertEquals(200, response.getStatus());
538 public void test4_insertIntoTableCriticalNoLockID() throws Exception {
539 System.out.println("Testing critical insert into table without lockid");
541 JsonInsert jsonInsert = new JsonInsert();
542 Map<String, String> consistencyInfo = new HashMap<>();
543 Map<String, Object> values = new HashMap<>();
544 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
545 values.put("emp_name", "testname");
546 values.put("emp_salary", 500);
547 consistencyInfo.put("type", "critical");
548 jsonInsert.setConsistencyInfo(consistencyInfo);
549 jsonInsert.setKeyspaceName(keyspaceName);
550 jsonInsert.setTableName(tableName);
551 jsonInsert.setValues(values);
552 Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
553 appName, authorization, jsonInsert, keyspaceName, tableName);
554 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
556 assertEquals(400, response.getStatus());
560 public void test4_insertIntoTableAtomic() throws Exception {
561 System.out.println("Testing atomic insert into table without lockid");
563 JsonInsert jsonInsert = new JsonInsert();
564 Map<String, String> consistencyInfo = new HashMap<>();
565 Map<String, Object> values = new HashMap<>();
566 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
567 values.put("emp_name", "testname");
568 values.put("emp_salary", 500);
569 consistencyInfo.put("type", "atomic");
570 jsonInsert.setConsistencyInfo(consistencyInfo);
571 jsonInsert.setKeyspaceName(keyspaceName);
572 jsonInsert.setTableName(tableName);
573 jsonInsert.setValues(values);
574 Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
575 appName, authorization, jsonInsert, keyspaceName, tableName);
576 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
578 assertEquals(200, response.getStatus());
582 public void test4_insertIntoTableNoName() throws Exception {
583 System.out.println("Testing insert into table w/o table name");
585 JsonInsert jsonInsert = new JsonInsert();
586 Map<String, String> consistencyInfo = new HashMap<>();
587 Map<String, Object> values = new HashMap<>();
588 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
589 values.put("emp_name", "testname");
590 values.put("emp_salary", 500);
591 consistencyInfo.put("type", "eventual");
592 jsonInsert.setConsistencyInfo(consistencyInfo);
593 jsonInsert.setKeyspaceName(keyspaceName);
594 jsonInsert.setTableName(tableName);
595 jsonInsert.setValues(values);
596 Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
597 appName, authorization, jsonInsert, "", "");
598 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
600 assertEquals(400, response.getStatus());
604 public void test4_insertIntoTable2() throws Exception {
605 System.out.println("Testing insert into table #2");
607 JsonInsert jsonInsert = new JsonInsert();
608 Map<String, String> consistencyInfo = new HashMap<>();
609 Map<String, Object> values = new HashMap<>();
610 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
611 values.put("emp_name", "test1");
612 values.put("emp_salary", 1500);
613 consistencyInfo.put("type", "eventual");
614 jsonInsert.setConsistencyInfo(consistencyInfo);
615 jsonInsert.setKeyspaceName(keyspaceName);
616 jsonInsert.setTableName(tableName);
617 jsonInsert.setValues(values);
618 Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
619 authorization, jsonInsert, keyspaceName, tableName);
620 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
622 assertEquals(200, response.getStatus());
627 public void test4_insertIntoTable3() throws Exception {
628 System.out.println("Testing insert into table with bad credentials");
630 JsonInsert jsonInsert = new JsonInsert();
631 Map<String, String> consistencyInfo = new HashMap<>();
632 Map<String, Object> values = new HashMap<>();
633 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
634 values.put("emp_name", "test1");
635 values.put("emp_salary", 1500);
636 consistencyInfo.put("type", "eventual");
637 jsonInsert.setConsistencyInfo(consistencyInfo);
638 jsonInsert.setKeyspaceName(keyspaceName);
639 jsonInsert.setTableName(tableName);
640 jsonInsert.setValues(values);
641 Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
642 wrongAuthorization, jsonInsert, keyspaceName, tableName);
643 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
645 assertEquals(401, response.getStatus());
650 public void test4_insertIntoTable4() throws Exception {
651 System.out.println("Testing insert into wrong table");
653 JsonInsert jsonInsert = new JsonInsert();
654 Map<String, String> consistencyInfo = new HashMap<>();
655 Map<String, Object> values = new HashMap<>();
656 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
657 values.put("emp_name", "test1");
658 values.put("emp_salary", 1500);
659 consistencyInfo.put("type", "eventual");
660 jsonInsert.setConsistencyInfo(consistencyInfo);
661 jsonInsert.setKeyspaceName(keyspaceName);
662 jsonInsert.setTableName(tableName);
663 jsonInsert.setValues(values);
664 Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
665 authorization, jsonInsert, keyspaceName, "wrong");
666 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
668 assertEquals(400, response.getStatus());
672 public void test4_insertBlobIntoTable() throws Exception {
673 System.out.println("Testing insert a blob into table");
675 JsonInsert jsonInsert = new JsonInsert();
676 Map<String, String> consistencyInfo = new HashMap<>();
677 Map<String, Object> values = new HashMap<>();
678 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
679 values.put("emp_name", "testname");
680 values.put("emp_salary", 500);
681 values.put("binary", "somestuffhere");
682 consistencyInfo.put("type", "eventual");
683 jsonInsert.setConsistencyInfo(consistencyInfo);
684 jsonInsert.setKeyspaceName(keyspaceName);
685 jsonInsert.setTableName(tableName);
686 jsonInsert.setValues(values);
687 Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
688 authorization, jsonInsert, keyspaceName, tableName);
689 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
691 assertEquals(200, response.getStatus());
695 public void test5_updateTable() throws Exception {
696 System.out.println("Testing update table");
699 JsonUpdate jsonUpdate = new JsonUpdate();
700 Map<String, String> consistencyInfo = new HashMap<>();
701 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
702 Map<String, Object> values = new HashMap<>();
703 row.add("emp_name", "testname");
704 values.put("emp_salary", 2500);
705 consistencyInfo.put("type", "atomic");
706 jsonUpdate.setConsistencyInfo(consistencyInfo);
707 jsonUpdate.setKeyspaceName(keyspaceName);
708 jsonUpdate.setTableName(tableName);
709 jsonUpdate.setValues(values);
710 Mockito.when(info.getQueryParameters()).thenReturn(row);
711 Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
712 authorization, jsonUpdate, keyspaceName, tableName, info);
714 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
716 assertEquals(200, response.getStatus());
720 public void test5_updateTableNoName() throws Exception {
721 System.out.println("Testing update table without tablename");
724 JsonUpdate jsonUpdate = new JsonUpdate();
725 Map<String, String> consistencyInfo = new HashMap<>();
726 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
727 Map<String, Object> values = new HashMap<>();
728 row.add("emp_name", "testname");
729 values.put("emp_salary", 2500);
730 consistencyInfo.put("type", "atomic");
731 jsonUpdate.setConsistencyInfo(consistencyInfo);
732 jsonUpdate.setKeyspaceName(keyspaceName);
733 jsonUpdate.setTableName(tableName);
734 jsonUpdate.setValues(values);
735 Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
736 authorization, jsonUpdate, "", "", info);
738 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
740 assertEquals(400, response.getStatus());
743 // need mock code to create error for MusicCore methods
745 public void test5_updateTableAuthE() throws Exception {
746 System.out.println("Testing update table #2");
748 // MockitoAnnotations.initMocks(this);
749 JsonUpdate jsonUpdate = new JsonUpdate();
750 Map<String, String> consistencyInfo = new HashMap<>();
751 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
752 Map<String, Object> values = new HashMap<>();
753 row.add("emp_name", "testname");
754 values.put("emp_salary", 2500);
755 consistencyInfo.put("type", "atomic");
756 jsonUpdate.setConsistencyInfo(consistencyInfo);
757 jsonUpdate.setKeyspaceName(keyspaceName);
758 jsonUpdate.setTableName(tableName);
759 jsonUpdate.setValues(values);
760 // add ttl & timestamp
761 // Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
762 Mockito.when(info.getQueryParameters()).thenReturn(row);
763 // Map<String, Object> m1= new HashMap<>() ;
764 // Mockito.when(MusicCore.autheticateUser(appName,userId,password,keyspaceName,"abc66ccc-d857-4e90-b1e5-df98a3d40ce6","updateTable")).thenReturn(m1);
765 Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
766 authorization, jsonUpdate, keyspaceName, tableName, info);
767 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
769 assertEquals(200, response.getStatus());
774 public void test5_updateTableAuthException1() throws Exception {
775 System.out.println("Testing update table authentication error");
777 JsonUpdate jsonUpdate = new JsonUpdate();
778 Map<String, String> consistencyInfo = new HashMap<>();
779 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
780 Map<String, Object> values = new HashMap<>();
781 row.add("emp_name", "testname");
782 values.put("emp_salary", 2500);
783 consistencyInfo.put("type", "atomic");
784 jsonUpdate.setConsistencyInfo(consistencyInfo);
785 jsonUpdate.setKeyspaceName(keyspaceName);
786 jsonUpdate.setTableName(tableName);
787 jsonUpdate.setValues(values);
789 Mockito.when(info.getQueryParameters()).thenReturn(row);
790 String authDatax = ":";
791 String authorizationx = new String(Base64.encode(authDatax.getBytes()));
792 Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
793 authorizationx, jsonUpdate, keyspaceName, tableName, info);
794 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
796 assertEquals(401, response.getStatus());
801 public void test5_updateTableAuthEmpty() throws Exception {
802 System.out.println("Testing update table without authentication");
805 JsonUpdate jsonUpdate = new JsonUpdate();
806 Map<String, String> consistencyInfo = new HashMap<>();
807 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
808 Map<String, Object> values = new HashMap<>();
809 row.add("emp_name", "testname");
810 values.put("emp_salary", 2500);
811 consistencyInfo.put("type", "atomic");
812 jsonUpdate.setConsistencyInfo(consistencyInfo);
813 jsonUpdate.setKeyspaceName(keyspaceName);
814 jsonUpdate.setTableName(tableName);
815 jsonUpdate.setValues(values);
817 Mockito.when(info.getQueryParameters()).thenReturn(row);
818 String authDatax = ":" + password;
819 String authorizationx = new String(Base64.encode(authDatax.getBytes()));
820 String appNamex = "xx";
821 Response response = data.updateTable("1", "1", "1", "", appNamex, authorizationx, jsonUpdate, keyspaceName,
823 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
825 assertEquals(401, response.getStatus());
829 public void test6_selectAtomic() throws Exception {
830 System.out.println("Testing select atomic");
831 createAndInsertIntoTable();
832 JsonInsert jsonInsert = new JsonInsert();
833 Map<String, String> consistencyInfo = new HashMap<>();
834 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
835 row.add("emp_name", "testname");
836 consistencyInfo.put("type", "atomic");
837 jsonInsert.setConsistencyInfo(consistencyInfo);
838 Mockito.when(info.getQueryParameters()).thenReturn(row);
839 Response response = data.selectCritical("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
840 appName, authorization, jsonInsert, keyspaceName, tableName,info);
841 HashMap<String,HashMap<String,Object>> map = (HashMap<String, HashMap<String, Object>>) response.getEntity();
842 HashMap<String, Object> result = map.get("result");
843 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
845 Map<String, String> row0 = (Map<String, String>) result.get("row 0");
846 assertEquals("testname", row0.get("emp_name"));
847 assertEquals(BigInteger.valueOf(500), row0.get("emp_salary"));
851 public void test6_select() throws Exception {
852 System.out.println("Testing select");
853 createAndInsertIntoTable();
854 JsonSelect jsonSelect = new JsonSelect();
855 Map<String, String> consistencyInfo = new HashMap<>();
856 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
857 row.add("emp_name", "testname");
858 consistencyInfo.put("type", "atomic");
859 jsonSelect.setConsistencyInfo(consistencyInfo);
860 Mockito.when(info.getQueryParameters()).thenReturn(row);
861 Response response = data.select("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization,
862 keyspaceName, tableName, info);
863 HashMap<String, HashMap<String, Object>> map = (HashMap<String, HashMap<String, Object>>) response.getEntity();
864 HashMap<String, Object> result = map.get("result");
865 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
867 Map<String, String> row0 = (Map<String, String>) result.get("row 0");
868 assertEquals("testname", row0.get("emp_name"));
869 assertEquals(BigInteger.valueOf(500), row0.get("emp_salary"));
873 public void test6_deleteFromTable() throws Exception {
874 System.out.println("Testing delete from table");
875 createAndInsertIntoTable();
876 JsonDelete jsonDelete = new JsonDelete();
877 Map<String, String> consistencyInfo = new HashMap<>();
878 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
879 row.add("emp_name", "test1");
880 consistencyInfo.put("type", "atomic");
881 jsonDelete.setConsistencyInfo(consistencyInfo);
882 Mockito.when(info.getQueryParameters()).thenReturn(row);
883 Response response = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
884 authorization, jsonDelete, keyspaceName, tableName, info);
885 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
891 public void test6_deleteFromTable1() throws Exception {
892 System.out.println("Testing delete from table missing delete object");
893 createAndInsertIntoTable();
895 JsonDelete jsonDelete = new JsonDelete();
896 Map<String, String> consistencyInfo = new HashMap<>();
897 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
898 consistencyInfo.put("type", "atomic");
899 jsonDelete.setConsistencyInfo(consistencyInfo);
900 // Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
901 Mockito.when(info.getQueryParameters()).thenReturn(row);
902 Response response = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
903 authorization, jsonDelete, keyspaceName, tableName, info);
904 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
906 assertEquals(400, response.getStatus());
911 public void test6_deleteFromTable2() throws Exception {
912 System.out.println("Testing delete from table missing delete object");
913 createAndInsertIntoTable();
914 JsonDelete jsonDelete = new JsonDelete();
915 Map<String, String> consistencyInfo = new HashMap<>();
916 consistencyInfo.put("type", "atomic");
917 jsonDelete.setConsistencyInfo(consistencyInfo);
918 Response response = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
919 authorization, null, keyspaceName, tableName, info);
920 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
922 assertEquals(400, response.getStatus());
926 public void test7_dropTable() throws Exception {
927 System.out.println("Testing drop table");
929 JsonTable jsonTable = new JsonTable();
930 Map<String, String> consistencyInfo = new HashMap<>();
931 consistencyInfo.put("type", "atomic");
932 jsonTable.setConsistencyInfo(consistencyInfo);
933 Response response = data.dropTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
934 authorization, keyspaceName, tableName);
935 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
937 assertEquals(200, response.getStatus());
942 public void test8_deleteKeyspace() throws Exception {
943 System.out.println("Testing drop keyspace");
945 JsonKeySpace jsonKeyspace = new JsonKeySpace();
946 Map<String, String> consistencyInfo = new HashMap<>();
947 Map<String, Object> replicationInfo = new HashMap<>();
948 consistencyInfo.put("type", "eventual");
949 replicationInfo.put("class", "SimpleStrategy");
950 replicationInfo.put("replication_factor", 1);
951 jsonKeyspace.setConsistencyInfo(consistencyInfo);
952 jsonKeyspace.setDurabilityOfWrites("true");
953 jsonKeyspace.setKeyspaceName("TestApp1");
954 jsonKeyspace.setReplicationInfo(replicationInfo);
955 Response response = data.dropKeySpace("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", authorization,
956 appName, keyspaceName);
957 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
959 assertEquals(400, response.getStatus());
962 private static void createKeyspace() throws Exception {
963 // shouldn't really be doing this here, but create keyspace is currently turned off
964 PreparedQueryObject query = new PreparedQueryObject();
965 query.appendQueryString(CassandraCQL.createKeySpace);
966 MusicCore.eventualPut(query);
968 boolean isAAF = false;
969 String hashedpwd = BCrypt.hashpw(password, BCrypt.gensalt());
970 query = new PreparedQueryObject();
971 query.appendQueryString("INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
972 + "password, username, is_aaf) values (?,?,?,?,?,?,?)");
973 query.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
974 query.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspaceName));
975 query.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
976 query.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
977 query.addValue(MusicUtil.convertToActualDataType(DataType.text(), hashedpwd));
978 query.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
979 query.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
980 CachingUtil.updateMusicCache(keyspaceName, appName);
981 CachingUtil.updateMusicValidateCache(appName, userId, hashedpwd);
982 MusicCore.eventualPut(query);
985 private void clearAllTablesFromKeyspace() throws MusicServiceException {
986 ArrayList<String> tableNames = new ArrayList<>();
987 PreparedQueryObject query = new PreparedQueryObject();
988 query.appendQueryString(
989 "SELECT table_name FROM system_schema.tables WHERE keyspace_name = '" + keyspaceName + "';");
990 ResultSet rs = MusicCore.get(query);
992 tableNames.add(row.getString("table_name"));
994 for (String table : tableNames) {
995 query = new PreparedQueryObject();
996 query.appendQueryString("DROP TABLE " + keyspaceName + "." + table);
997 MusicCore.eventualPut(query);
1002 * Create a table {@link tableName} in {@link keyspaceName}
1006 private void createTable() throws Exception {
1007 JsonTable jsonTable = new JsonTable();
1008 Map<String, String> consistencyInfo = new HashMap<>();
1009 Map<String, String> fields = new HashMap<>();
1010 fields.put("uuid", "text");
1011 fields.put("emp_name", "text");
1012 fields.put("emp_salary", "varint");
1013 fields.put("binary", "blob");
1014 fields.put("PRIMARY KEY", "(emp_name)");
1015 consistencyInfo.put("type", "eventual");
1016 jsonTable.setConsistencyInfo(consistencyInfo);
1017 jsonTable.setKeyspaceName(keyspaceName);
1018 jsonTable.setPrimaryKey("emp_name");
1019 jsonTable.setTableName(tableName);
1020 jsonTable.setFields(fields);
1021 // Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
1022 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
1023 authorization, jsonTable, keyspaceName, tableName);
1027 * Create table {@link createTable} and insert into said table
1031 private void createAndInsertIntoTable() throws Exception {
1034 JsonInsert jsonInsert = new JsonInsert();
1035 Map<String, String> consistencyInfo = new HashMap<>();
1036 Map<String, Object> values = new HashMap<>();
1037 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
1038 values.put("emp_name", "testname");
1039 values.put("emp_salary", 500);
1040 values.put("binary", "binarydatahere");
1041 consistencyInfo.put("type", "eventual");
1042 jsonInsert.setConsistencyInfo(consistencyInfo);
1043 jsonInsert.setKeyspaceName(keyspaceName);
1044 jsonInsert.setTableName(tableName);
1045 jsonInsert.setValues(values);
1046 Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
1047 authorization, jsonInsert, keyspaceName, tableName);