Elffuss BraidHub

kikocis/java-driver public

0d06ef6c86a5 · 8.9 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;
19
20import com.google.common.collect.Lists;
21import org.testng.annotations.BeforeMethod;
22import org.testng.annotations.Test;
23
24import static org.assertj.core.api.Assertions.fail;
25
26import com.datastax.driver.core.*;
27import com.datastax.driver.core.exceptions.InvalidQueryException;
28import com.datastax.driver.core.utils.CassandraVersion;
29import com.datastax.driver.mapping.annotations.*;
30
31import static com.datastax.driver.core.Assertions.assertThat;
32
33/**
34 * Tests to ensure validity of {@link com.datastax.driver.mapping.annotations.Computed}
35 * annotation to map computed fields.
36 */
37@SuppressWarnings("unused")
38@CassandraVersion(major = 2.0)
39public class MapperComputedFieldsTest extends CCMBridge.PerClassSingleNodeCluster {
40
41 @Override
42 protected Collection<String> getTableDefinitions() {
43 return Lists.newArrayList(
44 "CREATE TABLE user (login text primary key, name text)",
45 "INSERT INTO user (login, name) VALUES ('testlogin', 'test name')");
46 }
47
48 MappingManager mappingManager;
49 Mapper<User> userMapper;
50
51 @BeforeMethod(groups = "short")
52 void setup() {
53 mappingManager = new MappingManager(session);
54 userMapper = mappingManager.mapper(User.class);
55 }
56
57 @Test(groups = "short")
58 void should_save_and_get_entity_with_computed_fields() {
59 long writeTime = System.currentTimeMillis() * 1000;
60 User newUser = new User("testlogin2", "blah");
61 newUser.setWriteTime(1); // will be ignored
62
63 userMapper.save(newUser);
64
65 User fetched = userMapper.get("testlogin2");
66 assertThat(fetched.getLogin()).isEqualTo("testlogin2");
67 assertThat(fetched.getName()).isEqualTo("blah");
68 // write time should be within 30 seconds.
69 assertThat(fetched.getWriteTime()).isGreaterThanOrEqualTo(writeTime).isLessThan(writeTime + 30000000L);
70 assertThat(fetched.getTtl()).isNull(); // TTL should be null since it was not set.
71
72 // Overwrite with TTL
73 session.execute("insert into user (login, name) values ('testlogin2', 'blah') using TTL 600");
74 fetched = userMapper.get("testlogin2");
75 assertThat(fetched.getWriteTime()).isGreaterThanOrEqualTo(writeTime).isLessThan(writeTime + 30000000L);
76 assertThat(fetched.getTtl()).isBetween(570, 600); // TTL should be within 30 secs.
77
78 // cleanup
79 userMapper.delete(newUser);
80 }
81
82 @Test(groups = "short")
83 void should_add_aliases_for_fields_in_select_queries() {
84 BoundStatement bs = (BoundStatement)userMapper.getQuery("test");
85 assertThat(bs.preparedStatement().getQueryString())
86 .contains("SELECT", "\"login\" AS col1", "\"name\" AS col2", "writetime(\"name\") AS col3");
87 }
88
89 @Test(groups = "short")
90 public void should_map_aliased_resultset_to_objects() {
91 Statement getQuery = userMapper.getQuery("testlogin");
92 getQuery.setConsistencyLevel(ConsistencyLevel.QUORUM);
93 ResultSet rs = session.execute(getQuery);
94
95 Result<User> result = userMapper.mapAliased(rs);
96 User user = result.one();
97
98 assertThat(user.getLogin()).isEqualTo("testlogin");
99 }
100
101 @Test(groups = "short")
102 void should_map_unaliased_resultset_to_objects() {
103 UserAccessor userAccessor = mappingManager.createAccessor(UserAccessor.class);
104 ResultSet rs = userAccessor.all();
105
106 Result<User> result = userMapper.map(rs);
107 User user = result.one();
108 assertThat(user.getLogin()).isEqualTo("testlogin");
109 assertThat(user.getWriteTime()).isEqualTo(0);
110 }
111
112 @Test(groups = "short", expectedExceptions = IllegalArgumentException.class)
113 void should_fail_if_computed_field_is_not_right_type() {
114 mappingManager.mapper(User_WrongComputedType.class);
115 }
116
117 @Test(groups = "short")
118 void should_fail_if_computed_field_marked_with_column_annotation() {
119 Mapper<User_WrongAnnotationForComputed> mapper = mappingManager.mapper(User_WrongAnnotationForComputed.class);
120 try {
121 mapper.save(new User_WrongAnnotationForComputed("test", "foo"));
122 fail("Expected an InvalidQueryException");
123 } catch (InvalidQueryException e) {/*expected*/}
124
125 try {
126 User_WrongAnnotationForComputed saved = mapper.get(42);
127 fail("Expected an InvalidQueryException");
128 } catch (InvalidQueryException e) {/*expected*/}
129 }
130
131 @Table(name = "user")
132 public static class User {
133 @PartitionKey
134 private String login;
135 private String name;
136
137 public User() {
138 }
139
140 public User(String login, String name) {
141 this.login = login;
142 this.name = name;
143 }
144
145 // quotes in the column name inserted on purpose
146 // to test the alias generation mechanism
147 @Computed(formula = "writetime(\"name\")")
148 long writeTime;
149
150 @Computed(formula = "ttl(name)")
151 Integer ttl;
152
153 public String getLogin() {
154 return login;
155 }
156
157 public void setLogin(String login) {
158 this.login = login;
159 }
160
161 public String getName() {
162 return name;
163 }
164
165 public void setName(String name) {
166 this.name = name;
167 }
168
169 public long getWriteTime() {
170 return writeTime;
171 }
172
173 public void setWriteTime(long writeTime) {
174 this.writeTime = writeTime;
175 }
176
177 public Integer getTtl() {
178 return ttl;
179 }
180
181 public void setTtl(Integer ttl) {
182 this.ttl = ttl;
183 }
184 }
185
186 @Accessor
187 interface UserAccessor {
188 @Query("select * from user")
189 ResultSet all();
190 }
191
192 @Table(name = "key_value")
193 public static class KeyValue {
194 @PartitionKey
195 private int key;
196 private String v;
197
198 @Computed(formula = "writetime(\"v\")")
199 long writeTime;
200
201 @Computed(formula = "ttl(v)")
202 Integer ttl;
203
204 public KeyValue() {
205 }
206
207 public KeyValue(int k, String val) {
208 this.key = k;
209 this.v = val;
210 }
211
212 public int getKey() {
213 return this.key;
214 }
215
216 public void setKey(int pk) {
217 this.key = pk;
218 }
219
220 public String getV() {
221 return this.v;
222 }
223
224 public void setV(String val) {
225 this.v = val;
226 }
227
228 public long getWriteTime() {
229 return this.writeTime;
230 }
231
232 public void setWriteTime(long pk) {
233 this.writeTime = pk;
234 }
235
236 public Integer getTtl() {
237 return this.ttl;
238 }
239
240 public void setTtl(Integer ttl) {
241 this.ttl = ttl;
242 }
243 }
244
245 @Table(name = "user")
246 public static class User_WrongComputedType {
247 @PartitionKey
248 private String login;
249 private String name;
250
251 @Column(name = "writetime(v)")
252 byte writeTime;
253
254 public String getLogin() {
255 return login;
256 }
257
258 public void setLogin(String login) {
259 this.login = login;
260 }
261
262 public String getName() {
263 return name;
264 }
265
266 public void setName(String name) {
267 this.name = name;
268 }
269
270 public byte getWriteTime() {
271 return writeTime;
272 }
273
274 public void setWriteTime(byte writeTime) {
275 this.writeTime = writeTime;
276 }
277 }
278
279 @Table(name = "user")
280 public static class User_WrongAnnotationForComputed {
281 @PartitionKey
282 private String login;
283 private String name;
284
285 @Column(name = "writetime(v)")
286 long writeTime;
287
288 public User_WrongAnnotationForComputed() {
289 }
290
291 public User_WrongAnnotationForComputed(String login, String name) {
292 this.login = login;
293 this.name = name;
294 }
295
296 public String getLogin() {
297 return login;
298 }
299
300 public void setLogin(String login) {
301 this.login = login;
302 }
303
304 public String getName() {
305 return name;
306 }
307
308 public void setName(String name) {
309 this.name = name;
310 }
311
312 public long getWriteTime() {
313 return writeTime;
314 }
315
316 public void setWriteTime(long writeTime) {
317 this.writeTime = writeTime;
318 }
319 }
320}