mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#919 - io.ebean package initial
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package org.tests.json.include;
|
||||
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.config.JsonConfig;
|
||||
import io.ebean.text.json.JsonWriteOptions;
|
||||
import org.tests.model.basic.Order;
|
||||
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<>());
|
||||
|
||||
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<>());
|
||||
|
||||
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 org.tests.json.include;
|
||||
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.config.JsonConfig;
|
||||
import io.ebean.text.json.JsonWriteOptions;
|
||||
import org.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<>());
|
||||
|
||||
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<>());
|
||||
|
||||
JsonWriteOptions options = new JsonWriteOptions();
|
||||
options.setInclude(JsonConfig.Include.NON_EMPTY);
|
||||
|
||||
String asJson = Ebean.json().toJson(bean, options);
|
||||
|
||||
String expectedJson = "{\"id\":99,\"content\":{}}";
|
||||
|
||||
assertEquals(expectedJson, asJson);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.tests.json.include;
|
||||
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.config.JsonConfig;
|
||||
import io.ebean.text.json.JsonWriteOptions;
|
||||
import org.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<>());
|
||||
|
||||
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<>());
|
||||
|
||||
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 org.tests.json.include;
|
||||
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.config.JsonConfig;
|
||||
import io.ebean.text.json.JsonWriteOptions;
|
||||
import org.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,65 @@
|
||||
package org.tests.json.include;
|
||||
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.config.JsonConfig;
|
||||
import io.ebean.text.PathProperties;
|
||||
import io.ebean.text.json.JsonWriteOptions;
|
||||
import org.tests.json.transientproperties.EJsonTransientEntityList;
|
||||
import org.tests.json.transientproperties.EJsonTransientList;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
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<>());
|
||||
|
||||
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<>());
|
||||
|
||||
JsonWriteOptions options = new JsonWriteOptions();
|
||||
options.setInclude(JsonConfig.Include.NON_EMPTY);
|
||||
|
||||
String asJson = Ebean.json().toJson(bean, options);
|
||||
|
||||
String expectedJson = "{\"id\":99}";
|
||||
|
||||
assertEquals(expectedJson, asJson);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToJson_with_transientExcludeFromPathProperties() throws Exception {
|
||||
|
||||
EJsonTransientEntityList bean = new EJsonTransientEntityList();
|
||||
bean.setId(99L);
|
||||
bean.setName("John");
|
||||
|
||||
PathProperties pathProps = PathProperties.parse("id,name");
|
||||
|
||||
String asJson = Ebean.json().toJson(bean, pathProps);
|
||||
|
||||
assertThat(asJson).isEqualTo("{\"id\":99,\"name\":\"John\"}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user