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.datastore.MusicDataStoreHandle;
48 import org.onap.music.datastore.PreparedQueryObject;
49 import org.onap.music.datastore.jsonobjects.JsonDelete;
50 import org.onap.music.datastore.jsonobjects.JsonInsert;
51 import org.onap.music.datastore.jsonobjects.JsonKeySpace;
52 import org.onap.music.datastore.jsonobjects.JsonSelect;
53 import org.onap.music.datastore.jsonobjects.JsonTable;
54 import org.onap.music.datastore.jsonobjects.JsonUpdate;
55 import org.onap.music.exceptions.MusicServiceException;
56 import org.onap.music.main.MusicCore;
57 import org.onap.music.main.MusicUtil;
58 import org.onap.music.main.ResultType;
59 import org.onap.music.rest.RestMusicDataAPI;
60 import org.onap.music.rest.RestMusicLocksAPI;
61 import com.datastax.driver.core.DataType;
62 import com.datastax.driver.core.ResultSet;
63 import com.datastax.driver.core.Row;
64 import com.sun.jersey.core.util.Base64;
65 import com.sun.jersey.core.util.MultivaluedMapImpl;
67 @RunWith(MockitoJUnitRunner.class)
68 public class TstRestMusicDataAPI {
70 RestMusicDataAPI data = new RestMusicDataAPI();
71 RestMusicLocksAPI lock = new RestMusicLocksAPI();
72 static PreparedQueryObject testObject;
75 HttpServletResponse http;
80 static String appName = "TestApp";
81 static String userId = "TestUser";
82 static String password = "TestPassword";
83 static String authData = userId + ":" + password;
84 static String wrongAuthData = userId + ":" + "pass";
85 static String authorization = new String(Base64.encode(authData.getBytes()));
86 static String wrongAuthorization = new String(Base64.encode(wrongAuthData.getBytes()));
87 static boolean isAAF = false;
88 static UUID uuid = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40ce6");
89 static String keyspaceName = "testcassa";
90 static String tableName = "employees";
91 static String xLatestVersion = "X-latestVersion";
92 static String onboardUUID = null;
93 static String aid = TestsUsingCassandra.aid;
96 public static void init() throws Exception {
97 System.out.println("Testing RestMusicData class");
100 } catch (Exception e) {
102 throw new Exception("Unable to initialize before TestRestMusicData test class. " + e.getMessage());
107 public void afterEachTest() throws MusicServiceException {
108 clearAllTablesFromKeyspace();
112 public static void tearDownAfterClass() throws Exception {
113 testObject = new PreparedQueryObject();
114 testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName);
115 MusicCore.eventualPut(testObject);
119 public void test1_createKeyspace() throws Exception {
120 System.out.println("Testing create keyspace");
121 JsonKeySpace jsonKeyspace = new JsonKeySpace();
122 Map<String, String> consistencyInfo = new HashMap<>();
123 Map<String, Object> replicationInfo = new HashMap<>();
124 consistencyInfo.put("type", "eventual");
125 replicationInfo.put("class", "SimpleStrategy");
126 replicationInfo.put("replication_factor", 1);
127 jsonKeyspace.setConsistencyInfo(consistencyInfo);
128 jsonKeyspace.setDurabilityOfWrites("true");
129 jsonKeyspace.setKeyspaceName(keyspaceName);
130 jsonKeyspace.setReplicationInfo(replicationInfo);
131 // Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
133 data.createKeySpace("1", "1", "1", null, authorization, appName, jsonKeyspace, keyspaceName);
134 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
135 assertEquals(400, response.getStatus());
136 Map<String, String> respMap = (Map<String, String>) response.getEntity();
137 assertEquals(ResultType.FAILURE, respMap.get("status"));
141 public void test3_createTable() throws Exception {
142 System.out.println("Testing create table");
143 JsonTable jsonTable = new JsonTable();
144 Map<String, String> consistencyInfo = new HashMap<>();
145 Map<String, String> fields = new HashMap<>();
146 fields.put("uuid", "text");
147 fields.put("emp_name", "text");
148 fields.put("emp_salary", "varint");
149 fields.put("PRIMARY KEY", "(emp_name)");
150 consistencyInfo.put("type", "eventual");
151 jsonTable.setConsistencyInfo(consistencyInfo);
152 jsonTable.setKeyspaceName(keyspaceName);
153 jsonTable.setPrimaryKey("emp_name");
154 jsonTable.setTableName(tableName);
155 jsonTable.setFields(fields);
156 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
157 authorization, jsonTable, keyspaceName, tableName);
158 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
159 assertEquals(200, response.getStatus());
163 public void test3_createTableNoName() throws Exception {
164 System.out.println("Testing create table without name");
165 JsonTable jsonTable = new JsonTable();
166 Map<String, String> consistencyInfo = new HashMap<>();
167 Map<String, String> fields = new HashMap<>();
168 fields.put("uuid", "text");
169 fields.put("emp_name", "text");
170 fields.put("emp_salary", "varint");
171 fields.put("PRIMARY KEY", "(emp_name)");
172 consistencyInfo.put("type", "eventual");
173 jsonTable.setConsistencyInfo(consistencyInfo);
174 jsonTable.setKeyspaceName(keyspaceName);
175 jsonTable.setPrimaryKey("emp_name");
176 jsonTable.setTableName("");
177 jsonTable.setFields(fields);
178 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
179 authorization, jsonTable, keyspaceName, "");
180 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
181 assertEquals(400, response.getStatus());
186 public void test3_createTableClusterOrderBad() throws Exception {
187 System.out.println("Testing create table bad clustering");
188 JsonTable jsonTable = new JsonTable();
189 Map<String, String> consistencyInfo = new HashMap<>();
190 Map<String, String> fields = new HashMap<>();
191 fields.put("uuid", "text");
192 fields.put("emp_name", "text");
193 fields.put("emp_salary", "varint");
194 fields.put("PRIMARY KEY", "(emp_name,emp_salary)");
195 consistencyInfo.put("type", "eventual");
196 jsonTable.setConsistencyInfo(consistencyInfo);
197 jsonTable.setKeyspaceName(keyspaceName);
198 jsonTable.setPrimaryKey("emp_name,emp_salary");
199 jsonTable.setClusteringOrder("ASC");
200 jsonTable.setTableName(tableName);
201 jsonTable.setFields(fields);
202 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
203 authorization, jsonTable, keyspaceName, tableName);
204 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
205 assertEquals(400, response.getStatus());
209 public void test3_createTable_withPropertiesNotNull() throws Exception {
210 System.out.println("Testing create table with properties");
211 JsonTable jsonTable = new JsonTable();
212 Map<String, String> consistencyInfo = new HashMap<>();
213 Map<String, String> fields = new HashMap<>();
214 fields.put("uuid", "text");
215 fields.put("emp_name", "text");
216 fields.put("emp_salary", "varint");
217 fields.put("PRIMARY KEY", "(emp_name)");
218 consistencyInfo.put("type", "eventual");
219 Map<String, Object> properties = new HashMap<>();
220 properties.put("comment", "Testing prperties not null");
221 jsonTable.setConsistencyInfo(consistencyInfo);
222 jsonTable.setKeyspaceName(keyspaceName);
223 jsonTable.setPrimaryKey("emp_name");
224 String tableName_prop = tableName + "_Prop";
225 jsonTable.setTableName(tableName_prop);
226 jsonTable.setFields(fields);
227 jsonTable.setProperties(properties);
229 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
230 authorization, jsonTable, keyspaceName, tableName_prop);
231 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
232 assertEquals(200, response.getStatus());
236 public void test3_createTable_duplicateTable() throws Exception {
237 System.out.println("Testing creating duplicate tables");
238 JsonTable jsonTable = new JsonTable();
239 Map<String, String> consistencyInfo = new HashMap<>();
240 Map<String, String> fields = new HashMap<>();
241 fields.put("uuid", "text");
242 fields.put("emp_name", "text");
243 fields.put("emp_salary", "varint");
244 fields.put("PRIMARY KEY", "(emp_name)");
245 consistencyInfo.put("type", "eventual");
246 jsonTable.setConsistencyInfo(consistencyInfo);
247 jsonTable.setKeyspaceName(keyspaceName);
248 jsonTable.setPrimaryKey("emp_name");
249 String tableNameDup = tableName + "x";
250 jsonTable.setTableName(tableNameDup);
251 jsonTable.setFields(fields);
252 // Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
253 Response response1 = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
254 authorization, jsonTable, keyspaceName, tableNameDup);
256 Response response2 = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
257 authorization, jsonTable, keyspaceName, tableNameDup);
258 System.out.println("Status: " + response2.getStatus() + ". Entity " + response2.getEntity());
260 assertEquals(400, response2.getStatus());
261 Map<String, String> respMap = (Map<String, String>) response2.getEntity();
262 assertEquals(ResultType.FAILURE, respMap.get("status"));
263 assertEquals("Table " + keyspaceName + "." + tableNameDup + " already exists", respMap.get("error"));
267 // Improper parenthesis in key field
269 public void test3_createTable_badParantesis() throws Exception {
270 System.out.println("Testing malformed create table request");
271 String tableNameC = "testTable0";
272 JsonTable jsonTable = new JsonTable();
273 Map<String, String> consistencyInfo = new HashMap<>();
274 Map<String, String> fields = new HashMap<>();
275 fields.put("uuid", "text");
276 fields.put("emp_name", "text");
277 fields.put("emp_salary", "varint");
278 fields.put("PRIMARY KEY", "(emp_name),emp_id)");
279 fields.put("emp_id", "varint");
280 consistencyInfo.put("type", "eventual");
281 jsonTable.setConsistencyInfo(consistencyInfo);
282 jsonTable.setKeyspaceName(keyspaceName);
283 jsonTable.setPrimaryKey("emp_name");
284 jsonTable.setTableName(tableNameC);
285 jsonTable.setClusteringOrder("emp_id Desc");
286 jsonTable.setFields(fields);
287 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
288 authorization, jsonTable, keyspaceName, tableNameC);
289 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
291 assertEquals(400, response.getStatus());
295 // good clustering key
297 public void test3_createTable_1_clusterKey_good() throws Exception {
298 System.out.println("Testing create w/ clusterKey");
300 String tableNameC = "testTableC1";
301 JsonTable jsonTable = new JsonTable();
302 Map<String, String> consistencyInfo = new HashMap<>();
303 Map<String, String> fields = new HashMap<>();
304 fields.put("uuid", "text");
305 fields.put("emp_name", "text");
306 fields.put("emp_salary", "varint");
307 fields.put("PRIMARY KEY", "((emp_name),emp_salary)");
308 consistencyInfo.put("type", "eventual");
309 jsonTable.setConsistencyInfo(consistencyInfo);
310 jsonTable.setKeyspaceName(keyspaceName);
311 // jsonTable.setPrimaryKey("emp_name");
312 jsonTable.setTableName(tableNameC);
313 jsonTable.setClusteringOrder("emp_salary ASC");
314 jsonTable.setFields(fields);
315 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
316 authorization, jsonTable, keyspaceName, tableNameC);
317 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
319 assertEquals(200, response.getStatus());
322 // bad partition key=clustering key
324 public void test3_createTable_2_clusterKey_bad() throws Exception {
325 System.out.println("Testing create w/ bad clusterKey");
326 String tableNameC = "testTableC2";
327 JsonTable jsonTable = new JsonTable();
328 Map<String, String> consistencyInfo = new HashMap<>();
329 Map<String, String> fields = new HashMap<>();
330 fields.put("uuid", "text");
331 fields.put("emp_name", "text");
332 fields.put("emp_salary", "varint");
333 fields.put("PRIMARY KEY", "((emp_name),emp_name)");
334 consistencyInfo.put("type", "eventual");
335 jsonTable.setConsistencyInfo(consistencyInfo);
336 jsonTable.setKeyspaceName(keyspaceName);
337 jsonTable.setPrimaryKey("emp_name"); // "PRIMARY KEY" overrides if primaryKey present
338 jsonTable.setTableName(tableNameC);
339 jsonTable.setClusteringOrder("emp_salary ASC");
340 jsonTable.setFields(fields);
341 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
342 authorization, jsonTable, keyspaceName, tableNameC);
343 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
345 assertEquals(400, response.getStatus());
348 // good composite partition key,clustering key
350 public void test3_createTable_3_pfartition_clusterKey_good() throws Exception {
351 System.out.println("Testing create w/ composite partition key, clusterKey");
353 String tableNameC = "testTableC3";
354 JsonTable jsonTable = new JsonTable();
355 Map<String, String> consistencyInfo = new HashMap<>();
356 Map<String, String> fields = new HashMap<>();
357 fields.put("uuid", "text");
358 fields.put("emp_name", "text");
359 fields.put("emp_id", "varint");
360 fields.put("emp_salary", "varint");
361 fields.put("PRIMARY KEY", "((emp_name,emp_id),emp_salary)");
362 consistencyInfo.put("type", "eventual");
363 jsonTable.setConsistencyInfo(consistencyInfo);
364 jsonTable.setKeyspaceName(keyspaceName);
365 jsonTable.setPrimaryKey("emp_name");
366 jsonTable.setTableName(tableNameC);
367 jsonTable.setClusteringOrder("emp_salary ASC");
368 jsonTable.setFields(fields);
369 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
370 authorization, jsonTable, keyspaceName, tableNameC);
371 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
373 assertEquals(200, response.getStatus());
376 // bad - wrong cols in order by of composite partition key,clustering key
378 public void test3_createTable_5_clusteringOrder_bad() throws Exception {
379 System.out.println("Testing create table bad request with clustering & composite keys");
380 String tableNameC = "testTableC5";
381 JsonTable jsonTable = new JsonTable();
382 Map<String, String> consistencyInfo = new HashMap<>();
383 Map<String, String> fields = new HashMap<>();
384 fields.put("uuid", "text");
385 fields.put("emp_name", "text");
386 fields.put("emp_id", "varint");
387 fields.put("emp_salary", "varint");
388 fields.put("PRIMARY KEY", "((uuid,emp_name),emp_id,emp_salary)");
389 consistencyInfo.put("type", "eventual");
390 jsonTable.setConsistencyInfo(consistencyInfo);
391 jsonTable.setKeyspaceName(keyspaceName);
392 jsonTable.setPrimaryKey("emp_name");
393 jsonTable.setTableName(tableNameC);
394 jsonTable.setClusteringOrder("emp_idx desc, emp_salary ASC");
395 jsonTable.setFields(fields);
396 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
397 authorization, jsonTable, keyspaceName, tableNameC);
398 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
400 assertEquals(400, response.getStatus());
404 // good clustering key, need to pass queryparameter
406 public void test3_createTableIndex_1() throws Exception {
407 System.out.println("Testing index in create table");
408 String tableNameC = "testTableCinx";
409 JsonTable jsonTable = new JsonTable();
410 Map<String, String> consistencyInfo = new HashMap<>();
411 Map<String, String> fields = new HashMap<>();
412 fields.put("uuid", "text");
413 fields.put("emp_name", "text");
414 fields.put("emp_salary", "varint");
415 fields.put("PRIMARY KEY", "((emp_name),emp_salary)");
416 consistencyInfo.put("type", "eventual");
417 jsonTable.setConsistencyInfo(consistencyInfo);
418 jsonTable.setKeyspaceName(keyspaceName);
419 jsonTable.setTableName(tableNameC);
420 jsonTable.setClusteringOrder("emp_salary ASC");
421 jsonTable.setFields(fields);
422 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
423 authorization, jsonTable, keyspaceName, tableNameC);
424 // if 200 print to log otherwise fail assertEquals(200, response.getStatus());
425 // info.setQueryParameters("index_name=inx_uuid");
426 Map<String, String> queryParametersMap = new HashMap<String, String>();
428 queryParametersMap.put("index_name", "inxuuid");
429 Mockito.when(info.getQueryParameters()).thenReturn(new MultivaluedHashMap<String, String>(queryParametersMap));
430 response = data.createIndex("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization,
431 keyspaceName, tableNameC, "uuid", info);
432 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
434 assertEquals(200, response.getStatus());
437 // create index without table name
439 public void test3_createTableIndexNoName() throws Exception {
440 System.out.println("Testing index in create table w/o tablename");
441 String tableNameC = "testTableCinx";
442 JsonTable jsonTable = new JsonTable();
443 Map<String, String> consistencyInfo = new HashMap<>();
444 Map<String, String> fields = new HashMap<>();
445 fields.put("uuid", "text");
446 fields.put("emp_name", "text");
447 fields.put("emp_salary", "varint");
448 fields.put("PRIMARY KEY", "((emp_name),emp_salary)");
449 consistencyInfo.put("type", "eventual");
450 jsonTable.setConsistencyInfo(consistencyInfo);
451 jsonTable.setKeyspaceName(keyspaceName);
452 jsonTable.setTableName(tableNameC);
453 jsonTable.setClusteringOrder("emp_salary ASC");
454 jsonTable.setFields(fields);
455 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
456 authorization, jsonTable, keyspaceName, tableNameC);
457 // if 200 print to log otherwise fail assertEquals(200, response.getStatus());
458 // info.setQueryParameters("index_name=inx_uuid");
459 Map<String, String> queryParametersMap = new HashMap<String, String>();
461 queryParametersMap.put("index_name", "inxuuid");
462 response = data.createIndex("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, "",
464 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
466 assertEquals(400, response.getStatus());
470 public void test4_insertIntoTable() throws Exception {
471 System.out.println("Testing insert into table");
473 JsonInsert jsonInsert = new JsonInsert();
474 Map<String, String> consistencyInfo = new HashMap<>();
475 Map<String, Object> values = new HashMap<>();
476 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
477 values.put("emp_name", "testname");
478 values.put("emp_salary", 500);
479 consistencyInfo.put("type", "eventual");
480 jsonInsert.setConsistencyInfo(consistencyInfo);
481 jsonInsert.setKeyspaceName(keyspaceName);
482 jsonInsert.setTableName(tableName);
483 jsonInsert.setValues(values);
484 Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
485 authorization, jsonInsert, keyspaceName, tableName);
486 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
488 assertEquals(200, response.getStatus());
492 public void test4_insertIntoTableCriticalNoLockID() throws Exception {
493 System.out.println("Testing critical insert into table without lockid");
495 JsonInsert jsonInsert = new JsonInsert();
496 Map<String, String> consistencyInfo = new HashMap<>();
497 Map<String, Object> values = new HashMap<>();
498 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
499 values.put("emp_name", "testname");
500 values.put("emp_salary", 500);
501 consistencyInfo.put("type", "critical");
502 jsonInsert.setConsistencyInfo(consistencyInfo);
503 jsonInsert.setKeyspaceName(keyspaceName);
504 jsonInsert.setTableName(tableName);
505 jsonInsert.setValues(values);
506 Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
507 appName, authorization, jsonInsert, keyspaceName, tableName);
508 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
510 assertEquals(400, response.getStatus());
514 public void test4_insertIntoTableAtomic() throws Exception {
515 System.out.println("Testing atomic insert into table without lockid");
517 JsonInsert jsonInsert = new JsonInsert();
518 Map<String, String> consistencyInfo = new HashMap<>();
519 Map<String, Object> values = new HashMap<>();
520 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
521 values.put("emp_name", "testname");
522 values.put("emp_salary", 500);
523 consistencyInfo.put("type", "atomic");
524 jsonInsert.setConsistencyInfo(consistencyInfo);
525 jsonInsert.setKeyspaceName(keyspaceName);
526 jsonInsert.setTableName(tableName);
527 jsonInsert.setValues(values);
528 Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
529 appName, authorization, jsonInsert, keyspaceName, tableName);
530 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
532 assertEquals(200, response.getStatus());
536 public void test4_insertIntoTableNoName() throws Exception {
537 System.out.println("Testing insert into table w/o table name");
539 JsonInsert jsonInsert = new JsonInsert();
540 Map<String, String> consistencyInfo = new HashMap<>();
541 Map<String, Object> values = new HashMap<>();
542 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
543 values.put("emp_name", "testname");
544 values.put("emp_salary", 500);
545 consistencyInfo.put("type", "eventual");
546 jsonInsert.setConsistencyInfo(consistencyInfo);
547 jsonInsert.setKeyspaceName(keyspaceName);
548 jsonInsert.setTableName(tableName);
549 jsonInsert.setValues(values);
550 Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
551 appName, authorization, jsonInsert, "", "");
552 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
554 assertEquals(400, response.getStatus());
558 public void test4_insertIntoTable2() throws Exception {
559 System.out.println("Testing insert into table #2");
561 JsonInsert jsonInsert = new JsonInsert();
562 Map<String, String> consistencyInfo = new HashMap<>();
563 Map<String, Object> values = new HashMap<>();
564 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
565 values.put("emp_name", "test1");
566 values.put("emp_salary", 1500);
567 consistencyInfo.put("type", "eventual");
568 jsonInsert.setConsistencyInfo(consistencyInfo);
569 jsonInsert.setKeyspaceName(keyspaceName);
570 jsonInsert.setTableName(tableName);
571 jsonInsert.setValues(values);
572 Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
573 authorization, jsonInsert, keyspaceName, tableName);
574 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
576 assertEquals(200, response.getStatus());
581 public void test4_insertIntoTable4() throws Exception {
582 System.out.println("Testing insert into wrong table");
584 JsonInsert jsonInsert = new JsonInsert();
585 Map<String, String> consistencyInfo = new HashMap<>();
586 Map<String, Object> values = new HashMap<>();
587 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
588 values.put("emp_name", "test1");
589 values.put("emp_salary", 1500);
590 consistencyInfo.put("type", "eventual");
591 jsonInsert.setConsistencyInfo(consistencyInfo);
592 jsonInsert.setKeyspaceName(keyspaceName);
593 jsonInsert.setTableName(tableName);
594 jsonInsert.setValues(values);
595 Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
596 authorization, jsonInsert, keyspaceName, "wrong");
597 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
599 assertEquals(400, response.getStatus());
603 public void test4_insertBlobIntoTable() throws Exception {
604 System.out.println("Testing insert a blob into table");
606 JsonInsert jsonInsert = new JsonInsert();
607 Map<String, String> consistencyInfo = new HashMap<>();
608 Map<String, Object> values = new HashMap<>();
609 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
610 values.put("emp_name", "testname");
611 values.put("emp_salary", 500);
612 values.put("binary", "somestuffhere");
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());
626 public void test5_updateTable() throws Exception {
627 System.out.println("Testing update table");
630 JsonUpdate jsonUpdate = new JsonUpdate();
631 Map<String, String> consistencyInfo = new HashMap<>();
632 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
633 Map<String, Object> values = new HashMap<>();
634 row.add("emp_name", "testname");
635 values.put("emp_salary", 2500);
636 consistencyInfo.put("type", "atomic");
637 jsonUpdate.setConsistencyInfo(consistencyInfo);
638 jsonUpdate.setKeyspaceName(keyspaceName);
639 jsonUpdate.setTableName(tableName);
640 jsonUpdate.setValues(values);
641 Mockito.when(info.getQueryParameters()).thenReturn(row);
642 Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
643 authorization, jsonUpdate, keyspaceName, tableName, info);
645 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
647 assertEquals(200, response.getStatus());
651 public void test5_updateTable_tableDNE() throws Exception {
652 System.out.println("Testing update table that does not exist");
655 JsonUpdate jsonUpdate = new JsonUpdate();
656 Map<String, String> consistencyInfo = new HashMap<>();
657 Map<String, Object> values = new HashMap<>();
658 values.put("emp_salary", 2500);
659 consistencyInfo.put("type", "atomic");
660 jsonUpdate.setConsistencyInfo(consistencyInfo);
661 jsonUpdate.setKeyspaceName(keyspaceName);
662 jsonUpdate.setTableName("wrong_"+tableName);
663 jsonUpdate.setValues(values);
664 Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
665 authorization, jsonUpdate, keyspaceName, "wrong_"+ tableName, info);
667 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
669 assertEquals(400, response.getStatus());
673 public void test5_updateTableNoName() throws Exception {
674 System.out.println("Testing update table without tablename");
677 JsonUpdate jsonUpdate = new JsonUpdate();
678 Map<String, String> consistencyInfo = new HashMap<>();
679 Map<String, Object> values = new HashMap<>();
680 values.put("emp_salary", 2500);
681 consistencyInfo.put("type", "atomic");
682 jsonUpdate.setConsistencyInfo(consistencyInfo);
683 jsonUpdate.setKeyspaceName(keyspaceName);
684 jsonUpdate.setTableName(tableName);
685 jsonUpdate.setValues(values);
686 Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
687 authorization, jsonUpdate, "", "", info);
689 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
691 assertEquals(400, response.getStatus());
694 // need mock code to create error for MusicCore methods
696 public void test5_updateTableAuthE() throws Exception {
697 System.out.println("Testing update table #2");
699 // MockitoAnnotations.initMocks(this);
700 JsonUpdate jsonUpdate = new JsonUpdate();
701 Map<String, String> consistencyInfo = new HashMap<>();
702 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
703 Map<String, Object> values = new HashMap<>();
704 row.add("emp_name", "testname");
705 values.put("emp_salary", 2500);
706 consistencyInfo.put("type", "atomic");
707 jsonUpdate.setConsistencyInfo(consistencyInfo);
708 jsonUpdate.setKeyspaceName(keyspaceName);
709 jsonUpdate.setTableName(tableName);
710 jsonUpdate.setValues(values);
711 // add ttl & timestamp
712 // Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
713 Mockito.when(info.getQueryParameters()).thenReturn(row);
714 // Map<String, Object> m1= new HashMap<>() ;
715 // Mockito.when(MusicCore.autheticateUser(appName,userId,password,keyspaceName,"abc66ccc-d857-4e90-b1e5-df98a3d40ce6","updateTable")).thenReturn(m1);
716 Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
717 authorization, jsonUpdate, keyspaceName, tableName, info);
718 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
720 assertEquals(200, response.getStatus());
724 public void test6_critical_selectAtomic() throws Exception {
725 System.out.println("Testing critical select atomic");
726 createAndInsertIntoTable();
727 JsonInsert jsonInsert = new JsonInsert();
728 Map<String, String> consistencyInfo = new HashMap<>();
729 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
730 row.add("emp_name", "testname");
731 consistencyInfo.put("type", "atomic");
732 jsonInsert.setConsistencyInfo(consistencyInfo);
733 Mockito.when(info.getQueryParameters()).thenReturn(row);
734 Response response = data.selectCritical("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
735 appName, authorization, jsonInsert, keyspaceName, tableName,info);
736 HashMap<String,HashMap<String,Object>> map = (HashMap<String, HashMap<String, Object>>) response.getEntity();
737 HashMap<String, Object> result = map.get("result");
738 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
740 Map<String, String> row0 = (Map<String, String>) result.get("row 0");
741 assertEquals("testname", row0.get("emp_name"));
742 assertEquals(BigInteger.valueOf(500), row0.get("emp_salary"));
746 public void test6_critical_selectCritical_nolockid() throws Exception {
747 System.out.println("Testing critical select critical w/o lockid");
748 createAndInsertIntoTable();
749 JsonInsert jsonInsert = new JsonInsert();
750 Map<String, String> consistencyInfo = new HashMap<>();
751 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
752 row.add("emp_name", "testname");
753 consistencyInfo.put("type", "critical");
754 jsonInsert.setConsistencyInfo(consistencyInfo);
755 Mockito.when(info.getQueryParameters()).thenReturn(row);
756 Response response = data.selectCritical("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
757 appName, authorization, jsonInsert, keyspaceName, tableName,info);
758 HashMap<String,HashMap<String,Object>> map = (HashMap<String, HashMap<String, Object>>) response.getEntity();
759 HashMap<String, Object> result = map.get("result");
760 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
762 assertEquals(400, response.getStatus());
766 public void test6_critical_select_nulltable() throws Exception {
767 System.out.println("Testing critical select w/ null tablename");
768 createAndInsertIntoTable();
769 JsonInsert jsonInsert = new JsonInsert();
770 Map<String, String> consistencyInfo = new HashMap<>();
771 consistencyInfo.put("type", "atomic");
772 jsonInsert.setConsistencyInfo(consistencyInfo);
773 Response response = data.selectCritical("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
774 appName, authorization, jsonInsert, keyspaceName, null,info);
775 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
777 assertEquals(400, response.getStatus());
781 public void test6_select() throws Exception {
782 System.out.println("Testing select");
783 createAndInsertIntoTable();
784 JsonSelect jsonSelect = new JsonSelect();
785 Map<String, String> consistencyInfo = new HashMap<>();
786 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
787 row.add("emp_name", "testname");
788 consistencyInfo.put("type", "atomic");
789 jsonSelect.setConsistencyInfo(consistencyInfo);
790 Mockito.when(info.getQueryParameters()).thenReturn(row);
791 Response response = data.select("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization,
792 keyspaceName, tableName, info);
793 HashMap<String, HashMap<String, Object>> map = (HashMap<String, HashMap<String, Object>>) response.getEntity();
794 HashMap<String, Object> result = map.get("result");
795 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
797 Map<String, String> row0 = (Map<String, String>) result.get("row 0");
798 assertEquals("testname", row0.get("emp_name"));
799 assertEquals(BigInteger.valueOf(500), row0.get("emp_salary"));
803 public void test6_select_nullTablename() throws Exception {
804 System.out.println("Testing select w/ null tablename");
805 createAndInsertIntoTable();
806 JsonSelect jsonSelect = new JsonSelect();
807 Map<String, String> consistencyInfo = new HashMap<>();
808 consistencyInfo.put("type", "atomic");
809 jsonSelect.setConsistencyInfo(consistencyInfo);
810 Response response = data.select("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
811 appName, wrongAuthorization, keyspaceName, null, info);
812 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
814 assertEquals(400, response.getStatus());
818 public void test6_deleteFromTable() throws Exception {
819 System.out.println("Testing delete from table");
820 createAndInsertIntoTable();
821 JsonDelete jsonDelete = new JsonDelete();
822 Map<String, String> consistencyInfo = new HashMap<>();
823 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
824 row.add("emp_name", "test1");
825 consistencyInfo.put("type", "atomic");
826 jsonDelete.setConsistencyInfo(consistencyInfo);
827 Mockito.when(info.getQueryParameters()).thenReturn(row);
828 Response response = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
829 authorization, jsonDelete, keyspaceName, tableName, info);
830 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
831 assertEquals(200, response.getStatus());
835 public void test6_deleteFromTable_missingTablename() throws Exception {
836 System.out.println("Testing delete from table w/ null tablename");
837 createAndInsertIntoTable();
838 JsonDelete jsonDelete = new JsonDelete();
839 Map<String, String> consistencyInfo = new HashMap<>();
840 consistencyInfo.put("type", "atomic");
841 jsonDelete.setConsistencyInfo(consistencyInfo);
842 Response response = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
843 wrongAuthorization, jsonDelete, keyspaceName, null, info);
844 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
845 assertEquals(400, response.getStatus());
851 public void test6_deleteFromTable1() throws Exception {
852 System.out.println("Testing delete from table missing delete object");
853 createAndInsertIntoTable();
855 JsonDelete jsonDelete = new JsonDelete();
856 Map<String, String> consistencyInfo = new HashMap<>();
857 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
858 consistencyInfo.put("type", "atomic");
859 jsonDelete.setConsistencyInfo(consistencyInfo);
860 Mockito.when(info.getQueryParameters()).thenReturn(row);
861 Response response = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
862 authorization, jsonDelete, keyspaceName, tableName, info);
863 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
865 assertEquals(400, response.getStatus());
870 public void test6_deleteFromTable2() throws Exception {
871 System.out.println("Testing delete from table missing delete object");
872 createAndInsertIntoTable();
873 JsonDelete jsonDelete = new JsonDelete();
874 Map<String, String> consistencyInfo = new HashMap<>();
875 consistencyInfo.put("type", "atomic");
876 jsonDelete.setConsistencyInfo(consistencyInfo);
877 Response response = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
878 authorization, null, keyspaceName, tableName, info);
879 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
881 assertEquals(400, response.getStatus());
885 public void test7_dropTable() throws Exception {
886 System.out.println("Testing drop table");
888 JsonTable jsonTable = new JsonTable();
889 Map<String, String> consistencyInfo = new HashMap<>();
890 consistencyInfo.put("type", "atomic");
891 jsonTable.setConsistencyInfo(consistencyInfo);
892 Response response = data.dropTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
893 authorization, keyspaceName, tableName);
894 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
896 assertEquals(200, response.getStatus());
900 public void test7_dropTable_nullTablename() throws Exception {
901 System.out.println("Testing drop table w/ null tablename");
903 JsonTable jsonTable = new JsonTable();
904 Map<String, String> consistencyInfo = new HashMap<>();
905 consistencyInfo.put("type", "atomic");
906 jsonTable.setConsistencyInfo(consistencyInfo);
907 Response response = data.dropTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
908 authorization, keyspaceName, null);
909 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
911 assertEquals(400, response.getStatus());
916 public void test8_deleteKeyspace() throws Exception {
917 System.out.println("Testing drop keyspace");
919 JsonKeySpace jsonKeyspace = new JsonKeySpace();
920 Map<String, String> consistencyInfo = new HashMap<>();
921 Map<String, Object> replicationInfo = new HashMap<>();
922 consistencyInfo.put("type", "eventual");
923 replicationInfo.put("class", "SimpleStrategy");
924 replicationInfo.put("replication_factor", 1);
925 jsonKeyspace.setConsistencyInfo(consistencyInfo);
926 jsonKeyspace.setDurabilityOfWrites("true");
927 jsonKeyspace.setKeyspaceName("TestApp1");
928 jsonKeyspace.setReplicationInfo(replicationInfo);
929 Response response = data.dropKeySpace("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", authorization,
930 appName, keyspaceName);
931 System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
933 assertEquals(400, response.getStatus());
936 private static void createKeyspace() throws Exception {
937 // shouldn't really be doing this here, but create keyspace is currently turned off
938 PreparedQueryObject query = new PreparedQueryObject();
939 query.appendQueryString(CassandraCQL.createKeySpace);
940 MusicCore.eventualPut(query);
942 boolean isAAF = false;
943 String hashedpwd = BCrypt.hashpw(password, BCrypt.gensalt());
944 query = new PreparedQueryObject();
945 query.appendQueryString("INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
946 + "password, username, is_aaf) values (?,?,?,?,?,?,?)");
947 query.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
948 query.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspaceName));
949 query.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
950 query.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
951 query.addValue(MusicUtil.convertToActualDataType(DataType.text(), hashedpwd));
952 query.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
953 query.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
954 MusicCore.eventualPut(query);
957 private void clearAllTablesFromKeyspace() throws MusicServiceException {
958 ArrayList<String> tableNames = new ArrayList<>();
959 PreparedQueryObject query = new PreparedQueryObject();
960 query.appendQueryString(
961 "SELECT table_name FROM system_schema.tables WHERE keyspace_name = '" + keyspaceName + "';");
962 ResultSet rs = MusicCore.get(query);
964 tableNames.add(row.getString("table_name"));
966 for (String table : tableNames) {
967 query = new PreparedQueryObject();
968 query.appendQueryString("DROP TABLE " + keyspaceName + "." + table);
969 MusicCore.eventualPut(query);
974 * Create a table {@link tableName} in {@link keyspaceName}
978 private void createTable() throws Exception {
979 JsonTable jsonTable = new JsonTable();
980 Map<String, String> consistencyInfo = new HashMap<>();
981 Map<String, String> fields = new HashMap<>();
982 fields.put("uuid", "text");
983 fields.put("emp_name", "text");
984 fields.put("emp_salary", "varint");
985 fields.put("binary", "blob");
986 fields.put("PRIMARY KEY", "(emp_name)");
987 consistencyInfo.put("type", "eventual");
988 jsonTable.setConsistencyInfo(consistencyInfo);
989 jsonTable.setKeyspaceName(keyspaceName);
990 jsonTable.setPrimaryKey("emp_name");
991 jsonTable.setTableName(tableName);
992 jsonTable.setFields(fields);
993 // Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
994 Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
995 authorization, jsonTable, keyspaceName, tableName);
999 * Create table {@link createTable} and insert into said table
1003 private void createAndInsertIntoTable() throws Exception {
1006 JsonInsert jsonInsert = new JsonInsert();
1007 Map<String, String> consistencyInfo = new HashMap<>();
1008 Map<String, Object> values = new HashMap<>();
1009 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
1010 values.put("emp_name", "testname");
1011 values.put("emp_salary", 500);
1012 values.put("binary", "binarydatahere");
1013 consistencyInfo.put("type", "eventual");
1014 jsonInsert.setConsistencyInfo(consistencyInfo);
1015 jsonInsert.setKeyspaceName(keyspaceName);
1016 jsonInsert.setTableName(tableName);
1017 jsonInsert.setValues(values);
1018 Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
1019 authorization, jsonInsert, keyspaceName, tableName);