FIX: mutationDetection = SOURCE works also for special json types

This commit is contained in:
Roland Praml
2021-08-13 10:47:14 +02:00
parent a17683c549
commit 965c1e803f
8 changed files with 446 additions and 117 deletions
@@ -11,19 +11,19 @@ public class ScalarTypeJsonListTest extends BasePlatformArrayTypeFactoryTest {
@Test
public void typeFor_expect_nullToEmpty_when_postgresNonNull() throws SQLException {
assertBindNullTo_PGObjectEmpty(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSONB, DocPropertyType.OBJECT, false));
assertBindNullTo_PGObjectEmpty(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSON, DocPropertyType.OBJECT, false));
assertBindNullTo_PGObjectEmpty(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSONB, DocPropertyType.OBJECT, false, false));
assertBindNullTo_PGObjectEmpty(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSON, DocPropertyType.OBJECT, false, false));
assertBindNullTo_EmptyString(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSONVarchar, DocPropertyType.OBJECT, false));
assertBindNullTo_EmptyString(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSONVarchar, DocPropertyType.OBJECT, false, false));
}
@Test
public void typeFor_expect_nullToNull_when_nullable() throws SQLException {
assertBindNullTo_PGObjectNull(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSONB, DocPropertyType.OBJECT, true));
assertBindNullTo_PGObjectNull(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSON, DocPropertyType.OBJECT, true));
assertBindNullTo_PGObjectNull(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSONB, DocPropertyType.OBJECT, true, false));
assertBindNullTo_PGObjectNull(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSON, DocPropertyType.OBJECT, true, false));
assertBindNullTo_Null(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSONVarchar, DocPropertyType.OBJECT, true));
assertBindNullTo_Null(ScalarTypeJsonList.typeFor(true, ExtraDbTypes.JSONVarchar, DocPropertyType.OBJECT, true, false));
}
}
@@ -0,0 +1,120 @@
package org.tests.json;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebean.ValuePair;
import io.ebean.annotation.ForPlatform;
import io.ebean.annotation.Platform;
import io.ebean.text.TextException;
import org.assertj.core.api.SoftAssertions;
import org.ebeantest.LoggedSqlCollector;
import org.junit.Ignore;
import org.junit.Test;
import org.tests.model.json.EBasicJsonList;
import org.tests.model.json.EBasicOldValue;
import org.tests.model.json.PlainBean;
import javax.persistence.PersistenceException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class TestOldValue extends BaseTestCase {
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testDbJsonOldValue() throws Exception {
EBasicOldValue bean = new EBasicOldValue();
bean.getStringList().add("sl1");
bean.getStringSet().add("ss1");
bean.getObjectMap().put("sk1","sm1");
bean.getLongList().add(1L);
bean.getLongSet().add(1001L);
bean.getLongMap().put("lk1",2001L);
bean.getIntList().add(2);
bean.getIntSet().add(1002);
bean.getIntMap().put("ik1",2002);
DB.save(bean);
bean = DB.find(EBasicOldValue.class, bean.getId());
bean.getStringList().add("sl2");
bean.getStringSet().add("ss2");
bean.getObjectMap().put("sk2","sm2");
bean.getLongList().add(5L);
bean.getLongSet().add(1005L);
bean.getLongMap().put("lk2",2005L);
bean.getIntList().add(6);
bean.getIntSet().add(1006);
bean.getIntMap().put("ik2",2006);
Map<String, ValuePair> dirty = DB.getBeanState(bean).getDirtyValues();
SoftAssertions softly = new SoftAssertions();
softly.assertThat(dirty).hasSize(9);
softly.assertThat((List)dirty.get("stringList").getOldValue()).containsExactly("sl1");
softly.assertThat((List)dirty.get("stringList").getNewValue()).containsExactly("sl1", "sl2");
softly.assertThat((List)dirty.get("longList").getOldValue()).containsExactly(1L);
softly.assertThat((List)dirty.get("longList").getNewValue()).containsExactly(1L, 5L);
softly.assertThat((List)dirty.get("intList").getOldValue()).containsExactly(2);
softly.assertThat((List)dirty.get("intList").getNewValue()).containsExactly(2, 6);
softly.assertThat((Set)dirty.get("stringSet").getOldValue()).containsExactly("ss1");
softly.assertThat((Set)dirty.get("stringSet").getNewValue()).containsExactly("ss1", "ss2");
softly.assertThat((Set)dirty.get("longSet").getOldValue()).containsExactly(1001L);
softly.assertThat((Set)dirty.get("longSet").getNewValue()).containsExactly(1001L, 1005L);
softly.assertThat((Set)dirty.get("intSet").getOldValue()).containsExactly(1002);
softly.assertThat((Set)dirty.get("intSet").getNewValue()).containsExactly(1002, 1006);
softly.assertThat((Map)dirty.get("objectMap").getOldValue()).containsEntry("sk1","sm1").hasSize(1);
softly.assertThat((Map)dirty.get("objectMap").getNewValue()).containsEntry("sk1","sm1").containsEntry("sk2","sm2").hasSize(2);
softly.assertThat((Map)dirty.get("longMap").getOldValue()).containsEntry("lk1",2001L).hasSize(1);
softly.assertThat((Map)dirty.get("longMap").getNewValue()).containsEntry("lk1",2001L).containsEntry("lk2",2005L).hasSize(2);
softly.assertThat((Map)dirty.get("intMap").getOldValue()).containsEntry("ik1",2002).hasSize(1);
softly.assertThat((Map)dirty.get("intMap").getNewValue()).containsEntry("ik1",2002).containsEntry("ik2",2006).hasSize(2);
softly.assertAll();
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
@Ignore("Old value detection does not work for @DbArray")
public void testDbArrayOldValue() throws Exception {
EBasicOldValue bean = new EBasicOldValue();
bean.getStringArr().add("sa1");
DB.save(bean);
bean = DB.find(EBasicOldValue.class, bean.getId());
bean.getStringArr().add("sa2");
Map<String, ValuePair> dirty = DB.getBeanState(bean).getDirtyValues();
SoftAssertions softly = new SoftAssertions();
softly.assertThat(dirty).hasSize(1);
softly.assertThat((List)dirty.get("stringArr").getOldValue()).containsExactly("sa1");
softly.assertThat((List)dirty.get("stringArr").getNewValue()).containsExactly("sa1", "sa2");
softly.assertAll();
}
}
@@ -0,0 +1,146 @@
package org.tests.model.json;
import io.ebean.annotation.DbArray;
import io.ebean.annotation.DbJson;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.*;
import static io.ebean.annotation.MutationDetection.SOURCE;
@Entity
public class EBasicOldValue {
@Id
Long id;
String name;
@DbJson(mutationDetection = SOURCE)
Set<String> stringSet = new LinkedHashSet<>();
@DbJson(mutationDetection = SOURCE)
Set<Long> longSet = new LinkedHashSet<>();
@DbJson(mutationDetection = SOURCE)
Set<Integer> intSet = new LinkedHashSet<>();
@DbJson(mutationDetection = SOURCE)
List<String> stringList = new ArrayList<>();
@DbJson(mutationDetection = SOURCE)
List<Long> longList = new ArrayList<>();
@DbJson(mutationDetection = SOURCE)
List<Integer> intList = new ArrayList<>();
@DbJson(mutationDetection = SOURCE)
Map<String, Object> objectMap = new LinkedHashMap<>();
@DbJson(mutationDetection = SOURCE)
Map<String, Long> longMap = new LinkedHashMap<>();
@DbJson(mutationDetection = SOURCE)
Map<String, Integer> intMap = new LinkedHashMap<>();
@DbArray()
List<String> stringArr = new ArrayList<>();
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 Set<String> getStringSet() {
return stringSet;
}
public void setStringSet(Set<String> stringSet) {
this.stringSet = stringSet;
}
public Set<Long> getLongSet() {
return longSet;
}
public void setLongSet(Set<Long> longSet) {
this.longSet = longSet;
}
public Set<Integer> getIntSet() {
return intSet;
}
public void setIntSet(Set<Integer> intSet) {
this.intSet = intSet;
}
public List<String> getStringList() {
return stringList;
}
public void setStringList(List<String> stringList) {
this.stringList = stringList;
}
public List<Long> getLongList() {
return longList;
}
public void setLongList(List<Long> longList) {
this.longList = longList;
}
public List<Integer> getIntList() {
return intList;
}
public void setIntList(List<Integer> intList) {
this.intList = intList;
}
public Map<String, Object> getObjectMap() {
return objectMap;
}
public void setObjectMap(Map<String, Object> objectMap) {
this.objectMap = objectMap;
}
public Map<String, Long> getLongMap() {
return longMap;
}
public void setLongMap(Map<String, Long> longMap) {
this.longMap = longMap;
}
public Map<String, Integer> getIntMap() {
return intMap;
}
public void setIntMap(Map<String, Integer> intMap) {
this.intMap = intMap;
}
public List<String> getStringArr() {
return stringArr;
}
public void setStringArr(List<String> stringArr) {
this.stringArr = stringArr;
}
}