| 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.CCMTestsSupport; |
| 19 | import com.datastax.driver.core.ResultSet; |
| 20 | import com.datastax.driver.mapping.annotations.*; |
| 21 | import com.google.common.base.Objects; |
| 22 | import com.google.common.collect.Maps; |
| 23 | import org.testng.annotations.BeforeMethod; |
| 24 | import org.testng.annotations.Test; |
| 25 | |
| 26 | import java.util.*; |
| 27 | |
| 28 | import static org.assertj.core.api.Assertions.assertThat; |
| 29 | import static org.testng.Assert.*; |
| 30 | |
| 31 | @SuppressWarnings("unused") |
| 32 | public class MapperUDTTest extends CCMTestsSupport { |
| 33 | |
| 34 | @Override |
| 35 | public void onTestContextInitialized() { |
| 36 | execute("CREATE TYPE address (street text, city text, zip_code int, phones set<text>)", |
| 37 | "CREATE TYPE user_id (id uuid, login text)", |
| 38 | "CREATE TABLE users (user_id frozen<user_id> PRIMARY KEY, name text, mainaddress frozen<address>, other_addresses map<text,frozen<address>>)"); |
| 39 | } |
| 40 | |
| 41 | @BeforeMethod(groups = "short") |
| 42 | public void clean() { |
| 43 | session().execute("TRUNCATE users"); |
| 44 | } |
| 45 | |
| 46 | @Table(name = "users", |
| 47 | readConsistency = "QUORUM", |
| 48 | writeConsistency = "QUORUM") |
| 49 | public static class User { |
| 50 | |
| 51 | @PartitionKey |
| 52 | @Column(name = "user_id") |
| 53 | @Frozen |
| 54 | private UserId userId; |
| 55 | |
| 56 | private String name; |
| 57 | |
| 58 | @Frozen |
| 59 | private Address mainAddress; |
| 60 | |
| 61 | @Column(name = "other_addresses") |
| 62 | @FrozenValue |
| 63 | private Map<String, Address> otherAddresses = Maps.newHashMap(); |
| 64 | |
| 65 | public User() { |
| 66 | } |
| 67 | |
| 68 | public User(String name, Address address) { |
| 69 | this.userId = new UserId(UUID.randomUUID(), name); |
| 70 | this.name = name; |
| 71 | this.mainAddress = address; |
| 72 | this.otherAddresses = new HashMap<String, Address>(); |
| 73 | } |
| 74 | |
| 75 | public UserId getUserId() { |
| 76 | return userId; |
| 77 | } |
| 78 | |
| 79 | public void setUserId(UserId userId) { |
| 80 | this.userId = userId; |
| 81 | } |
| 82 | |
| 83 | public String getName() { |
| 84 | return name; |
| 85 | } |
| 86 | |
| 87 | public void setName(String name) { |
| 88 | this.name = name; |
| 89 | } |
| 90 | |
| 91 | public Address getMainAddress() { |
| 92 | return mainAddress; |
| 93 | } |
| 94 | |
| 95 | public void setMainAddress(Address address) { |
| 96 | this.mainAddress = address; |
| 97 | } |
| 98 | |
| 99 | public Map<String, Address> getOtherAddresses() { |
| 100 | return otherAddresses; |
| 101 | } |
| 102 | |
| 103 | public void setOtherAddresses(Map<String, Address> otherAddresses) { |
| 104 | this.otherAddresses = otherAddresses; |
| 105 | } |
| 106 | |
| 107 | public void addOtherAddress(String name, Address address) { |
| 108 | this.otherAddresses.put(name, address); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public boolean equals(Object other) { |
| 113 | if (other instanceof User) { |
| 114 | User that = (User) other; |
| 115 | return Objects.equal(this.userId, that.userId) && |
| 116 | Objects.equal(this.name, that.name) && |
| 117 | Objects.equal(this.mainAddress, that.mainAddress) && |
| 118 | Objects.equal(this.otherAddresses, that.otherAddresses); |
| 119 | } |
| 120 | return false; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | @UDT(name = "address") |
| 125 | public static class Address { |
| 126 | |
| 127 | // Dummy constant to test that static fields are properly ignored |
| 128 | public static final int FOO = 1; |
| 129 | |
| 130 | private String street; |
| 131 | |
| 132 | @Field // not strictly required, but we want to check that the annotation works without a name |
| 133 | private String city; |
| 134 | |
| 135 | @Field(name = "zip_code") |
| 136 | private int zipCode; |
| 137 | |
| 138 | private Set<String> phones; |
| 139 | |
| 140 | public Address() { |
| 141 | } |
| 142 | |
| 143 | public Address(String street, String city, int zipCode, String... phones) { |
| 144 | this.street = street; |
| 145 | this.city = city; |
| 146 | this.zipCode = zipCode; |
| 147 | this.phones = new HashSet<String>(); |
| 148 | Collections.addAll(this.phones, phones); |
| 149 | } |
| 150 | |
| 151 | public String getStreet() { |
| 152 | return street; |
| 153 | } |
| 154 | |
| 155 | public void setStreet(String street) { |
| 156 | this.street = street; |
| 157 | } |
| 158 | |
| 159 | public String getCity() { |
| 160 | return city; |
| 161 | } |
| 162 | |
| 163 | public void setCity(String city) { |
| 164 | this.city = city; |
| 165 | } |
| 166 | |
| 167 | public int getZipCode() { |
| 168 | return zipCode; |
| 169 | } |
| 170 | |
| 171 | public void setZipCode(int zipCode) { |
| 172 | this.zipCode = zipCode; |
| 173 | } |
| 174 | |
| 175 | public Set<String> getPhones() { |
| 176 | return phones; |
| 177 | } |
| 178 | |
| 179 | public void setPhones(Set<String> phones) { |
| 180 | this.phones = phones; |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public boolean equals(Object other) { |
| 185 | if (other instanceof Address) { |
| 186 | Address that = (Address) other; |
| 187 | return Objects.equal(this.street, that.street) && |
| 188 | Objects.equal(this.city, that.city) && |
| 189 | Objects.equal(this.zipCode, that.zipCode) && |
| 190 | Objects.equal(this.phones, that.phones); |
| 191 | } |
| 192 | return false; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | @UDT(name = "user_id") |
| 197 | public static class UserId { |
| 198 | |
| 199 | private UUID id; |
| 200 | |
| 201 | private String login; |
| 202 | |
| 203 | public UserId() { |
| 204 | } |
| 205 | |
| 206 | public UserId(UUID id, String login) { |
| 207 | this.id = id; |
| 208 | this.login = login; |
| 209 | } |
| 210 | |
| 211 | public UUID getId() { |
| 212 | return id; |
| 213 | } |
| 214 | |
| 215 | public void setId(UUID id) { |
| 216 | this.id = id; |
| 217 | } |
| 218 | |
| 219 | public String getLogin() { |
| 220 | return login; |
| 221 | } |
| 222 | |
| 223 | public void setLogin(String login) { |
| 224 | this.login = login; |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public boolean equals(Object other) { |
| 229 | if (other instanceof UserId) { |
| 230 | UserId that = (UserId) other; |
| 231 | return Objects.equal(this.id, that.id) && |
| 232 | Objects.equal(this.login, that.login); |
| 233 | } |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | } |
| 238 | |
| 239 | @Accessor |
| 240 | public interface UserAccessor { |
| 241 | @Query("SELECT * FROM users WHERE user_id=:userId") |
| 242 | User getOne(@Param("userId") UserId id); |
| 243 | |
| 244 | @Query("UPDATE users SET other_addresses[:name]=:address WHERE user_id=:id") |
| 245 | ResultSet addAddress(@Param("id") UserId id, @Param("name") String addressName, @Param("address") Address address); |
| 246 | |
| 247 | @Query("UPDATE users SET other_addresses=:addresses where user_id=:id") |
| 248 | ResultSet setOtherAddresses(@Param("id") UserId id, @Param("addresses") Map<String, Address> addresses); |
| 249 | |
| 250 | @Query("SELECT * FROM users") |
| 251 | Result<User> getAll(); |
| 252 | } |
| 253 | |
| 254 | @Test(groups = "short") |
| 255 | public void testSimpleEntity() throws Exception { |
| 256 | Mapper<User> m = new MappingManager(session()).mapper(User.class); |
| 257 | |
| 258 | User u1 = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245")); |
| 259 | u1.addOtherAddress("work", new Address("5 Main Street", "Springfield", 12345, "23431342")); |
| 260 | m.save(u1); |
| 261 | |
| 262 | assertEquals(m.get(u1.getUserId()), u1); |
| 263 | } |
| 264 | |
| 265 | @Test(groups = "short") |
| 266 | public void should_handle_null_UDT_value() throws Exception { |
| 267 | Mapper<User> m = new MappingManager(session()).mapper(User.class); |
| 268 | |
| 269 | User u1 = new User("Paul", null); |
| 270 | m.save(u1); |
| 271 | |
| 272 | assertNull(m.get(u1.getUserId()).getMainAddress()); |
| 273 | } |
| 274 | |
| 275 | @Test(groups = "short") |
| 276 | public void testAccessor() throws Exception { |
| 277 | MappingManager manager = new MappingManager(session()); |
| 278 | |
| 279 | Mapper<User> m = new MappingManager(session()).mapper(User.class); |
| 280 | User u1 = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245")); |
| 281 | m.save(u1); |
| 282 | |
| 283 | UserAccessor userAccessor = manager.createAccessor(UserAccessor.class); |
| 284 | |
| 285 | Address workAddress = new Address("5 Main Street", "Springfield", 12345, "23431342"); |
| 286 | userAccessor.addAddress(u1.getUserId(), "work", workAddress); |
| 287 | |
| 288 | User u2 = userAccessor.getOne(u1.getUserId()); |
| 289 | assertEquals(workAddress, u2.getOtherAddresses().get("work")); |
| 290 | |
| 291 | // Adding a null value should remove it from the list. |
| 292 | userAccessor.addAddress(u1.getUserId(), "work", null); |
| 293 | User u3 = userAccessor.getOne(u1.getUserId()); |
| 294 | assertThat(u3.getOtherAddresses()).doesNotContainKey("work"); |
| 295 | |
| 296 | // Add a bunch of other addresses |
| 297 | Map<String, Address> otherAddresses = Maps.newHashMap(); |
| 298 | otherAddresses.put("work", workAddress); |
| 299 | otherAddresses.put("cabin", new Address("42 Middle of Nowhere", "Lake of the Woods", 49553, "8675309")); |
| 300 | |
| 301 | userAccessor.setOtherAddresses(u1.getUserId(), otherAddresses); |
| 302 | User u4 = userAccessor.getOne(u1.getUserId()); |
| 303 | assertThat(u4.getOtherAddresses()).isEqualTo(otherAddresses); |
| 304 | |
| 305 | // Nullify other addresses |
| 306 | userAccessor.setOtherAddresses(u1.getUserId(), null); |
| 307 | User u5 = userAccessor.getOne(u1.getUserId()); |
| 308 | assertThat(u5.getOtherAddresses()).isEmpty(); |
| 309 | |
| 310 | // No argument call |
| 311 | Result<User> u = userAccessor.getAll(); |
| 312 | assertEquals(u.one(), u5); |
| 313 | assertTrue(u.isExhausted()); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Ensures that an entity that has an UDT as part of its primary key |
| 318 | * can be successfully stored and retrieved. |
| 319 | * |
| 320 | * @jira_ticket JAVA-831 |
| 321 | */ |
| 322 | @Test(groups = "short") |
| 323 | public void should_handle_UDT_in_partition_key() throws Exception { |
| 324 | Mapper<User> m = new MappingManager(session()).mapper(User.class); |
| 325 | User u1 = new User("Paul", null); |
| 326 | m.save(u1); |
| 327 | User u2 = m.get(u1.getUserId()); |
| 328 | assertThat(u1).isEqualTo(u2); |
| 329 | m.delete(u1.getUserId()); |
| 330 | assertThat(m.get(u1.getUserId())).isNull(); |
| 331 | } |
| 332 | |
| 333 | } |