2 * ============LICENSE_START========================================== org.onap.music
3 * =================================================================== Copyright (c) 2017 AT&T
4 * Intellectual Property ===================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6 * in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the License
11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing permissions and limitations under
15 * ============LICENSE_END=============================================
16 * ====================================================================
18 package org.onap.music.unittests;
20 import static org.junit.Assert.assertEquals;
21 import static org.onap.music.main.MusicCore.mLockHandle;
23 import java.util.HashMap;
24 import java.util.List;
26 import java.util.UUID;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.ws.rs.core.MultivaluedMap;
29 import javax.ws.rs.core.UriInfo;
30 import org.apache.curator.test.TestingServer;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.FixMethodOrder;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.junit.runners.MethodSorters;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.mockito.runners.MockitoJUnitRunner;
40 import org.onap.music.datastore.PreparedQueryObject;
41 import org.onap.music.datastore.jsonobjects.JsonDelete;
42 import org.onap.music.datastore.jsonobjects.JsonInsert;
43 import org.onap.music.datastore.jsonobjects.JsonKeySpace;
44 import org.onap.music.datastore.jsonobjects.JsonOnboard;
45 import org.onap.music.datastore.jsonobjects.JsonSelect;
46 import org.onap.music.datastore.jsonobjects.JsonTable;
47 import org.onap.music.datastore.jsonobjects.JsonUpdate;
48 import org.onap.music.lockingservice.MusicLockingService;
49 import org.onap.music.main.CachingUtil;
50 import org.onap.music.main.MusicCore;
51 import org.onap.music.main.MusicUtil;
52 import org.onap.music.main.ResultType;
53 import org.onap.music.rest.RestMusicAdminAPI;
54 import org.onap.music.rest.RestMusicDataAPI;
55 import org.onap.music.rest.RestMusicLocksAPI;
56 import com.datastax.driver.core.DataType;
57 import com.datastax.driver.core.ResultSet;
58 import com.datastax.driver.core.Row;
59 import com.sun.jersey.core.util.MultivaluedMapImpl;
61 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
62 @RunWith(MockitoJUnitRunner.class)
63 public class TestRestMusicData {
65 RestMusicDataAPI data = new RestMusicDataAPI();
66 RestMusicAdminAPI admin = new RestMusicAdminAPI();
67 RestMusicLocksAPI lock = new RestMusicLocksAPI();
68 static PreparedQueryObject testObject;
69 static TestingServer zkServer;
72 HttpServletResponse http;
77 static String appName = "TestApp";
78 static String userId = "TestUser";
79 static String password = "TestPassword";
80 static boolean isAAF = false;
81 static UUID uuid = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40ce6");
82 static String keyspaceName = "testCassa";
83 static String tableName = "employees";
84 static String xLatestVersion = "X-latestVersion";
85 static String onboardUUID = null;
86 static String lockId = null;
87 static String lockName = "testCassa.employees.sample3";
90 public static void init() throws Exception {
92 MusicCore.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra();
93 zkServer = new TestingServer(2181, new File(" mp/zk"));
94 MusicCore.mLockHandle = new MusicLockingService();
95 } catch (Exception e) {
101 public static void tearDownAfterClass() throws Exception {
102 System.out.println("After class");
103 testObject = new PreparedQueryObject();
104 testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName);
105 MusicCore.eventualPut(testObject);
106 testObject = new PreparedQueryObject();
107 testObject.appendQueryString("DROP KEYSPACE IF EXISTS admin");
108 MusicCore.eventualPut(testObject);
109 MusicCore.mDstoreHandle.close();
110 MusicCore.mLockHandle.getzkLockHandle().close();
111 MusicCore.mLockHandle.close();
116 public void Test1_createKeyspace() throws Exception {
117 testObject = new PreparedQueryObject();
118 testObject.appendQueryString("CREATE KEYSPACE admin WITH REPLICATION = "
119 + "{'class' : 'SimpleStrategy' , "
120 + "'replication_factor': 1} AND DURABLE_WRITES = true");
121 MusicCore.eventualPut(testObject);
122 testObject = new PreparedQueryObject();
123 testObject.appendQueryString(
124 "CREATE TABLE admin.keyspace_master (" + " uuid uuid, keyspace_name text,"
125 + " application_name text, is_api boolean,"
126 + " password text, username text,"
127 + " is_aaf boolean, PRIMARY KEY (uuid)\n" + ");");
128 MusicCore.eventualPut(testObject);
130 testObject = new PreparedQueryObject();
131 testObject.appendQueryString(
132 "INSERT INTO admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
133 + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)");
134 testObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
135 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(),
136 MusicUtil.DEFAULTKEYSPACENAME));
137 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
138 testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
139 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), password));
140 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
141 testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
142 MusicCore.eventualPut(testObject);
144 testObject = new PreparedQueryObject();
145 testObject.appendQueryString(
146 "INSERT INTO admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
147 + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)");
148 testObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(),
149 UUID.fromString("bbc66ccc-d857-4e90-b1e5-df98a3d40de6")));
150 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(),
151 MusicUtil.DEFAULTKEYSPACENAME));
152 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), "TestApp1"));
153 testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
154 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), password));
155 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), "TestUser1"));
156 testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
157 MusicCore.eventualPut(testObject);
159 testObject = new PreparedQueryObject();
160 testObject.appendQueryString(
161 "select uuid from admin.keyspace_master where application_name = ? allow filtering");
162 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
163 ResultSet rs = MusicCore.get(testObject);
164 List<Row> rows = rs.all();
165 if (rows.size() > 0) {
166 System.out.println("#######UUID is:" + rows.get(0).getUUID("uuid"));
171 public void Test2_createKeyspace() throws Exception {
172 JsonKeySpace jsonKeyspace = new JsonKeySpace();
173 Map<String, String> consistencyInfo = new HashMap<>();
174 Map<String, Object> replicationInfo = new HashMap<>();
175 consistencyInfo.put("type", "eventual");
176 replicationInfo.put("class", "SimpleStrategy");
177 replicationInfo.put("replication_factor", 1);
178 jsonKeyspace.setConsistencyInfo(consistencyInfo);
179 jsonKeyspace.setDurabilityOfWrites("true");
180 jsonKeyspace.setKeyspaceName(keyspaceName);
181 jsonKeyspace.setReplicationInfo(replicationInfo);
182 Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
183 Map<String, Object> resultMap = data.createKeySpace("1", "1", "1", null, appName, userId,
184 password, jsonKeyspace, keyspaceName, http);
185 assertEquals(ResultType.SUCCESS, resultMap.get("status"));
189 public void Test3_createKeyspace1() throws Exception {
190 JsonKeySpace jsonKeyspace = new JsonKeySpace();
191 Map<String, String> consistencyInfo = new HashMap<>();
192 Map<String, Object> replicationInfo = new HashMap<>();
193 consistencyInfo.put("type", "eventual");
194 replicationInfo.put("class", "SimpleStrategy");
195 replicationInfo.put("replication_factor", 1);
196 jsonKeyspace.setConsistencyInfo(consistencyInfo);
197 jsonKeyspace.setDurabilityOfWrites("true");
198 jsonKeyspace.setKeyspaceName("TestApp1");
199 jsonKeyspace.setReplicationInfo(replicationInfo);
200 Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
201 Map<String, Object> resultMap = data.createKeySpace("1", "1", "1", null, "TestApp1",
202 "TestUser1", password, jsonKeyspace, keyspaceName, http);
203 System.out.println("#######status is " + resultMap.get("Exception"));
204 assertEquals("Keyspace testcassa already exists",
205 resultMap.get("error"));;
209 public void Test3_createTable() throws Exception {
210 JsonTable jsonTable = new JsonTable();
211 Map<String, String> consistencyInfo = new HashMap<>();
212 Map<String, String> fields = new HashMap<>();
213 fields.put("uuid", "text");
214 fields.put("emp_name", "text");
215 fields.put("emp_salary", "varint");
216 fields.put("PRIMARY KEY", "(emp_name)");
217 consistencyInfo.put("type", "eventual");
218 jsonTable.setConsistencyInfo(consistencyInfo);
219 jsonTable.setKeyspaceName(keyspaceName);
220 jsonTable.setPrimaryKey("emp_name");
221 jsonTable.setTableName(tableName);
222 jsonTable.setFields(fields);
223 Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
224 Map<String, Object> resultMap = data.createTable("1", "1", "1",
225 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, userId, password,
226 jsonTable, keyspaceName, tableName, http);
227 assertEquals(ResultType.SUCCESS, resultMap.get("status"));
231 public void Test4_insertIntoTable() throws Exception {
232 JsonInsert jsonInsert = new JsonInsert();
233 Map<String, String> consistencyInfo = new HashMap<>();
234 Map<String, Object> values = new HashMap<>();
235 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
236 values.put("emp_name", "testName");
237 values.put("emp_salary", 500);
238 consistencyInfo.put("type", "eventual");
239 jsonInsert.setConsistencyInfo(consistencyInfo);
240 jsonInsert.setKeyspaceName(keyspaceName);
241 jsonInsert.setTableName(tableName);
242 jsonInsert.setValues(values);
243 Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
244 Map<String, Object> resultMap = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
245 appName, userId, password, jsonInsert, keyspaceName, tableName, http);
246 assertEquals(ResultType.SUCCESS, resultMap.get("status"));
250 public void Test4_insertIntoTable2() throws Exception {
251 JsonInsert jsonInsert = new JsonInsert();
252 Map<String, String> consistencyInfo = new HashMap<>();
253 Map<String, Object> values = new HashMap<>();
254 values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
255 values.put("emp_name", "test1");
256 values.put("emp_salary", 1500);
257 consistencyInfo.put("type", "eventual");
258 jsonInsert.setConsistencyInfo(consistencyInfo);
259 jsonInsert.setKeyspaceName(keyspaceName);
260 jsonInsert.setTableName(tableName);
261 jsonInsert.setValues(values);
262 Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
263 Map<String, Object> resultMap = data.insertIntoTable("1", "1", "1",
264 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, userId, password,
265 jsonInsert, keyspaceName, tableName, http);
266 assertEquals(ResultType.SUCCESS, resultMap.get("status"));
270 public void Test5_updateTable() throws Exception {
271 JsonUpdate jsonUpdate = new JsonUpdate();
272 Map<String, String> consistencyInfo = new HashMap<>();
273 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
274 Map<String, Object> values = new HashMap<>();
275 row.add("emp_name", "testName");
276 values.put("emp_salary", 2500);
277 consistencyInfo.put("type", "atomic");
278 jsonUpdate.setConsistencyInfo(consistencyInfo);
279 jsonUpdate.setKeyspaceName(keyspaceName);
280 jsonUpdate.setTableName(tableName);
281 jsonUpdate.setValues(values);
282 Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
283 Mockito.when(info.getQueryParameters()).thenReturn(row);
284 Map<String, Object> resultMap = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
285 userId, password, jsonUpdate, keyspaceName, tableName, info, http);
286 assertEquals(ResultType.SUCCESS, resultMap.get("status"));
290 public void Test6_select() throws Exception {
291 JsonSelect jsonSelect = new JsonSelect();
292 Map<String, String> consistencyInfo = new HashMap<>();
293 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
294 row.add("emp_name", "testName");
295 consistencyInfo.put("type", "atomic");
296 jsonSelect.setConsistencyInfo(consistencyInfo);
297 Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
298 Mockito.when(info.getQueryParameters()).thenReturn(row);
299 Map<String, Object> resultMap = data.select("1", "1", "1",
300 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, userId, password, keyspaceName, tableName, info, http);
301 assertEquals("2500", ((HashMap<String,HashMap<String,Object>>) resultMap.get("result")).get("row 0").get("emp_salary").toString());
305 public void Test6_selectCritical() throws Exception {
306 JsonInsert jsonInsert = new JsonInsert();
307 Map<String, String> consistencyInfo = new HashMap<>();
308 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
309 row.add("emp_name", "testName");
310 consistencyInfo.put("type", "atomic");
311 jsonInsert.setConsistencyInfo(consistencyInfo);
312 Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
313 Mockito.when(info.getQueryParameters()).thenReturn(row);
314 Map<String, Object> resultMap = data.selectCritical("1", "1", "1",
315 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, userId, password, jsonInsert, keyspaceName, tableName,
317 assertEquals("2500", ((HashMap<String,HashMap<String,Object>>) resultMap.get("result")).get("row 0").get("emp_salary").toString());
321 public void Test6_deleteFromTable() throws Exception {
322 JsonDelete jsonDelete = new JsonDelete();
323 Map<String, String> consistencyInfo = new HashMap<>();
324 MultivaluedMap<String, String> row = new MultivaluedMapImpl();
325 row.add("emp_name", "test1");
326 consistencyInfo.put("type", "atomic");
327 jsonDelete.setConsistencyInfo(consistencyInfo);
328 Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
329 Mockito.when(info.getQueryParameters()).thenReturn(row);
330 Map<String, Object> resultMap = data.deleteFromTable("1", "1", "1",
331 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, userId, password,
332 jsonDelete, keyspaceName, tableName, info, http);
333 assertEquals(ResultType.SUCCESS, resultMap.get("status"));
337 public void Test7_dropTable() throws Exception {
338 JsonTable jsonTable = new JsonTable();
339 Map<String, String> consistencyInfo = new HashMap<>();
340 consistencyInfo.put("type", "atomic");
341 jsonTable.setConsistencyInfo(consistencyInfo);
342 Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
343 Map<String, Object> resultMap = data.dropTable("1", "1", "1",
344 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, userId, password,
345 jsonTable, keyspaceName, tableName, http);
346 assertEquals(ResultType.SUCCESS, resultMap.get("status"));
350 public void Test8_deleteKeyspace() throws Exception {
351 JsonKeySpace jsonKeyspace = new JsonKeySpace();
352 Map<String, String> consistencyInfo = new HashMap<>();
353 Map<String, Object> replicationInfo = new HashMap<>();
354 consistencyInfo.put("type", "eventual");
355 replicationInfo.put("class", "SimpleStrategy");
356 replicationInfo.put("replication_factor", 1);
357 jsonKeyspace.setConsistencyInfo(consistencyInfo);
358 jsonKeyspace.setDurabilityOfWrites("true");
359 jsonKeyspace.setKeyspaceName("TestApp1");
360 jsonKeyspace.setReplicationInfo(replicationInfo);
361 Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
362 Map<String, Object> resultMap = data.dropKeySpace("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
363 appName, userId, password, jsonKeyspace, keyspaceName, http);
364 assertEquals(ResultType.SUCCESS, resultMap.get("status"));
368 public void Test6_onboard() throws Exception {
369 JsonOnboard jsonOnboard = new JsonOnboard();
370 jsonOnboard.setAppname("TestApp2");
371 jsonOnboard.setIsAAF("false");
372 jsonOnboard.setUserId("TestUser2");
373 jsonOnboard.setPassword("TestPassword2");
374 Map<String, Object> resultMap = admin.onboardAppWithMusic(jsonOnboard, http);
375 resultMap.containsKey("success");
376 onboardUUID = resultMap.get("Generated AID").toString();
377 assertEquals("Your application TestApp2 has been onboarded with MUSIC.", resultMap.get("Success"));
381 public void Test7_onboardSearch() throws Exception {
382 JsonOnboard jsonOnboard = new JsonOnboard();
383 jsonOnboard.setAppname("TestApp2");
384 jsonOnboard.setIsAAF("false");
385 jsonOnboard.setAid(onboardUUID);
386 Map<String, Object> resultMap = admin.getOnboardedInfoSearch(jsonOnboard, http);
387 resultMap.containsKey("success");
388 assertEquals(MusicUtil.DEFAULTKEYSPACENAME, resultMap.get(onboardUUID));
393 public void Test8_onboardUpdate() throws Exception {
394 JsonOnboard jsonOnboard = new JsonOnboard();
395 jsonOnboard.setIsAAF("false");
396 jsonOnboard.setUserId("TestUser3");
397 jsonOnboard.setPassword("TestPassword3");
398 jsonOnboard.setAid(onboardUUID);
399 Map<String, Object> resultMap = admin.updateOnboardApp(jsonOnboard, http);
400 resultMap.containsKey("success");
401 assertEquals("Your application has been updated successfully", resultMap.get("Success"));
405 public void Test9_onboardDelete() throws Exception {
406 JsonOnboard jsonOnboard = new JsonOnboard();
407 jsonOnboard.setAppname("TestApp2");
408 jsonOnboard.setAid(onboardUUID);
409 Map<String, Object> resultMap = admin.deleteOnboardApp(jsonOnboard, http);
410 resultMap.containsKey("success");
411 assertEquals("Your application has been deleted successfully", resultMap.get("Success"));
415 public void Test3_createLockReference() throws Exception {
416 Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
417 Map<String, Object> resultMap = lock.createLockReference(lockName, uuid.toString(), appName, null, null, http);
418 @SuppressWarnings("unchecked")
419 Map<String, Object> resultMap1 = (Map<String, Object>) resultMap.get("lock");
420 lockId = (String) resultMap1.get("lock");
421 assertEquals(ResultType.SUCCESS, resultMap.get("status"));
425 public void Test4_accquireLock() throws Exception {
426 Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
427 Map<String, Object> resultMap = lock.accquireLock(lockId, uuid.toString(), appName, null, null, http);
428 assertEquals(ResultType.SUCCESS, resultMap.get("status"));
432 public void Test5_currentLockHolder() throws Exception {
433 Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
434 Map<String, Object> resultMap = lock.currentLockHolder(lockName, uuid.toString(), appName, null, null, http);
435 assertEquals(ResultType.SUCCESS, resultMap.get("status"));
439 public void Test7_unLock() throws Exception {
440 Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
441 Map<String, Object> resultMap = lock.unLock(lockId, uuid.toString(), appName, null, null, http);
442 assertEquals(ResultType.SUCCESS, resultMap.get("status"));
446 public void Test8_delete() throws Exception {
447 Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
448 Map<String, Object> resultMap = lock.deleteLock(lockName, uuid.toString(), appName, null, null, http);
449 assertEquals(ResultType.SUCCESS, resultMap.get("status"));