Elffuss BraidHub

kikocis/java-driver public

2faf4660456e · 7.4 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 private String street;
125
126 private String city;
127
128 @Field(name = "zip_code")
129 private int zipCode;
130
131 private Set<String> phones;
132
133 public Address() {
134 }
135
136 public Address(String street, String city, int zipCode, String... phones) {
137 this.street = street;
138 this.city = city;
139 this.zipCode = zipCode;
140 this.phones = new HashSet<String>();
141 for (String phone : phones) {
142 this.phones.add(phone);
143 }
144 }
145
146 public String getStreet() {
147 return street;
148 }
149
150 public void setStreet(String street) {
151 this.street = street;
152 }
153
154 public String getCity() {
155 return city;
156 }
157
158 public void setCity(String city) {
159 this.city = city;
160 }
161
162 public int getZipCode() {
163 return zipCode;
164 }
165
166 public void setZipCode(int zipCode) {
167 this.zipCode = zipCode;
168 }
169
170 public Set<String> getPhones() {
171 return phones;
172 }
173
174 public void setPhones(Set<String> phones) {
175 this.phones = phones;
176 }
177
178 @Override
179 public boolean equals(Object other) {
180 if (other instanceof Address) {
181 Address that = (Address) other;
182 return Objects.equal(this.street, that.street) &&
183 Objects.equal(this.city, that.city) &&
184 Objects.equal(this.zipCode, that.zipCode) &&
185 Objects.equal(this.phones, that.phones);
186 }
187 return false;
188 }
189 }
190
191 @Accessor
192 public interface UserAccessor {
193 @Query("SELECT * FROM ks.users WHERE user_id=:userId")
194 User getOne(@Param("userId") UUID userId);
195
196 @Query("UPDATE ks.users SET other_addresses[:name]=:address WHERE user_id=:id")
197 ResultSet addAddress(@Param("id") UUID id, @Param("name") String addressName, @Param("address") Address address);
198
199 @Query("SELECT * FROM ks.users")
200 public Result<User> getAll();
201 }
202
203 @Test(groups = "short")
204 public void testSimpleEntity() throws Exception {
205 Mapper<User> m = new MappingManager(session).mapper(User.class);
206
207 User u1 = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245"));
208 u1.addOtherAddress("work", new Address("5 Main Street", "Springfield", 12345, "23431342"));
209 m.save(u1);
210
211 assertEquals(m.get(u1.getUserId()), u1);
212 }
213
214 @Test(groups = "short")
215 public void should_handle_null_UDT_value() throws Exception {
216 Mapper<User> m = new MappingManager(session).mapper(User.class);
217
218 User u1 = new User("Paul", null);
219 m.save(u1);
220
221 assertNull(m.get(u1.getUserId()).getMainAddress());
222 }
223
224 @Test(groups = "short")
225 public void testAccessor() throws Exception {
226 MappingManager manager = new MappingManager(session);
227
228 Mapper<User> m = new MappingManager(session).mapper(User.class);
229 User u1 = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245"));
230 m.save(u1);
231
232 UserAccessor userAccessor = manager.createAccessor(UserAccessor.class);
233
234 Address workAddress = new Address("5 Main Street", "Springfield", 12345, "23431342");
235 userAccessor.addAddress(u1.getUserId(), "work", workAddress);
236
237 User u2 = userAccessor.getOne(u1.getUserId());
238 assertEquals(workAddress, u2.getOtherAddresses().get("work"));
239
240 // No argument call
241 Result<User> u = userAccessor.getAll();
242 assertEquals(u.one(), u2);
243 assertTrue(u.isExhausted());
244 }
245}