Remove zookeeper reference
[music.git] / src / test / java / org / onap / music / unittests / TestsUsingCassandra.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
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
10  * 
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  * 
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.
18  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 package org.onap.music.unittests;
24
25 import java.util.List;
26 import java.util.UUID;
27
28 import org.junit.AfterClass;
29 import org.junit.BeforeClass;
30 import org.junit.runner.RunWith;
31 import org.junit.runners.Suite;
32 import org.junit.runners.Suite.SuiteClasses;
33 import org.mindrot.jbcrypt.BCrypt;
34 import org.onap.music.datastore.MusicDataStoreHandle;
35 import org.onap.music.datastore.PreparedQueryObject;
36 import org.onap.music.lockingservice.cassandra.CassaLockStore;
37 import org.onap.music.main.MusicCore;
38 import org.onap.music.main.MusicUtil;
39
40 import com.datastax.driver.core.DataType;
41 import com.datastax.driver.core.ResultSet;
42 import com.datastax.driver.core.Row;
43 import com.sun.jersey.core.util.Base64;
44
45 @RunWith(Suite.class)
46 @SuiteClasses({ TstRestMusicDataAPI.class, TstRestMusicLockAPI.class, TstRestMusicAdminAPI.class,
47     TstRestMusicConditionalAPI.class, TstCachingUtil.class})
48 public class TestsUsingCassandra {
49
50         static String appName = "TestApp";
51         static String userId = "TestUser";
52         static String password = "TestPassword";
53         static String authData = userId+":"+password;
54         static String wrongAuthData = userId+":"+"pass";
55         static String authorization = new String(Base64.encode(authData.getBytes()));
56         static String wrongAuthorization = new String(Base64.encode(wrongAuthData.getBytes()));
57         static boolean isAAF = false;
58         static UUID uuid = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40ce6");
59         static String keyspaceName = "testcassa";
60         static String tableName = "employees";
61         static String xLatestVersion = "X-latestVersion";
62         static String onboardUUID = null;
63         static String aid = "abc66ccc-d857-4e90-b1e5-df98a3d40ce6";
64         
65         @BeforeClass
66         public static void beforeClass() throws Exception {
67                 MusicDataStoreHandle.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra();
68                 MusicCore.mLockHandle = new CassaLockStore(MusicDataStoreHandle.mDstoreHandle);
69                 createAdminTable();
70         }
71         
72         @AfterClass
73         public static void afterClass() {
74                 PreparedQueryObject testObject = new PreparedQueryObject();
75                 testObject.appendQueryString("DROP KEYSPACE IF EXISTS admin");
76                 MusicCore.eventualPut(testObject);
77                 if(MusicDataStoreHandle.mDstoreHandle!=null)
78                         MusicDataStoreHandle.mDstoreHandle.close();
79         }
80         
81         private static void createAdminTable() throws Exception {
82                 PreparedQueryObject testObject = new PreparedQueryObject();
83                 testObject.appendQueryString(CassandraCQL.createAdminKeyspace);
84                 MusicCore.eventualPut(testObject);
85                 testObject = new PreparedQueryObject();
86                 testObject.appendQueryString(CassandraCQL.createAdminTable);
87                 MusicCore.eventualPut(testObject);
88
89                 testObject = new PreparedQueryObject();
90                 testObject.appendQueryString(
91                                 "INSERT INTO admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
92                                                 + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)");
93                 testObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
94                 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(),
95                         keyspaceName));
96                 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
97                 testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
98                 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), BCrypt.hashpw(password, BCrypt.gensalt())));
99                 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
100                 testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
101                 MusicCore.eventualPut(testObject);
102
103                 testObject = new PreparedQueryObject();
104                 testObject.appendQueryString(
105                                 "select uuid from admin.keyspace_master where application_name = ? allow filtering");
106                 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
107                 ResultSet rs = MusicCore.get(testObject);
108                 List<Row> rows = rs.all();
109                 if (rows.size() > 0) {
110                         System.out.println("#######UUID is:" + rows.get(0).getUUID("uuid"));
111                 }
112         }
113 }