BAEL-14 - renaming test, merging to json module, fixing failing tests inside the json module

This commit is contained in:
Slavisa Baeldung 2016-07-25 14:31:25 +02:00
parent 2c6f98ff02
commit daa3a5d944
8 changed files with 75 additions and 93 deletions

View File

@ -1,6 +0,0 @@
=========
## Fast-Json
### Relevant Articles:
- [A Guide to FastJson](http://www.baeldung.com/????????)

View File

@ -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>

7
json/README.md Normal file
View File

@ -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/????????)

View File

@ -13,6 +13,12 @@
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.13</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -1,31 +1,32 @@
package org.baeldung.json.schema;
import org.everit.json.schema.Schema;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.Test;
public class JSONSchemaTest {
@Test
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")));
Schema schema = SchemaLoader.load(jsonSchema);
schema.validate(jsonSubject);
}
@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")));
Schema schema = SchemaLoader.load(jsonSchema);
schema.validate(jsonSubject);
}
}
package com.baeldung.json.schema;
import org.everit.json.schema.Schema;
import org.everit.json.schema.ValidationException;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.Test;
public class JSONSchemaTest {
@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")));
Schema schema = SchemaLoader.load(jsonSchema);
schema.validate(jsonSubject);
}
@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")));
Schema schema = SchemaLoader.load(jsonSchema);
schema.validate(jsonSubject);
}
}

View File

@ -1,14 +1,4 @@
package com.baeldung.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;
package fast_json;
import com.alibaba.fastjson.JSON;
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.NameFilter;
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 {
private List<Person> listOfPersons = new ArrayList<Person>();
private List<Person> listOfPersons;
@Before
public void setUp() {
listOfPersons.add(new Person(15, "John", "Doe", new Date()));
listOfPersons.add(new Person(20, "Janette", "Doe", new Date()));
listOfPersons = new ArrayList<Person>();
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
public void convertObjectToJsonTest() {
public void whenJavaList_thanConvertToJsonCorrect() {
String personJsonFormat = JSON.toJSONString(listOfPersons);
assertEquals(
personJsonFormat,
@ -38,16 +40,16 @@ public class FastJsonTests {
}
@Test
public void convertJsonFormatToObject() {
public void whenJson_thanConvertToObjectCorrect() {
String personJsonFormat = JSON.toJSONString(listOfPersons.get(0));
Person newPerson = JSON.parseObject(personJsonFormat, Person.class);
assertEquals(newPerson.getAge(), 0); // serialize is set to false for age attribute
assertEquals(newPerson.getFirstName(), listOfPersons.get(0).getFirstName());
assertEquals(newPerson.getLastName(), listOfPersons.get(0).getLastName());
assertEquals(newPerson.getLastName(), listOfPersons.get(0).getLastName());
}
@Test
public void createJsonObjectTest() throws ParseException {
public void whenGenerateJson_thanGenerationCorrect() throws ParseException {
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < 2; i++) {
JSONObject jsonObject = new JSONObject();
@ -64,10 +66,10 @@ public class FastJsonTests {
}
@Test
public void convertObjectToJsonUsingContextFilterTest() {
public void givenContextFilter_whenJavaObject_thanJsonCorrect() {
ContextValueFilter valueFilter = new ContextValueFilter() {
public Object process(BeanContext context, Object object,
String name, Object value) {
String name, Object value) {
if (name.equals("DATE OF BIRTH")) {
return "NOT TO DISCLOSE";
}
@ -82,7 +84,7 @@ public class FastJsonTests {
}
@Test
public void convertObjectToJsonUsingSerializeConfig() {
public void givenSerializeConfig_whenJavaObject_thanJsonCorrect() {
NameFilter formatName = new NameFilter() {
public String process(Object object, String name, Object value) {
return name.toLowerCase().replace(" ", "_");
@ -96,5 +98,7 @@ public class FastJsonTests {
"[{\"first_name\":\"Doe\",\"last_name\":\"John\","
+ "\"date_of_birth\":\"2016-07-24\"},{\"first_name\":\"Doe\",\"last_name\":"
+ "\"Janette\",\"date_of_birth\":\"2016-07-24\"}]");
// resetting custom serializer
SerializeConfig.getGlobalInstance().put(Person.class, null);
}
}

View File

@ -1,9 +1,9 @@
package com.baeldung.fast_json;
import java.util.Date;
package fast_json;
import com.alibaba.fastjson.annotation.JSONField;
import java.util.Date;
public class Person {
@JSONField(name = "AGE", serialize = false, deserialize = false)

View File

@ -28,7 +28,7 @@
<module>javaxval</module>
<module>jooq-spring</module>
<module>json-path</module>
<module>fast-json</module>
<module>json</module>
<module>mockito</module>
<module>mocks</module>
<module>jee7schedule</module>