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