Elffuss BraidHub

kikocis/java-driver public

31b3bab3142a · 7.5 KiBRawPermalink
1/*
2 * Copyright (C) 2012-2014 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 org.testng.annotations.BeforeMethod;
21
22import com.google.common.base.Objects;
23import org.testng.annotations.Test;
24import static org.testng.Assert.assertEquals;
25import static org.testng.Assert.assertNull;
26import static org.testng.Assert.assertTrue;
27
28import com.datastax.driver.core.CCMBridge;
29import com.datastax.driver.core.ResultSet;
30import com.datastax.driver.core.utils.UUIDs;
31
32import com.datastax.driver.mapping.annotations.*;
33
34public class MapperUDTTest extends CCMBridge.PerClassSingleNodeCluster {
35
36 protected Collection<String> getTableDefinitions() {
37 return Arrays.asList("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(keyspace = "ks", 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;
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(keyspace = "ks", 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 private String city;
131
132 @Field(name = "zip_code")
133 private int zipCode;
134
135 private Set<String> phones;
136
137 public Address() {
138 }
139
140 public Address(String street, String city, int zipCode, String... phones) {
141 this.street = street;
142 this.city = city;
143 this.zipCode = zipCode;
144 this.phones = new HashSet<String>();
145 for (String phone : phones) {
146 this.phones.add(phone);
147 }
148 }
149
150 public String getStreet() {
151 return street;
152 }
153
154 public void setStreet(String street) {
155 this.street = street;
156 }
157
158 public String getCity() {
159 return city;
160 }
161
162 public void setCity(String city) {
163 this.city = city;
164 }
165
166 public int getZipCode() {
167 return zipCode;
168 }
169
170 public void setZipCode(int zipCode) {
171 this.zipCode = zipCode;
172 }
173
174 public Set<String> getPhones() {
175 return phones;
176 }
177
178 public void setPhones(Set<String> phones) {
179 this.phones = phones;
180 }
181
182 @Override
183 public boolean equals(Object other) {
184 if (other instanceof Address) {
185 Address that = (Address) other;
186 return Objects.equal(this.street, that.street) &&
187 Objects.equal(this.city, that.city) &&
188 Objects.equal(this.zipCode, that.zipCode) &&
189 Objects.equal(this.phones, that.phones);
190 }
191 return false;
192 }
193 }
194
195 @Accessor
196 public interface UserAccessor {
197 @Query("SELECT * FROM ks.users WHERE user_id=:userId")
198 User getOne(@Param("userId") UUID userId);
199
200 @Query("UPDATE ks.users SET other_addresses[:name]=:address WHERE user_id=:id")
201 ResultSet addAddress(@Param("id") UUID id, @Param("name") String addressName, @Param("address") Address address);
202
203 @Query("SELECT * FROM ks.users")
204 public Result<User> getAll();
205 }
206
207 @Test(groups = "short")
208 public void testSimpleEntity() throws Exception {
209 Mapper<User> m = new MappingManager(session).mapper(User.class);
210
211 User u1 = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245"));
212 u1.addOtherAddress("work", new Address("5 Main Street", "Springfield", 12345, "23431342"));
213 m.save(u1);
214
215 assertEquals(m.get(u1.getUserId()), u1);
216 }
217
218 @Test(groups = "short")
219 public void should_handle_null_UDT_value() throws Exception {
220 Mapper<User> m = new MappingManager(session).mapper(User.class);
221
222 User u1 = new User("Paul", null);
223 m.save(u1);
224
225 assertNull(m.get(u1.getUserId()).getMainAddress());
226 }
227
228 @Test(groups = "short")
229 public void testAccessor() throws Exception {
230 MappingManager manager = new MappingManager(session);
231
232 Mapper<User> m = new MappingManager(session).mapper(User.class);
233 User u1 = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245"));
234 m.save(u1);
235
236 UserAccessor userAccessor = manager.createAccessor(UserAccessor.class);
237
238 Address workAddress = new Address("5 Main Street", "Springfield", 12345, "23431342");
239 userAccessor.addAddress(u1.getUserId(), "work", workAddress);
240
241 User u2 = userAccessor.getOne(u1.getUserId());
242 assertEquals(workAddress, u2.getOtherAddresses().get("work"));
243
244 // No argument call
245 Result<User> u = userAccessor.getAll();
246 assertEquals(u.one(), u2);
247 assertTrue(u.isExhausted());
248 }
249}