| 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.exceptions.InvalidQueryException; |
| 21 | import com.datastax.driver.core.utils.CassandraVersion; |
| 22 | import com.datastax.driver.core.utils.UUIDs; |
| 23 | import com.datastax.driver.mapping.annotations.*; |
| 24 | import com.google.common.base.Objects; |
| 25 | import com.google.common.collect.Maps; |
| 26 | import org.assertj.core.data.MapEntry; |
| 27 | import org.testng.annotations.AfterMethod; |
| 28 | import org.testng.annotations.BeforeMethod; |
| 29 | import org.testng.annotations.Test; |
| 30 | |
| 31 | import java.util.*; |
| 32 | |
| 33 | import static com.datastax.driver.core.CreateCCM.TestMode.PER_METHOD; |
| 34 | import static org.assertj.core.api.Assertions.assertThat; |
| 35 | import static org.assertj.core.api.Assertions.fail; |
| 36 | import static org.testng.Assert.*; |
| 37 | |
| 38 | @SuppressWarnings({"unused", "WeakerAccess"}) |
| 39 | @CassandraVersion(major = 2.1) |
| 40 | @CreateCCM(PER_METHOD) |
| 41 | public class MapperUDTTest extends CCMTestsSupport { |
| 42 | |
| 43 | @BeforeMethod(groups = "short") |
| 44 | public void createObjects() { |
| 45 | execute("CREATE TYPE address (street text, city text, \"ZIP_code\" int, phones set<text>)", |
| 46 | "CREATE TABLE users (user_id uuid PRIMARY KEY, name text, mainaddress frozen<address>, other_addresses map<text,frozen<address>>)"); |
| 47 | } |
| 48 | |
| 49 | @AfterMethod(groups = "short") |
| 50 | public void deleteObjects() { |
| 51 | execute("DROP TABLE IF EXISTS users", |
| 52 | "DROP TYPE IF EXISTS address"); |
| 53 | } |
| 54 | |
| 55 | @Table(name = "users", |
| 56 | readConsistency = "QUORUM", |
| 57 | writeConsistency = "QUORUM") |
| 58 | public static class User { |
| 59 | @PartitionKey |
| 60 | @Column(name = "user_id") |
| 61 | private UUID userId; |
| 62 | |
| 63 | private String name; |
| 64 | |
| 65 | @Frozen |
| 66 | private Address mainAddress; |
| 67 | |
| 68 | @Column(name = "other_addresses") |
| 69 | @FrozenValue |
| 70 | private Map<String, Address> otherAddresses = Maps.newHashMap(); |
| 71 | |
| 72 | public User() { |
| 73 | } |
| 74 | |
| 75 | public User(String name, Address address) { |
| 76 | this.userId = UUIDs.random(); |
| 77 | this.name = name; |
| 78 | this.mainAddress = address; |
| 79 | this.otherAddresses = new HashMap<String, Address>(); |
| 80 | } |
| 81 | |
| 82 | public UUID getUserId() { |
| 83 | return userId; |
| 84 | } |
| 85 | |
| 86 | public void setUserId(UUID userId) { |
| 87 | this.userId = userId; |
| 88 | } |
| 89 | |
| 90 | public String getName() { |
| 91 | return name; |
| 92 | } |
| 93 | |
| 94 | public void setName(String name) { |
| 95 | this.name = name; |
| 96 | } |
| 97 | |
| 98 | public Address getMainAddress() { |
| 99 | return mainAddress; |
| 100 | } |
| 101 | |
| 102 | public void setMainAddress(Address address) { |
| 103 | this.mainAddress = address; |
| 104 | } |
| 105 | |
| 106 | public Map<String, Address> getOtherAddresses() { |
| 107 | return otherAddresses; |
| 108 | } |
| 109 | |
| 110 | public void setOtherAddresses(Map<String, Address> otherAddresses) { |
| 111 | this.otherAddresses = otherAddresses; |
| 112 | } |
| 113 | |
| 114 | public void addOtherAddress(String name, Address address) { |
| 115 | this.otherAddresses.put(name, address); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public boolean equals(Object other) { |
| 120 | if (this == other) |
| 121 | return true; |
| 122 | if (other instanceof User) { |
| 123 | User that = (User) other; |
| 124 | return Objects.equal(this.userId, that.userId) && |
| 125 | Objects.equal(this.name, that.name) && |
| 126 | Objects.equal(this.mainAddress, that.mainAddress) && |
| 127 | Objects.equal(this.otherAddresses, that.otherAddresses); |
| 128 | } |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public int hashCode() { |
| 134 | return Objects.hashCode(this.userId, this.name, this.mainAddress, this.otherAddresses); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public String toString() { |
| 139 | return Objects.toStringHelper(User.class) |
| 140 | .add("userId", userId) |
| 141 | .add("name", name) |
| 142 | .add("mainAddress", mainAddress) |
| 143 | .add("otherAddresses", otherAddresses) |
| 144 | .toString(); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | @UDT(name = "address") |
| 149 | public static class Address { |
| 150 | |
| 151 | // Dummy constant to test that static fields are properly ignored |
| 152 | public static final int FOO = 1; |
| 153 | |
| 154 | @Field // not strictly required, but we want to check that the annotation works without a name |
| 155 | private String city; |
| 156 | |
| 157 | // Declared out of order compared to the UDT definition, to make sure that we serialize fields in the correct order (JAVA-884) |
| 158 | private String street; |
| 159 | |
| 160 | @Field(name = "ZIP_code", caseSensitive = true) |
| 161 | private int zipCode; |
| 162 | |
| 163 | private Set<String> phones; |
| 164 | |
| 165 | public Address() { |
| 166 | } |
| 167 | |
| 168 | public Address(String street, String city, int zipCode, String... phones) { |
| 169 | this.street = street; |
| 170 | this.city = city; |
| 171 | this.zipCode = zipCode; |
| 172 | this.phones = new HashSet<String>(); |
| 173 | Collections.addAll(this.phones, phones); |
| 174 | } |
| 175 | |
| 176 | public String getStreet() { |
| 177 | return street; |
| 178 | } |
| 179 | |
| 180 | public void setStreet(String street) { |
| 181 | this.street = street; |
| 182 | } |
| 183 | |
| 184 | public String getCity() { |
| 185 | return city; |
| 186 | } |
| 187 | |
| 188 | public void setCity(String city) { |
| 189 | this.city = city; |
| 190 | } |
| 191 | |
| 192 | public int getZipCode() { |
| 193 | return zipCode; |
| 194 | } |
| 195 | |
| 196 | public void setZipCode(int zipCode) { |
| 197 | this.zipCode = zipCode; |
| 198 | } |
| 199 | |
| 200 | public Set<String> getPhones() { |
| 201 | return phones; |
| 202 | } |
| 203 | |
| 204 | public void setPhones(Set<String> phones) { |
| 205 | this.phones = phones; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public boolean equals(Object other) { |
| 210 | if (this == other) |
| 211 | return true; |
| 212 | if (other instanceof Address) { |
| 213 | Address that = (Address) other; |
| 214 | return Objects.equal(this.street, that.street) && |
| 215 | Objects.equal(this.city, that.city) && |
| 216 | Objects.equal(this.zipCode, that.zipCode) && |
| 217 | Objects.equal(this.phones, that.phones); |
| 218 | } |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public int hashCode() { |
| 224 | return Objects.hashCode(this.street, this.city, this.zipCode, this.phones); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public String toString() { |
| 229 | return Objects.toStringHelper(Address.class) |
| 230 | .add("street", street) |
| 231 | .add("city", city) |
| 232 | .add("zip", zipCode) |
| 233 | .add("phones", phones) |
| 234 | .toString(); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | @Accessor |
| 239 | public interface UserAccessor { |
| 240 | @Query("SELECT * FROM users WHERE user_id=:userId") |
| 241 | User getOne(@Param("userId") UUID userId); |
| 242 | |
| 243 | @Query("UPDATE users SET other_addresses[:name]=:address WHERE user_id=:id") |
| 244 | ResultSet addAddress(@Param("id") UUID id, @Param("name") String addressName, @Param("address") Address address); |
| 245 | |
| 246 | @Query("UPDATE users SET other_addresses=:addresses where user_id=:id") |
| 247 | ResultSet setOtherAddresses(@Param("id") UUID id, @Param("addresses") Map<String, Address> addresses); |
| 248 | |
| 249 | @Query("SELECT * FROM users") |
| 250 | Result<User> getAll(); |
| 251 | } |
| 252 | |
| 253 | @Test(groups = "short") |
| 254 | public void testSimpleEntity() throws Exception { |
| 255 | Mapper<User> m = new MappingManager(session()).mapper(User.class); |
| 256 | |
| 257 | User u1 = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245")); |
| 258 | u1.addOtherAddress("work", new Address("5 Main Street", "Springfield", 12345, "23431342")); |
| 259 | m.save(u1); |
| 260 | |
| 261 | assertEquals(m.get(u1.getUserId()), u1); |
| 262 | } |
| 263 | |
| 264 | @Test(groups = "short") |
| 265 | public void should_handle_null_UDT_value() throws Exception { |
| 266 | Mapper<User> m = new MappingManager(session()).mapper(User.class); |
| 267 | |
| 268 | User u1 = new User("Paul", null); |
| 269 | m.save(u1); |
| 270 | |
| 271 | assertNull(m.get(u1.getUserId()).getMainAddress()); |
| 272 | } |
| 273 | |
| 274 | @Test(groups = "short") |
| 275 | public void testAccessor() throws Exception { |
| 276 | MappingManager manager = new MappingManager(session()); |
| 277 | |
| 278 | Mapper<User> m = new MappingManager(session()).mapper(User.class); |
| 279 | User u1 = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245")); |
| 280 | m.save(u1); |
| 281 | |
| 282 | UserAccessor userAccessor = manager.createAccessor(UserAccessor.class); |
| 283 | |
| 284 | Address workAddress = new Address("5 Main Street", "Springfield", 12345, "23431342"); |
| 285 | userAccessor.addAddress(u1.getUserId(), "work", workAddress); |
| 286 | |
| 287 | User u2 = userAccessor.getOne(u1.getUserId()); |
| 288 | assertEquals(workAddress, u2.getOtherAddresses().get("work")); |
| 289 | |
| 290 | // Adding a null value should remove it from the list. |
| 291 | userAccessor.addAddress(u1.getUserId(), "work", null); |
| 292 | User u3 = userAccessor.getOne(u1.getUserId()); |
| 293 | assertThat(u3.getOtherAddresses()).doesNotContainKey("work"); |
| 294 | |
| 295 | // Add a bunch of other addresses |
| 296 | Map<String, Address> otherAddresses = Maps.newHashMap(); |
| 297 | otherAddresses.put("work", workAddress); |
| 298 | otherAddresses.put("cabin", new Address("42 Middle of Nowhere", "Lake of the Woods", 49553, "8675309")); |
| 299 | |
| 300 | userAccessor.setOtherAddresses(u1.getUserId(), otherAddresses); |
| 301 | User u4 = userAccessor.getOne(u1.getUserId()); |
| 302 | assertThat(u4.getOtherAddresses()).isEqualTo(otherAddresses); |
| 303 | |
| 304 | // Nullify other addresses |
| 305 | userAccessor.setOtherAddresses(u1.getUserId(), null); |
| 306 | User u5 = userAccessor.getOne(u1.getUserId()); |
| 307 | assertThat(u5.getOtherAddresses()).isEmpty(); |
| 308 | |
| 309 | // No argument call |
| 310 | Result<User> u = userAccessor.getAll(); |
| 311 | assertEquals(u.one(), u5); |
| 312 | assertTrue(u.isExhausted()); |
| 313 | } |
| 314 | |
| 315 | @Test(groups = "short") |
| 316 | public void should_be_able_to_use_udtCodec_standalone() { |
| 317 | // Create a separate Cluster/Session to start with a CodecRegistry from scratch (so not already registered). |
| 318 | Cluster cluster = register(Cluster.builder() |
| 319 | .addContactPoints(getContactPoints()) |
| 320 | .withPort(ccm().getBinaryPort()) |
| 321 | .build()); |
| 322 | CodecRegistry registry = cluster.getConfiguration().getCodecRegistry(); |
| 323 | Session session = cluster.connect(keyspace); |
| 324 | |
| 325 | UUID userId = UUIDs.random(); |
| 326 | |
| 327 | // Create a user. |
| 328 | session.execute("update users SET other_addresses['condo']={street: '101 Ocean Ln', city: 'Jacksonville, FL', \"ZIP_code\": 89898, phones: {'8675309'}} " + |
| 329 | " WHERE user_id=" + TypeCodec.uuid().format(userId)); |
| 330 | session.execute("update users SET mainaddress={street: '42 Middle of Nowhere', city: 'Lake of the Woods', \"ZIP_code\": 49553, phones: {'8675039'}} " + |
| 331 | " WHERE user_Id=" + TypeCodec.uuid().format(userId)); |
| 332 | |
| 333 | // Get the user. |
| 334 | Row row = session.execute("select * from users where user_id=?", userId).one(); |
| 335 | |
| 336 | UDTValue udtValue = row.getUDTValue("mainaddress"); |
| 337 | assertThat(udtValue.getString("street")).isEqualTo("42 Middle of Nowhere"); |
| 338 | |
| 339 | Object udtObject = row.getObject("mainaddress"); |
| 340 | assertThat(udtObject).isEqualTo(udtValue); |
| 341 | |
| 342 | // There shouldn't be a codec for address. |
| 343 | try { |
| 344 | assertThat(registry.codecFor(udtValue.getType(), Address.class)); |
| 345 | fail("Didn't expect to find codec for udtType <-> Address"); |
| 346 | } catch (CodecNotFoundException e) { |
| 347 | // expected. |
| 348 | } |
| 349 | |
| 350 | // Expect a this_udt <-> UDTValue codec to exist. This is a pretty safe bet or else we wouldn't get |
| 351 | // this value back. |
| 352 | TypeCodec<UDTValue> udtCodec = registry.codecFor(udtValue.getType()); |
| 353 | assertThat(udtCodec.getCqlType()).isEqualTo(udtValue.getType()); |
| 354 | assertThat(udtCodec.getJavaType().getRawType()).isEqualTo(UDTValue.class); |
| 355 | |
| 356 | // Retrieve codec for Address, if it can be mapped it will be created, if already registered it'll be used. |
| 357 | TypeCodec<Address> codec = new MappingManager(session).udtCodec(Address.class); |
| 358 | |
| 359 | // The codec should be registered after we call udtCodec. |
| 360 | assertThat(registry.codecFor(udtValue.getType(), Address.class)).isEqualTo(codec); |
| 361 | |
| 362 | // Should be able to retrieve as an Address. |
| 363 | Address mainAddress = row.get("mainaddress", Address.class); |
| 364 | assertThat(mainAddress.getCity()).isEqualTo("Lake of the Woods"); |
| 365 | assertThat(mainAddress.getStreet()).isEqualTo("42 Middle of Nowhere"); |
| 366 | assertThat(mainAddress.getZipCode()).isEqualTo(49553); |
| 367 | assertThat(mainAddress.getPhones()).containsExactly("8675039"); |
| 368 | |
| 369 | // Should be able to retrieve within a Map. |
| 370 | Address expectedOtherAddress = new Address(); |
| 371 | expectedOtherAddress.setStreet("101 Ocean Ln"); |
| 372 | expectedOtherAddress.setCity("Jacksonville, FL"); |
| 373 | expectedOtherAddress.setZipCode(89898); |
| 374 | expectedOtherAddress.setPhones(Collections.singleton("8675309")); |
| 375 | |
| 376 | Map<String, Address> otherAddresses = row.getMap("other_addresses", String.class, Address.class); |
| 377 | assertThat(otherAddresses).containsOnly(MapEntry.entry("condo", expectedOtherAddress)); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Ensures that if a table is altered after a {@link Mapper} is created that it continues to work as long as |
| 382 | * the change is compatible. A new column is added to the table in this case, which is a compatible change. |
| 383 | * <p> |
| 384 | * It also ensures that after the change is made that requesting a new {@link Mapper} returns a different instance |
| 385 | * instead of the existing one. |
| 386 | * |
| 387 | * @jira_ticket JAVA-1126 |
| 388 | * @test_category object_mapper |
| 389 | */ |
| 390 | @Test(groups = "short") |
| 391 | public void should_work_normally_when_when_table_is_altered_but_remains_compatible() { |
| 392 | MappingManager manager = new MappingManager(session()); |
| 393 | User expected = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245")); |
| 394 | Mapper<User> m = manager.mapper(User.class); |
| 395 | Mapper<User> m1 = manager.mapper(User.class); |
| 396 | assertThat(m1).isSameAs(m); |
| 397 | session().execute("ALTER TABLE users ADD foo text"); |
| 398 | m.save(expected); |
| 399 | User retrieved = m.get(expected.getUserId()); |
| 400 | assertThat(retrieved).isEqualTo(expected); |
| 401 | |
| 402 | // Mapper should be recreated since table changed. |
| 403 | Mapper<User> m2 = manager.mapper(User.class); |
| 404 | assertThat(m2).isNotSameAs(m); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Ensures that if a UDT is altered after a {@link Mapper} is created that has a UDT that it continues to work as |
| 409 | * long as the change is compatible. A new field is added to the table in this case, which is a compatible change. |
| 410 | * <p> |
| 411 | * It also ensures that after the change is made that requesting a new {@link TypeCodec} for that UDT and a new |
| 412 | * {@link Mapper} for a table using that UDT returns different instances instead of the existing ones. |
| 413 | * |
| 414 | * @jira_ticket JAVA-1126 |
| 415 | * @test_category object_mapper |
| 416 | */ |
| 417 | @Test(groups = "short") |
| 418 | public void should_work_normally_when_udt_is_altered_but_remains_compatible() { |
| 419 | MappingManager manager = new MappingManager(session()); |
| 420 | User expected = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245")); |
| 421 | Mapper<User> m = manager.mapper(User.class); |
| 422 | TypeCodec c = manager.udtCodec(Address.class); |
| 423 | TypeCodec c1 = manager.udtCodec(Address.class); |
| 424 | assertThat(c1).isSameAs(c); |
| 425 | session().execute("ALTER TYPE address ADD foo text"); |
| 426 | m.save(expected); |
| 427 | User retrieved = m.get(expected.getUserId()); |
| 428 | assertThat(retrieved).isEqualTo(expected); |
| 429 | |
| 430 | // Codec should be recreated when requested since UDT and thus the table changed. |
| 431 | Mapper m2 = manager.mapper(User.class); |
| 432 | assertThat(m2).isNotSameAs(m); |
| 433 | |
| 434 | // Codec should be recreated when requested since UDT changed. |
| 435 | TypeCodec c2 = manager.udtCodec(Address.class); |
| 436 | assertThat(c2).isNotSameAs(c); |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Ensures that if a table is dropped after a {@link Mapper} is created that the {@link Mapper} can no longer |
| 441 | * successfully make queries and that attempting to create a new {@link Mapper} throws an |
| 442 | * {@link IllegalArgumentException}. |
| 443 | * <p> |
| 444 | * It also ensures that requesting a {@link Mapper} fails as the previous {@link Mapper} was evicted and a new |
| 445 | * one cannot be created as the table has been dropped. |
| 446 | * |
| 447 | * @jira_ticket JAVA-1126 |
| 448 | * @test_category object_mapper |
| 449 | */ |
| 450 | @Test(groups = "short") |
| 451 | public void should_throw_error_when_table_is_dropped() { |
| 452 | User user = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245")); |
| 453 | MappingManager manager = new MappingManager(session()); |
| 454 | Mapper<User> mapper = manager.mapper(User.class); |
| 455 | session().execute("DROP TABLE users"); |
| 456 | // usage of stale mapper |
| 457 | try { |
| 458 | mapper.save(user); |
| 459 | fail("Expected InvalidQueryException"); |
| 460 | } catch (InvalidQueryException e) { |
| 461 | assertThat(e.getMessage()).contains("unconfigured", "users"); |
| 462 | } |
| 463 | try { |
| 464 | mapper.get(user.getUserId()); |
| 465 | fail("Expected InvalidQueryException"); |
| 466 | } catch (InvalidQueryException e) { |
| 467 | assertThat(e.getMessage()).contains("unconfigured", "users"); |
| 468 | } |
| 469 | // trying to use a new mapper |
| 470 | try { |
| 471 | manager.mapper(User.class); |
| 472 | fail("Expected IllegalArgumentException"); |
| 473 | } catch (IllegalArgumentException e) { |
| 474 | assertThat(e.getMessage()).isEqualTo("Table or materialized view users does not exist in keyspace " + keyspace); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Ensures that if a table is altered after a {@link Mapper} is created that it no longer continues to work if |
| 480 | * the change is not compatible. A column is dropped from the table in this case, which is an incompatible change. |
| 481 | * <p> |
| 482 | * It also ensures that requesting a {@link Mapper} fails as the previous {@link Mapper} was evicted and a new |
| 483 | * one cannot be created as a declared column is missing from the schema but is present in the class definition. |
| 484 | * |
| 485 | * @jira_ticket JAVA-1126 |
| 486 | * @test_category object_mapper |
| 487 | */ |
| 488 | @Test(groups = "short") |
| 489 | public void should_throw_error_when_table_is_altered_and_is_not_compatible_anymore() { |
| 490 | User user = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245")); |
| 491 | MappingManager manager = new MappingManager(session()); |
| 492 | Mapper<User> mapper = manager.mapper(User.class); |
| 493 | session().execute("ALTER TABLE users DROP mainaddress"); |
| 494 | // usage of stale mapper |
| 495 | try { |
| 496 | mapper.save(user); |
| 497 | fail("Expected InvalidQueryException"); |
| 498 | } catch (InvalidQueryException e) { |
| 499 | assertThat(e.getMessage()).isEqualTo("Unknown identifier mainaddress"); |
| 500 | } |
| 501 | try { |
| 502 | mapper.get(user.getUserId()); |
| 503 | fail("Expected InvalidQueryException"); |
| 504 | } catch (InvalidQueryException e) { |
| 505 | assertThat(e.getMessage()).isEqualTo("Undefined name mainaddress in selection clause"); |
| 506 | } |
| 507 | // trying to use a new mapper |
| 508 | try { |
| 509 | manager.mapper(User.class); |
| 510 | fail("Expected IllegalArgumentException"); |
| 511 | } catch (IllegalArgumentException e) { |
| 512 | assertThat(e.getMessage()).isEqualTo(String.format("Column \"mainaddress\" does not exist in table %s.users", keyspace)); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * Ensures that if a UDT is altered after a {@link Mapper} is created that it no longer continues to work if |
| 518 | * the change made to the UDT is not compatible. A field is renamed in the UDT in this case, which is not a |
| 519 | * compatible change. |
| 520 | * <p> |
| 521 | * It also ensures that requesting a {@link Mapper} fails as the previous {@link Mapper} was evicted and a new |
| 522 | * one cannot be created as a declared field is missing from the schema but is present in the class definition. |
| 523 | * <p> |
| 524 | * Also verifies that a {@link TypeCodec} cannot be requested for the UDT as well. |
| 525 | * |
| 526 | * @jira_ticket JAVA-1126 |
| 527 | * @test_category object_mapper |
| 528 | */ |
| 529 | @Test(groups = "short") |
| 530 | public void should_throw_error_when_udt_is_altered_and_is_not_compatible_anymore() { |
| 531 | User user = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245")); |
| 532 | MappingManager manager = new MappingManager(session()); |
| 533 | Mapper<User> mapper = manager.mapper(User.class); |
| 534 | session().execute("ALTER TYPE address RENAME \"ZIP_code\" to zip_code"); |
| 535 | // usage of stale mapper |
| 536 | try { |
| 537 | mapper.save(user); |
| 538 | fail("Expected CodecNotFoundException"); |
| 539 | } catch (CodecNotFoundException e) { |
| 540 | // ok, codec could not be created |
| 541 | } |
| 542 | // insert manually to be able to test retrieval |
| 543 | session().execute("INSERT INTO users (user_id, name, mainaddress) VALUES (?, 'Paul', { street : '12 4th Street', zip_code : 12345 })", user.getUserId()); |
| 544 | try { |
| 545 | mapper.get(user.getUserId()); |
| 546 | fail("Expected CodecNotFoundException"); |
| 547 | } catch (CodecNotFoundException e) { |
| 548 | // ok, codec could not be created |
| 549 | } |
| 550 | // trying to use a new mapper |
| 551 | try { |
| 552 | manager.mapper(User.class); |
| 553 | fail("Expected IllegalArgumentException"); |
| 554 | } catch (IllegalArgumentException e) { |
| 555 | assertThat(e.getMessage()).isEqualTo(String.format("Field \"ZIP_code\" does not exist in type %s.address", keyspace)); |
| 556 | } |
| 557 | try { |
| 558 | manager.udtCodec(Address.class); |
| 559 | fail("Expected IllegalArgumentException"); |
| 560 | } catch (IllegalArgumentException e) { |
| 561 | assertThat(e.getMessage()).isEqualTo(String.format("Field \"ZIP_code\" does not exist in type %s.address", keyspace)); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | @UDT(name = "address") |
| 566 | public static class AddressUnknownField { |
| 567 | |
| 568 | private String city; |
| 569 | |
| 570 | private String street; |
| 571 | |
| 572 | @Field(name = "ZIP_code", caseSensitive = true) |
| 573 | private int zipCode; |
| 574 | |
| 575 | private Set<String> phones; |
| 576 | |
| 577 | public String province; |
| 578 | |
| 579 | public AddressUnknownField() { |
| 580 | } |
| 581 | |
| 582 | public String getStreet() { |
| 583 | return street; |
| 584 | } |
| 585 | |
| 586 | public void setStreet(String street) { |
| 587 | this.street = street; |
| 588 | } |
| 589 | |
| 590 | public String getCity() { |
| 591 | return city; |
| 592 | } |
| 593 | |
| 594 | public void setCity(String city) { |
| 595 | this.city = city; |
| 596 | } |
| 597 | |
| 598 | public int getZipCode() { |
| 599 | return zipCode; |
| 600 | } |
| 601 | |
| 602 | public void setZipCode(int zipCode) { |
| 603 | this.zipCode = zipCode; |
| 604 | } |
| 605 | |
| 606 | public Set<String> getPhones() { |
| 607 | return phones; |
| 608 | } |
| 609 | |
| 610 | public void setPhones(Set<String> phones) { |
| 611 | this.phones = phones; |
| 612 | } |
| 613 | |
| 614 | public String getProvince() { |
| 615 | return province; |
| 616 | } |
| 617 | |
| 618 | public void setProvince(String province) { |
| 619 | this.province = province; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | |
| 624 | /** |
| 625 | * Ensures that when attempting to create a {@link TypeCodec} from a class that has a field that does not exist in |
| 626 | * the UDT that an {@link IllegalArgumentException} is thrown. |
| 627 | * |
| 628 | * @jira_ticket JAVA-1126 |
| 629 | * @test_category object_mapper |
| 630 | */ |
| 631 | @Test(groups = "short", expectedExceptions = IllegalArgumentException.class) |
| 632 | public void should_fail_to_create_codec_if_class_has_field_not_in_udt() { |
| 633 | MappingManager manager = new MappingManager(session()); |
| 634 | manager.getUDTCodec(AddressUnknownField.class); |
| 635 | } |
| 636 | |
| 637 | @UDT(name = "nonexistent") |
| 638 | public static class NonExistentUDT { |
| 639 | public String name; |
| 640 | |
| 641 | public void setName(String name) { |
| 642 | this.name = name; |
| 643 | } |
| 644 | |
| 645 | public String getName() { |
| 646 | return this.name; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * Ensures that when attempting to create a {@link TypeCodec} from a class that has a {@link UDT} annotation with |
| 652 | * a name that doesn't exist in the current keyspace that an {@link IllegalArgumentException} is thrown. |
| 653 | * |
| 654 | * @jira_ticket JAVA-1126 |
| 655 | * @test_category object_mapper |
| 656 | */ |
| 657 | @Test(groups = "short", expectedExceptions = IllegalArgumentException.class) |
| 658 | public void should_fail_to_create_codec_if_udt_does_not_exist() { |
| 659 | MappingManager manager = new MappingManager(session()); |
| 660 | manager.getUDTCodec(NonExistentUDT.class); |
| 661 | } |
| 662 | |
| 663 | @Table(name = "users") |
| 664 | public static class UserWithAddressUnknownField { |
| 665 | @PartitionKey |
| 666 | @Column(name = "user_id") |
| 667 | private UUID userId; |
| 668 | |
| 669 | private String name; |
| 670 | |
| 671 | @Frozen |
| 672 | private AddressUnknownField mainAddress; |
| 673 | |
| 674 | @Column(name = "other_addresses") |
| 675 | @FrozenValue |
| 676 | private Map<String, Address> otherAddresses = Maps.newHashMap(); |
| 677 | |
| 678 | public UserWithAddressUnknownField() { |
| 679 | } |
| 680 | |
| 681 | public UUID getUserId() { |
| 682 | return userId; |
| 683 | } |
| 684 | |
| 685 | public void setUserId(UUID userId) { |
| 686 | this.userId = userId; |
| 687 | } |
| 688 | |
| 689 | public String getName() { |
| 690 | return name; |
| 691 | } |
| 692 | |
| 693 | public void setName(String name) { |
| 694 | this.name = name; |
| 695 | } |
| 696 | |
| 697 | public AddressUnknownField getMainAddress() { |
| 698 | return mainAddress; |
| 699 | } |
| 700 | |
| 701 | public void setMainAddress(AddressUnknownField address) { |
| 702 | this.mainAddress = address; |
| 703 | } |
| 704 | |
| 705 | public Map<String, Address> getOtherAddresses() { |
| 706 | return otherAddresses; |
| 707 | } |
| 708 | |
| 709 | public void setOtherAddresses(Map<String, Address> otherAddresses) { |
| 710 | this.otherAddresses = otherAddresses; |
| 711 | } |
| 712 | |
| 713 | public void addOtherAddress(String name, Address address) { |
| 714 | this.otherAddresses.put(name, address); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * Ensures that when attempting to create a {@link Mapper} from a class that has a field that is a class mapping to |
| 720 | * a user type that has a field that does not exist in that UDT that an {@link IllegalArgumentException} is thrown. |
| 721 | * |
| 722 | * @jira_ticket JAVA-1126 |
| 723 | * @test_category object_mapper |
| 724 | */ |
| 725 | @Test(groups = "short", expectedExceptions = IllegalArgumentException.class) |
| 726 | public void should_fail_to_create_mapper_if_class_has_udt_field_class_that_has_field_not_in_udt() { |
| 727 | MappingManager manager = new MappingManager(session()); |
| 728 | manager.mapper(UserWithAddressUnknownField.class); |
| 729 | } |
| 730 | |
| 731 | } |