forked from apache/cassandra-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathMapperKeyspaceTest.java
More file actions
270 lines (227 loc) · 8.59 KB
/
Copy pathMapperKeyspaceTest.java
File metadata and controls
270 lines (227 loc) · 8.59 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* 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 static com.datastax.driver.core.Assertions.assertThat;
import com.datastax.driver.core.CCMTestsSupport;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.utils.CassandraVersion;
import com.datastax.driver.mapping.annotations.PartitionKey;
import com.datastax.driver.mapping.annotations.Table;
import com.datastax.driver.mapping.annotations.UDT;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@CassandraVersion("2.1")
public class MapperKeyspaceTest extends CCMTestsSupport {
@Override
public void onTestContextInitialized() {
execute(
"CREATE KEYSPACE IF NOT EXISTS MapperKeyspaceTest WITH replication = {'class': 'NetworkTopologyStrategy', 'datacenter1': 1}",
"CREATE TYPE IF NOT EXISTS MapperKeyspaceTest.address(street text)",
"CREATE TABLE IF NOT EXISTS MapperKeyspaceTest.user(name text PRIMARY KEY, address frozen<address>)",
"CREATE KEYSPACE IF NOT EXISTS MapperKeyspaceTest2 WITH replication = {'class': 'NetworkTopologyStrategy', 'datacenter1': 1}",
"CREATE TYPE IF NOT EXISTS MapperKeyspaceTest2.address(street text)",
"CREATE TABLE IF NOT EXISTS MapperKeyspaceTest2.user(name text PRIMARY KEY, address frozen<address>)");
}
@BeforeMethod(groups = "short")
public void setup() {
execute("TRUNCATE MapperKeyspaceTest.user", "TRUNCATE MapperKeyspaceTest2.user");
}
@Test(
groups = "short",
expectedExceptions = IllegalArgumentException.class,
expectedExceptionsMessageRegExp = ".* you must provide a keyspace name .*")
public void should_fail_if_no_keyspace_provided() {
new TestScenario<UserWithoutKeyspace>(UserWithoutKeyspace.class).check();
}
@Test(groups = "short")
public void should_use_session_keyspace() {
// Only works if it's not defined by the annotation
new TestScenario<UserWithoutKeyspace>(UserWithoutKeyspace.class)
.withSessionKeyspace("MapperKeyspaceTest")
.withExpectedKeyspace("MapperKeyspaceTest")
.check();
}
@Test(groups = "short")
public void should_use_annotation_keyspace() {
new TestScenario<UserWithKeyspace>(UserWithKeyspace.class)
.withExpectedKeyspace("MapperKeyspaceTest")
.check();
// Annotation takes precedence over session keyspace:
new TestScenario<UserWithKeyspace>(UserWithKeyspace.class)
.withSessionKeyspace("MapperKeyspaceTest2")
.withExpectedKeyspace("MapperKeyspaceTest")
.check();
}
@Test(groups = "short")
public void should_use_keyspace_provided_when_creating_mapper() {
// Provided nowhere except in the mapper call:
new TestScenario<UserWithoutKeyspace>(UserWithoutKeyspace.class)
.withMapperKeyspace("MapperKeyspaceTest")
.withExpectedKeyspace("MapperKeyspaceTest")
.check();
// Mapper takes precedence over session:
new TestScenario<UserWithoutKeyspace>(UserWithoutKeyspace.class)
.withSessionKeyspace("MapperKeyspaceTest")
.withMapperKeyspace("MapperKeyspaceTest2")
.withExpectedKeyspace("MapperKeyspaceTest2")
.check();
// Mapper takes precedence over annotation:
new TestScenario<UserWithKeyspace>(UserWithKeyspace.class)
.withMapperKeyspace("MapperKeyspaceTest2")
.withExpectedKeyspace("MapperKeyspaceTest2")
.check();
}
private class TestScenario<UserT extends User> {
private final Class<UserT> userClass;
private final UserT sampleUser;
private String sessionKeyspace;
private String mapperKeyspace;
private String expectedKeyspace;
TestScenario(Class<UserT> userClass) {
this.userClass = userClass;
this.sampleUser = buildSampleUser(userClass);
}
@SuppressWarnings("unchecked")
private UserT buildSampleUser(Class<UserT> userClass) {
// Hacky but we know the subclasses so no need to be more fancy
if (userClass == UserWithKeyspace.class) {
return (UserT) new UserWithKeyspace("user1", new AddressWithKeyspace("street"));
} else if (userClass == UserWithoutKeyspace.class) {
return (UserT) new UserWithoutKeyspace("user1", new AddressWithoutKeyspace("street"));
} else {
throw new AssertionError("Unsupported user class " + userClass);
}
}
/** The keyspace passed to Cluster.connect() */
TestScenario withSessionKeyspace(String sessionKeyspace) {
this.sessionKeyspace = sessionKeyspace;
return this;
}
/** The keyspace passed to MappingManager.mapper() */
TestScenario withMapperKeyspace(String mapperKeyspace) {
this.mapperKeyspace = mapperKeyspace;
return this;
}
/** The keyspace where we expect data to be saved */
TestScenario withExpectedKeyspace(String expectedKeyspace) {
this.expectedKeyspace = expectedKeyspace;
return this;
}
void check() {
Session session = cluster().connect(sessionKeyspace);
MappingManager manager = new MappingManager(session);
Mapper<UserT> mapper = manager.mapper(userClass, mapperKeyspace);
mapper.save(sampleUser);
assertThat(expectedKeyspace).isNotNull();
Row row =
session()
.execute(
String.format("SELECT * FROM %s.user WHERE name = ?", expectedKeyspace),
sampleUser.getName())
.one();
assertThat(row).isNotNull();
UserT loadedUser = mapper.get(sampleUser.getName());
assertThat(loadedUser.getName()).isEqualTo(sampleUser.getName());
assertThat(loadedUser.getAddress().getStreet())
.isEqualTo(sampleUser.getAddress().getStreet());
mapper.delete(sampleUser);
row =
session()
.execute(
String.format("SELECT * FROM %s.user WHERE name = ?", expectedKeyspace),
sampleUser.getName())
.one();
assertThat(row).isNull();
}
}
@SuppressWarnings("unused")
public static class Address {
private String street;
public Address() {}
public Address(String street) {
this.street = street;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
@SuppressWarnings("unused")
@UDT(keyspace = "MapperKeyspaceTest", name = "address")
public static class AddressWithKeyspace extends Address {
public AddressWithKeyspace() {}
public AddressWithKeyspace(String street) {
super(street);
}
}
@SuppressWarnings("unused")
@UDT(name = "address")
public static class AddressWithoutKeyspace extends Address {
public AddressWithoutKeyspace() {}
public AddressWithoutKeyspace(String street) {
super(street);
}
}
@SuppressWarnings("unused")
public static class User<AddressT extends Address> {
@PartitionKey private String name;
private AddressT address;
public User() {}
public User(String name, AddressT address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public AddressT getAddress() {
return address;
}
public void setAddress(AddressT address) {
this.address = address;
}
}
@SuppressWarnings("unused")
@Table(keyspace = "MapperKeyspaceTest", name = "user")
public static class UserWithKeyspace extends User<AddressWithKeyspace> {
public UserWithKeyspace() {}
public UserWithKeyspace(String name, AddressWithKeyspace address) {
super(name, address);
}
@Override
public AddressWithKeyspace getAddress() {
return super.getAddress();
}
}
@SuppressWarnings("unused")
@Table(name = "user")
public static class UserWithoutKeyspace extends User<AddressWithoutKeyspace> {
public UserWithoutKeyspace() {}
public UserWithoutKeyspace(String name, AddressWithoutKeyspace address) {
super(name, address);
}
@Override
public AddressWithoutKeyspace getAddress() {
return super.getAddress();
}
}
}