BAEL-14 - renaming test, merging to json module, fixing failing tests inside the json module
This commit is contained in:
parent
2c6f98ff02
commit
daa3a5d944
|
@ -1,6 +0,0 @@
|
||||||
=========
|
|
||||||
|
|
||||||
## Fast-Json
|
|
||||||
|
|
||||||
### Relevant Articles:
|
|
||||||
- [A Guide to FastJson](http://www.baeldung.com/????????)
|
|
|
@ -1,30 +0,0 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
|
|
||||||
<groupId>com.baeldung</groupId>
|
|
||||||
<artifactId>fast-json</artifactId>
|
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
|
|
||||||
<name>fast-json</name>
|
|
||||||
<url>http://maven.apache.org</url>
|
|
||||||
|
|
||||||
<properties>
|
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>junit</groupId>
|
|
||||||
<artifactId>junit</artifactId>
|
|
||||||
<version>4.12</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.alibaba</groupId>
|
|
||||||
<artifactId>fastjson</artifactId>
|
|
||||||
<version>1.2.13</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
</project>
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
=========
|
||||||
|
|
||||||
|
## Fast-Json
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Introduction to JSON Schema in Java](http://www.baeldung.com/introduction-to-json-schema-in-java)
|
||||||
|
- [A Guide to FastJson](http://www.baeldung.com/????????)
|
|
@ -13,6 +13,12 @@
|
||||||
<version>1.3.0</version>
|
<version>1.3.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>1.2.13</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
|
|
|
@ -1,31 +1,32 @@
|
||||||
package org.baeldung.json.schema;
|
package com.baeldung.json.schema;
|
||||||
|
|
||||||
import org.everit.json.schema.Schema;
|
import org.everit.json.schema.Schema;
|
||||||
import org.everit.json.schema.loader.SchemaLoader;
|
import org.everit.json.schema.ValidationException;
|
||||||
|
import org.everit.json.schema.loader.SchemaLoader;
|
||||||
import org.json.JSONObject;
|
|
||||||
import org.json.JSONTokener;
|
import org.json.JSONObject;
|
||||||
import org.junit.Test;
|
import org.json.JSONTokener;
|
||||||
|
import org.junit.Test;
|
||||||
public class JSONSchemaTest {
|
|
||||||
|
public class JSONSchemaTest {
|
||||||
@Test
|
|
||||||
public void givenInvalidInput_whenValidating_thenInvalid() {
|
@Test(expected = ValidationException.class)
|
||||||
|
public void givenInvalidInput_whenValidating_thenInvalid() {
|
||||||
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json")));
|
|
||||||
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_invalid.json")));
|
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json")));
|
||||||
|
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_invalid.json")));
|
||||||
Schema schema = SchemaLoader.load(jsonSchema);
|
|
||||||
schema.validate(jsonSubject);
|
Schema schema = SchemaLoader.load(jsonSchema);
|
||||||
}
|
schema.validate(jsonSubject);
|
||||||
|
}
|
||||||
@Test
|
|
||||||
public void givenValidInput_whenValidating_thenValid() {
|
@Test
|
||||||
|
public void givenValidInput_whenValidating_thenValid() {
|
||||||
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json")));
|
|
||||||
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_valid.json")));
|
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json")));
|
||||||
|
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_valid.json")));
|
||||||
Schema schema = SchemaLoader.load(jsonSchema);
|
|
||||||
schema.validate(jsonSubject);
|
Schema schema = SchemaLoader.load(jsonSchema);
|
||||||
}
|
schema.validate(jsonSubject);
|
||||||
}
|
}
|
||||||
|
}
|
|
@ -1,14 +1,4 @@
|
||||||
package com.baeldung.fast_json;
|
package fast_json;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
import java.text.ParseException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONArray;
|
||||||
|
@ -17,18 +7,30 @@ import com.alibaba.fastjson.serializer.BeanContext;
|
||||||
import com.alibaba.fastjson.serializer.ContextValueFilter;
|
import com.alibaba.fastjson.serializer.ContextValueFilter;
|
||||||
import com.alibaba.fastjson.serializer.NameFilter;
|
import com.alibaba.fastjson.serializer.NameFilter;
|
||||||
import com.alibaba.fastjson.serializer.SerializeConfig;
|
import com.alibaba.fastjson.serializer.SerializeConfig;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
public class FastJsonTests {
|
public class FastJsonTests {
|
||||||
private List<Person> listOfPersons = new ArrayList<Person>();
|
private List<Person> listOfPersons;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
listOfPersons.add(new Person(15, "John", "Doe", new Date()));
|
listOfPersons = new ArrayList<Person>();
|
||||||
listOfPersons.add(new Person(20, "Janette", "Doe", new Date()));
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.set(2016, 6, 24);
|
||||||
|
listOfPersons.add(new Person(15, "John", "Doe", calendar.getTime()));
|
||||||
|
listOfPersons.add(new Person(20, "Janette", "Doe", calendar.getTime()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertObjectToJsonTest() {
|
public void whenJavaList_thanConvertToJsonCorrect() {
|
||||||
String personJsonFormat = JSON.toJSONString(listOfPersons);
|
String personJsonFormat = JSON.toJSONString(listOfPersons);
|
||||||
assertEquals(
|
assertEquals(
|
||||||
personJsonFormat,
|
personJsonFormat,
|
||||||
|
@ -38,16 +40,16 @@ public class FastJsonTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertJsonFormatToObject() {
|
public void whenJson_thanConvertToObjectCorrect() {
|
||||||
String personJsonFormat = JSON.toJSONString(listOfPersons.get(0));
|
String personJsonFormat = JSON.toJSONString(listOfPersons.get(0));
|
||||||
Person newPerson = JSON.parseObject(personJsonFormat, Person.class);
|
Person newPerson = JSON.parseObject(personJsonFormat, Person.class);
|
||||||
assertEquals(newPerson.getAge(), 0); // serialize is set to false for age attribute
|
assertEquals(newPerson.getAge(), 0); // serialize is set to false for age attribute
|
||||||
assertEquals(newPerson.getFirstName(), listOfPersons.get(0).getFirstName());
|
assertEquals(newPerson.getFirstName(), listOfPersons.get(0).getFirstName());
|
||||||
assertEquals(newPerson.getLastName(), listOfPersons.get(0).getLastName());
|
assertEquals(newPerson.getLastName(), listOfPersons.get(0).getLastName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createJsonObjectTest() throws ParseException {
|
public void whenGenerateJson_thanGenerationCorrect() throws ParseException {
|
||||||
JSONArray jsonArray = new JSONArray();
|
JSONArray jsonArray = new JSONArray();
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
JSONObject jsonObject = new JSONObject();
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
@ -64,10 +66,10 @@ public class FastJsonTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertObjectToJsonUsingContextFilterTest() {
|
public void givenContextFilter_whenJavaObject_thanJsonCorrect() {
|
||||||
ContextValueFilter valueFilter = new ContextValueFilter() {
|
ContextValueFilter valueFilter = new ContextValueFilter() {
|
||||||
public Object process(BeanContext context, Object object,
|
public Object process(BeanContext context, Object object,
|
||||||
String name, Object value) {
|
String name, Object value) {
|
||||||
if (name.equals("DATE OF BIRTH")) {
|
if (name.equals("DATE OF BIRTH")) {
|
||||||
return "NOT TO DISCLOSE";
|
return "NOT TO DISCLOSE";
|
||||||
}
|
}
|
||||||
|
@ -82,7 +84,7 @@ public class FastJsonTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertObjectToJsonUsingSerializeConfig() {
|
public void givenSerializeConfig_whenJavaObject_thanJsonCorrect() {
|
||||||
NameFilter formatName = new NameFilter() {
|
NameFilter formatName = new NameFilter() {
|
||||||
public String process(Object object, String name, Object value) {
|
public String process(Object object, String name, Object value) {
|
||||||
return name.toLowerCase().replace(" ", "_");
|
return name.toLowerCase().replace(" ", "_");
|
||||||
|
@ -96,5 +98,7 @@ public class FastJsonTests {
|
||||||
"[{\"first_name\":\"Doe\",\"last_name\":\"John\","
|
"[{\"first_name\":\"Doe\",\"last_name\":\"John\","
|
||||||
+ "\"date_of_birth\":\"2016-07-24\"},{\"first_name\":\"Doe\",\"last_name\":"
|
+ "\"date_of_birth\":\"2016-07-24\"},{\"first_name\":\"Doe\",\"last_name\":"
|
||||||
+ "\"Janette\",\"date_of_birth\":\"2016-07-24\"}]");
|
+ "\"Janette\",\"date_of_birth\":\"2016-07-24\"}]");
|
||||||
|
// resetting custom serializer
|
||||||
|
SerializeConfig.getGlobalInstance().put(Person.class, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,9 +1,9 @@
|
||||||
package com.baeldung.fast_json;
|
package fast_json;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import com.alibaba.fastjson.annotation.JSONField;
|
import com.alibaba.fastjson.annotation.JSONField;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
public class Person {
|
public class Person {
|
||||||
|
|
||||||
@JSONField(name = "AGE", serialize = false, deserialize = false)
|
@JSONField(name = "AGE", serialize = false, deserialize = false)
|
2
pom.xml
2
pom.xml
|
@ -28,7 +28,7 @@
|
||||||
<module>javaxval</module>
|
<module>javaxval</module>
|
||||||
<module>jooq-spring</module>
|
<module>jooq-spring</module>
|
||||||
<module>json-path</module>
|
<module>json-path</module>
|
||||||
<module>fast-json</module>
|
<module>json</module>
|
||||||
<module>mockito</module>
|
<module>mockito</module>
|
||||||
<module>mocks</module>
|
<module>mocks</module>
|
||||||
<module>jee7schedule</module>
|
<module>jee7schedule</module>
|
||||||
|
|
Loading…
Reference in New Issue