Add swagger.json
[music.git] / src / test / java / org / onap / music / unittests / CassandraCQL.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
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
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 package org.onap.music.unittests;
23 /**
24  * @author srupane
25  * 
26  */
27
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;
38 import java.util.Map;
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;
48
49 public class CassandraCQL {
50
51     public static final String createKeySpace =
52                     "CREATE KEYSPACE IF NOT EXISTS testCassa WITH replication = {'class':'SimpleStrategy','replication_factor':1} AND durable_writes = true;";
53
54     public static final String dropKeyspace = "DROP KEYSPACE IF EXISTS testCassa";
55
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};";
62
63     public static final String insertIntoTablePrepared1 =
64                     "INSERT INTO testCassa.employees (vector_ts,emp_id,emp_name,emp_salary) VALUES (?,?,?,?); ";
65
66     public static final String insertIntoTablePrepared2 =
67                     "INSERT INTO testCassa.employees (vector_ts,emp_id,emp_name,emp_salary,address) VALUES (?,?,?,?,?);";
68
69     public static final String selectALL = "SELECT *  FROM testCassa.employees;";
70
71     public static final String selectSpecific =
72                     "SELECT *  FROM testCassa.employees WHERE emp_name= ?;";
73
74     public static final String updatePreparedQuery =
75                     "UPDATE testCassa.employees  SET vector_ts=?,address= ? WHERE emp_name= ?;";
76
77     public static final String deleteFromTable = " ";
78
79     public static final String deleteFromTablePrepared = " ";
80
81     // Set Values for Prepared Query
82
83     public static List<Object> setPreparedInsertValues1() {
84
85         List<Object> preppreparedInsertValues1 = new ArrayList<>();
86         String vectorTs =
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;
96     }
97
98     public static List<Object> setPreparedInsertValues2() {
99
100         List<Object> preparedInsertValues2 = new ArrayList<>();
101         String vectorTs =
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;
115     }
116
117     public static List<Object> setPreparedUpdateValues() {
118
119         List<Object> preparedUpdateValues = new ArrayList<>();
120         String vectorTs =
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 Some Way");
126         address.put("City", "New York");
127         preparedUpdateValues.add(address);
128         preparedUpdateValues.add(emp_name);
129         return preparedUpdateValues;
130     }
131
132     // Generate Different Prepared Query Objects
133     /**
134      * Query Object for Get
135      * 
136      * @return
137      */
138     public static PreparedQueryObject setPreparedGetQuery() {
139
140         PreparedQueryObject queryObject = new PreparedQueryObject();
141         String emp_name1 = "Mr Test one";
142         queryObject.appendQueryString(selectSpecific);
143         queryObject.addValue(emp_name1);
144         return queryObject;
145     }
146
147     /**
148      * Query Object 1 for Insert
149      * 
150      * @return
151      */
152     public static PreparedQueryObject setPreparedInsertQueryObject1() {
153
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);
160             }
161         }
162         return queryobject;
163
164     }
165
166     /**
167      * Query Object 2 for Insert
168      * 
169      * @return
170      */
171     public static PreparedQueryObject setPreparedInsertQueryObject2() {
172
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);
179             }
180         }
181         return queryobject;
182
183     }
184
185     /**
186      * Query Object for Update
187      * 
188      * @return
189      */
190     public static PreparedQueryObject setPreparedUpdateQueryObject() {
191
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);
198             }
199         }
200         return queryobject;
201
202     }
203
204     private static ArrayList<String> getAllPossibleLocalIps() {
205         ArrayList<String> allPossibleIps = new ArrayList<String>();
206         try {
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());
214                 }
215             }
216         } catch (SocketException e) {
217             System.out.println(e.getMessage());
218         }
219         return allPossibleIps;
220     }
221
222     public static MusicDataStore connectToEmbeddedCassandra() {
223         Iterator<String> it = getAllPossibleLocalIps().iterator();
224         String address = "localhost";
225
226         Cluster cluster = null;
227         Session session = null;
228         while (it.hasNext()) {
229             try {
230
231                 try {
232                     EmbeddedCassandraServerHelper.startEmbeddedCassandra(60000);
233                 } catch (ConfigurationException | TTransportException | IOException e) {
234
235                     System.out.println(e.getMessage());
236                 }
237
238                 cluster = new Cluster.Builder().addContactPoint(address).withPort(9142).build();
239                 session = cluster.connect();
240
241                 break;
242             } catch (NoHostAvailableException e) {
243                 address = it.next();
244                 System.out.println(e.getMessage());
245
246             }
247         }
248         return new MusicDataStore(cluster, session);
249
250     }
251
252 }