Elffuss BraidHub

kikocis/java-driver public

2358e77d8126 · 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.core.utils.CassandraVersion;
14import com.datastax.driver.mapping.annotations.*;
15
16@CassandraVersion(major=2.1)
17public class UDTFieldMapperTest {
18
19 @Test(groups = "short")
20 public void udt_and_tables_with_ks_created_in_another_session_should_be_mapped() {
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 CCMBridge ccm = null;
69 Cluster cluster = null;
70
71 try {
72 // Create type and table
73 ccm = CCMBridge.create("test", 1);
74 cluster = Cluster.builder().addContactPoint(CCMBridge.ipOfNode(1)).build();
75 Session session1 = cluster.connect();
76 session1.execute("create schema if not exists java_509 " +
77 "with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };");
78 session1.execute("use java_509");
79 session1.execute("create type my_tuple (" +
80 "type text, " +
81 "value text);");
82 session1.execute("create table my_hash (" +
83 "key int primary key, " +
84 "properties map<text, frozen<my_tuple>>);");
85 cluster.close();
86
87 // Create entities with another connection
88 cluster = Cluster.builder().addContactPoint(CCMBridge.ipOfNode(1)).build();
89 Session session2 = cluster.newSession();
90
91 session2.execute("use java_509");
92 Mapper<MyHash> hashMapper = new MappingManager(session2).mapper(MyHash.class);
93 hashMapper.save(new MyHash(
94 1,
95 ImmutableMap.of("key-1", new MyTuple("first-half-1", "second-half-1"))));
96 hashMapper.save(new MyHash(
97 2,
98 ImmutableMap.of("key-2", new MyTuple("first-half-2", null))));
99 hashMapper.save(new MyHash(
100 3,
101 ImmutableMap.of("key-3", new MyTuple(null, "second-half-3"))));
102 hashMapper.save(new MyHash(
103 4,
104 new HashMap<String, MyTuple>()));
105 hashMapper.save(new MyHash(
106 5,
107 null));
108 } finally {
109 if (cluster != null)
110 cluster.close();
111 if (ccm != null)
112 ccm.remove();
113 }
114 }
115
116 @UDT(name = "my_tuple")
117 static class MyTuple {
118 @Field(name = "type")
119 private String type;
120 @Field(name = "value")
121 private String value;
122
123 public MyTuple() {
124 }
125
126 public MyTuple(String type, String value) {
127 this.type = type;
128 this.value = value;
129 }
130
131 public String getType() {
132 return type;
133 }
134
135 public void setType(String type) {
136 this.type = type;
137 }
138
139 public String getValue() {
140 return value;
141 }
142
143 public void setValue(String value) {
144 this.value = value;
145 }
146 }
147
148 @Table(name = "my_hash")
149 static class MyHash {
150 @PartitionKey
151 @Column(name = "key")
152 private int key;
153
154 @FrozenValue
155 @Column(name = "properties")
156 private Map<String, MyTuple> properties;
157
158 public MyHash() {
159
160 }
161
162 public MyHash(int key, Map<String, MyTuple> properties) {
163
164 this.key = key;
165 this.properties = properties;
166 }
167
168 public int getKey() {
169 return key;
170 }
171
172 public void setKey(int key) {
173 this.key = key;
174 }
175
176 public Map<String, MyTuple> getProperties() {
177 return properties;
178 }
179
180 public void setProperties(Map<String, MyTuple> properties) {
181 this.properties = properties;
182 }
183 }
184
185 @UDT(name = "my_tuple", keyspace = "java_509")
186 static class MyTupleWithKeyspace {
187 @Field(name = "type")
188 private String type;
189 @Field(name = "value")
190 private String value;
191
192 public MyTupleWithKeyspace() {
193 }
194
195 public MyTupleWithKeyspace(String type, String value) {
196 this.type = type;
197 this.value = value;
198 }
199
200 public String getType() {
201 return type;
202 }
203
204 public void setType(String type) {
205 this.type = type;
206 }
207
208 public String getValue() {
209 return value;
210 }
211
212 public void setValue(String value) {
213 this.value = value;
214 }
215 }
216
217 @Table(name = "my_hash", keyspace = "java_509")
218 static class MyHashWithKeyspace {
219 @PartitionKey
220 @Column(name = "key")
221 private int key;
222
223 @FrozenValue
224 @Column(name = "properties")
225 private Map<String, MyTupleWithKeyspace> properties;
226
227 public MyHashWithKeyspace() {
228 }
229
230 public MyHashWithKeyspace(int key, Map<String, MyTupleWithKeyspace> properties) {
231 this.key = key;
232 this.properties = properties;
233 }
234
235 public int getKey() {
236 return key;
237 }
238
239 public void setKey(int key) {
240 this.key = key;
241 }
242
243 public Map<String, MyTupleWithKeyspace> getProperties() {
244 return properties;
245 }
246
247 public void setProperties(Map<String, MyTupleWithKeyspace> properties) {
248 this.properties = properties;
249 }
250 }
251}