Elffuss BraidHub

kikocis/java-driver public

07915c7df825 · 8.8 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 java.util.*;
19
20import com.google.common.collect.Maps;
21import org.testng.annotations.BeforeMethod;
22
23import com.google.common.base.Objects;
24import org.testng.annotations.Test;
25
26import static org.assertj.core.api.Assertions.assertThat;
27import static org.testng.Assert.assertEquals;
28import static org.testng.Assert.assertNull;
29import static org.testng.Assert.assertTrue;
30
31import com.datastax.driver.core.CCMBridge;
32import com.datastax.driver.core.ResultSet;
33import com.datastax.driver.core.Row;
34import com.datastax.driver.core.utils.UUIDs;
35
36import com.datastax.driver.mapping.annotations.*;
37
38public class MapperUDTTest extends CCMBridge.PerClassSingleNodeCluster {
39
40 protected Collection<String> getTableDefinitions() {
41 return Arrays.asList("CREATE TYPE address (street text, city text, zip_code int, phones set<text>)",
42 "CREATE TABLE users (user_id uuid PRIMARY KEY, name text, mainaddress frozen<address>, other_addresses map<text,frozen<address>>)");
43 }
44
45 @BeforeMethod(groups = "short")
46 public void clean() {
47 session.execute("TRUNCATE users");
48 }
49
50 @Table(name = "users",
51 readConsistency = "QUORUM",
52 writeConsistency = "QUORUM")
53 public static class User {
54 @PartitionKey
55 @Column(name = "user_id")
56 private UUID 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 = UUIDs.random();
72 this.name = name;
73 this.mainAddress = address;
74 this.otherAddresses = new HashMap<String, Address>();
75 }
76
77 public UUID getUserId() {
78 return userId;
79 }
80
81 public void setUserId(UUID 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 for (String phone : phones) {
151 this.phones.add(phone);
152 }
153 }
154
155 public String getStreet() {
156 return street;
157 }
158
159 public void setStreet(String street) {
160 this.street = street;
161 }
162
163 public String getCity() {
164 return city;
165 }
166
167 public void setCity(String city) {
168 this.city = city;
169 }
170
171 public int getZipCode() {
172 return zipCode;
173 }
174
175 public void setZipCode(int zipCode) {
176 this.zipCode = zipCode;
177 }
178
179 public Set<String> getPhones() {
180 return phones;
181 }
182
183 public void setPhones(Set<String> phones) {
184 this.phones = phones;
185 }
186
187 @Override
188 public boolean equals(Object other) {
189 if (other instanceof Address) {
190 Address that = (Address) other;
191 return Objects.equal(this.street, that.street) &&
192 Objects.equal(this.city, that.city) &&
193 Objects.equal(this.zipCode, that.zipCode) &&
194 Objects.equal(this.phones, that.phones);
195 }
196 return false;
197 }
198 }
199
200 @Accessor
201 public interface UserAccessor {
202 @Query("SELECT * FROM users WHERE user_id=:userId")
203 User getOne(@Param("userId") UUID userId);
204
205 @Query("UPDATE users SET other_addresses[:name]=:address WHERE user_id=:id")
206 ResultSet addAddress(@Param("id") UUID id, @Param("name") String addressName, @Param("address") Address address);
207
208 @Query("UPDATE users SET other_addresses=:addresses where user_id=:id")
209 ResultSet setOtherAddresses(@Param("id") UUID id, @Param("addresses") Map<String, Address> addresses);
210
211 @Query("SELECT * FROM users")
212 public Result<User> getAll();
213 }
214
215 @Test(groups = "short")
216 public void testSimpleEntity() throws Exception {
217 Mapper<User> m = new MappingManager(session).mapper(User.class);
218
219 User u1 = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245"));
220 u1.addOtherAddress("work", new Address("5 Main Street", "Springfield", 12345, "23431342"));
221 m.save(u1);
222
223 assertEquals(m.get(u1.getUserId()), u1);
224 }
225
226 @Test(groups = "short")
227 public void should_handle_null_UDT_value() throws Exception {
228 Mapper<User> m = new MappingManager(session).mapper(User.class);
229
230 User u1 = new User("Paul", null);
231 m.save(u1);
232
233 assertNull(m.get(u1.getUserId()).getMainAddress());
234 }
235
236 @Test(groups = "short")
237 public void testAccessor() throws Exception {
238 MappingManager manager = new MappingManager(session);
239
240 Mapper<User> m = new MappingManager(session).mapper(User.class);
241 User u1 = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245"));
242 m.save(u1);
243
244 UserAccessor userAccessor = manager.createAccessor(UserAccessor.class);
245
246 Address workAddress = new Address("5 Main Street", "Springfield", 12345, "23431342");
247 userAccessor.addAddress(u1.getUserId(), "work", workAddress);
248
249 User u2 = userAccessor.getOne(u1.getUserId());
250 assertEquals(workAddress, u2.getOtherAddresses().get("work"));
251
252 // Adding a null value should remove it from the list.
253 userAccessor.addAddress(u1.getUserId(), "work", null);
254 User u3 = userAccessor.getOne(u1.getUserId());
255 assertThat(u3.getOtherAddresses()).doesNotContainKey("work");
256
257 // Add a bunch of other addresses
258 Map<String, Address> otherAddresses = Maps.newHashMap();
259 otherAddresses.put("work", workAddress);
260 otherAddresses.put("cabin", new Address("42 Middle of Nowhere", "Lake of the Woods", 49553, "8675309"));
261
262 userAccessor.setOtherAddresses(u1.getUserId(), otherAddresses);
263 User u4 = userAccessor.getOne(u1.getUserId());
264 assertThat(u4.getOtherAddresses()).isEqualTo(otherAddresses);
265
266 // Nullify other addresses
267 userAccessor.setOtherAddresses(u1.getUserId(), null);
268 User u5 = userAccessor.getOne(u1.getUserId());
269 assertThat(u5.getOtherAddresses()).isEmpty();
270
271 // No argument call
272 Result<User> u = userAccessor.getAll();
273 assertEquals(u.one(), u5);
274 assertTrue(u.isExhausted());
275 }
276}