Update MUSIC test cases.
[music.git] / src / test / java / org / onap / music / unittests / TstRestMusicAdminAPI.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
19 package org.onap.music.unittests;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertNotEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.UUID;
32
33 import javax.servlet.http.HttpServletResponse;
34 import javax.ws.rs.core.MultivaluedMap;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.UriInfo;
37
38 import org.apache.curator.test.TestingServer;
39 import org.junit.AfterClass;
40 import org.junit.BeforeClass;
41 import org.junit.FixMethodOrder;
42 import org.junit.Ignore;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.junit.runners.MethodSorters;
46 import org.mindrot.jbcrypt.BCrypt;
47 import org.mockito.InjectMocks;
48 import org.mockito.Mock;
49 import org.mockito.Mockito;
50 import org.mockito.MockitoAnnotations;
51 import org.onap.music.authentication.MusicAuthentication;
52 import org.onap.music.conductor.conditionals.JsonConditional;
53 import org.onap.music.conductor.conditionals.RestMusicConditionalAPI;
54 import org.onap.music.datastore.MusicDataStoreHandle;
55 import org.onap.music.datastore.PreparedQueryObject;
56 import org.onap.music.datastore.jsonobjects.JsonDelete;
57 import org.onap.music.datastore.jsonobjects.JsonInsert;
58 import org.onap.music.datastore.jsonobjects.JsonKeySpace;
59 import org.onap.music.datastore.jsonobjects.JsonLeasedLock;
60 import org.onap.music.datastore.jsonobjects.JsonOnboard;
61 import org.onap.music.datastore.jsonobjects.JsonSelect;
62 import org.onap.music.datastore.jsonobjects.JsonTable;
63 import org.onap.music.datastore.jsonobjects.JsonUpdate;
64 import org.onap.music.exceptions.MusicServiceException;
65 import org.onap.music.lockingservice.zookeeper.MusicLockingService;
66 import org.onap.music.main.MusicCore;
67 import org.onap.music.main.MusicUtil;
68 import org.onap.music.main.ResultType;
69 import org.onap.music.rest.RestMusicAdminAPI;
70 import org.onap.music.rest.RestMusicBmAPI;
71 import org.onap.music.rest.RestMusicDataAPI;
72 import org.onap.music.rest.RestMusicHealthCheckAPI;
73 import org.onap.music.rest.RestMusicLocksAPI;
74 import org.onap.music.rest.RestMusicTestAPI;
75 import org.onap.music.rest.RestMusicVersionAPI;
76 import org.onap.music.service.impl.MusicZKCore;
77
78 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
79
80 import com.datastax.driver.core.DataType;
81 import com.datastax.driver.core.ResultSet;
82 import com.datastax.driver.core.Row;
83 import com.sun.jersey.core.util.Base64;
84 import com.sun.jersey.core.util.MultivaluedMapImpl;
85
86 @Ignore //TODO need to resolve static calls to music authenticate
87 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
88 //@RunWith(PowerMockRunner.class)
89 //@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
90 //@PrepareForTest(MusicAuthentication.class)
91 public class TstRestMusicAdminAPI {
92
93     RestMusicAdminAPI admin = new RestMusicAdminAPI();
94     static PreparedQueryObject testObject;
95
96     @Mock
97     HttpServletResponse http;
98
99     @Mock
100     UriInfo info;
101     
102     static String appName = "TestApp";
103     static String userId = "TestUser";
104     static String password = "TestPassword";
105     static String adminName = "username";
106     static String adminPassword = "password";
107     static String adminAuthData = adminName +":"+adminPassword;
108     static String wrongAdminAuthData = adminName+"123"+":"+adminPassword;
109     static String authData = userId+":"+password;
110     static String wrongAuthData = userId+":"+"pass";
111     static String authorization = new String(Base64.encode(authData.getBytes()));
112     static String wrongAuthorization = new String(Base64.encode(wrongAuthData.getBytes()));
113     static String adminAuthorization = new String(Base64.encode(adminAuthData.getBytes()));
114     static String worngAdminAuthorization = new String(Base64.encode(wrongAdminAuthData.getBytes()));
115     
116     static boolean isAAF = false;
117     static UUID uuid = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40ce6");
118     static String keyspaceName = "testCassa";
119     static String tableName = "employees";
120     static String tableNameConditional = "Conductor";
121     static String xLatestVersion = "X-latestVersion";
122     static String onboardUUID = null;
123     static String lockId = null;
124     static String lockName = "testCassa.employees.sample3";
125
126     @BeforeClass
127     public static void init() throws Exception {
128                 System.out.println("Testing RestMusicAdmin class");
129                 //PowerMockito.mockStatic(MusicAuthentication.class);
130         try {
131                 MusicDataStoreHandle.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra();
132                         createAdminTable();
133                 } catch (Exception e) {
134                         e.printStackTrace();
135                         throw new Exception("Unable to initialize before TestRestMusicData test class. " + e.getMessage());
136                 }
137     }
138
139     @AfterClass
140     public static void tearDownAfterClass() throws Exception {
141         testObject = new PreparedQueryObject();
142         testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName);
143         MusicCore.eventualPut(testObject);
144         testObject = new PreparedQueryObject();
145         testObject.appendQueryString("DROP KEYSPACE IF EXISTS admin");
146         MusicCore.eventualPut(testObject);
147         MusicDataStoreHandle.mDstoreHandle.close();
148     }
149
150     
151     //TODO FIX tests for admin
152     
153
154     @Test
155     public void test6_onboard() throws Exception {
156         System.out.println("Testing application onboarding");
157         
158         authenticateTrue();
159         
160         JsonOnboard jsonOnboard = new JsonOnboard();
161         jsonOnboard.setAppname("TestApp2");
162         jsonOnboard.setIsAAF("false"); jsonOnboard.setUserId("TestUser2");
163         jsonOnboard.setPassword("TestPassword2");
164
165         Response response = admin.onboardAppWithMusic(jsonOnboard,adminAuthorization);
166         System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity());
167         /*resultMap.containsKey("success"); onboardUUID =
168                         resultMap.get("Generated AID").toString();
169         assertEquals("Your application TestApp2 has been onboarded with MUSIC.",
170                         resultMap.get("Success")); */
171         assertEquals(200, response.getStatus());
172     }
173
174         @Test
175     public void Test6_onboard_duplicate() throws Exception {
176         JsonOnboard jsonOnboard = new JsonOnboard();
177         jsonOnboard.setAppname("TestApp2");
178         jsonOnboard.setIsAAF("false");
179         jsonOnboard.setUserId("TestUser2");
180         jsonOnboard.setPassword("TestPassword2");
181         Response response = admin.onboardAppWithMusic(jsonOnboard,adminAuthorization);
182         assertEquals(204, response.getStatus());
183     }
184
185     // Missing appname
186     @Test
187     public void Test6_onboard1() throws Exception {
188         JsonOnboard jsonOnboard = new JsonOnboard();
189         jsonOnboard.setIsAAF("false");
190         jsonOnboard.setUserId("TestUser2");
191         jsonOnboard.setPassword("TestPassword2");
192         Map<String, Object> resultMap = (Map<String, Object>) admin.onboardAppWithMusic(jsonOnboard,adminAuthorization).getEntity();
193 //        assertTrue(resultMap.containsKey("error"));
194         //System.out.println("--->" + resultMap.toString());
195         //assertEquals("Unauthorized: Please check the request parameters. Some of the required values appName(ns), userId, password, isAAF are missing.", resultMap.get("Exception"));
196     }
197
198
199     @Test
200     public void Test7_onboardSearch() throws Exception {
201         JsonOnboard jsonOnboard = new JsonOnboard();
202         jsonOnboard.setAppname("TestApp2");
203         jsonOnboard.setIsAAF("false");
204         jsonOnboard.setAid(onboardUUID);
205         Map<String, Object> resultMap = (Map<String, Object>) admin.getOnboardedInfoSearch(jsonOnboard,adminAuthorization).getEntity();
206         resultMap.containsKey("success");
207         assertEquals(null, resultMap.get(onboardUUID));
208     }
209
210     // Missing appname
211     @Test
212     public void Test7_onboardSearch1() throws Exception {
213         JsonOnboard jsonOnboard = new JsonOnboard();
214         jsonOnboard.setIsAAF("false");
215         jsonOnboard.setAid(onboardUUID);
216         Map<String, Object> resultMap = (Map<String, Object>) admin.getOnboardedInfoSearch(jsonOnboard,adminAuthorization).getEntity();
217         System.out.println("--->" + resultMap.toString());
218         resultMap.containsKey("success");
219         assertEquals(null, resultMap.get(onboardUUID));
220     }
221     
222     @Test
223     public void Test7_onboardSearch_empty() throws Exception {
224         JsonOnboard jsonOnboard = new JsonOnboard();
225         Response response =  admin.getOnboardedInfoSearch(jsonOnboard,adminAuthorization);
226       //  assertEquals(400, response.getStatus());
227     }
228
229     @Test
230     public void Test7_onboardSearch_invalidAid() throws Exception {
231         JsonOnboard jsonOnboard = new JsonOnboard();
232         jsonOnboard.setAppname("TestApp2");
233         jsonOnboard.setIsAAF("false");
234         jsonOnboard.setAid("abc66ccc-d857-4e90-b1e5-df98a3d40ce6");
235         Response response = admin.getOnboardedInfoSearch(jsonOnboard,adminAuthorization);
236        // assertEquals(400, response.getStatus());
237     }
238
239     @Test
240     public void Test8_onboardUpdate() throws Exception {
241         JsonOnboard jsonOnboard = new JsonOnboard();
242         jsonOnboard.setIsAAF("false");
243         jsonOnboard.setUserId("TestUser3");
244         jsonOnboard.setPassword("TestPassword3");
245         jsonOnboard.setAid(onboardUUID);
246         Map<String, Object> resultMap = (Map<String, Object>) admin.updateOnboardApp(jsonOnboard,adminAuthorization).getEntity();
247         System.out.println("--->" + resultMap.toString());
248         resultMap.containsKey("success");
249         assertNotNull(resultMap);
250     }
251
252     // Aid null
253     @Test
254     public void Test8_onboardUpdate1() throws Exception {
255         JsonOnboard jsonOnboard = new JsonOnboard();
256         jsonOnboard.setIsAAF("false");
257         jsonOnboard.setUserId("TestUser3");
258         jsonOnboard.setPassword("TestPassword3");
259         Map<String, Object> resultMap = (Map<String, Object>) admin.updateOnboardApp(jsonOnboard,adminAuthorization).getEntity();
260         System.out.println("--->" + resultMap.toString());
261         assertNotNull(resultMap);
262     }
263
264     // Appname not null
265     @Test
266     public void Test8_onboardUpdate2() throws Exception {
267         JsonOnboard jsonOnboard = new JsonOnboard();
268         jsonOnboard.setAppname("TestApp2");
269         jsonOnboard.setIsAAF("false");
270         jsonOnboard.setUserId("TestUser3");
271         jsonOnboard.setPassword("TestPassword3");
272         jsonOnboard.setAid(onboardUUID);
273         Map<String, Object> resultMap = (Map<String, Object>) admin.updateOnboardApp(jsonOnboard,adminAuthorization).getEntity();
274         assertNotNull(resultMap);
275     }
276
277     // All null
278     @Test
279     public void Test8_onboardUpdate3() throws Exception {
280         JsonOnboard jsonOnboard = new JsonOnboard();
281         jsonOnboard.setAid(onboardUUID);
282         Map<String, Object> resultMap = (Map<String, Object>) admin.updateOnboardApp(jsonOnboard,adminAuthorization).getEntity();
283         assertNotNull(resultMap);
284     }
285
286     @Test
287     public void Test9_onboardDelete() throws Exception {
288         JsonOnboard jsonOnboard = new JsonOnboard();
289         jsonOnboard.setAppname("TestApp2");
290         jsonOnboard.setAid(onboardUUID);
291         Map<String, Object> resultMap = (Map<String, Object>) admin.deleteOnboardApp(jsonOnboard,adminAuthorization).getEntity();
292         resultMap.containsKey("success");
293         assertNotNull(resultMap);
294     }
295
296     @Test
297     public void Test9_onboardDelete1() throws Exception {
298         JsonOnboard jsonOnboard = new JsonOnboard();
299         Map<String, Object> resultMap = (Map<String, Object>) admin.deleteOnboardApp(jsonOnboard,adminAuthorization).getEntity();
300         assertNotNull(resultMap);
301     }
302
303     //Music Health Check
304     @Test
305     public void Test3_HealthCheck_cassandra() {
306         String consistency = "ONE";
307         RestMusicHealthCheckAPI healthCheck = new RestMusicHealthCheckAPI();
308         HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class);
309         Response response = healthCheck.cassandraStatus(servletResponse, consistency);
310         assertEquals(200, response.getStatus());
311     }
312
313     @Test
314     public void Test3_HealthCheck_cassandra_cosistencyQuorum() {
315         String consistency = "QUORUM";
316         RestMusicHealthCheckAPI healthCheck = new RestMusicHealthCheckAPI();
317         HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class);
318         Response response = healthCheck.cassandraStatus(servletResponse, consistency);
319         assertEquals(200, response.getStatus());
320     }
321
322     @Ignore
323     @Test
324     public void Test3_HealthCheck_zookeeper() {
325         RestMusicHealthCheckAPI healthCheck = new RestMusicHealthCheckAPI();
326         HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class);
327         Response response = healthCheck.ZKStatus(servletResponse);
328         assertEquals(200, response.getStatus());
329     }
330
331     @Ignore
332     public void Test4_pureZKcreate() throws Exception {
333         RestMusicBmAPI bmApi = new RestMusicBmAPI();
334         bmApi.pureZkCreate("sample");
335     }
336
337     
338     @Ignore
339     public void Test4_pureZKUpdate() throws Exception {
340         RestMusicBmAPI bmApi = new RestMusicBmAPI();
341         bmApi.pureZkCreate("sample1");
342         JsonInsert jsonInsert = new JsonInsert();
343         Map<String, String> consistencyInfo = new HashMap<>();
344         Map<String, Object> values = new HashMap<>();
345         values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
346         values.put("emp_name", "testName_create");
347         values.put("emp_salary", 500);
348         consistencyInfo.put("type", "eventual");
349         jsonInsert.setConsistencyInfo(consistencyInfo);
350         jsonInsert.setKeyspaceName(keyspaceName);
351         jsonInsert.setTableName(tableName);
352         jsonInsert.setValues(values);
353         bmApi.pureZkUpdate(jsonInsert, "sampleNode1");
354     }
355
356     @Ignore
357     public void Test4_pureZKGet() throws Exception {
358         RestMusicBmAPI bmApi = new RestMusicBmAPI();
359         bmApi.pureZkGet("sample");
360     }
361
362     /*
363      * @Test public void Test5_ZKAtomicPut_atomic() throws Exception {
364      * RestMusicBmAPI bmApi = new RestMusicBmAPI(); JsonInsert jsonInsert = new
365      * JsonInsert(); Map<String, String> consistencyInfo = new HashMap<>();
366      * Map<String, Object> values = new HashMap<>(); values.put("uuid",
367      * "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); values.put("emp_name",
368      * "testName_create"); values.put("emp_salary", 1500);
369      * consistencyInfo.put("type", "atomic");
370      * jsonInsert.setConsistencyInfo(consistencyInfo);
371      * jsonInsert.setKeyspaceName(keyspaceName); jsonInsert.setTableName(tableName);
372      * jsonInsert.setValues(values); bmApi.pureZkAtomicPut(jsonInsert, lockName,
373      * "sampleNode1"); }
374      */
375     /*
376      * @Test public void Test5_ZKAtomicPut_atomic_with_delete() throws Exception {
377      * RestMusicBmAPI bmApi = new RestMusicBmAPI(); JsonInsert jsonInsert = new
378      * JsonInsert(); Map<String, String> consistencyInfo = new HashMap<>();
379      * Map<String, Object> values = new HashMap<>(); values.put("uuid",
380      * "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); values.put("emp_name",
381      * "testName_create"); values.put("emp_salary", 1500);
382      * consistencyInfo.put("type", "atomic_delete_lock");
383      * jsonInsert.setConsistencyInfo(consistencyInfo);
384      * jsonInsert.setKeyspaceName(keyspaceName); jsonInsert.setTableName(tableName);
385      * jsonInsert.setValues(values); bmApi.pureZkAtomicPut(jsonInsert, lockName,
386      * "sampleNode1"); }
387      */
388
389     /*
390      * @Test public void Test5_ZKAtomicGet_atomic() throws Exception {
391      * RestMusicBmAPI bmApi = new RestMusicBmAPI(); JsonInsert jsonInsert = new
392      * JsonInsert(); Map<String, String> consistencyInfo = new HashMap<>();
393      * Map<String, Object> values = new HashMap<>(); values.put("uuid",
394      * "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); values.put("emp_name",
395      * "testName_create"); values.put("emp_salary", 1500);
396      * consistencyInfo.put("type", "atomic_delete_lock");
397      * jsonInsert.setConsistencyInfo(consistencyInfo);
398      * jsonInsert.setKeyspaceName(keyspaceName); jsonInsert.setTableName(tableName);
399      * jsonInsert.setValues(values); bmApi.pureZkAtomicGet(jsonInsert, lockName,
400      * "sampleNode1"); }
401      */
402
403     /*
404      * @Test public void Test5_ZKAtomicGet_atomic_with_delete() throws Exception {
405      * RestMusicBmAPI bmApi = new RestMusicBmAPI(); JsonInsert jsonInsert = new
406      * JsonInsert(); Map<String, String> consistencyInfo = new HashMap<>();
407      * Map<String, Object> values = new HashMap<>(); values.put("uuid",
408      * "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); values.put("emp_name",
409      * "testName_create"); values.put("emp_salary", 1500);
410      * consistencyInfo.put("type", "atomic_delete_lock");
411      * jsonInsert.setConsistencyInfo(consistencyInfo);
412      * jsonInsert.setKeyspaceName(keyspaceName); jsonInsert.setTableName(tableName);
413      * jsonInsert.setValues(values); bmApi.pureZkAtomicGet(jsonInsert, lockName,
414      * "sampleNode1"); }
415      */
416
417     @Ignore
418     @Test
419     public void Test5_updateCassa() throws Exception {
420         RestMusicBmAPI bmApi = new RestMusicBmAPI();
421         JsonInsert jsonInsert = new JsonInsert();
422         Map<String, String> consistencyInfo = new HashMap<>();
423         Map<String, Object> values = new HashMap<>();
424         values.put("emp_salary", 1500);
425         consistencyInfo.put("type", "eventual");
426         jsonInsert.setConsistencyInfo(consistencyInfo);
427         jsonInsert.setKeyspaceName(keyspaceName);
428         jsonInsert.setTableName(tableName);
429         jsonInsert.setValues(values);
430         MultivaluedMap<String, String> row = new MultivaluedMapImpl();
431         row.add("emp_name", "testName_create");
432         Mockito.when(info.getQueryParameters()).thenReturn(row);
433         //bmApi.updateTableCassa(jsonInsert, keyspaceName, tableName, info);
434     }
435
436     // RestMusicConditional
437     @Test
438     public void Test5_createTable_conditional() throws Exception {
439         JsonTable jsonTable = new JsonTable();
440         Map<String, String> consistencyInfo = new HashMap<>();
441         Map<String, String> fields = new HashMap<>();
442         fields.put("id", "text");
443         fields.put("plans", "Map<text,text>");
444         fields.put("PRIMARY KEY", "(id)");
445         consistencyInfo.put("type", "eventual");
446         jsonTable.setConsistencyInfo(consistencyInfo);
447         jsonTable.setKeyspaceName(keyspaceName);
448         jsonTable.setPrimaryKey("id");
449         jsonTable.setTableName(tableNameConditional);
450         jsonTable.setFields(fields);
451         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
452         /*Response response = data.createTable("1", "1", "1",
453                         "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",appName, authorization,
454                         jsonTable, keyspaceName, tableNameConditional);
455         System.out.println("#######status is " + response.getStatus());
456         System.out.println("Entity" + response.getEntity());
457         //assertEquals(200, response.getStatus());
458         assertEquals(401, response.getStatus());
459         */
460     }
461
462     @Test
463     public void Test6_insertConditional() throws Exception {
464         RestMusicConditionalAPI conditionalApi = new RestMusicConditionalAPI();
465         JsonConditional json = new JsonConditional();
466         json.setPrimaryKey("id");
467         json.setPrimaryKeyValue("123|abc|port");
468         json.setCasscadeColumnName("plans");
469         Map<String, Object> tableValues =  new HashMap<>();
470         tableValues.put("id", "123|abc|port");
471         json.setTableValues(tableValues);
472         Map<String, Object> columnData =  new HashMap<>();
473         Map<String, String> column =  new HashMap<>();
474         column.put("created", "time");
475         columnData.put("key", "P2");
476         columnData.put("value", column);
477         json.setCasscadeColumnData(columnData);
478         Map<String, String> cond = new HashMap<>();
479         Map<String, String> cond1 = new HashMap<>();
480         Map<String, Map<String, String>> conditions = new HashMap<String, Map<String, String>>();
481         cond.put("status", "under-spin-up");
482         cond1.put("status", "parked");
483         conditions.put("exists", cond);
484         conditions.put("nonexists", cond1);
485         json.setConditions(conditions);
486         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
487         Response response = conditionalApi.insertConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
488                 appName, authorization, keyspaceName, tableNameConditional, json);
489         assertEquals(401, response.getStatus());
490     }
491
492     @Test
493     public void Test6_insertConditional_primaryKey_null() throws Exception {
494         RestMusicConditionalAPI conditionalApi = new RestMusicConditionalAPI();
495         JsonConditional json = new JsonConditional();
496         json.setPrimaryKeyValue("123|abc|port");
497         json.setCasscadeColumnName("plans");
498         Map<String, Object> tableValues =  new HashMap<>();
499         tableValues.put("id", "123|abc|port");
500         json.setTableValues(tableValues);
501         Map<String, Object> columnData =  new HashMap<>();
502         Map<String, String> column =  new HashMap<>();
503         column.put("created", "time");
504         columnData.put("key", "P2");
505         columnData.put("value", column);
506         json.setCasscadeColumnData(columnData);
507         Map<String, String> cond = new HashMap<>();
508         Map<String, String> cond1 = new HashMap<>();
509         Map<String, Map<String, String>> conditions = new HashMap<String, Map<String, String>>();
510         cond.put("status", "under-spin-up");
511         cond1.put("status", "parked");
512         conditions.put("exists", cond);
513         conditions.put("nonexists", cond1);
514         json.setConditions(conditions);
515         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
516         Response response = conditionalApi.insertConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
517                 appName, authorization, keyspaceName, tableNameConditional, json);
518         assertEquals(401, response.getStatus());
519     }
520
521     @Test
522     public void Test6_insertConditional_wrongAuth() throws Exception {
523         RestMusicConditionalAPI conditionalApi = new RestMusicConditionalAPI();
524         JsonConditional json = new JsonConditional();
525         json.setPrimaryKey("id");
526         json.setPrimaryKeyValue("123|abc|port");
527         json.setCasscadeColumnName("plans");
528         Map<String, Object> tableValues =  new HashMap<>();
529         tableValues.put("id", "123|abc|port");
530         json.setTableValues(tableValues);
531         Map<String, Object> columnData =  new HashMap<>();
532         Map<String, String> column =  new HashMap<>();
533         column.put("created", "time");
534         columnData.put("key", "P2");
535         columnData.put("value", column);
536         json.setCasscadeColumnData(columnData);
537         Map<String, String> cond = new HashMap<>();
538         Map<String, String> cond1 = new HashMap<>();
539         Map<String, Map<String, String>> conditions = new HashMap<String, Map<String, String>>();
540         cond.put("status", "under-spin-up");
541         cond1.put("status", "parked");
542         conditions.put("exists", cond);
543         conditions.put("nonexists", cond1);
544         json.setConditions(conditions);
545         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
546         Response response = conditionalApi.insertConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
547                 appName, wrongAuthorization, keyspaceName, tableNameConditional, json);
548         assertEquals(401, response.getStatus());
549     }
550
551     @Test
552     public void Test7_updateConditional() throws Exception {
553         RestMusicConditionalAPI conditionalApi = new RestMusicConditionalAPI();
554         JsonConditional json = new JsonConditional();
555         json.setPrimaryKey("id");
556         json.setPrimaryKeyValue("123|abc|port");
557         json.setCasscadeColumnName("plans");
558         Map<String, Object> tableValues =  new HashMap<>();
559         tableValues.put("id", "123|abc|port");
560         json.setTableValues(tableValues);
561         Map<String, Object> columnData =  new HashMap<>();
562         Map<String, String> column =  new HashMap<>();
563         column.put("created", "time");
564         columnData.put("key", "P2");
565         columnData.put("value", column);
566         json.setCasscadeColumnData(columnData);
567         Map<String, String> cond = new HashMap<>();
568         Map<String, String> cond1 = new HashMap<>();
569         Map<String, Map<String, String>> conditions = new HashMap<String, Map<String, String>>();
570         cond.put("updated", "new time");
571         conditions.put("exists", cond);
572         conditions.put("nonexists", cond1);
573         json.setConditions(conditions);
574         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
575         Response response = conditionalApi.updateConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
576                 appName, authorization, keyspaceName, tableNameConditional, json);
577         assertEquals(401, response.getStatus());
578     }
579
580     @Test
581     public void Test7_updateConditional_wrongAuth() throws Exception {
582         RestMusicConditionalAPI conditionalApi = new RestMusicConditionalAPI();
583         JsonConditional json = new JsonConditional();
584         json.setPrimaryKey("id");
585         json.setPrimaryKeyValue("123|abc|port");
586         json.setCasscadeColumnName("plans");
587         Map<String, Object> tableValues =  new HashMap<>();
588         tableValues.put("id", "123|abc|port");
589         json.setTableValues(tableValues);
590         Map<String, Object> columnData =  new HashMap<>();
591         Map<String, String> column =  new HashMap<>();
592         column.put("created", "time");
593         columnData.put("key", "P2");
594         columnData.put("value", column);
595         json.setCasscadeColumnData(columnData);
596         Map<String, String> cond = new HashMap<>();
597         Map<String, String> cond1 = new HashMap<>();
598         Map<String, Map<String, String>> conditions = new HashMap<String, Map<String, String>>();
599         cond.put("updated", "new time");
600         conditions.put("exists", cond);
601         conditions.put("nonexists", cond1);
602         json.setConditions(conditions);
603         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
604         Response response = conditionalApi.updateConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
605                 appName, wrongAuthorization, keyspaceName, tableNameConditional, json);
606         assertEquals(401, response.getStatus());
607     }
608
609     @Test
610     public void Test7_updateConditional_primarykey_null() throws Exception {
611         RestMusicConditionalAPI conditionalApi = new RestMusicConditionalAPI();
612         JsonConditional json = new JsonConditional();
613         json.setPrimaryKeyValue("123|abc|port");
614         json.setCasscadeColumnName("plans");
615         Map<String, Object> tableValues =  new HashMap<>();
616         tableValues.put("id", "123|abc|port");
617         json.setTableValues(tableValues);
618         Map<String, Object> columnData =  new HashMap<>();
619         Map<String, String> column =  new HashMap<>();
620         column.put("created", "time");
621         columnData.put("key", "P2");
622         columnData.put("value", column);
623         json.setCasscadeColumnData(columnData);
624         Map<String, String> cond = new HashMap<>();
625         Map<String, String> cond1 = new HashMap<>();
626         Map<String, Map<String, String>> conditions = new HashMap<String, Map<String, String>>();
627         cond.put("updated", "new time");
628         conditions.put("exists", cond);
629         conditions.put("nonexists", cond1);
630         json.setConditions(conditions);
631         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
632         Response response = conditionalApi.updateConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",
633                 appName, authorization, keyspaceName, tableNameConditional, json);
634         assertEquals(401, response.getStatus());
635     }
636     
637     @Ignore
638     @Test
639     public void Test8_HealthCheck_cassandra_musicHealthCheck() {
640         RestMusicHealthCheckAPI healthCheck = new RestMusicHealthCheckAPI();
641         Response response = healthCheck.musicHealthCheck();
642         assertEquals(200, response.getStatus());
643     }
644     
645     
646     private static void createAdminTable() throws Exception {
647                 testObject = new PreparedQueryObject();
648                 testObject.appendQueryString(CassandraCQL.createAdminKeyspace);
649                 MusicCore.eventualPut(testObject);
650                 testObject = new PreparedQueryObject();
651                 testObject.appendQueryString(CassandraCQL.createAdminTable);
652                 MusicCore.eventualPut(testObject);
653
654                 testObject = new PreparedQueryObject();
655                 testObject.appendQueryString(
656                                 "INSERT INTO admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
657                                                 + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)");
658                 testObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
659                 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(),
660                                 MusicUtil.DEFAULTKEYSPACENAME));
661                 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
662                 testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
663                 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), BCrypt.hashpw(password, BCrypt.gensalt())));
664                 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
665                 testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
666                 MusicCore.eventualPut(testObject);
667
668                 testObject = new PreparedQueryObject();
669                 testObject.appendQueryString(
670                                 "select uuid from admin.keyspace_master where application_name = ? allow filtering");
671                 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
672                 ResultSet rs = MusicCore.get(testObject);
673                 List<Row> rows = rs.all();
674                 if (rows.size() > 0) {
675                         System.out.println("#######UUID is:" + rows.get(0).getUUID("uuid"));
676                 }
677         }
678     
679     /**
680      * Confirm an authentication
681      * @throws Exception 
682      */
683     private void authenticateTrue() throws Exception {
684                 //PowerMockito.when(MusicAuthentication.authenticateAdmin(Mockito.anyString())).thenReturn(true);
685         }
686    
687 }