[JAVA-9317] Update tests to use JsonUnit for improved assertions
This commit is contained in:
parent
ff081330bc
commit
c86e2a8655
|
@ -63,6 +63,12 @@
|
||||||
<version>${commons-collections4.version}</version>
|
<version>${commons-collections4.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.javacrumbs.json-unit</groupId>
|
||||||
|
<artifactId>json-unit-assertj</artifactId>
|
||||||
|
<version>${json-unit-assertj.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
@ -72,6 +78,7 @@
|
||||||
<json.version>20211205</json.version>
|
<json.version>20211205</json.version>
|
||||||
<gson.version>2.8.5</gson.version>
|
<gson.version>2.8.5</gson.version>
|
||||||
<javax.version>1.1.2</javax.version>
|
<javax.version>1.1.2</javax.version>
|
||||||
|
<json-unit-assertj.version>2.28.0</json-unit-assertj.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -1,36 +1,44 @@
|
||||||
package com.baeldung.jsonjava;
|
package com.baeldung.jsonjava;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import org.json.CDL;
|
import org.json.CDL;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONTokener;
|
import org.json.JSONTokener;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class CDLIntegrationTest {
|
public class CDLIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCommaDelimitedText_thenConvertToJSONArray() {
|
public void givenCommaDelimitedText_thenConvertToJSONArray() {
|
||||||
JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada"));
|
JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada"));
|
||||||
assertEquals("[\"England\",\"USA\",\"Canada\"]", ja.toString());
|
|
||||||
|
assertThatJson(ja)
|
||||||
|
.isEqualTo("[\"England\",\"USA\",\"Canada\"]");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenJSONArray_thenConvertToCommaDelimitedText() {
|
public void givenJSONArray_thenConvertToCommaDelimitedText() {
|
||||||
JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]");
|
JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]");
|
||||||
|
|
||||||
String cdt = CDL.rowToString(ja);
|
String cdt = CDL.rowToString(ja);
|
||||||
assertEquals("England,USA,Canada", cdt.toString().trim());
|
|
||||||
|
assertThat(cdt.trim()).isEqualTo("England,USA,Canada");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCommaDelimitedText_thenGetJSONArrayOfJSONObjects() {
|
public void givenCommaDelimitedText_thenGetJSONArrayOfJSONObjects() {
|
||||||
String string =
|
String string =
|
||||||
"name, city, age \n" +
|
"name, city, age \n" +
|
||||||
"john, chicago, 22 \n" +
|
"john, chicago, 22 \n" +
|
||||||
"gary, florida, 35 \n" +
|
"gary, florida, 35 \n" +
|
||||||
"sal, vegas, 18";
|
"sal, vegas, 18";
|
||||||
|
|
||||||
JSONArray result = CDL.toJSONArray(string);
|
JSONArray result = CDL.toJSONArray(string);
|
||||||
assertEquals("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]", result.toString());
|
|
||||||
|
assertThatJson(result)
|
||||||
|
.isEqualTo("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -39,14 +47,16 @@ public class CDLIntegrationTest {
|
||||||
ja.put("name");
|
ja.put("name");
|
||||||
ja.put("city");
|
ja.put("city");
|
||||||
ja.put("age");
|
ja.put("age");
|
||||||
|
|
||||||
String string =
|
String string =
|
||||||
"john, chicago, 22 \n" +
|
"john, chicago, 22 \n" +
|
||||||
"gary, florida, 35 \n" +
|
"gary, florida, 35 \n" +
|
||||||
"sal, vegas, 18";
|
"sal, vegas, 18";
|
||||||
|
|
||||||
JSONArray result = CDL.toJSONArray(ja, string);
|
JSONArray result = CDL.toJSONArray(ja, string);
|
||||||
assertEquals("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]", result.toString());
|
|
||||||
|
assertThatJson(result)
|
||||||
|
.isEqualTo("[{\"name\":\"john\",\"city\":\"chicago\",\"age\":\"22\"},{\"name\":\"gary\",\"city\":\"florida\",\"age\":\"35\"},{\"name\":\"sal\",\"city\":\"vegas\",\"age\":\"18\"}]");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,21 @@
|
||||||
package com.baeldung.jsonjava;
|
package com.baeldung.jsonjava;
|
||||||
|
|
||||||
|
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import org.json.Cookie;
|
import org.json.Cookie;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class CookieIntegrationTest {
|
public class CookieIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCookieString_thenConvertToJSONObject() {
|
public void givenCookieString_thenConvertToJSONObject() {
|
||||||
String cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
|
String cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
|
||||||
JSONObject cookieJO = Cookie.toJSONObject(cookie);
|
JSONObject cookieJO = Cookie.toJSONObject(cookie);
|
||||||
|
|
||||||
assertEquals("{\"path\":\"/\",\"expires\":\"Thu, 18 Dec 2013 12:00:00 UTC\",\"name\":\"username\",\"value\":\"John Doe\"}", cookieJO.toString());
|
assertThatJson(cookieJO)
|
||||||
|
.isEqualTo("{\"path\":\"/\",\"expires\":\"Thu, 18 Dec 2013 12:00:00 UTC\",\"name\":\"username\",\"value\":\"John Doe\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package com.baeldung.jsonjava;
|
package com.baeldung.jsonjava;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import org.json.HTTP;
|
import org.json.HTTP;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class HTTPIntegrationTest {
|
public class HTTPIntegrationTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenJSONObject_thenConvertToHTTPHeader() {
|
public void givenJSONObject_thenConvertToHTTPHeader() {
|
||||||
|
@ -12,14 +14,16 @@ public class HTTPIntegrationTest {
|
||||||
jo.put("Method", "POST");
|
jo.put("Method", "POST");
|
||||||
jo.put("Request-URI", "http://www.example.com/");
|
jo.put("Request-URI", "http://www.example.com/");
|
||||||
jo.put("HTTP-Version", "HTTP/1.1");
|
jo.put("HTTP-Version", "HTTP/1.1");
|
||||||
|
|
||||||
assertEquals("POST \"http://www.example.com/\" HTTP/1.1"+HTTP.CRLF+HTTP.CRLF, HTTP.toString(jo));
|
assertThat(HTTP.toString(jo))
|
||||||
|
.isEqualTo("POST \"http://www.example.com/\" HTTP/1.1" + HTTP.CRLF + HTTP.CRLF);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenHTTPHeader_thenConvertToJSONObject() {
|
public void givenHTTPHeader_thenConvertToJSONObject() {
|
||||||
JSONObject obj = HTTP.toJSONObject("POST \"http://www.example.com/\" HTTP/1.1");
|
JSONObject obj = HTTP.toJSONObject("POST \"http://www.example.com/\" HTTP/1.1");
|
||||||
|
|
||||||
assertEquals("{\"Request-URI\":\"http://www.example.com/\",\"Method\":\"POST\",\"HTTP-Version\":\"HTTP/1.1\"}", obj.toString());
|
assertThatJson(obj)
|
||||||
|
.isEqualTo("{\"Request-URI\":\"http://www.example.com/\",\"Method\":\"POST\",\"HTTP-Version\":\"HTTP/1.1\"}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
package com.baeldung.jsonjava;
|
package com.baeldung.jsonjava;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertThat;
|
import java.util.List;
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class JSONArrayGetValueByKeyUnitTest {
|
public class JSONArrayGetValueByKeyUnitTest {
|
||||||
|
|
||||||
|
@ -19,7 +17,8 @@ public class JSONArrayGetValueByKeyUnitTest {
|
||||||
|
|
||||||
List<String> actualValues = obj.getValuesByKeyInJSONArray(jsonStr, "name");
|
List<String> actualValues = obj.getValuesByKeyInJSONArray(jsonStr, "name");
|
||||||
|
|
||||||
assertThat(actualValues, equalTo(Arrays.asList("John", "Gary", "Selena")));
|
assertThat(actualValues)
|
||||||
|
.containsExactlyInAnyOrder("John", "Gary", "Selena");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -29,7 +28,8 @@ public class JSONArrayGetValueByKeyUnitTest {
|
||||||
|
|
||||||
List<String> actualValues = obj.getValuesByKeyInJSONArrayUsingJava8(jsonStr, "name");
|
List<String> actualValues = obj.getValuesByKeyInJSONArrayUsingJava8(jsonStr, "name");
|
||||||
|
|
||||||
assertThat(actualValues, equalTo(Arrays.asList("John", "Gary", "Selena")));
|
assertThat(actualValues)
|
||||||
|
.containsExactlyInAnyOrder("John", "Gary", "Selena");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,36 +1,40 @@
|
||||||
package com.baeldung.jsonjava;
|
package com.baeldung.jsonjava;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||||
|
|
||||||
public class JSONArrayIntegrationTest {
|
public class JSONArrayIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenJSONJava_thenCreateNewJSONArrayFromScratch() {
|
public void givenJSONJava_thenCreateNewJSONArrayFromScratch() {
|
||||||
JSONArray ja = new JSONArray();
|
JSONArray ja = new JSONArray();
|
||||||
ja.put(Boolean.TRUE);
|
ja.put(Boolean.TRUE);
|
||||||
ja.put("lorem ipsum");
|
ja.put("lorem ipsum");
|
||||||
|
|
||||||
// We can also put a JSONObject in JSONArray
|
// We can also put a JSONObject in JSONArray
|
||||||
JSONObject jo = new JSONObject();
|
JSONObject jo = new JSONObject();
|
||||||
jo.put("name", "jon doe");
|
jo.put("name", "jon doe");
|
||||||
jo.put("age", "22");
|
jo.put("age", "22");
|
||||||
jo.put("city", "chicago");
|
jo.put("city", "chicago");
|
||||||
|
|
||||||
ja.put(jo);
|
ja.put(jo);
|
||||||
|
|
||||||
assertEquals("[true,\"lorem ipsum\",{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}]", ja.toString());
|
assertThatJson(ja)
|
||||||
|
.isEqualTo("[true,\"lorem ipsum\",{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}]");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenJsonString_thenCreateNewJSONArray() {
|
public void givenJsonString_thenCreateNewJSONArray() {
|
||||||
JSONArray ja = new JSONArray("[true, \"lorem ipsum\", 215]");
|
JSONArray ja = new JSONArray("[true, \"lorem ipsum\", 215]");
|
||||||
assertEquals("[true,\"lorem ipsum\",215]", ja.toString());
|
|
||||||
|
assertThatJson(ja)
|
||||||
|
.isEqualTo("[true,\"lorem ipsum\",215]");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -40,8 +44,10 @@ public class JSONArrayIntegrationTest {
|
||||||
list.add("Texas");
|
list.add("Texas");
|
||||||
list.add("Hawaii");
|
list.add("Hawaii");
|
||||||
list.add("Alaska");
|
list.add("Alaska");
|
||||||
|
|
||||||
JSONArray ja = new JSONArray(list);
|
JSONArray ja = new JSONArray(list);
|
||||||
assertEquals("[\"California\",\"Texas\",\"Hawaii\",\"Alaska\"]", ja.toString());
|
|
||||||
|
assertThatJson(ja)
|
||||||
|
.isEqualTo("[\"California\",\"Texas\",\"Hawaii\",\"Alaska\"]");
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,23 +1,24 @@
|
||||||
package com.baeldung.jsonjava;
|
package com.baeldung.jsonjava;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||||
|
|
||||||
public class JSONObjectIntegrationTest {
|
public class JSONObjectIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenJSONJava_thenCreateNewJSONObject() {
|
public void givenJSONJava_thenCreateNewJSONObject() {
|
||||||
JSONObject jo = new JSONObject();
|
JSONObject jo = new JSONObject();
|
||||||
jo.put("name", "jon doe");
|
jo.put("name", "jon doe");
|
||||||
jo.put("age", "22");
|
jo.put("age", "22");
|
||||||
jo.put("city", "chicago");
|
jo.put("city", "chicago");
|
||||||
|
|
||||||
assertEquals("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}", jo.toString());
|
assertThatJson(jo)
|
||||||
|
.isEqualTo("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -27,8 +28,9 @@ public class JSONObjectIntegrationTest {
|
||||||
map.put("age", "22");
|
map.put("age", "22");
|
||||||
map.put("city", "chicago");
|
map.put("city", "chicago");
|
||||||
JSONObject jo = new JSONObject(map);
|
JSONObject jo = new JSONObject(map);
|
||||||
|
|
||||||
assertEquals("{\"name\":\"jon doe\",\"city\":\"chicago\",\"age\":\"22\"}", jo.toString());
|
assertThatJson(jo)
|
||||||
|
.isEqualTo("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -36,7 +38,8 @@ public class JSONObjectIntegrationTest {
|
||||||
JSONObject jo = new JSONObject(
|
JSONObject jo = new JSONObject(
|
||||||
"{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}"
|
"{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}"
|
||||||
);
|
);
|
||||||
|
|
||||||
assertEquals("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}", jo.toString());
|
assertThatJson(jo)
|
||||||
|
.isEqualTo("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,22 @@
|
||||||
package com.baeldung.jsonjava;
|
package com.baeldung.jsonjava;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import org.json.JSONTokener;
|
import org.json.JSONTokener;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class JSONTokenerIntegrationTest {
|
public class JSONTokenerIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenString_convertItToJSONTokens() {
|
public void givenString_convertItToJSONTokens() {
|
||||||
String str = "Sample String";
|
String str = "Sample String";
|
||||||
JSONTokener jt = new JSONTokener(str);
|
JSONTokener jt = new JSONTokener(str);
|
||||||
|
|
||||||
char[] expectedTokens = str.toCharArray();
|
char[] expectedTokens = str.toCharArray();
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
|
||||||
while(jt.more()) {
|
while (jt.more()) {
|
||||||
assertEquals(expectedTokens[index++], jt.next());
|
assertThat(jt.next()).isEqualTo(expectedTokens[index++]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,22 @@
|
||||||
package com.baeldung.jsonjava;
|
package com.baeldung.jsonjava;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||||
|
|
||||||
public class ObjectToFromJSONIntegrationTest {
|
public class ObjectToFromJSONIntegrationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDemoBean_thenCreateJSONObject() {
|
public void givenDemoBean_thenCreateJSONObject() {
|
||||||
DemoBean demo = new DemoBean();
|
DemoBean demo = new DemoBean();
|
||||||
demo.setId(1);
|
demo.setId(1);
|
||||||
demo.setName("lorem ipsum");
|
demo.setName("lorem ipsum");
|
||||||
demo.setActive(true);
|
demo.setActive(true);
|
||||||
|
|
||||||
JSONObject jo = new JSONObject(demo);
|
JSONObject jo = new JSONObject(demo);
|
||||||
assertEquals("{\"name\":\"lorem ipsum\",\"active\":true,\"id\":1}", jo.toString());
|
|
||||||
|
assertThatJson(jo)
|
||||||
|
.isEqualTo("{\"name\":\"lorem ipsum\",\"active\":true,\"id\":1}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue