From daa3a5d944e1426e5f006ce4d83ab84cfe4f835a Mon Sep 17 00:00:00 2001 From: Slavisa Baeldung Date: Mon, 25 Jul 2016 14:31:25 +0200 Subject: [PATCH] BAEL-14 - renaming test, merging to json module, fixing failing tests inside the json module --- fast-json/README.md | 6 -- fast-json/pom.xml | 30 --------- json/README.md | 7 +++ json/pom.xml | 6 ++ .../baeldung/json/schema/JSONSchemaTest.java | 63 ++++++++++--------- .../test/java}/fast_json/FastJsonTests.java | 48 +++++++------- .../src/test/java}/fast_json/Person.java | 6 +- pom.xml | 2 +- 8 files changed, 75 insertions(+), 93 deletions(-) delete mode 100644 fast-json/README.md delete mode 100644 fast-json/pom.xml create mode 100644 json/README.md rename json/src/test/java/{org => com}/baeldung/json/schema/JSONSchemaTest.java (88%) rename {fast-json/src/test/java/com/baeldung => json/src/test/java}/fast_json/FastJsonTests.java (77%) rename {fast-json/src/main/java/com/baeldung => json/src/test/java}/fast_json/Person.java (97%) diff --git a/fast-json/README.md b/fast-json/README.md deleted file mode 100644 index 3bffe81f61..0000000000 --- a/fast-json/README.md +++ /dev/null @@ -1,6 +0,0 @@ -========= - -## Fast-Json - -### Relevant Articles: -- [A Guide to FastJson](http://www.baeldung.com/????????) diff --git a/fast-json/pom.xml b/fast-json/pom.xml deleted file mode 100644 index b7627e4135..0000000000 --- a/fast-json/pom.xml +++ /dev/null @@ -1,30 +0,0 @@ - - 4.0.0 - - com.baeldung - fast-json - 0.0.1-SNAPSHOT - jar - - fast-json - http://maven.apache.org - - - UTF-8 - - - - - junit - junit - 4.12 - test - - - com.alibaba - fastjson - 1.2.13 - - - diff --git a/json/README.md b/json/README.md new file mode 100644 index 0000000000..c47eca3e84 --- /dev/null +++ b/json/README.md @@ -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/????????) diff --git a/json/pom.xml b/json/pom.xml index 47f1f25aa2..4d9efe56e4 100644 --- a/json/pom.xml +++ b/json/pom.xml @@ -13,6 +13,12 @@ 1.3.0 + + com.alibaba + fastjson + 1.2.13 + + junit junit diff --git a/json/src/test/java/org/baeldung/json/schema/JSONSchemaTest.java b/json/src/test/java/com/baeldung/json/schema/JSONSchemaTest.java similarity index 88% rename from json/src/test/java/org/baeldung/json/schema/JSONSchemaTest.java rename to json/src/test/java/com/baeldung/json/schema/JSONSchemaTest.java index 273fd48bac..bd4aaee682 100644 --- a/json/src/test/java/org/baeldung/json/schema/JSONSchemaTest.java +++ b/json/src/test/java/com/baeldung/json/schema/JSONSchemaTest.java @@ -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); + } +} diff --git a/fast-json/src/test/java/com/baeldung/fast_json/FastJsonTests.java b/json/src/test/java/fast_json/FastJsonTests.java similarity index 77% rename from fast-json/src/test/java/com/baeldung/fast_json/FastJsonTests.java rename to json/src/test/java/fast_json/FastJsonTests.java index 9e6771e98b..7b7e3c0434 100644 --- a/fast-json/src/test/java/com/baeldung/fast_json/FastJsonTests.java +++ b/json/src/test/java/fast_json/FastJsonTests.java @@ -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 listOfPersons = new ArrayList(); + private List 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(); + 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); } } diff --git a/fast-json/src/main/java/com/baeldung/fast_json/Person.java b/json/src/test/java/fast_json/Person.java similarity index 97% rename from fast-json/src/main/java/com/baeldung/fast_json/Person.java rename to json/src/test/java/fast_json/Person.java index 4600eaa712..0eac58cdfd 100644 --- a/fast-json/src/main/java/com/baeldung/fast_json/Person.java +++ b/json/src/test/java/fast_json/Person.java @@ -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) diff --git a/pom.xml b/pom.xml index 68841212d4..483dfa4a0f 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ javaxval jooq-spring json-path - fast-json + json mockito mocks jee7schedule