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