FIX: DB.json().toBean(target) can update existing lists (#85)

* FIX: DB.json().toBean(target) can update existing lists

* remove sys.print, typo

---------

Co-authored-by: Roland Praml <roland.praml@foconis.de>
Co-authored-by: Juri Skrobko <juri.skrobko@foconis.de>
This commit is contained in:
Roland Praml
2023-03-17 11:04:53 +01:00
committed by Juri Skrobko
co-authored by Roland Praml Juri Skrobko
parent e23bbf5f94
commit 3d7bebd244
9 changed files with 155 additions and 36 deletions
@@ -12,14 +12,19 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.Address;
import org.tests.model.basic.Contact;
import org.tests.model.basic.ContactNote;
import org.tests.model.basic.Customer;
import java.io.IOException;
import java.io.StringReader;
import java.util.Comparator;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestJsonBeanDescriptorParse extends BaseTestCase {
@@ -32,6 +37,17 @@ public class TestJsonBeanDescriptorParse extends BaseTestCase {
address.setLine1("foo");
DB.save(address);
customer.setBillingAddress(address);
Contact alfred = new Contact("Alfred", "P");
alfred.setId(789); // set some deterministic id
ContactNote note = new ContactNote("Drinks", "Order 100l beer");
note.setId(890);
alfred.getNotes().add(note);
customer.getContacts().add(alfred);
Contact anton = new Contact("Anton", "P");
anton.setId(790);
anton.getNotes().add(new ContactNote("Equipment", "Organize barbecue"));
customer.getContacts().add(anton);
DB.save(customer);
}
@@ -59,6 +75,38 @@ public class TestJsonBeanDescriptorParse extends BaseTestCase {
assertThat(beanState.dirtyValues()).isEmpty();
}
@Test
public void testJsonManyUpdate() throws IOException {
Customer customer = DB.find(Customer.class, 234);
String json =
"{\"contacts\": [ "
+ " {\"id\": 789, \"lastName\": \"Praml\", \"notes\": ["
+ " {\"id\": 890,\"title\": \"Beer\",\"note\": \"Order 200l beer\",\"version\": 17},"
+ " {\"title\": \"Food\",\"note\": \"Order 20 steaks\"}"
+ " ]},"
+ " {\"id\": 790, \"firstName\": \"Anton\", \"lastName\": null, \"notes\" : null } "
+ "]}";
DB.json().toBean(customer, json);
DB.save(customer);
customer = DB.find(Customer.class, 234);
assertThat(customer.getContacts()).hasSize(2);
customer.getContacts().sort(Comparator.comparing(Contact::getId));
Contact contact = customer.getContacts().get(0);
assertThat(contact.getFirstName()).isEqualTo("Alfred");
assertThat(contact.getLastName()).isEqualTo("Praml");
assertThat(contact.getNotes()).hasSize(2).extracting(ContactNote::getNote)
.containsExactlyInAnyOrder("Order 200l beer", "Order 20 steaks");
contact = customer.getContacts().get(1);
assertThat(contact.getFirstName()).isEqualTo("Anton");
assertThat(contact.getLastName()).isEqualTo(null);
assertThat(contact.getNotes()).hasSize(1);
}
@Test
public void testJsonUpdate() throws IOException {
Customer customer = DB.find(Customer.class, 234);