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