#280 - ENH: Add ServerConfig.setJsonInclude(...) ALL, NON_NULL, NON_EMPTY

This commit is contained in:
Robin Bygrave committed 2015-07-20 08:45:46 +12:00
1 parent 160e4d7dd1
commit f6466d3211
55 files changed
+616 -109

No files matched your search

@@ -0,0 +1,53 @@
package com.avaje.tests.json.include;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.config.JsonConfig;
import com.avaje.ebean.text.json.JsonWriteOptions;
import com.avaje.tests.model.basic.Order;
import com.avaje.tests.model.basic.OrderDetail;
import org.junit.Test;
import java.util.ArrayList;
import static org.junit.Assert.assertEquals;
public class TestJsonExcludeEmptyList {
@Test
public void testToBeanToJson_NonNull() throws Exception {
Order bean = new Order();
bean.setId(99);
bean.setStatus(null);
bean.setOrderDate(null);
bean.setDetails(new ArrayList<OrderDetail>());
JsonWriteOptions options = new JsonWriteOptions();
options.setInclude(JsonConfig.Include.NON_NULL);
String asJson = Ebean.json().toJson(bean, options);
String expectedJson = "{\"id\":99,\"details\":[]}";
assertEquals(expectedJson, asJson);
}
@Test
public void testToBeanToJson_NonEmpty() throws Exception {
Order bean = new Order();
bean.setId(99);
bean.setStatus(null);
bean.setOrderDate(null);
bean.setDetails(new ArrayList<OrderDetail>());
JsonWriteOptions options = new JsonWriteOptions();
options.setInclude(JsonConfig.Include.NON_EMPTY);
String asJson = Ebean.json().toJson(bean, options);
String expectedJson = "{\"id\":99}";
assertEquals(expectedJson, asJson);
}
}
@@ -0,0 +1,48 @@
package com.avaje.tests.json.include;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.config.JsonConfig;
import com.avaje.ebean.text.json.JsonWriteOptions;
import com.avaje.tests.model.json.EBasicJsonMap;
import org.junit.Test;
import java.util.LinkedHashMap;
import static org.junit.Assert.assertEquals;
public class TestJsonExcludeEmptyMap {
@Test
public void testToBeanToJson_NonNull() throws Exception {
EBasicJsonMap bean = new EBasicJsonMap();
bean.setId(99L);
bean.setContent(new LinkedHashMap<String, Object>());
JsonWriteOptions options = new JsonWriteOptions();
options.setInclude(JsonConfig.Include.NON_NULL);
String asJson = Ebean.json().toJson(bean, options);
String expectedJson = "{\"id\":99,\"content\":{}}";
assertEquals(expectedJson, asJson);
}
@Test
public void testToBeanToJson_NonEmpty() throws Exception {
EBasicJsonMap bean = new EBasicJsonMap();
bean.setId(99L);
bean.setContent(new LinkedHashMap<String, Object>());
JsonWriteOptions options = new JsonWriteOptions();
options.setInclude(JsonConfig.Include.NON_EMPTY);
String asJson = Ebean.json().toJson(bean, options);
String expectedJson = "{\"id\":99}";
assertEquals(expectedJson, asJson);
}
}
@@ -0,0 +1,49 @@
package com.avaje.tests.json.include;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.config.JsonConfig;
import com.avaje.ebean.text.json.JsonWriteOptions;
import com.avaje.tests.model.basic.Attribute;
import com.avaje.tests.model.basic.AttributeHolder;
import org.junit.Test;
import java.util.LinkedHashSet;
import static org.junit.Assert.assertEquals;
public class TestJsonExcludeEmptySet {
@Test
public void testToBeanToJson_NonNull() throws Exception {
AttributeHolder bean = new AttributeHolder();
bean.setId(99);
bean.setAttributes(new LinkedHashSet<Attribute>());
JsonWriteOptions options = new JsonWriteOptions();
options.setInclude(JsonConfig.Include.NON_NULL);
String asJson = Ebean.json().toJson(bean, options);
String expectedJson = "{\"id\":99,\"attributes\":[]}";
assertEquals(expectedJson, asJson);
}
@Test
public void testToBeanToJson_NonEmpty() throws Exception {
AttributeHolder bean = new AttributeHolder();
bean.setId(99);
bean.setAttributes(new LinkedHashSet<Attribute>());
JsonWriteOptions options = new JsonWriteOptions();
options.setInclude(JsonConfig.Include.NON_EMPTY);
String asJson = Ebean.json().toJson(bean, options);
String expectedJson = "{\"id\":99}";
assertEquals(expectedJson, asJson);
}
}
@@ -0,0 +1,31 @@
package com.avaje.tests.json.include;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.config.JsonConfig;
import com.avaje.ebean.text.json.JsonWriteOptions;
import com.avaje.tests.json.transientproperties.EJsonTransientObject;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestJsonExcludeNull {
@Test
public void testToBeanToJson() throws Exception {
EJsonTransientObject bean = new EJsonTransientObject();
bean.setId(99L);
bean.setName(null);
JsonWriteOptions options = new JsonWriteOptions();
options.setInclude(JsonConfig.Include.NON_NULL);
String asJson = Ebean.json().toJson(bean, options);
String expectedJson = "{\"id\":99}";
assertEquals(expectedJson, asJson);
}
}
@@ -0,0 +1,48 @@
package com.avaje.tests.json.include;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.config.JsonConfig;
import com.avaje.ebean.text.json.JsonWriteOptions;
import com.avaje.tests.json.transientproperties.EJsonTransientList;
import org.junit.Test;
import java.util.ArrayList;
import static org.junit.Assert.assertEquals;
public class TestJsonExcludeTransientEmptyList {
@Test
public void testToBeanToJson_NonNull() throws Exception {
EJsonTransientList bean = new EJsonTransientList();
bean.setId(99L);
bean.setFileNames(new ArrayList<String>());
JsonWriteOptions options = new JsonWriteOptions();
options.setInclude(JsonConfig.Include.NON_NULL);
String asJson = Ebean.json().toJson(bean, options);
String expectedJson = "{\"id\":99,\"fileNames\":[]}";
assertEquals(expectedJson, asJson);
}
@Test
public void testToBeanToJson_NonEmpty() throws Exception {
EJsonTransientList bean = new EJsonTransientList();
bean.setId(99L);
bean.setFileNames(new ArrayList<String>());
JsonWriteOptions options = new JsonWriteOptions();
options.setInclude(JsonConfig.Include.NON_EMPTY);
String asJson = Ebean.json().toJson(bean, options);
String expectedJson = "{\"id\":99}";
assertEquals(expectedJson, asJson);
}
}