Elffuss BraidHub

kikocis/java-driver public

22494c9e09ae · 7.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 = "user")
193 public static class User_WrongComputedType {
194 @PartitionKey
195 private String login;
196 private String name;
197
198 @Column(name = "writetime(v)")
199 byte writeTime;
200
201 public String getLogin() {
202 return login;
203 }
204
205 public void setLogin(String login) {
206 this.login = login;
207 }
208
209 public String getName() {
210 return name;
211 }
212
213 public void setName(String name) {
214 this.name = name;
215 }
216
217 public byte getWriteTime() {
218 return writeTime;
219 }
220
221 public void setWriteTime(byte writeTime) {
222 this.writeTime = writeTime;
223 }
224 }
225
226 @Table(name = "user")
227 public static class User_WrongAnnotationForComputed {
228 @PartitionKey
229 private String login;
230 private String name;
231
232 @Column(name = "writetime(v)")
233 long writeTime;
234
235 public User_WrongAnnotationForComputed() {
236 }
237
238 public User_WrongAnnotationForComputed(String login, String name) {
239 this.login = login;
240 this.name = name;
241 }
242
243 public String getLogin() {
244 return login;
245 }
246
247 public void setLogin(String login) {
248 this.login = login;
249 }
250
251 public String getName() {
252 return name;
253 }
254
255 public void setName(String name) {
256 this.name = name;
257 }
258
259 public long getWriteTime() {
260 return writeTime;
261 }
262
263 public void setWriteTime(long writeTime) {
264 this.writeTime = writeTime;
265 }
266 }
267}