Elffuss BraidHub

kikocis/java-driver public

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