-
-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathTestMergeCustomer.java
More file actions
401 lines (295 loc) · 14.5 KB
/
TestMergeCustomer.java
File metadata and controls
401 lines (295 loc) · 14.5 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package org.tests.merge;
import io.ebean.*;
import io.ebean.test.LoggedSql;
import io.ebean.text.PathProperties;
import io.ebean.xtest.BaseTestCase;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
public class TestMergeCustomer extends BaseTestCase {
private Random random = new Random();
@Test
public void customerOnly_defaultOptions_expect_updateOnly() {
MCustomer mCustomer = partial("cust1", "(id,name,version)");
mCustomer.setName("NotCust0");
LoggedSql.start();
DB.merge(mCustomer);
List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(1);
assertSql(sql.get(0)).contains("update mcustomer set name=?, version=? where id=? and version=?");
}
/**
* So this is effectively the same as a stateless update.
*/
@Test
public void customerOnly_expect_updateOnly() {
MCustomer mCustomer = partial("cust1", "(id,name,version)");
mCustomer.setName("NotCust1");
MergeOptions options = new MergeOptionsBuilder().build();
LoggedSql.start();
DB.merge(mCustomer, options);
List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(1);
assertSql(sql.get(0)).contains("update mcustomer set name=?, version=? where id=? and version=?");
}
@Test
public void customerOnly_setClientGeneratedIds_expect_selectAndUpdate() {
MCustomer mCustomer = partial("cust2", "(id,name,version)");
mCustomer.setName("NotCust2");
MergeOptions options = new MergeOptionsBuilder().setClientGeneratedIds().build();
LoggedSql.start();
server().merge(mCustomer, options);
List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(2);
assertSql(sql.get(0)).contains("select t0.id from mcustomer t0 where t0.id = ?");
assertSql(sql.get(1)).contains("update mcustomer set name=?, version=? where id=? and version=?");
}
@Test
public void customerWithAddresses_setClientGeneratedIds_expect_selectAndUpdate() {
MCustomer mCustomer = partial("cust3", "(id,name,version,shippingAddress(*),billingAddress(*))");
mCustomer.setName("NotCust3");
mCustomer.getBillingAddress().setStreet("modBillStreet");
mCustomer.getShippingAddress().setCity("modShipCity");
MergeOptions options = new MergeOptionsBuilder()
.addPath("shippingAddress")
.addPath("billingAddress")
.setClientGeneratedIds()
.build();
LoggedSql.start();
server().merge(mCustomer, options);
List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(6);
assertSql(sql.get(0)).contains("select t0.id, t0.shipping_address_id, t0.billing_address_id from mcustomer t0 where t0.id = ?");
assertSql(sql.get(1)).contains("update maddress set street=?, city=?, version=? where id=? and version=?");
assertSqlBind(sql, 2, 3);
assertThat(sql.get(5)).contains("update mcustomer set name=?, version=?, shipping_address_id=?, billing_address_id=? where id=? and version=?");
}
@Test
public void customerWithAddresses_newAddress_setClientGeneratedIds_expect_insertAddress() {
MCustomer mCustomer = partial("cust3", "(id,name,version,shippingAddress(*),billingAddress(*))");
mCustomer.setName("NotCust3");
// new billing address - no Id value so must be insert
mCustomer.setBillingAddress(new MAddress("Short", "Mid Wicket"));
mCustomer.getShippingAddress().setCity("modShipCity");
MergeOptions options = new MergeOptionsBuilder()
.addPath("shippingAddress")
.addPath("billingAddress")
.setClientGeneratedIds()
.build();
LoggedSql.start();
server().merge(mCustomer, options);
List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(8);
assertSql(sql.get(0)).contains("select t0.id, t0.shipping_address_id, t0.billing_address_id from mcustomer t0 where t0.id = ?");
assertSql(sql.get(1)).contains("insert into maddress (id, street, city, version) values (?,?,?,?)");
assertSqlBind(sql.get(2));
assertThat(sql.get(4)).contains("update maddress set street=?, city=?, version=? where id=? and version=?");
assertSqlBind(sql.get(5));
assertThat(sql.get(7)).contains("update mcustomer set name=?, version=?, shipping_address_id=?, billing_address_id=? where id=? and version=?");
}
@Test
public void customerWithAddresses_newAddressWithId_setClientGeneratedIds_expect_additionalCheckForAddressInsert() {
MCustomer mCustomer = partial("cust3", "(id,name,version,shippingAddress(*),billingAddress(*))");
mCustomer.setName("NotCust3");
// new billing address - has Id value + setClientGeneratedIds ... so extra query to check
MAddress mAddress = new MAddress("Short", "Mid Wicket");
mAddress.setId(UUID.randomUUID());
mCustomer.setBillingAddress(mAddress);
mCustomer.getShippingAddress().setCity("modShipCity");
MergeOptions options = new MergeOptionsBuilder()
.addPath("shippingAddress")
.addPath("billingAddress")
.setClientGeneratedIds() // As we are using clientIds ... we don't know if the new UUID is an insert or update without checking
.build();
LoggedSql.start();
server().merge(mCustomer, options);
List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(9);
assertSql(sql.get(0)).contains("select t0.id, t0.shipping_address_id, t0.billing_address_id from mcustomer t0 where t0.id = ?");
// Additional check to see if the address with the unknown UUID is 'insert' or 'update'
assertSql(sql.get(1)).contains("select t0.id from maddress t0 where t0.id = ?");
assertSql(sql.get(2)).contains("insert into maddress (id, street, city, version) values (?,?,?,?)");
assertSqlBind(sql.get(3));
assertThat(sql.get(5)).contains("update maddress set street=?, city=?, version=? where id=? and version=?");
assertSqlBind(sql.get(6));
assertThat(sql.get(8)).contains("update mcustomer set name=?, version=?, shipping_address_id=?, billing_address_id=? where id=? and version=?");
}
@Test
public void assocOne_onlineIsNull() {
MCustomer c = new MCustomer("Null Address");
c.setBillingAddress(new MAddress("Cow corner", "Mid Wicket"));
DB.save(c);
MCustomer mCustomer = rebuildViaJson(c);
MAddress mAddress = new MAddress("Silly", "Mid Wicket");
mAddress.setId(UUID.randomUUID());
mCustomer.setShippingAddress(mAddress);
MergeOptions options = new MergeOptionsBuilder()
.addPath("shippingAddress")
.addPath("billingAddress")
.setClientGeneratedIds()
.build();
DB.merge(mCustomer, options);
}
private MCustomer rebuildViaJson(MCustomer input) {
String asJson = DB.json().toJson(input);
return DB.json().toBean(MCustomer.class, asJson);
}
@Test
public void whenContacts_isNull_expect_deleteContacts() {
MCustomer mCustomer = partial("cust6", "(id,name,version,shippingAddress(id),billingAddress(id),contacts(*))");
// null contacts ... but in path so this means - delete all contacts
mCustomer.setContacts(null);
MergeOptions options = new MergeOptionsBuilder()
.addPath("contacts")
.build();
LoggedSql.start();
server().merge(mCustomer, options);
List<String> sql = LoggedSql.stop();
if (isPersistBatchOnCascade()) {
assertThat(sql).hasSize(26);
}
assertSql(sql.get(0)).contains("select t0.id, t1.id from mcustomer t0 left join mcontact t1 on t1.customer_id = t0.id where t0.id = ?");
assertSql(sql.get(1)).contains("delete from mcontact_message where contact_id = ?");
assertThat(sql.get(4)).contains("delete from mcontact where id=?");
assertThat(sql.get(25)).contains("update mcustomer set name=?, version=?, shipping_address_id=?, billing_address_id=? where id=? and version=?");
}
@Test
public void whenContacts_isEmpty_expect_deleteContacts() {
MCustomer mCustomer = partial("cust6", "(id,name,version,shippingAddress(id),billingAddress(id),contacts(*))");
// empty contacts ... but in path so this means - delete all contacts
mCustomer.setContacts(new ArrayList<>());
MergeOptions options = new MergeOptionsBuilder()
.addPath("contacts")
.build();
LoggedSql.start();
server().merge(mCustomer, options);
List<String> sql = LoggedSql.stop();
if (isPersistBatchOnCascade()) {
assertThat(sql).hasSize(26);
}
assertSql(sql.get(0)).contains("select t0.id, t1.id from mcustomer t0 left join mcontact t1 on t1.customer_id = t0.id where t0.id = ?");
assertSql(sql.get(1)).contains("delete from mcontact_message where contact_id = ?");
assertThat(sql.get(4)).contains("delete from mcontact where id=?");
assertThat(sql.get(25)).contains("update mcustomer set name=?, version=?, shipping_address_id=?, billing_address_id=? where id=? and version=?");
}
@Test
public void whenContacts_mixed_expect_deleteInsertUpdateContacts() {
MCustomer mCustomer = partial("cust6", "(id,name,version,shippingAddress(id),billingAddress(id),contacts(*))");
List<MContact> contacts = mCustomer.getContacts();
MContact mContact = new MContact("z@a.com", "z", "zed");
mContact.setId(UUID.randomUUID());
contacts.add(mContact);
contacts.get(0).setEmail("a@beta.com");
contacts.get(1).setEmail("b@beta.com");
contacts.remove(4);
contacts.remove(2);
MContact mContactEnd = new MContact("z@z.com", "zx", "zedXtra");
mContactEnd.setId(UUID.randomUUID());
contacts.add(mContactEnd);
MergeOptions options = new MergeOptionsBuilder()
.addPath("contacts")
.build();
LoggedSql.start();
server().merge(mCustomer, options);
List<String> sql = LoggedSql.stop();
if (isPersistBatchOnCascade()) {
assertThat(sql).hasSize(20);
assertSql(sql.get(0)).contains("select t0.id, t1.id from mcustomer t0 left join mcontact t1 on t1.customer_id = t0.id where t0.id = ?");
assertSql(sql.get(1)).contains("delete from mcontact_message where contact_id = ?");
assertThat(sql.get(4)).contains("delete from mcontact where id=?");
assertThat(sql.get(9)).contains("update mcustomer set name=?, version=?, shipping_address_id=?, billing_address_id=? where id=? and version=?");
}
if (isPersistBatchOnCascade()) {
assertThat(sql.get(10)).contains("insert into mcontact");
assertThat(sql.get(11)).contains("-- bind(");
assertThat(sql.get(14)).contains("update mcontact set email=?, first_name=?, last_name=?, version=?, customer_id=? where id=? and version=?");
} else {
assertThat(sql.get(6)).contains("update mcontact set email=?, first_name=?, last_name=?, version=?, customer_id=? where id=? and version=?");
assertThat(sql.get(7)).contains("update mcontact set email=?, first_name=?, last_name=?, version=?, customer_id=? where id=? and version=?");
assertThat(sql.get(10)).contains("insert into mcontact");
assertThat(sql.get(11)).contains("insert into mcontact");
}
}
@Test
public void fullMonty() {
MCustomer cust1 = customer("monty1");
modify(cust1);
MergeOptions options = new MergeOptionsBuilder()
.addPath("billingAddress")
.addPath("shippingAddress")
.addPath("contacts")
.addPath("contacts.messages")
.setClientGeneratedIds()
.setDeletePermanent()
.build();
LoggedSql.start();
server().merge(cust1, options);
List<String> sql = LoggedSql.stop();
if (isPersistBatchOnCascade()) {
assertSql(sql.get(0)).contains("select t0.id, t0.billing_address_id, t0.shipping_address_id, t1.id from mcustomer t0 left join mcontact t1 on t1.customer_id = t0.id where t0.id = ? order by t0.id");
if (isH2() || isHana()) {
// with nested OneToMany .. we need a second query to read the contact message ids
assertSql(sql.get(1)).contains("select t0.contact_id, t0.id from mcontact_message t0 where (t0.contact_id) in (?,?,?,?,?,?,?,?,?,?)");
}
assertSql(sql.get(2)).contains("delete from mcontact_message where contact_id = ?");
assertThat(sql.get(5)).contains("delete from mcontact where id=?");
assertThat(sql.get(6)).contains("delete from mcontact_message where contact_id = ?");
assertThat(sql.get(9)).contains("delete from mcontact where id=?");
assertThat(sql.get(10)).contains("update maddress set street=?, city=?, version=? where id=? and version=?");
assertThat(sql.get(17)).contains("update mcontact set email=?, first_name=?, last_name=?, version=?, customer_id=? where id=? and version=?");
assertSqlBind(sql, 18, 21);
assertThat(sql.get(23)).contains("update mcontact_message set title=?, subject=?, notes=?, version=?, contact_id=? where id=? and version=?");
}
}
private void modify(MCustomer cust) {
List<MContact> contacts = cust.getContacts();
MContact mContact = new MContact("z@a.com", "z", "zed");
mContact.setId(UUID.randomUUID());
contacts.add(mContact);
contacts.get(0).setEmail("a@beta.com");
contacts.get(1).setEmail("b@beta.com");
contacts.remove(4);
contacts.remove(2);
cust.setName(cust.getName()+" modified");
cust.getBillingAddress().setStreet("Short");
MAddress mAddress = new MAddress("Broken", "Dreams");
mAddress.setId(UUID.randomUUID());
cust.setShippingAddress(null);
}
private MCustomer partial(String name, String fetchGraph) {
MCustomer cust1 = buildCustomer(name);
DB.save(cust1);
FetchPath fetchPath = PathProperties.parse(fetchGraph);
String asJson = DB.json().toJson(cust1, fetchPath);
return DB.json().toBean(MCustomer.class, asJson);
}
private MCustomer customer(String name) {
MCustomer cust1 = buildCustomer(name);
DB.save(cust1);
String asJson = DB.json().toJson(cust1);
return DB.json().toBean(MCustomer.class, asJson);
}
private MCustomer buildCustomer(String name) {
MCustomer c = new MCustomer(name);
c.setShippingAddress(new MAddress("Fleet st", "London"));
c.setBillingAddress(new MAddress("Cow corner", "Mid Wicket"));
c.getContacts().add(addContact("a@a.com", "a", "alligator"));
c.getContacts().add(addContact("b@a.com", "b", "beaver"));
c.getContacts().add(addContact("c@a.com", "c", "crow"));
c.getContacts().add(addContact("d@a.com", "d", "dog"));
c.getContacts().add(addContact("e@a.com", "e", "ent"));
c.getContacts().add(addContact("f@a.com", "f", "frog"));
return c;
}
private MContact addContact(String email, String first, String last) {
MContact mContact = new MContact(email, first, last);
int i = 1 + random.nextInt(2);
for (int j = 0; j < i; j++) {
mContact.getMessages().add(new MContactMessage(first+" "+i, last+" "+i));
}
return mContact;
}
}