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