Elffuss BraidHub

kikocis/java-driver public

22494c9e09ae · 13.7 KiBRawPermalink
1/*
2 * Copyright (C) 2012-2015 DataStax Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.datastax.driver.mapping;
17
18import java.util.Collection;
19import java.util.concurrent.ExecutionException;
20
21import com.google.common.collect.Lists;
22import org.testng.SkipException;
23import org.testng.annotations.BeforeMethod;
24import org.testng.annotations.Test;
25
26import static org.assertj.core.api.Assertions.assertThat;
27import static org.assertj.core.api.Assertions.fail;
28
29import com.datastax.driver.core.*;
30import com.datastax.driver.core.exceptions.NoHostAvailableException;
31import com.datastax.driver.core.exceptions.UnavailableException;
32import com.datastax.driver.core.utils.CassandraVersion;
33import com.datastax.driver.mapping.annotations.PartitionKey;
34import com.datastax.driver.mapping.annotations.Table;
35
36import static com.datastax.driver.core.ConsistencyLevel.TWO;
37import static com.datastax.driver.core.ProtocolVersion.V1;
38import static com.datastax.driver.mapping.Mapper.Option;
39
40public class MapperOptionTest extends CCMBridge.PerClassSingleNodeCluster {
41
42 ProtocolVersion protocolVersion;
43 Mapper<User> mapper;
44
45 @Override
46 protected Collection<String> getTableDefinitions() {
47 return Lists.newArrayList("CREATE TABLE user (key int primary key, v text)");
48 }
49
50 @BeforeMethod(groups = "short")
51 public void setup() {
52 mapper = new MappingManager(session).mapper(User.class);
53 protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersionEnum();
54 }
55
56 @Test(groups = "short")
57 @CassandraVersion(major = 2.0)
58 void should_use_save_options() {
59 Long tsValue = futureTimestamp();
60 mapper.saveAsync(new User(42, "helloworld"), Option.timestamp(tsValue), Option.tracing(true));
61 assertThat(mapper.get(42).getV()).isEqualTo("helloworld");
62 Long tsReturned = session.execute("SELECT writetime(v) FROM user WHERE key=" + 42).one().getLong(0);
63 assertThat(tsReturned).isEqualTo(tsValue);
64 }
65
66 @Test(groups = "short")
67 @CassandraVersion(major = 2.0)
68 void should_use_delete_options() {
69 User todelete = new User(45, "todelete");
70 mapper.save(todelete);
71 Option opt = Option.timestamp(35);
72 BoundStatement bs = (BoundStatement)mapper.deleteQuery(45, opt, Option.consistencyLevel(ConsistencyLevel.ALL));
73 assertThat(bs.preparedStatement().getQueryString()).contains("USING TIMESTAMP");
74 }
75
76 @Test(groups = "short", expectedExceptions = {IllegalArgumentException.class})
77 @CassandraVersion(major = 2.0)
78 void should_use_get_options() {
79 User todelete = new User(45, "toget");
80 mapper.save(todelete);
81
82 Option opt = Option.tracing(true);
83 BoundStatement bs = (BoundStatement)mapper.getQuery(45, opt);
84 assertThat(bs.isTracing()).isTrue();
85
86 User us = mapper.mapAliased(session.execute(bs)).one();
87 assertThat(us.getV()).isEqualTo("toget");
88
89 mapper.getQuery(45, Option.timestamp(1337));
90 }
91
92 @Test(groups = "short")
93 @CassandraVersion(major = 2.0)
94 void should_use_options_only_once() {
95 Long tsValue = futureTimestamp();
96 mapper.save(new User(43, "helloworld"), Option.timestamp(tsValue));
97 mapper.save(new User(44, "test"));
98 Long tsReturned = session.execute("SELECT writetime(v) FROM user WHERE key=" + 44).one().getLong(0);
99 // Assuming we cannot go back in time (yet) and execute the write at ts=1
100 assertThat(tsReturned).isNotEqualTo(tsValue);
101 }
102
103 @Test(groups = "short")
104 @CassandraVersion(major = 2.0)
105 void should_use_default_options() {
106 mapper.setDefaultSaveOptions(Option.timestamp(644746L), Option.ttl(76324));
107 BoundStatement bs = (BoundStatement)mapper.saveQuery(new User(46, "rjhrgce"));
108 assertThat(bs.preparedStatement().getQueryString()).contains("TIMESTAMP").contains("TTL");
109
110 mapper.resetDefaultSaveOptions();
111 bs = (BoundStatement)mapper.saveQuery(new User(47, "rjhrgce"));
112 assertThat(bs.preparedStatement().getQueryString()).doesNotContain("TIMESTAMP").doesNotContain("TTL");
113
114 mapper.setDefaultDeleteOptions(Option.timestamp(3245L), Option.tracing(true));
115 bs = (BoundStatement)mapper.deleteQuery(47);
116 assertThat(bs.preparedStatement().getQueryString()).contains("TIMESTAMP");
117 assertThat(bs.isTracing()).isTrue();
118
119 mapper.resetDefaultDeleteOptions();
120 bs = (BoundStatement)mapper.deleteQuery(47);
121 assertThat(bs.preparedStatement().getQueryString()).doesNotContain("TIMESTAMP");
122 assertThat(bs.isTracing()).isFalse();
123
124 bs = (BoundStatement)mapper.saveQuery(new User(46, "rjhrgce"), Option.timestamp(23), Option.consistencyLevel(ConsistencyLevel.ANY));
125 assertThat(bs.getConsistencyLevel()).isEqualTo(ConsistencyLevel.ANY);
126
127 mapper.setDefaultGetOptions(Option.tracing(true));
128 bs = (BoundStatement)mapper.getQuery(46);
129 assertThat(bs.isTracing()).isTrue();
130
131 mapper.resetDefaultGetOptions();
132 bs = (BoundStatement)mapper.getQuery(46);
133 assertThat(bs.isTracing()).isFalse();
134 }
135
136 @Test(groups = "short")
137 @CassandraVersion(major = 2.0)
138 void should_use_explicit_options_over_default_options() {
139 long defaultTimestamp = futureTimestamp();
140 long explicitTimestamp = futureTimestamp();
141
142 mapper.setDefaultSaveOptions(Option.timestamp(defaultTimestamp));
143
144 mapper.save(new User(42, "helloworld"), Option.timestamp(explicitTimestamp));
145 Long savedTimestamp = session.execute("SELECT writetime(v) FROM user WHERE key=" + 42).one().getLong(0);
146 assertThat(savedTimestamp).isEqualTo(explicitTimestamp);
147 }
148
149 /** Cover all versions of save() to check that methods that call each other properly propagate the options */
150 @Test(groups = "short")
151 @CassandraVersion(major = 2.0)
152 void should_use_save_options_for_all_variants() throws ExecutionException, InterruptedException {
153 Long timestamp = futureTimestamp();
154 User user = new User(42, "helloworld");
155
156 mapper.save(user, Option.timestamp(timestamp));
157 assertThat(getWriteTime(user.getKey())).isEqualTo(timestamp);
158 mapper.delete(user.getKey());
159
160 mapper.saveAsync(user, Option.timestamp(timestamp)).get();
161 assertThat(getWriteTime(user.getKey())).isEqualTo(timestamp);
162 mapper.delete(user.getKey());
163
164 session.execute(mapper.saveQuery(user, Option.timestamp(timestamp)));
165 assertThat(getWriteTime(user.getKey())).isEqualTo(timestamp);
166 mapper.delete(user.getKey());
167
168 // Set as default and try optionless methods
169 mapper.setDefaultSaveOptions(Option.timestamp(timestamp));
170
171 mapper.save(user);
172 assertThat(getWriteTime(user.getKey())).isEqualTo(timestamp);
173 mapper.delete(user.getKey());
174
175 mapper.saveAsync(user).get();
176 assertThat(getWriteTime(user.getKey())).isEqualTo(timestamp);
177 mapper.delete(user.getKey());
178
179 session.execute(mapper.saveQuery(user));
180 assertThat(getWriteTime(user.getKey())).isEqualTo(timestamp);
181 mapper.delete(user.getKey());
182 }
183
184 private Long getWriteTime(int key) {
185 Row row = session.execute("SELECT writetime(v) FROM user WHERE key=" + key).one();
186 return row.getLong(0);
187 }
188
189 @Test(groups = "short")
190 @CassandraVersion(major = 2.0)
191 void should_use_get_options_for_all_variants() throws InterruptedException {
192 try {
193 mapper.get(42, Option.consistencyLevel(TWO));
194 fail("Expected a NoHostAvailableException");
195 } catch (NoHostAvailableException e) {
196 assertFirstNodeUnavailable(e);
197 }
198
199 try {
200 mapper.getAsync(42, Option.consistencyLevel(TWO)).get();
201 fail("Expected an ExecutionException");
202 } catch (ExecutionException e) {
203 assertFirstNodeUnavailable(e);
204 }
205
206 Statement statement = mapper.getQuery(42, Option.consistencyLevel(TWO));
207 assertThat(statement.getConsistencyLevel()).isEqualTo(TWO);
208
209 // Set as default and try optionless methods
210 mapper.setDefaultGetOptions(Option.consistencyLevel(TWO));
211
212 try {
213 mapper.get(42);
214 fail("Expected a NoHostAvailableException");
215 } catch (NoHostAvailableException e) {
216 assertFirstNodeUnavailable(e);
217 }
218
219 try {
220 mapper.getAsync(42).get();
221 fail("Expected an ExecutionException");
222 } catch (ExecutionException e) {
223 assertFirstNodeUnavailable(e);
224 }
225
226 statement = mapper.getQuery(42);
227 assertThat(statement.getConsistencyLevel()).isEqualTo(TWO);
228 }
229
230 @Test(groups = "short")
231 @CassandraVersion(major = 2.0)
232 void should_use_delete_options_for_all_variants() throws InterruptedException {
233 User user = new User(42, "helloworld");
234
235 try {
236 mapper.delete(user, Option.consistencyLevel(TWO));
237 fail("Expected a NoHostAvailableException");
238 } catch (NoHostAvailableException e) {
239 assertFirstNodeUnavailable(e);
240 }
241
242 try {
243 mapper.deleteAsync(user, Option.consistencyLevel(TWO)).get();
244 fail("Expected an ExecutionException");
245 } catch (ExecutionException e) {
246 assertFirstNodeUnavailable(e);
247 }
248
249 Statement statement = mapper.deleteQuery(user, Option.consistencyLevel(TWO));
250 assertThat(statement.getConsistencyLevel()).isEqualTo(TWO);
251
252 try {
253 mapper.delete(user.getKey(), Option.consistencyLevel(TWO));
254 fail("Expected a NoHostAvailableException");
255 } catch (NoHostAvailableException e) {
256 assertFirstNodeUnavailable(e);
257 }
258
259 try {
260 mapper.deleteAsync(user.getKey(), Option.consistencyLevel(TWO)).get();
261 fail("Expected an ExecutionException");
262 } catch (ExecutionException e) {
263 assertFirstNodeUnavailable(e);
264 }
265
266 statement = mapper.deleteQuery(user.getKey(), Option.consistencyLevel(TWO));
267 assertThat(statement.getConsistencyLevel()).isEqualTo(TWO);
268
269 // Set as default and try optionless methods
270 mapper.setDefaultDeleteOptions(Option.consistencyLevel(TWO));
271
272 try {
273 mapper.delete(user);
274 fail("Expected a NoHostAvailableException");
275 } catch (NoHostAvailableException e) {
276 assertFirstNodeUnavailable(e);
277 }
278
279 try {
280 mapper.deleteAsync(user).get();
281 fail("Expected an ExecutionException");
282 } catch (ExecutionException e) {
283 assertFirstNodeUnavailable(e);
284 }
285
286 statement = mapper.deleteQuery(user);
287 assertThat(statement.getConsistencyLevel()).isEqualTo(TWO);
288
289 try {
290 mapper.delete(user.getKey());
291 fail("Expected a NoHostAvailableException");
292 } catch (NoHostAvailableException e) {
293 assertFirstNodeUnavailable(e);
294 }
295
296 try {
297 mapper.deleteAsync(user.getKey()).get();
298 fail("Expected an ExecutionException");
299 } catch (ExecutionException e) {
300 assertFirstNodeUnavailable(e);
301 }
302
303 statement = mapper.deleteQuery(user.getKey());
304 assertThat(statement.getConsistencyLevel()).isEqualTo(TWO);
305 }
306
307 @Test(groups="short", expectedExceptions = IllegalArgumentException.class)
308 void should_fail_if_option_does_not_apply_to_query() {
309 mapper.get(42, Option.ttl(1));
310 }
311
312 @Test(groups="short", expectedExceptions = IllegalArgumentException.class)
313 void should_fail_when_using_ttl_with_protocol_v1() {
314 if (protocolVersion.compareTo(V1) > 0)
315 throw new SkipException("Skipped when protocol version > V1");
316
317 mapper.saveQuery(new User(42, "helloworld"), Option.ttl(15));
318 }
319
320 @Test(groups="short", expectedExceptions = IllegalArgumentException.class)
321 void should_fail_when_using_timestamp_with_protocol_v1() {
322 if (protocolVersion.compareTo(V1) > 0)
323 throw new SkipException("Skipped when protocol version > V1");
324
325 mapper.saveQuery(new User(42, "helloworld"), Option.timestamp(15));
326 }
327
328 private static void assertFirstNodeUnavailable(NoHostAvailableException e) {
329 Throwable node1Error = e.getErrors().values().iterator().next();
330 assertThat(node1Error).isInstanceOf(UnavailableException.class);
331 }
332
333 private static void assertFirstNodeUnavailable(ExecutionException e) {
334 Throwable cause = e.getCause();
335 assertThat(cause).isInstanceOf(NoHostAvailableException.class);
336 assertFirstNodeUnavailable((NoHostAvailableException)cause);
337 }
338
339 private static long futureTimestamp() {
340 return (System.currentTimeMillis() + 1000) * 1000;
341 }
342
343 @Table(name = "user", writeConsistency = "ONE")
344 public static class User {
345 @PartitionKey
346 private int key;
347 private String v;
348
349 public User() {
350 }
351
352 public User(int k, String val) {
353 this.key = k;
354 this.v = val;
355 }
356
357 public int getKey() {
358 return this.key;
359 }
360
361 public void setKey(int pk) {
362 this.key = pk;
363 }
364
365 public String getV() {
366 return this.v;
367 }
368
369 public void setV(String val) {
370 this.v = val;
371 }
372 }
373}