Elffuss BraidHub

kikocis/java-driver public

5c9db3fa0bee · 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 com.datastax.driver.core.CCMBridge;
19import com.datastax.driver.core.Cluster;
20import com.datastax.driver.core.Session;
21import com.datastax.driver.core.utils.CassandraVersion;
22import com.datastax.driver.mapping.annotations.*;
23import com.google.common.collect.ImmutableMap;
24import org.testng.annotations.Test;
25
26import java.util.HashMap;
27import java.util.Map;
28
29@CassandraVersion(major = 2.1)
30@SuppressWarnings("unused")
31public class UDTFieldMapperTest {
32
33 @Test(groups = "short")
34 public void udt_and_tables_with_ks_created_in_another_session_should_be_mapped() {
35 CCMBridge ccm = null;
36 Cluster cluster = null;
37
38 try {
39 // Create type and table
40 ccm = CCMBridge.builder().build();
41 cluster = Cluster.builder()
42 .addContactPointsWithPorts(ccm.addressOfNode(1))
43 .withAddressTranslator(ccm.addressTranslator())
44 .build();
45 Session session1 = cluster.connect();
46 session1.execute("create schema if not exists java_509 " +
47 "with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };");
48 session1.execute("create type java_509.my_tuple (" +
49 "type text, " +
50 "value text);");
51 session1.execute("create table java_509.my_hash (" +
52 "key int primary key, " +
53 "properties map<text, frozen<java_509.my_tuple>>);");
54 cluster.close();
55
56 // Create entities with another connection
57 cluster = Cluster.builder()
58 .addContactPointsWithPorts(ccm.addressOfNode(1))
59 .withAddressTranslator(ccm.addressTranslator())
60 .build();
61 Session session2 = cluster.newSession();
62 Mapper<MyHashWithKeyspace> hashMapper = new MappingManager(session2).mapper(MyHashWithKeyspace.class);
63 hashMapper.save(new MyHashWithKeyspace(
64 1,
65 ImmutableMap.of("key-1", new MyTupleWithKeyspace("first-half-1", "second-half-1"))));
66 hashMapper.save(new MyHashWithKeyspace(
67 2,
68 ImmutableMap.of("key-2", new MyTupleWithKeyspace("first-half-2", null))));
69 hashMapper.save(new MyHashWithKeyspace(
70 3,
71 ImmutableMap.of("key-3", new MyTupleWithKeyspace(null, "second-half-3"))));
72 hashMapper.save(new MyHashWithKeyspace(
73 4,
74 new HashMap<String, MyTupleWithKeyspace>()));
75 hashMapper.save(new MyHashWithKeyspace(
76 5,
77 null));
78 } finally {
79 if (cluster != null)
80 cluster.close();
81 if (ccm != null)
82 ccm.remove();
83 }
84 }
85
86 @Test(groups = "short")
87 public void udt_and_tables_without_ks_created_in_another_session_should_be_mapped() {
88 CCMBridge ccm = null;
89 Cluster cluster = null;
90
91 try {
92 // Create type and table
93 ccm = CCMBridge.builder().build();
94 cluster = Cluster.builder()
95 .addContactPointsWithPorts(ccm.addressOfNode(1))
96 .withAddressTranslator(ccm.addressTranslator())
97 .build();
98 Session session1 = cluster.connect();
99 session1.execute("create schema if not exists java_509 " +
100 "with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };");
101 session1.execute("use java_509");
102 session1.execute("create type my_tuple (" +
103 "type text, " +
104 "value text);");
105 session1.execute("create table my_hash (" +
106 "key int primary key, " +
107 "properties map<text, frozen<my_tuple>>);");
108 cluster.close();
109
110 // Create entities with another connection
111 cluster = Cluster.builder()
112 .addContactPointsWithPorts(ccm.addressOfNode(1))
113 .withAddressTranslator(ccm.addressTranslator())
114 .build();
115 Session session2 = cluster.newSession();
116
117 session2.execute("use java_509");
118 Mapper<MyHash> hashMapper = new MappingManager(session2).mapper(MyHash.class);
119 hashMapper.save(new MyHash(
120 1,
121 ImmutableMap.of("key-1", new MyTuple("first-half-1", "second-half-1"))));
122 hashMapper.save(new MyHash(
123 2,
124 ImmutableMap.of("key-2", new MyTuple("first-half-2", null))));
125 hashMapper.save(new MyHash(
126 3,
127 ImmutableMap.of("key-3", new MyTuple(null, "second-half-3"))));
128 hashMapper.save(new MyHash(
129 4,
130 new HashMap<String, MyTuple>()));
131 hashMapper.save(new MyHash(
132 5,
133 null));
134 } finally {
135 if (cluster != null)
136 cluster.close();
137 if (ccm != null)
138 ccm.remove();
139 }
140 }
141
142 @UDT(name = "my_tuple")
143 static class MyTuple {
144 @Field(name = "type")
145 private String type;
146 @Field(name = "value")
147 private String value;
148
149 public MyTuple() {
150 }
151
152 public MyTuple(String type, String value) {
153 this.type = type;
154 this.value = value;
155 }
156
157 public String getType() {
158 return type;
159 }
160
161 public void setType(String type) {
162 this.type = type;
163 }
164
165 public String getValue() {
166 return value;
167 }
168
169 public void setValue(String value) {
170 this.value = value;
171 }
172 }
173
174 @Table(name = "my_hash")
175 static class MyHash {
176 @PartitionKey
177 @Column(name = "key")
178 private int key;
179
180 @FrozenValue
181 @Column(name = "properties")
182 private Map<String, MyTuple> properties;
183
184 public MyHash() {
185
186 }
187
188 public MyHash(int key, Map<String, MyTuple> properties) {
189
190 this.key = key;
191 this.properties = properties;
192 }
193
194 public int getKey() {
195 return key;
196 }
197
198 public void setKey(int key) {
199 this.key = key;
200 }
201
202 public Map<String, MyTuple> getProperties() {
203 return properties;
204 }
205
206 public void setProperties(Map<String, MyTuple> properties) {
207 this.properties = properties;
208 }
209 }
210
211 @UDT(name = "my_tuple", keyspace = "java_509")
212 static class MyTupleWithKeyspace {
213 @Field(name = "type")
214 private String type;
215 @Field(name = "value")
216 private String value;
217
218 public MyTupleWithKeyspace() {
219 }
220
221 public MyTupleWithKeyspace(String type, String value) {
222 this.type = type;
223 this.value = value;
224 }
225
226 public String getType() {
227 return type;
228 }
229
230 public void setType(String type) {
231 this.type = type;
232 }
233
234 public String getValue() {
235 return value;
236 }
237
238 public void setValue(String value) {
239 this.value = value;
240 }
241 }
242
243 @Table(name = "my_hash", keyspace = "java_509")
244 static class MyHashWithKeyspace {
245 @PartitionKey
246 @Column(name = "key")
247 private int key;
248
249 @FrozenValue
250 @Column(name = "properties")
251 private Map<String, MyTupleWithKeyspace> properties;
252
253 public MyHashWithKeyspace() {
254 }
255
256 public MyHashWithKeyspace(int key, Map<String, MyTupleWithKeyspace> properties) {
257 this.key = key;
258 this.properties = properties;
259 }
260
261 public int getKey() {
262 return key;
263 }
264
265 public void setKey(int key) {
266 this.key = key;
267 }
268
269 public Map<String, MyTupleWithKeyspace> getProperties() {
270 return properties;
271 }
272
273 public void setProperties(Map<String, MyTupleWithKeyspace> properties) {
274 this.properties = properties;
275 }
276 }
277}