Elffuss BraidHub

kikocis/java-driver public

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