Elffuss BraidHub

kikocis/java-driver public

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