Elffuss BraidHub

kikocis/java-driver public

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