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