CADI and a few small updates.
[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.MusicDataStore;
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 import org.springframework.test.util.ReflectionTestUtils;
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,
47     TstRestMusicConditionalAPI.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         ReflectionTestUtils.setField(MusicDataStoreHandle.class, "mDstoreHandle",
68                 CassandraCQL.connectToEmbeddedCassandra());
69         MusicCore.mLockHandle = new CassaLockStore(MusicDataStoreHandle.getDSHandle());
70         createAdminTable();
71     }
72     
73     @AfterClass
74     public static void afterClass() {
75         PreparedQueryObject testObject = new PreparedQueryObject();
76         testObject.appendQueryString("DROP KEYSPACE IF EXISTS admin");
77         MusicCore.eventualPut(testObject);
78         MusicDataStore mds = (MusicDataStore) ReflectionTestUtils.getField(MusicDataStoreHandle.class, "mDstoreHandle");
79         if (mds != null)
80             mds.close();
81     }
82     
83     private static void createAdminTable() throws Exception {
84         PreparedQueryObject testObject = new PreparedQueryObject();
85         testObject.appendQueryString(CassandraCQL.createAdminKeyspace);
86         MusicCore.eventualPut(testObject);
87         testObject = new PreparedQueryObject();
88         testObject.appendQueryString(CassandraCQL.createAdminTable);
89         MusicCore.eventualPut(testObject);
90
91         testObject = new PreparedQueryObject();
92         testObject.appendQueryString(
93                 "INSERT INTO admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
94                         + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)");
95         testObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
96         testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(),
97                 keyspaceName));
98         testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
99         testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
100         testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), BCrypt.hashpw(password, BCrypt.gensalt())));
101         testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
102         testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
103         MusicCore.eventualPut(testObject);
104
105                 testObject = new PreparedQueryObject();
106                 testObject.appendQueryString(
107                                 "select uuid from admin.keyspace_master where application_name = ? allow filtering");
108                 testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
109                 ResultSet rs = MusicCore.get(testObject);
110                 List<Row> rows = rs.all();
111                 if (rows.size() > 0) {
112                         System.out.println("#######UUID is:" + rows.get(0).getUUID("uuid"));
113                         onboardUUID = rows.get(0).getUUID("uuid").toString();
114                 }
115         }
116 }