Elffuss BraidHub

kikocis/java-driver public

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