forked from apache/cassandra-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathUDTFieldMapperTest.java
More file actions
233 lines (193 loc) · 6.88 KB
/
Copy pathUDTFieldMapperTest.java
File metadata and controls
233 lines (193 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Copyright DataStax, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datastax.driver.mapping;
import com.datastax.driver.core.CCMConfig;
import com.datastax.driver.core.CCMTestsSupport;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.utils.CassandraVersion;
import com.datastax.driver.mapping.annotations.Column;
import com.datastax.driver.mapping.annotations.Field;
import com.datastax.driver.mapping.annotations.FrozenValue;
import com.datastax.driver.mapping.annotations.PartitionKey;
import com.datastax.driver.mapping.annotations.Table;
import com.datastax.driver.mapping.annotations.UDT;
import com.google.common.collect.ImmutableMap;
import java.util.HashMap;
import java.util.Map;
import org.testng.annotations.Test;
@CassandraVersion("2.1.0")
@CCMConfig(createCluster = false)
@SuppressWarnings("unused")
public class UDTFieldMapperTest extends CCMTestsSupport {
@Test(groups = "short")
public void udt_and_tables_with_ks_created_in_another_session_should_be_mapped() {
Cluster cluster1 = register(createClusterBuilder().build());
Session session1 = cluster1.connect();
// Create type and table
session1.execute(
"create keyspace if not exists java_509 "
+ "with replication = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 1 };");
session1.execute("create type java_509.my_tuple (" + "type text, " + "value text);");
session1.execute(
"create table java_509.my_hash ("
+ "key int primary key, "
+ "properties map<text, frozen<java_509.my_tuple>>);");
cluster1.close();
// Create entities with another connection
Cluster cluster2 = register(createClusterBuilder().build());
Session session2 = cluster2.newSession();
Mapper<MyHashWithKeyspace> hashMapper =
new MappingManager(session2).mapper(MyHashWithKeyspace.class);
hashMapper.save(
new MyHashWithKeyspace(
1, ImmutableMap.of("key-1", new MyTupleWithKeyspace("first-half-1", "second-half-1"))));
hashMapper.save(
new MyHashWithKeyspace(
2, ImmutableMap.of("key-2", new MyTupleWithKeyspace("first-half-2", null))));
hashMapper.save(
new MyHashWithKeyspace(
3, ImmutableMap.of("key-3", new MyTupleWithKeyspace(null, "second-half-3"))));
hashMapper.save(new MyHashWithKeyspace(4, new HashMap<String, MyTupleWithKeyspace>()));
hashMapper.save(new MyHashWithKeyspace(5, null));
}
@Test(groups = "short")
public void udt_and_tables_without_ks_created_in_another_session_should_be_mapped() {
Cluster cluster1 = register(createClusterBuilder().build());
Session session1 = cluster1.connect();
session1.execute(
"create keyspace if not exists java_509b "
+ "with replication = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 1 };");
session1.execute("use java_509b");
session1.execute("create type my_tuple (" + "type text, " + "value text);");
session1.execute(
"create table my_hash ("
+ "key int primary key, "
+ "properties map<text, frozen<my_tuple>>);");
cluster1.close();
// Create entities with another connection
Cluster cluster2 = register(createClusterBuilder().build());
Session session2 = cluster2.newSession();
session2.execute("use java_509b");
Mapper<MyHash> hashMapper = new MappingManager(session2).mapper(MyHash.class);
hashMapper.save(
new MyHash(1, ImmutableMap.of("key-1", new MyTuple("first-half-1", "second-half-1"))));
hashMapper.save(new MyHash(2, ImmutableMap.of("key-2", new MyTuple("first-half-2", null))));
hashMapper.save(new MyHash(3, ImmutableMap.of("key-3", new MyTuple(null, "second-half-3"))));
hashMapper.save(new MyHash(4, new HashMap<String, MyTuple>()));
hashMapper.save(new MyHash(5, null));
}
@UDT(name = "my_tuple")
static class MyTuple {
@Field(name = "type")
private String type;
@Field(name = "value")
private String value;
public MyTuple() {}
public MyTuple(String type, String value) {
this.type = type;
this.value = value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
@Table(name = "my_hash")
static class MyHash {
@PartitionKey
@Column(name = "key")
private int key;
@FrozenValue
@Column(name = "properties")
private Map<String, MyTuple> properties;
public MyHash() {}
public MyHash(int key, Map<String, MyTuple> properties) {
this.key = key;
this.properties = properties;
}
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
public Map<String, MyTuple> getProperties() {
return properties;
}
public void setProperties(Map<String, MyTuple> properties) {
this.properties = properties;
}
}
@UDT(name = "my_tuple", keyspace = "java_509")
static class MyTupleWithKeyspace {
@Field(name = "type")
private String type;
@Field(name = "value")
private String value;
public MyTupleWithKeyspace() {}
public MyTupleWithKeyspace(String type, String value) {
this.type = type;
this.value = value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
@Table(name = "my_hash", keyspace = "java_509")
static class MyHashWithKeyspace {
@PartitionKey
@Column(name = "key")
private int key;
@FrozenValue
@Column(name = "properties")
private Map<String, MyTupleWithKeyspace> properties;
public MyHashWithKeyspace() {}
public MyHashWithKeyspace(int key, Map<String, MyTupleWithKeyspace> properties) {
this.key = key;
this.properties = properties;
}
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
public Map<String, MyTupleWithKeyspace> getProperties() {
return properties;
}
public void setProperties(Map<String, MyTupleWithKeyspace> properties) {
this.properties = properties;
}
}
}