METAMODEL-1205: Fixed CassandraUnit, Guava, Hadoop for JDK9+
[metamodel.git] / cassandra / src / test / java / org / apache / metamodel / cassandra / CassandraDataContextTest.java
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.metamodel.cassandra;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.util.Arrays;
28 import java.util.List;
29
30 import javax.swing.table.TableModel;
31
32 import org.apache.metamodel.data.DataSet;
33 import org.apache.metamodel.data.DataSetTableModel;
34 import org.apache.metamodel.data.FilteredDataSet;
35 import org.apache.metamodel.data.InMemoryDataSet;
36 import org.apache.metamodel.query.Query;
37 import org.apache.metamodel.query.parser.QueryParserException;
38 import org.apache.metamodel.schema.ColumnType;
39 import org.apache.metamodel.schema.Table;
40 import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
41 import org.junit.AfterClass;
42 import org.junit.Assume;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45
46 import com.datastax.driver.core.Cluster;
47 import com.datastax.driver.core.Session;
48
49 public class CassandraDataContextTest {
50
51 private static CassandraSimpleClient client = new CassandraSimpleClient();
52 private static Cluster cluster;
53 private static CassandraDataContext dc;
54
55 private static final int defaultCassandraPort = 9142;
56 private static final String cassandraNode = "127.0.0.1";
57 private static String keyspaceName = "my_keyspace";
58 private static String testTableName = "songs";
59 private static String testCounterTableName = "counter";
60 private static String firstRowId = "756716f7-2e54-4715-9f00-91dcbea6cf51";
61 private static String secondRowId = "756716f7-2e54-4715-9f00-91dcbea6cf52";
62 private static String thirdRowId = "756716f7-2e54-4715-9f00-91dcbea6cf53";
63 private static String firstRowTitle = "My first song";
64 private static String secondRowTitle = "My second song";
65 private static String thirdRowTitle = "My third song";
66 private static String urlName = "my_url";
67
68 @BeforeClass
69 public static void setUpCluster() throws Exception {
70 try {
71 EmbeddedCassandraServerHelper.startEmbeddedCassandra(EmbeddedCassandraServerHelper.DEFAULT_CASSANDRA_YML_FILE);
72 } catch (Throwable e) {
73 // CassandraUnit not working working on JDK 9+, see https://github.com/jsevellec/cassandra-unit/issues/249
74 Assume.assumeTrue("Embedded Cassandra server didn't come up: " + e.getMessage(), false);
75 return;
76 }
77 client.connect(cassandraNode, defaultCassandraPort);
78 cluster = client.getCluster();
79 Session session = cluster.connect();
80 createCassandraKeySpaceAndTable(session);
81 populateCassandraTableWithSomeData(session);
82 populateCassandraCounterTableWithSomeData(session);
83 dc = new CassandraDataContext(cluster, keyspaceName);
84 }
85
86 @AfterClass
87 public static void tearDownCluster() throws Exception {
88 client.close();
89 }
90
91 @Test
92 public void testSchemaAndSimpleQuery() throws Exception {
93 String existingTables = Arrays.toString(dc.getDefaultSchema().getTableNames().toArray());
94 assertTrue(existingTables.contains(testTableName));
95 assertTrue(existingTables.contains(testCounterTableName));
96
97 Table table = dc.getDefaultSchema().getTableByName(testTableName);
98
99 assertEquals(ColumnType.UUID, table.getColumnByName("id").getType());
100 assertEquals(ColumnType.STRING, table.getColumnByName("title").getType());
101 assertEquals(ColumnType.BOOLEAN, table.getColumnByName("hit").getType());
102 assertEquals(ColumnType.FLOAT, table.getColumnByName("duration").getType());
103 assertEquals(ColumnType.INTEGER, table.getColumnByName("position").getType());
104 assertEquals(ColumnType.TIMESTAMP, table.getColumnByName("creationtime").getType());
105
106 DataSet ds = dc.query().from(testTableName).select("id").and("title").execute();
107 assertEquals(CassandraDataSet.class, ds.getClass());
108
109 try {
110 assertTrue(ds.next());
111 assertEquals("Row[values=[" + secondRowId + ", " + secondRowTitle + "]]", ds.getRow().toString());
112 assertTrue(ds.next());
113 assertEquals("Row[values=[" + thirdRowId + ", " + thirdRowTitle + "]]", ds.getRow().toString());
114 assertTrue(ds.next());
115 assertEquals("Row[values=[" + firstRowId + ", " + firstRowTitle + "]]", ds.getRow().toString());
116 assertFalse(ds.next());
117 } finally {
118 ds.close();
119 }
120 }
121
122 @Test
123 public void testWhereColumnEqualsValues() throws Exception {
124 DataSet ds = dc.query().from(testTableName).select("id").and("title").where("title").isEquals(firstRowTitle)
125 .execute();
126
127 assertEquals(FilteredDataSet.class, ds.getClass());
128 try {
129 assertTrue(ds.next());
130 assertEquals("Row[values=[" + firstRowId + ", " + firstRowTitle + "]]", ds.getRow().toString());
131 assertFalse(ds.next());
132 } finally {
133 ds.close();
134 }
135 }
136
137 @Test
138 public void testPrimaryKeyLookup() throws Exception {
139 DataSet ds =
140 dc.query().from(testTableName).select("id").and("title").where("id").isEquals(firstRowId).execute();
141
142 assertEquals(InMemoryDataSet.class, ds.getClass());
143 try {
144 assertTrue(ds.next());
145 assertEquals("Row[values=[" + firstRowId + ", " + firstRowTitle + "]]", ds.getRow().toString());
146 assertFalse(ds.next());
147 } finally {
148 ds.close();
149 }
150 }
151
152 @Test
153 public void testWhereColumnInValues() throws Exception {
154 DataSet ds = dc.query().from(testTableName).select("id").and("title").where("title")
155 .in(firstRowTitle, secondRowTitle).orderBy("id").execute();
156 try {
157 assertTrue(ds.next());
158 assertEquals("Row[values=[" + firstRowId + ", " + firstRowTitle + "]]", ds.getRow().toString());
159 assertTrue(ds.next());
160 assertEquals("Row[values=[" + secondRowId + ", " + secondRowTitle + "]]", ds.getRow().toString());
161 assertFalse(ds.next());
162 } finally {
163 ds.close();
164 }
165 }
166
167 @Test
168 public void testMaxRows() throws Exception {
169 Table table = dc.getDefaultSchema().getTableByName(testTableName);
170 Query query = new Query().from(table).select(table.getColumns()).setMaxRows(2);
171 DataSet dataSet = dc.executeQuery(query);
172 TableModel tableModel = new DataSetTableModel(dataSet);
173
174 assertEquals(2, tableModel.getRowCount());
175 }
176
177 @Test
178 public void testCountQuery() throws Exception {
179 Table table = dc.getDefaultSchema().getTableByName(testTableName);
180 Query q = new Query().selectCount().from(table);
181 List<Object[]> data = dc.executeQuery(q).toObjectArrays();
182 assertEquals(1, data.size());
183 Object[] row = data.get(0);
184
185 assertEquals(1, row.length);
186 assertEquals("[3]", Arrays.toString(row));
187 }
188
189 @Test
190 public void testQueryForANonExistingTable() throws Exception {
191 boolean thrown = false;
192 try {
193 dc.query().from("nonExistingTable").select("user").and("message").execute();
194 } catch (IllegalArgumentException IAex) {
195 thrown = true;
196 }
197 assertTrue(thrown);
198 }
199
200 @Test
201 public void testQueryForAnExistingTableAndNonExistingField() throws Exception {
202 boolean thrown = false;
203 try {
204 dc.query().from(testTableName).select("nonExistingField").execute();
205 } catch (QueryParserException ex) {
206 thrown = true;
207 }
208 assertTrue(thrown);
209 }
210
211 @Test
212 public void testNonExistentKeystore() {
213 try {
214 new CassandraDataContext(cluster, "nonExistentKeyspace");
215 fail();
216 } catch (IllegalArgumentException e) {
217 assertNotNull(e);
218 }
219 }
220
221 @Test
222 public void testCounterDataType() throws Exception {
223 Table table = dc.getDefaultSchema().getTableByName(testCounterTableName);
224
225 assertEquals(ColumnType.BIGINT, table.getColumnByName("counter_value").getType());
226 assertEquals(ColumnType.STRING, table.getColumnByName("url_name").getType());
227
228 DataSet ds = dc.query().from(testCounterTableName).select("counter_value").and("url_name").execute();
229 assertEquals(CassandraDataSet.class, ds.getClass());
230
231 try {
232 assertTrue(ds.next());
233 assertEquals("Row[values=[" + 1 + ", " + urlName + "]]", ds.getRow().toString());
234 assertFalse(ds.next());
235 } finally {
236 ds.close();
237 }
238 }
239
240 private static void createCassandraKeySpaceAndTable(Session session) {
241 session.execute("CREATE KEYSPACE IF NOT EXISTS " + keyspaceName + " WITH replication "
242 + "= {'class':'SimpleStrategy', 'replication_factor':1};");
243 session.execute("DROP TABLE IF EXISTS " + keyspaceName + "." + testTableName + ";");
244 session.execute("CREATE TABLE IF NOT EXISTS " + keyspaceName + "." + testTableName + " ("
245 + "id uuid PRIMARY KEY," + "title text," + "hit boolean," + "duration float," + "position int,"
246 + "creationtime timestamp" + ");");
247 session.execute("DROP TABLE IF EXISTS " + keyspaceName + "." + testCounterTableName + ";");
248 session.execute("CREATE TABLE IF NOT EXISTS " + keyspaceName + "." + testCounterTableName + " ("
249 + "counter_value counter, url_name varchar, PRIMARY KEY (url_name)" + ");");
250 }
251
252 private static void populateCassandraTableWithSomeData(Session session) {
253 session.execute("INSERT INTO " + keyspaceName + "." + testTableName
254 + " (id, title, hit, duration, position, creationtime) " + "VALUES (" + firstRowId + ","
255 + "'My first song'," + "false," + "2.15," + "1," + "dateof(now()))" + ";");
256 session.execute("INSERT INTO " + keyspaceName + "." + testTableName
257 + " (id, title, hit, duration, position, creationtime) " + "VALUES (" + secondRowId + ","
258 + "'My second song'," + "true," + "2.55," + "2," + "dateof(now()))" + ";");
259 session.execute("INSERT INTO " + keyspaceName + "." + testTableName
260 + " (id, title, hit, duration, position, creationtime) " + "VALUES (" + thirdRowId + ","
261 + "'My third song'," + "false," + "3.15," + "3," + "dateof(now()))" + ";");
262 }
263
264 private static void populateCassandraCounterTableWithSomeData(Session session) {
265 session.execute("UPDATE " + keyspaceName + "." + testCounterTableName
266 + " SET counter_value = counter_value + 1 WHERE url_name='" + urlName + "';");
267 }
268 }