2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=============================================
20 * ====================================================================
22 package org.onap.music.unittests;
28 import java.io.IOException;
29 import java.math.BigInteger;
30 import java.net.InetAddress;
31 import java.net.NetworkInterface;
32 import java.net.SocketException;
33 import java.util.ArrayList;
34 import java.util.Enumeration;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.List;
39 import java.util.UUID;
40 import org.apache.cassandra.exceptions.ConfigurationException;
41 import org.apache.thrift.transport.TTransportException;
42 import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
43 import org.onap.music.datastore.MusicDataStore;
44 import org.onap.music.datastore.PreparedQueryObject;
45 import com.datastax.driver.core.Cluster;
46 import com.datastax.driver.core.Session;
47 import com.datastax.driver.core.exceptions.NoHostAvailableException;
49 public class CassandraCQL {
51 public static final String createKeySpace =
52 "CREATE KEYSPACE IF NOT EXISTS testCassa WITH replication = {'class':'SimpleStrategy','replication_factor':1} AND durable_writes = true;";
54 public static final String dropKeyspace = "DROP KEYSPACE IF EXISTS testCassa";
56 public static final String createTableEmployees =
57 "CREATE TABLE IF NOT EXISTS testCassa.employees "
58 + "(vector_ts text,emp_id uuid,emp_name text,emp_salary varint,address Map<text,text>,PRIMARY KEY (emp_name)) "
59 + "WITH comment='Financial Info of employees' "
60 + "AND compression={'sstable_compression':'DeflateCompressor','chunk_length_kb':64} "
61 + "AND compaction={'class':'SizeTieredCompactionStrategy','min_threshold':6};";
63 public static final String insertIntoTablePrepared1 =
64 "INSERT INTO testCassa.employees (vector_ts,emp_id,emp_name,emp_salary) VALUES (?,?,?,?); ";
66 public static final String insertIntoTablePrepared2 =
67 "INSERT INTO testCassa.employees (vector_ts,emp_id,emp_name,emp_salary,address) VALUES (?,?,?,?,?);";
69 public static final String selectALL = "SELECT * FROM testCassa.employees;";
71 public static final String selectSpecific =
72 "SELECT * FROM testCassa.employees WHERE emp_name= ?;";
74 public static final String updatePreparedQuery =
75 "UPDATE testCassa.employees SET vector_ts=?,address= ? WHERE emp_name= ?;";
77 public static final String deleteFromTable = " ";
79 public static final String deleteFromTablePrepared = " ";
81 // Set Values for Prepared Query
83 public static List<Object> setPreparedInsertValues1() {
85 List<Object> preppreparedInsertValues1 = new ArrayList<>();
87 String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis());
88 UUID emp_id = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40cd6");
89 BigInteger emp_salary = BigInteger.valueOf(23443);
90 String emp_name = "Mr Test one";
91 preppreparedInsertValues1.add(vectorTs);
92 preppreparedInsertValues1.add(emp_id);
93 preppreparedInsertValues1.add(emp_name);
94 preppreparedInsertValues1.add(emp_salary);
95 return preppreparedInsertValues1;
98 public static List<Object> setPreparedInsertValues2() {
100 List<Object> preparedInsertValues2 = new ArrayList<>();
102 String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis());
103 UUID emp_id = UUID.fromString("abc434cc-d657-4e90-b4e5-df4223d40cd6");
104 BigInteger emp_salary = BigInteger.valueOf(45655);
105 String emp_name = "Mr Test two";
106 Map<String, String> address = new HashMap<>();
107 preparedInsertValues2.add(vectorTs);
108 preparedInsertValues2.add(emp_id);
109 preparedInsertValues2.add(emp_name);
110 preparedInsertValues2.add(emp_salary);
111 address.put("Street", "1 att way");
112 address.put("City", "Bedmister");
113 preparedInsertValues2.add(address);
114 return preparedInsertValues2;
117 public static List<Object> setPreparedUpdateValues() {
119 List<Object> preparedUpdateValues = new ArrayList<>();
121 String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis());
122 Map<String, String> address = new HashMap<>();
123 preparedUpdateValues.add(vectorTs);
124 String emp_name = "Mr Test one";
125 address.put("Street", "101 Att Way");
126 address.put("City", "Bedmister");
127 preparedUpdateValues.add(address);
128 preparedUpdateValues.add(emp_name);
129 return preparedUpdateValues;
132 // Generate Different Prepared Query Objects
134 * Query Object for Get
138 public static PreparedQueryObject setPreparedGetQuery() {
140 PreparedQueryObject queryObject = new PreparedQueryObject();
141 String emp_name1 = "Mr Test one";
142 queryObject.appendQueryString(selectSpecific);
143 queryObject.addValue(emp_name1);
148 * Query Object 1 for Insert
152 public static PreparedQueryObject setPreparedInsertQueryObject1() {
154 PreparedQueryObject queryobject = new PreparedQueryObject();
155 queryobject.appendQueryString(insertIntoTablePrepared1);
156 List<Object> values = setPreparedInsertValues1();
157 if (!values.isEmpty() || values != null) {
158 for (Object o : values) {
159 queryobject.addValue(o);
167 * Query Object 2 for Insert
171 public static PreparedQueryObject setPreparedInsertQueryObject2() {
173 PreparedQueryObject queryobject = new PreparedQueryObject();
174 queryobject.appendQueryString(insertIntoTablePrepared2);
175 List<Object> values = setPreparedInsertValues2();
176 if (!values.isEmpty() || values != null) {
177 for (Object o : values) {
178 queryobject.addValue(o);
186 * Query Object for Update
190 public static PreparedQueryObject setPreparedUpdateQueryObject() {
192 PreparedQueryObject queryobject = new PreparedQueryObject();
193 queryobject.appendQueryString(updatePreparedQuery);
194 List<Object> values = setPreparedUpdateValues();
195 if (!values.isEmpty() || values != null) {
196 for (Object o : values) {
197 queryobject.addValue(o);
204 private static ArrayList<String> getAllPossibleLocalIps() {
205 ArrayList<String> allPossibleIps = new ArrayList<String>();
207 Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
208 while (en.hasMoreElements()) {
209 NetworkInterface ni = (NetworkInterface) en.nextElement();
210 Enumeration<InetAddress> ee = ni.getInetAddresses();
211 while (ee.hasMoreElements()) {
212 InetAddress ia = (InetAddress) ee.nextElement();
213 allPossibleIps.add(ia.getHostAddress());
216 } catch (SocketException e) {
217 System.out.println(e.getMessage());
219 return allPossibleIps;
222 public static MusicDataStore connectToEmbeddedCassandra() {
223 Iterator<String> it = getAllPossibleLocalIps().iterator();
224 String address = "localhost";
226 Cluster cluster = null;
227 Session session = null;
228 while (it.hasNext()) {
232 EmbeddedCassandraServerHelper.startEmbeddedCassandra(60000);
233 } catch (ConfigurationException | TTransportException | IOException e) {
235 System.out.println(e.getMessage());
238 cluster = new Cluster.Builder().addContactPoint(address).withPort(9142).build();
239 session = cluster.connect();
242 } catch (NoHostAvailableException e) {
244 System.out.println(e.getMessage());
248 return new MusicDataStore(cluster, session);