Elffuss BraidHub

kikocis/java-driver public

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