Refactor move tests from ebean-core to ebean-test

This commit is contained in:
rbygrave committed 2021-09-08 22:49:23 +12:00
1 parent 0b00a14fd3
commit eab56d323b
1650 files changed
+185 -147

No files matched your search

@@ -0,0 +1,33 @@
package org.tests.merge;
import javax.persistence.Entity;
@Entity
public class MAddress extends MBase {
private String street;
private String city;
public MAddress(String street, String city) {
this.street = street;
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
@@ -0,0 +1,33 @@
package org.tests.merge;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
import java.util.UUID;
@MappedSuperclass
public class MBase {
@Id @GeneratedValue
private UUID id;
@Version
private long version;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
}
@@ -0,0 +1,67 @@
package org.tests.merge;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import java.util.List;
@Entity
public class MContact extends MBase {
String email;
String firstName;
String lastName;
@ManyToOne
MCustomer customer;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "contact")
List<MContactMessage> messages;
public MContact(String email, String firstName, String lastName) {
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
}
public MCustomer getCustomer() {
return customer;
}
public void setCustomer(MCustomer customer) {
this.customer = customer;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public List<MContactMessage> getMessages() {
return messages;
}
public void setMessages(List<MContactMessage> messages) {
this.messages = messages;
}
}
@@ -0,0 +1,57 @@
package org.tests.merge;
import io.ebean.annotation.NotNull;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
@Entity
public class MContactMessage extends MBase {
private String title;
private String subject;
private String notes;
@NotNull
@ManyToOne
private MContact contact;
public MContactMessage(String title, String subject) {
this.title = title;
this.subject = subject;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public MContact getContact() {
return contact;
}
public void setContact(MContact contact) {
this.contact = contact;
}
}
@@ -0,0 +1,68 @@
package org.tests.merge;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import java.util.List;
@Entity
public class MCustomer extends MBase {
private String name;
private String notes;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer")
private List<MContact> contacts;
@ManyToOne(cascade = CascadeType.ALL)
private MAddress shippingAddress;
@ManyToOne(cascade = CascadeType.ALL)
private MAddress billingAddress;
public MCustomer(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public List<MContact> getContacts() {
return contacts;
}
public void setContacts(List<MContact> contacts) {
this.contacts = contacts;
}
public MAddress getShippingAddress() {
return shippingAddress;
}
public void setShippingAddress(MAddress shippingAddress) {
this.shippingAddress = shippingAddress;
}
public MAddress getBillingAddress() {
return billingAddress;
}
public void setBillingAddress(MAddress billingAddress) {
this.billingAddress = billingAddress;
}
}
@@ -0,0 +1,38 @@
package org.tests.merge;
import io.ebean.annotation.Identity;
import javax.persistence.Entity;
import javax.persistence.Id;
import static io.ebean.annotation.IdentityGenerated.BY_DEFAULT;
@Entity
public class MGroup {
@Id @Identity(generated = BY_DEFAULT)
private long id;
private String name;
public MGroup(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@@ -0,0 +1,59 @@
package org.tests.merge;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Version;
import java.util.ArrayList;
import java.util.List;
@Entity
public class MMachine {
@Id
private long id;
private String name;
@ManyToMany
private List<MGroup> groups = new ArrayList<>();
@Version
private long version;
public MMachine(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
public List<MGroup> getGroups() {
return groups;
}
public void setGroups(List<MGroup> groups) {
this.groups = groups;
}
}
@@ -0,0 +1,131 @@
package org.tests.merge;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebean.MergeOptions;
import io.ebean.MergeOptionsBuilder;
import org.ebeantest.LoggedSqlCollector;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.UUOne;
import org.tests.model.basic.UUTwo;
import java.util.List;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
public class TestMergeBasic extends BaseTestCase {
private UUOne rebuildViaJson(UUOne input) {
String asJson = DB.json().toJson(input);
return DB.json().toBean(UUOne.class, asJson);
}
private UUOne build() {
UUOne uuOne = buildGraph();
DB.save(uuOne);
return rebuildViaJson(uuOne);
}
@Test
public void with_setClientGeneratedIds_expect_noDifferenceWithToMany() {
UUOne one = build();
one.setDescription("mod");
List<UUTwo> comments = one.getComments();
comments.remove(0);
comments.add(0, new UUTwo("twoMore", UUID.randomUUID()));
comments.add(new UUTwo("twoExtra", UUID.randomUUID()));
MergeOptions options = new MergeOptionsBuilder()
.addPath("comments")
.setClientGeneratedIds()
.build();
LoggedSqlCollector.start();
DB.merge(one, options);
List<String> sql = LoggedSqlCollector.stop();
// fetch the Ids ... used to identity inserts, updates and deletes
assertSql(sql.get(0)).contains("select t0.id, t1.id from uuone t0 left join uutwo t1 on t1.master_id = t0.id where t0.id = ?");
// deletes of Ids that are no longer in the graph
assertSql(sql.get(1)).contains("delete from uutwo where id=?");
// cascade persist ... master
assertSql(sql.get(2)).contains("update uuone set name=?, description=?, version=? where id=? and version=?");
// persist children ...
if (isPersistBatchOnCascade()) {
assertThat(sql.get(3)).contains("insert into uutwo (id, name, notes, version, master_id) values (?,?,?,?,?)");
assertThat(sql.get(4)).contains("-- bind(");
assertThat(sql.get(5)).contains("-- bind(");
assertThat(sql.get(6)).contains("update uutwo set name=?, notes=?, version=?, master_id=? where id=? and version=?");
assertThat(sql.get(7)).contains("-- bind(");
assertThat(sql.get(8)).contains("-- bind(");
assertThat(sql.get(9)).contains("-- bind(");
} else {
assertThat(sql.get(3)).contains("insert into uutwo (id, name, notes, version, master_id) values (?,?,?,?,?);");
assertThat(sql.get(4)).contains("update uutwo set name=?, notes=?, version=?, master_id=? where id=? and version=?");
assertThat(sql.get(5)).contains("update uutwo set name=?, notes=?, version=?, master_id=? where id=? and version=?");
assertThat(sql.get(6)).contains("update uutwo set name=?, notes=?, version=?, master_id=? where id=? and version=?");
assertThat(sql.get(7)).contains("insert into uutwo (id, name, notes, version, master_id) values (?,?,?,?,?);");
}
}
@Test
public void test() {
UUOne one = build();
one.setDescription("mod");
List<UUTwo> comments = one.getComments();
comments.remove(0);
comments.add(0, new UUTwo("twoMore", UUID.randomUUID()));
comments.add(new UUTwo("twoExtra", UUID.randomUUID()));
MergeOptions options = new MergeOptionsBuilder()
.addPath("comments")
.build();
LoggedSqlCollector.start();
DB.merge(one, options);
List<String> sql = LoggedSqlCollector.stop();
// fetch the Ids ... used to identity inserts, updates and deletes
assertSql(sql.get(0)).contains("select t0.id, t1.id from uuone t0 left join uutwo t1 on t1.master_id = t0.id where t0.id = ?");
// deletes of Ids that are no longer in the graph
assertSql(sql.get(1)).contains("delete from uutwo where id=?");
// cascade persist ... master
assertSql(sql.get(2)).contains("update uuone set name=?, description=?, version=? where id=? and version=?");
// persist children ...
if (isPersistBatchOnCascade()) {
assertThat(sql.get(3)).contains("insert into uutwo (id, name, notes, version, master_id) values (?,?,?,?,?)");
assertThat(sql.get(4)).contains("-- bind(");
assertThat(sql.get(5)).contains("-- bind(");
assertThat(sql.get(6)).contains("update uutwo set name=?, notes=?, version=?, master_id=? where id=? and version=?");
assertThat(sql.get(9)).contains("-- bind(");
} else {
assertThat(sql.get(3)).contains("insert into uutwo (id, name, notes, version, master_id) values (?,?,?,?,?);");
assertThat(sql.get(6)).contains("update uutwo set name=?, notes=?, version=?, master_id=? where id=? and version=?");
}
}
private UUOne buildGraph() {
UUOne one = new UUOne("one1", UUID.randomUUID());
for (int i = 1; i < 5; i++) {
UUTwo two = new UUTwo("two" + i, UUID.randomUUID());
one.getComments().add(two);
}
return one;
}
}
@@ -0,0 +1,405 @@
package org.tests.merge;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebean.FetchPath;
import io.ebean.MergeOptions;
import io.ebean.MergeOptionsBuilder;
import io.ebean.text.PathProperties;
import org.ebeantest.LoggedSqlCollector;
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");
LoggedSqlCollector.start();
DB.merge(mCustomer);
List<String> sql = LoggedSqlCollector.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();
LoggedSqlCollector.start();
DB.merge(mCustomer, options);
List<String> sql = LoggedSqlCollector.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();
LoggedSqlCollector.start();
server().merge(mCustomer, options);
List<String> sql = LoggedSqlCollector.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();
LoggedSqlCollector.start();
server().merge(mCustomer, options);
List<String> sql = LoggedSqlCollector.stop();
assertThat(sql).hasSize(5);
assertSql(sql.get(0)).contains("select t0.id, t2.id, t1.id from mcustomer t0 left join maddress t2 on t2.id = t0.shipping_address_id left join maddress t1 on t1.id = t0.billing_address_id 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(4)).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();
LoggedSqlCollector.start();
server().merge(mCustomer, options);
List<String> sql = LoggedSqlCollector.stop();
assertThat(sql).hasSize(6);
assertSql(sql.get(0)).contains("select t0.id, t2.id, t1.id from mcustomer t0 left join maddress t2 on t2.id = t0.shipping_address_id left join maddress t1 on t1.id = t0.billing_address_id where t0.id = ?");
assertSql(sql.get(1)).contains("insert into maddress (id, street, city, version) values (?,?,?,?)");
assertSqlBind(sql.get(2));
assertThat(sql.get(3)).contains("update maddress set street=?, city=?, version=? where id=? and version=?");
assertSqlBind(sql.get(4));
assertThat(sql.get(5)).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();
LoggedSqlCollector.start();
server().merge(mCustomer, options);
List<String> sql = LoggedSqlCollector.stop();
assertThat(sql).hasSize(7);
assertSql(sql.get(0)).contains("select t0.id, t2.id, t1.id from mcustomer t0 left join maddress t2 on t2.id = t0.shipping_address_id left join maddress t1 on t1.id = t0.billing_address_id 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(4)).contains("update maddress set street=?, city=?, version=? where id=? and version=?");
assertSqlBind(sql.get(5));
assertThat(sql.get(6)).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();
LoggedSqlCollector.start();
server().merge(mCustomer, options);
List<String> sql = LoggedSqlCollector.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(3)).contains("delete from mcontact where id=?");
assertThat(sql.get(19)).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();
LoggedSqlCollector.start();
server().merge(mCustomer, options);
List<String> sql = LoggedSqlCollector.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(3)).contains("delete from mcontact where id=?");
assertThat(sql.get(19)).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();
LoggedSqlCollector.start();
server().merge(mCustomer, options);
List<String> sql = LoggedSqlCollector.stop();
if (isPersistBatchOnCascade()) {
assertThat(sql).hasSize(16);
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(3)).contains("delete from mcontact where id=?");
assertThat(sql.get(7)).contains("update mcustomer set name=?, version=?, shipping_address_id=?, billing_address_id=? where id=? and version=?");
}
if (isPersistBatchOnCascade()) {
assertThat(sql.get(8)).contains("insert into mcontact");
assertThat(sql.get(9)).contains("-- bind(");
assertThat(sql.get(11)).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();
LoggedSqlCollector.start();
server().merge(cust1, options);
List<String> sql = LoggedSqlCollector.stop();
if (isPersistBatchOnCascade()) {
assertSql(sql.get(0)).contains("select t0.id, t3.id, t1.id, t2.id from mcustomer t0 left join maddress t3 on t3.id = t0.shipping_address_id left join maddress t1 on t1.id = t0.billing_address_id left join mcontact t2 on t2.customer_id = t0.id where 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(4)).contains("delete from mcontact where id=?");
assertThat(sql.get(5)).contains("delete from mcontact_message where contact_id = ?");
assertThat(sql.get(7)).contains("delete from mcontact where id=?");
assertThat(sql.get(8)).contains("update maddress set street=?, city=?, version=? where id=? and version=?");
assertThat(sql.get(13)).contains("update mcontact set email=?, first_name=?, last_name=?, version=?, customer_id=? where id=? and version=?");
assertSqlBind(sql, 14, 17);
assertThat(sql.get(18)).contains("update mcontact_message set title=?, subject=?, notes=?, version=?, contact_id=? where id=? and version=?");
assertSqlBind(sql, 19, 22);
}
}
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;
}
}
@@ -0,0 +1,78 @@
package org.tests.merge;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebean.MergeOptions;
import io.ebean.MergeOptionsBuilder;
import org.ebeantest.LoggedSqlCollector;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class TestMergeM2M extends BaseTestCase {
@Test
public void m2mMerge() {
MGroup group1 = new MGroup(1, "gone");
MGroup group2 = new MGroup(2, "gtwo");
MGroup group3 = new MGroup(3, "gthree");
MGroup group4 = new MGroup(4, "gfour");
MGroup group5 = new MGroup(5, "gfive");
DB.save(group1);
DB.save(group2);
DB.save(group3);
DB.save(group4);
DB.save(group5);
MMachine m0 = new MMachine("mac1");
m0.getGroups().add(group1);
m0.getGroups().add(group2);
m0.getGroups().add(group3);
MergeOptions options = new MergeOptionsBuilder().addPath("groups").build();
LoggedSqlCollector.start();
DB.merge(m0, options);
List<String> sql = LoggedSqlCollector.current();
if (isPersistBatchOnCascade()) {
assertThat(sql).hasSize(6);
assertSql(sql.get(0)).contains("select");
assertSql(sql.get(1)).contains("insert into mmachine");
assertSql(sql.get(2)).contains("insert into mmachine_mgroup");
assertSqlBind(sql, 3, 5);
} else {
assertThat(sql).hasSize(5);
assertSql(sql.get(0)).contains("select");
assertSql(sql.get(1)).contains("insert into mmachine");
assertSql(sql.get(2)).contains("insert into mmachine_mgroup");
assertThat(sql.get(3)).contains("insert into mmachine_mgroup");
assertThat(sql.get(4)).contains("insert into mmachine_mgroup");
}
MMachine m1 = new MMachine("mac1");
m1.setId(m0.getId());
m1.setName("mac1-mod");
m1.getGroups().remove(group2);
m1.getGroups().remove(group3);
m1.getGroups().add(group4);
m1.getGroups().add(group5);
DB.merge(m1, options);
sql = LoggedSqlCollector.current();
assertThat(sql).hasSize(9);
assertSql(sql.get(0)).contains("select");
assertSql(sql.get(1)).contains("delete from mmachine_mgroup");
assertSqlBind(sql, 2, 4);
assertThat(sql.get(5)).contains("insert into mmachine_mgroup");
assertSqlBind(sql, 6, 7);
assertThat(sql.get(8)).contains("update mmachine");
}
}