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