e4d06f70e4eab1d620c08fb56695e0c1c1449df9
[music.git] / src / test / java / org / onap / music / unittests / TestRestMusicData.java
1 /*
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
7  * 
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * 
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
13  * the License.
14  * 
15  * ============LICENSE_END=============================================
16  * ====================================================================
17  */
18 package org.onap.music.unittests;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.onap.music.main.MusicCore.mLockHandle;
22 import java.io.File;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
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.rest.RestMusicAdminAPI;
53 import org.onap.music.rest.RestMusicDataAPI;
54 import org.onap.music.rest.RestMusicLocksAPI;
55 import com.datastax.driver.core.DataType;
56 import com.datastax.driver.core.ResultSet;
57 import com.datastax.driver.core.Row;
58 import com.sun.jersey.core.util.MultivaluedMapImpl;
59
60 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
61 @RunWith(MockitoJUnitRunner.class)
62 public class TestRestMusicData {
63
64     RestMusicDataAPI data = new RestMusicDataAPI();
65     RestMusicAdminAPI admin = new RestMusicAdminAPI();
66     RestMusicLocksAPI lock = new RestMusicLocksAPI(); 
67     static PreparedQueryObject testObject;
68     static TestingServer zkServer;
69
70     @Mock
71     HttpServletResponse http;
72
73     @Mock
74     UriInfo info;
75
76     static String appName = "TestApp";
77     static String userId = "TestUser";
78     static String password = "TestPassword";
79     static boolean isAAF = false;
80     static UUID uuid = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40ce6");
81     static String keyspaceName = "testCassa";
82     static String tableName = "employees";
83     static String xLatestVersion = "X-latestVersion";
84     static String onboardUUID = null;
85     static String lockId = null;
86     static String lockName = "testCassa.employees.sample3";
87
88     @BeforeClass
89     public static void init() throws Exception {
90         try {
91             MusicCore.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra();
92             zkServer = new TestingServer(2181, new File("    mp/zk"));
93             MusicCore.mLockHandle = new MusicLockingService();
94         } catch (Exception e) {
95             e.printStackTrace();
96         }
97     }
98
99     @AfterClass
100     public static void tearDownAfterClass() throws Exception {
101         System.out.println("After class");
102         testObject = new PreparedQueryObject();
103         testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName);
104         MusicCore.eventualPut(testObject);
105         testObject = new PreparedQueryObject();
106         testObject.appendQueryString("DROP KEYSPACE IF EXISTS admin");
107         MusicCore.eventualPut(testObject);
108         MusicCore.mDstoreHandle.close();
109         MusicCore.mLockHandle.getzkLockHandle().close();
110         MusicCore.mLockHandle.close();
111         zkServer.stop();
112     }
113
114     @Test
115     public void Test1_createKeyspace() throws Exception {
116         testObject = new PreparedQueryObject();
117         testObject.appendQueryString("CREATE KEYSPACE admin WITH REPLICATION = "
118                         + "{'class' : 'SimpleStrategy' , "
119                         + "'replication_factor': 1} AND DURABLE_WRITES = true");
120         MusicCore.eventualPut(testObject);
121         testObject = new PreparedQueryObject();
122         testObject.appendQueryString(
123                         "CREATE TABLE admin.keyspace_master (" + "  uuid uuid, keyspace_name text,"
124                                         + "  application_name text, is_api boolean,"
125                                         + "  password text, username text,"
126                                         + "  is_aaf boolean, PRIMARY KEY (uuid)\n" + ");");
127         MusicCore.eventualPut(testObject);
128
129         testObject = new PreparedQueryObject();
130         testObject.appendQueryString(
131                         "INSERT INTO admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
132                                         + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)");
133         testObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
134         testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(),
135                         MusicUtil.DEFAULTKEYSPACENAME));
136         testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
137         testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
138         testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), password));
139         testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
140         testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
141         MusicCore.eventualPut(testObject);
142
143         testObject = new PreparedQueryObject();
144         testObject.appendQueryString(
145                         "INSERT INTO admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
146                                         + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)");
147         testObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(),
148                         UUID.fromString("bbc66ccc-d857-4e90-b1e5-df98a3d40de6")));
149         testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(),
150                         MusicUtil.DEFAULTKEYSPACENAME));
151         testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), "TestApp1"));
152         testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
153         testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), password));
154         testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), "TestUser1"));
155         testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
156         MusicCore.eventualPut(testObject);
157
158         testObject = new PreparedQueryObject();
159         testObject.appendQueryString(
160                         "select uuid from admin.keyspace_master where application_name = ? allow filtering");
161         testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
162         ResultSet rs = MusicCore.get(testObject);
163         List<Row> rows = rs.all();
164         if (rows.size() > 0) {
165             System.out.println("#######UUID is:" + rows.get(0).getUUID("uuid"));
166         }
167     }
168
169     @Test
170     public void Test2_createKeyspace() throws Exception {
171         JsonKeySpace jsonKeyspace = new JsonKeySpace();
172         Map<String, String> consistencyInfo = new HashMap<>();
173         Map<String, Object> replicationInfo = new HashMap<>();
174         consistencyInfo.put("type", "eventual");
175         replicationInfo.put("class", "SimpleStrategy");
176         replicationInfo.put("replication_factor", 1);
177         jsonKeyspace.setConsistencyInfo(consistencyInfo);
178         jsonKeyspace.setDurabilityOfWrites("true");
179         jsonKeyspace.setKeyspaceName(keyspaceName);
180         jsonKeyspace.setReplicationInfo(replicationInfo);
181         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
182         Map<String, Object> resultMap = data.createKeySpace("1", "1", "1", null, appName, userId,
183                         password, jsonKeyspace, keyspaceName, http);
184         assertEquals(uuid.toString(), resultMap.get("aid"));
185     }
186
187     @Test
188     public void Test3_createKeyspace1() throws Exception {
189         JsonKeySpace jsonKeyspace = new JsonKeySpace();
190         Map<String, String> consistencyInfo = new HashMap<>();
191         Map<String, Object> replicationInfo = new HashMap<>();
192         consistencyInfo.put("type", "eventual");
193         replicationInfo.put("class", "SimpleStrategy");
194         replicationInfo.put("replication_factor", 1);
195         jsonKeyspace.setConsistencyInfo(consistencyInfo);
196         jsonKeyspace.setDurabilityOfWrites("true");
197         jsonKeyspace.setKeyspaceName("TestApp1");
198         jsonKeyspace.setReplicationInfo(replicationInfo);
199         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
200         Map<String, Object> resultMap = data.createKeySpace("1", "1", "1", null, "TestApp1",
201                         "TestUser1", password, jsonKeyspace, keyspaceName, http);
202         System.out.println("#######status is " + resultMap.get("Exception"));
203         assertEquals("Couldn't create keyspace. Please make sure all the information is correct.",
204                         resultMap.get("Exception"));
205     }
206
207     @Test
208     public void Test3_createTable() throws Exception {
209         JsonTable jsonTable = new JsonTable();
210         Map<String, String> consistencyInfo = new HashMap<>();
211         Map<String, String> fields = new HashMap<>();
212         fields.put("uuid", "text");
213         fields.put("emp_name", "text");
214         fields.put("emp_salary", "varint");
215         fields.put("PRIMARY KEY", "(emp_name)");
216         consistencyInfo.put("type", "eventual");
217         jsonTable.setConsistencyInfo(consistencyInfo);
218         jsonTable.setKeyspaceName(keyspaceName);
219         jsonTable.setPrimaryKey("emp_name");
220         jsonTable.setTableName(tableName);
221         jsonTable.setFields(fields);
222         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
223         Map<String, Object> resultMap = data.createTable("1", "1", "1",
224                         "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, userId, password,
225                         jsonTable, keyspaceName, tableName, http);
226         assertEquals("SUCCESS", resultMap.get("status"));
227     }
228
229     @Test
230     public void Test4_insertIntoTable() throws Exception {
231         JsonInsert jsonInsert = new JsonInsert();
232         Map<String, String> consistencyInfo = new HashMap<>();
233         Map<String, Object> values = new HashMap<>();
234         values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
235         values.put("emp_name", "testName");
236         values.put("emp_salary", 500);
237         consistencyInfo.put("type", "eventual");
238         jsonInsert.setConsistencyInfo(consistencyInfo);
239         jsonInsert.setKeyspaceName(keyspaceName);
240         jsonInsert.setTableName(tableName);
241         jsonInsert.setValues(values);
242         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
243         Map<String, Object> resultMap = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
244                 appName, userId, password, jsonInsert, keyspaceName, tableName, http);
245         assertEquals("Success", resultMap.get("result"));
246     }
247
248     @Test
249     public void Test4_insertIntoTable2() throws Exception {
250         JsonInsert jsonInsert = new JsonInsert();
251         Map<String, String> consistencyInfo = new HashMap<>();
252         Map<String, Object> values = new HashMap<>();
253         values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
254         values.put("emp_name", "test1");
255         values.put("emp_salary", 1500);
256         consistencyInfo.put("type", "eventual");
257         jsonInsert.setConsistencyInfo(consistencyInfo);
258         jsonInsert.setKeyspaceName(keyspaceName);
259         jsonInsert.setTableName(tableName);
260         jsonInsert.setValues(values);
261         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
262         Map<String, Object> resultMap = data.insertIntoTable("1", "1", "1",
263                         "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, userId, password,
264                         jsonInsert, keyspaceName, tableName, http);
265         assertEquals("Success", resultMap.get("result"));
266     }
267
268     @Test
269     public void Test5_updateTable() throws Exception {
270         JsonUpdate jsonUpdate = new JsonUpdate();
271         Map<String, String> consistencyInfo = new HashMap<>();
272         MultivaluedMap<String, String> row = new MultivaluedMapImpl();
273         Map<String, Object> values = new HashMap<>();
274         row.add("emp_name", "testName");
275         values.put("emp_salary", 2500);
276         consistencyInfo.put("type", "atomic");
277         jsonUpdate.setConsistencyInfo(consistencyInfo);
278         jsonUpdate.setKeyspaceName(keyspaceName);
279         jsonUpdate.setTableName(tableName);
280         jsonUpdate.setValues(values);
281         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
282         Mockito.when(info.getQueryParameters()).thenReturn(row);
283         Map<String, Object> resultMap = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
284                 userId, password, jsonUpdate, keyspaceName, tableName, info, http);
285         assertEquals("Success", resultMap.get("result"));
286     }
287
288     @Test
289     public void Test6_select() throws Exception {
290         JsonSelect jsonSelect = new JsonSelect();
291         Map<String, String> consistencyInfo = new HashMap<>();
292         MultivaluedMap<String, String> row = new MultivaluedMapImpl();
293         row.add("emp_name", "testName");
294         consistencyInfo.put("type", "atomic");
295         jsonSelect.setConsistencyInfo(consistencyInfo);
296         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
297         Mockito.when(info.getQueryParameters()).thenReturn(row);
298         Map<String, HashMap<String, Object>> resultMap = data.select("1", "1", "1",
299                 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, userId, password, keyspaceName, tableName, info, http);
300         assertEquals("2500", resultMap.get("row 0").get("emp_salary").toString());
301     }
302
303     @Test
304     public void Test6_selectCritical() throws Exception {
305         JsonInsert jsonInsert = new JsonInsert();
306         Map<String, String> consistencyInfo = new HashMap<>();
307         MultivaluedMap<String, String> row = new MultivaluedMapImpl();
308         row.add("emp_name", "testName");
309         consistencyInfo.put("type", "atomic");
310         jsonInsert.setConsistencyInfo(consistencyInfo);
311         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
312         Mockito.when(info.getQueryParameters()).thenReturn(row);
313         Map<String, HashMap<String, Object>> resultMap = data.selectCritical("1", "1", "1",
314                 "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, userId, password, jsonInsert, keyspaceName, tableName,
315                 info, http);
316         assertEquals("2500", resultMap.get("row 0").get("emp_salary").toString());
317     }
318
319     @Test
320     public void Test6_deleteFromTable() throws Exception {
321         JsonDelete jsonDelete = new JsonDelete();
322         Map<String, String> consistencyInfo = new HashMap<>();
323         MultivaluedMap<String, String> row = new MultivaluedMapImpl();
324         row.add("emp_name", "test1");
325         consistencyInfo.put("type", "atomic");
326         jsonDelete.setConsistencyInfo(consistencyInfo);
327         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
328         Mockito.when(info.getQueryParameters()).thenReturn(row);
329         Map<String, Object> resultMap = data.deleteFromTable("1", "1", "1",
330                         "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, userId, password,
331                         jsonDelete, keyspaceName, tableName, info, http);
332         assertEquals("Success", resultMap.get("result"));
333     }
334
335     @Test
336     public void Test7_dropTable() throws Exception {
337         JsonTable jsonTable = new JsonTable();
338         Map<String, String> consistencyInfo = new HashMap<>();
339         consistencyInfo.put("type", "atomic");
340         jsonTable.setConsistencyInfo(consistencyInfo);
341         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
342         Map<String, Object> resultMap = data.dropTable("1", "1", "1",
343                         "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, userId, password,
344                         jsonTable, keyspaceName, tableName, http);
345         assertEquals("SUCCESS", resultMap.get("status"));
346     }
347
348     @Test
349     public void Test8_deleteKeyspace() throws Exception {
350         JsonKeySpace jsonKeyspace = new JsonKeySpace();
351         Map<String, String> consistencyInfo = new HashMap<>();
352         Map<String, Object> replicationInfo = new HashMap<>();
353         consistencyInfo.put("type", "eventual");
354         replicationInfo.put("class", "SimpleStrategy");
355         replicationInfo.put("replication_factor", 1);
356         jsonKeyspace.setConsistencyInfo(consistencyInfo);
357         jsonKeyspace.setDurabilityOfWrites("true");
358         jsonKeyspace.setKeyspaceName("TestApp1");
359         jsonKeyspace.setReplicationInfo(replicationInfo);
360         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
361         Map<String, Object> resultMap = data.dropKeySpace("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
362                 appName, userId, password, jsonKeyspace, keyspaceName, http);
363         assertEquals("SUCCESS", resultMap.get("status"));
364     }
365     
366     @Test
367     public void Test6_onboard() throws Exception {
368         JsonOnboard jsonOnboard = new JsonOnboard();
369         jsonOnboard.setAppname("TestApp2");
370         jsonOnboard.setIsAAF("false");
371         jsonOnboard.setUserId("TestUser2");
372         jsonOnboard.setPassword("TestPassword2");
373         Map<String, Object> resultMap = admin.onboardAppWithMusic(jsonOnboard, http);
374         resultMap.containsKey("success");
375         onboardUUID = resultMap.get("Generated AID").toString();
376         assertEquals("Your application TestApp2 has been onboarded with MUSIC.", resultMap.get("Success"));
377     }
378
379     @Test
380     public void Test7_onboardSearch() throws Exception {
381         JsonOnboard jsonOnboard = new JsonOnboard();
382         jsonOnboard.setAppname("TestApp2");
383         jsonOnboard.setIsAAF("false");
384         jsonOnboard.setAid(onboardUUID);
385         Map<String, Object> resultMap = admin.getOnboardedInfoSearch(jsonOnboard, http);
386         resultMap.containsKey("success");
387         assertEquals(MusicUtil.DEFAULTKEYSPACENAME, resultMap.get(onboardUUID));
388
389     }
390
391     @Test
392     public void Test8_onboardUpdate() throws Exception {
393         JsonOnboard jsonOnboard = new JsonOnboard();
394         jsonOnboard.setIsAAF("false");
395         jsonOnboard.setUserId("TestUser3");
396         jsonOnboard.setPassword("TestPassword3");
397         jsonOnboard.setAid(onboardUUID);
398         Map<String, Object> resultMap = admin.updateOnboardApp(jsonOnboard, http);
399         resultMap.containsKey("success");
400         assertEquals("Your application has been updated successfully", resultMap.get("Success"));
401     }
402
403     @Test
404     public void Test9_onboardDelete() throws Exception {
405         JsonOnboard jsonOnboard = new JsonOnboard();
406         jsonOnboard.setAppname("TestApp2");
407         jsonOnboard.setAid(onboardUUID);
408         Map<String, Object> resultMap = admin.deleteOnboardApp(jsonOnboard, http);
409         resultMap.containsKey("success");
410         assertEquals("Your application has been deleted successfully", resultMap.get("Success"));
411     }
412
413     @Test
414     public void Test3_createLockReference() throws Exception {
415         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
416         Map<String, Object> resultMap = lock.createLockReference(lockName, uuid.toString(), appName, null, null, http);
417         @SuppressWarnings("unchecked")
418         Map<String, Object> resultMap1 = (Map<String, Object>) resultMap.get("lock");
419         lockId = (String) resultMap1.get("lock");
420         assertEquals("SUCCESS", resultMap.get("status").toString());
421     }
422
423     @Test
424     public void Test4_accquireLock() throws Exception {
425         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
426         Map<String, Object> resultMap = lock.accquireLock(lockId, uuid.toString(), appName, null, null, http);
427         assertEquals("SUCCESS", resultMap.get("status").toString());
428     }
429
430     @Test
431     public void Test5_currentLockHolder() throws Exception {
432         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
433         Map<String, Object> resultMap = lock.currentLockHolder(lockName, uuid.toString(), appName, null, null, http);
434         assertEquals("SUCCESS", resultMap.get("status").toString());
435     }
436
437     @Test
438     public void Test7_unLock() throws Exception {
439         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
440         Map<String, Object> resultMap = lock.unLock(lockId, uuid.toString(), appName, null, null, http);
441         assertEquals("SUCCESS", resultMap.get("status").toString());
442     }
443
444     @Test
445     public void Test8_delete() throws Exception {
446         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
447         Map<String, Object> resultMap = lock.deleteLock(lockName, uuid.toString(), appName, null, null, http);
448         assertEquals("SUCCESS", resultMap.get("status").toString());
449     }
450 }