Elffuss BraidHub

kikocis/java-driver public

c2df6e94606c · 8.7 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.CCMBridge;
19import com.datastax.driver.core.ResultSet;
20import com.datastax.driver.core.utils.UUIDs;
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
32public class MapperUDTTest extends CCMBridge.PerClassSingleNodeCluster {
33
34 protected Collection<String> getTableDefinitions() {
35 return Arrays.asList("CREATE TYPE address (street text, city text, zip_code int, phones set<text>)",
36 "CREATE TABLE users (user_id uuid PRIMARY KEY, name text, mainaddress frozen<address>, other_addresses map<text,frozen<address>>)");
37 }
38
39 @BeforeMethod(groups = "short")
40 public void clean() {
41 session.execute("TRUNCATE users");
42 }
43
44 @Table(name = "users",
45 readConsistency = "QUORUM",
46 writeConsistency = "QUORUM")
47 public static class User {
48 @PartitionKey
49 @Column(name = "user_id")
50 private UUID userId;
51
52 private String name;
53
54 @Frozen
55 private Address mainAddress;
56
57 @Column(name = "other_addresses")
58 @FrozenValue
59 private Map<String, Address> otherAddresses = Maps.newHashMap();
60
61 public User() {
62 }
63
64 public User(String name, Address address) {
65 this.userId = UUIDs.random();
66 this.name = name;
67 this.mainAddress = address;
68 this.otherAddresses = new HashMap<String, Address>();
69 }
70
71 public UUID getUserId() {
72 return userId;
73 }
74
75 public void setUserId(UUID userId) {
76 this.userId = userId;
77 }
78
79 public String getName() {
80 return name;
81 }
82
83 public void setName(String name) {
84 this.name = name;
85 }
86
87 public Address getMainAddress() {
88 return mainAddress;
89 }
90
91 public void setMainAddress(Address address) {
92 this.mainAddress = address;
93 }
94
95 public Map<String, Address> getOtherAddresses() {
96 return otherAddresses;
97 }
98
99 public void setOtherAddresses(Map<String, Address> otherAddresses) {
100 this.otherAddresses = otherAddresses;
101 }
102
103 public void addOtherAddress(String name, Address address) {
104 this.otherAddresses.put(name, address);
105 }
106
107 @Override
108 public boolean equals(Object other) {
109 if (other instanceof User) {
110 User that = (User) other;
111 return Objects.equal(this.userId, that.userId) &&
112 Objects.equal(this.name, that.name) &&
113 Objects.equal(this.mainAddress, that.mainAddress) &&
114 Objects.equal(this.otherAddresses, that.otherAddresses);
115 }
116 return false;
117 }
118 }
119
120 @UDT(name = "address")
121 public static class Address {
122
123 // Dummy constant to test that static fields are properly ignored
124 public static final int FOO = 1;
125
126 private String street;
127
128 @Field // not strictly required, but we want to check that the annotation works without a name
129 private String city;
130
131 @Field(name = "zip_code")
132 private int zipCode;
133
134 private Set<String> phones;
135
136 public Address() {
137 }
138
139 public Address(String street, String city, int zipCode, String... phones) {
140 this.street = street;
141 this.city = city;
142 this.zipCode = zipCode;
143 this.phones = new HashSet<String>();
144 for (String phone : phones) {
145 this.phones.add(phone);
146 }
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 public 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}