Elffuss BraidHub

kikocis/java-driver public

cd8368797258 · 7.1 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 com.google.common.base.Objects;
21import com.google.common.collect.ImmutableMap;
22import com.google.common.collect.Sets;
23import org.testng.annotations.Test;
24import org.testng.collections.Lists;
25
26import static org.testng.Assert.assertEquals;
27import static org.testng.Assert.assertNull;
28import static org.testng.Assert.assertTrue;
29
30import com.datastax.driver.core.CCMBridge;
31import com.datastax.driver.core.ResultSet;
32import com.datastax.driver.core.utils.UUIDs;
33import com.datastax.driver.mapping.annotations.*;
34
35public class MapperUDTTest extends CCMBridge.PerClassSingleNodeCluster {
36
37 protected Collection<String> getTableDefinitions() {
38 return Arrays.asList("CREATE TYPE address (street text, city text, zip_code int, phones set<text>)",
39 "CREATE TABLE users (user_id uuid PRIMARY KEY, name text, mainaddress frozen<address>, other_addresses map<text,frozen<address>>)");
40 }
41
42 @Table(keyspace = "ks", name = "users",
43 readConsistency = "QUORUM",
44 writeConsistency = "QUORUM")
45 public static class User {
46 @PartitionKey
47 @Column(name = "user_id")
48 private UUID userId;
49
50 private String name;
51
52 @Frozen
53 private Address mainAddress;
54
55 @Column(name = "other_addresses")
56 @FrozenValue
57 private Map<String, Address> otherAddresses;
58
59 public User() {
60 }
61
62 public User(String name, Address address) {
63 this.userId = UUIDs.random();
64 this.name = name;
65 this.mainAddress = address;
66 this.otherAddresses = new HashMap<String, Address>();
67 }
68
69 public UUID getUserId() {
70 return userId;
71 }
72
73 public void setUserId(UUID userId) {
74 this.userId = userId;
75 }
76
77 public String getName() {
78 return name;
79 }
80
81 public void setName(String name) {
82 this.name = name;
83 }
84
85 public Address getMainAddress() {
86 return mainAddress;
87 }
88
89 public void setMainAddress(Address address) {
90 this.mainAddress = address;
91 }
92
93 public Map<String, Address> getOtherAddresses() {
94 return otherAddresses;
95 }
96
97 public void setOtherAddresses(Map<String, Address> otherAddresses) {
98 this.otherAddresses = otherAddresses;
99 }
100
101 public void addOtherAddress(String name, Address address) {
102 this.otherAddresses.put(name, address);
103 }
104
105 @Override
106 public boolean equals(Object other) {
107 if (other instanceof User) {
108 User that = (User) other;
109 return Objects.equal(this.userId, that.userId) &&
110 Objects.equal(this.name, that.name) &&
111 Objects.equal(this.mainAddress, that.mainAddress) &&
112 Objects.equal(this.otherAddresses, that.otherAddresses);
113 }
114 return false;
115 }
116 }
117
118 @UDT(keyspace = "ks", name = "address")
119 public static class Address {
120 private String street;
121
122 private String city;
123
124 @Field(name = "zip_code")
125 private int zipCode;
126
127 private Set<String> phones;
128
129 public Address() {
130 }
131
132 public Address(String street, String city, int zipCode, String... phones) {
133 this.street = street;
134 this.city = city;
135 this.zipCode = zipCode;
136 this.phones = new HashSet<String>();
137 for (String phone : phones) {
138 this.phones.add(phone);
139 }
140 }
141
142 public String getStreet() {
143 return street;
144 }
145
146 public void setStreet(String street) {
147 this.street = street;
148 }
149
150 public String getCity() {
151 return city;
152 }
153
154 public void setCity(String city) {
155 this.city = city;
156 }
157
158 public int getZipCode() {
159 return zipCode;
160 }
161
162 public void setZipCode(int zipCode) {
163 this.zipCode = zipCode;
164 }
165
166 public Set<String> getPhones() {
167 return phones;
168 }
169
170 public void setPhones(Set<String> phones) {
171 this.phones = phones;
172 }
173
174 @Override
175 public boolean equals(Object other) {
176 if (other instanceof Address) {
177 Address that = (Address) other;
178 return Objects.equal(this.street, that.street) &&
179 Objects.equal(this.city, that.city) &&
180 Objects.equal(this.zipCode, that.zipCode) &&
181 Objects.equal(this.phones, that.phones);
182 }
183 return false;
184 }
185 }
186
187 @Accessor
188 public interface UserAccessor {
189 @Query("SELECT * FROM ks.users WHERE user_id=:userId")
190 User getOne(@Param("userId") UUID userId);
191
192 @Query("UPDATE ks.users SET other_addresses[:name]=:address WHERE user_id=:id")
193 ResultSet addAddress(@Param("id") UUID id, @Param("name") String addressName, @Param("address") Address address);
194
195 @Query("SELECT * FROM ks.users")
196 public Result<User> getAll();
197 }
198
199 @Test(groups = "short")
200 public void testSimpleEntity() throws Exception {
201 Mapper<User> m = new MappingManager(session).mapper(User.class);
202
203 User u1 = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245"));
204 u1.addOtherAddress("work", new Address("5 Main Street", "Springfield", 12345, "23431342"));
205 m.save(u1);
206
207 assertEquals(m.get(u1.getUserId()), u1);
208 }
209
210 @Test(groups = "short")
211 public void testAccessor() throws Exception {
212 MappingManager manager = new MappingManager(session);
213
214 Mapper<User> m = new MappingManager(session).mapper(User.class);
215 User u1 = new User("Paul", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245"));
216 m.save(u1);
217
218 UserAccessor userAccessor = manager.createAccessor(UserAccessor.class);
219
220 Address workAddress = new Address("5 Main Street", "Springfield", 12345, "23431342");
221 userAccessor.addAddress(u1.getUserId(), "work", workAddress);
222
223 User u2 = userAccessor.getOne(u1.getUserId());
224 assertEquals(workAddress, u2.getOtherAddresses().get("work"));
225
226 // No argument call
227 Result<User> u = userAccessor.getAll();
228 assertEquals(u.one(), u2);
229 assertTrue(u.isExhausted());
230 }
231}