@ -30,8 +30,8 @@ import java.util.List;
import java.util.Map ;
import java.util.TimeZone ;
import org.junit.Assert ;
import org.junit.Test ;
import org.junit.jupiter.api. Assertions ;
import org.junit.jupiter.api. Test ;
import com.fasterxml.jackson.databind.JsonNode ;
import com.fasterxml.jackson.databind.SerializationFeature ;
@ -59,7 +59,7 @@ public class JSONUtilsTest {
ArrayNode jsonNodes = arrayNode . addAll ( objects ) ;
String s = JSONUtils . toJsonString ( jsonNodes ) ;
Assert . assertEquals ( s , str ) ;
Assertions . assertEquals ( s , str ) ;
}
@ -74,7 +74,7 @@ public class JSONUtilsTest {
JsonNode jsonNodes = JSONUtils . toJsonNode ( property ) ;
String s = JSONUtils . toJsonString ( jsonNodes ) ;
Assert . assertEquals ( s , str ) ;
Assertions . assertEquals ( s , str ) ;
}
@ -86,7 +86,7 @@ public class JSONUtilsTest {
objectNode . put ( "a" , "b" ) ;
objectNode . put ( "b" , "d" ) ;
String s = JSONUtils . toJsonString ( objectNode ) ;
Assert . assertEquals ( s , jsonStr ) ;
Assertions . assertEquals ( s , jsonStr ) ;
}
@Test
@ -95,8 +95,8 @@ public class JSONUtilsTest {
String jsonStr = "{\"id\":\"1001\",\"name\":\"Jobs\"}" ;
Map < String , String > models = JSONUtils . toMap ( jsonStr ) ;
Assert . assertEquals ( "1001" , models . get ( "id" ) ) ;
Assert . assertEquals ( "Jobs" , models . get ( "name" ) ) ;
Assertions . assertEquals ( "1001" , models . get ( "id" ) ) ;
Assertions . assertEquals ( "Jobs" , models . get ( "name" ) ) ;
}
@ -110,7 +110,7 @@ public class JSONUtilsTest {
String str = "{\"direct\":\"IN\",\"prop\":\"ds\",\"type\":\"VARCHAR\",\"value\":\"sssssss\"}" ;
Property property1 = JSONUtils . parseObject ( str , Property . class ) ;
Direct direct = property1 . getDirect ( ) ;
Assert . assertEquals ( Direct . IN , direct ) ;
Assertions . assertEquals ( Direct . IN , direct ) ;
}
@Test
@ -120,12 +120,12 @@ public class JSONUtilsTest {
List < LinkedHashMap > maps = JSONUtils . toList ( str ,
LinkedHashMap . class ) ;
Assert . assertEquals ( 1 , maps . size ( ) ) ;
Assert . assertEquals ( "mysql200" , maps . get ( 0 ) . get ( "mysql service name" ) ) ;
Assert . assertEquals ( "192.168.xx.xx" , maps . get ( 0 ) . get ( "mysql address" ) ) ;
Assert . assertEquals ( "3306" , maps . get ( 0 ) . get ( "port" ) ) ;
Assert . assertEquals ( "80" , maps . get ( 0 ) . get ( "no index of number" ) ) ;
Assert . assertEquals ( "190" , maps . get ( 0 ) . get ( "database client connections" ) ) ;
Assertions . assertEquals ( 1 , maps . size ( ) ) ;
Assertions . assertEquals ( "mysql200" , maps . get ( 0 ) . get ( "mysql service name" ) ) ;
Assertions . assertEquals ( "192.168.xx.xx" , maps . get ( 0 ) . get ( "mysql address" ) ) ;
Assertions . assertEquals ( "3306" , maps . get ( 0 ) . get ( "port" ) ) ;
Assertions . assertEquals ( "80" , maps . get ( 0 ) . get ( "no index of number" ) ) ;
Assertions . assertEquals ( "190" , maps . get ( 0 ) . get ( "database client connections" ) ) ;
}
public String list2String ( ) {
@ -145,19 +145,18 @@ public class JSONUtilsTest {
@Test
public void testParseObject ( ) {
Assert . assertNull ( JSONUtils . parseObject ( "" ) ) ;
Assert . assertNull ( JSONUtils . parseObject ( "foo" , String . class ) ) ;
Assertions . assertNull ( JSONUtils . parseObject ( "" ) ) ;
Assertions . assertNull ( JSONUtils . parseObject ( "foo" , String . class ) ) ;
}
@Test
public void testNodeString ( ) {
Assert . assertEquals ( "" , JSONUtils . getNodeString ( "" , "key" ) ) ;
Assert . assertEquals ( "" , JSONUtils . getNodeString ( "abc" , "key" ) ) ;
Assert . assertEquals ( "" , JSONUtils . getNodeString ( "{\"bar\":\"foo\"}" , "key" ) ) ;
Assert . assertEquals ( "foo" , JSONUtils . getNodeString ( "{\"bar\":\"foo\"}" , "bar" ) ) ;
Assert . assertEquals ( "[1,2,3]" , JSONUtils . getNodeString ( "{\"bar\": [1,2,3]}" , "bar" ) ) ;
Assert . assertEquals ( "{\"1\":\"2\",\"2\":3}" ,
JSONUtils . getNodeString ( "{\"bar\": {\"1\":\"2\",\"2\":3}}" , "bar" ) ) ;
Assertions . assertEquals ( "" , JSONUtils . getNodeString ( "" , "key" ) ) ;
Assertions . assertEquals ( "" , JSONUtils . getNodeString ( "abc" , "key" ) ) ;
Assertions . assertEquals ( "" , JSONUtils . getNodeString ( "{\"bar\":\"foo\"}" , "key" ) ) ;
Assertions . assertEquals ( "foo" , JSONUtils . getNodeString ( "{\"bar\":\"foo\"}" , "bar" ) ) ;
Assertions . assertEquals ( "[1,2,3]" , JSONUtils . getNodeString ( "{\"bar\": [1,2,3]}" , "bar" ) ) ;
Assertions . assertEquals ( "{\"1\":\"2\",\"2\":3}" , JSONUtils . getNodeString ( "{\"bar\": {\"1\":\"2\",\"2\":3}}" , "bar" ) ) ;
}
@Test
@ -165,30 +164,28 @@ public class JSONUtilsTest {
String str = "foo" ;
byte [ ] serializeByte = JSONUtils . toJsonByteArray ( str ) ;
String deserialize = JSONUtils . parseObject ( serializeByte , String . class ) ;
Assert . assertEquals ( str , deserialize ) ;
Assertions . assertEquals ( str , deserialize ) ;
str = null ;
serializeByte = JSONUtils . toJsonByteArray ( str ) ;
deserialize = JSONUtils . parseObject ( serializeByte , String . class ) ;
Assert . assertNull ( deserialize ) ;
Assertions . assertNull ( deserialize ) ;
}
@Test
public void testToList ( ) {
Assert . assertEquals ( new ArrayList ( ) ,
JSONUtils . toList ( "A1B2C3" , null ) ) ;
Assert . assertEquals ( new ArrayList ( ) ,
JSONUtils . toList ( "" , null ) ) ;
Assertions . assertEquals ( new ArrayList ( ) , JSONUtils . toList ( "A1B2C3" , null ) ) ;
Assertions . assertEquals ( new ArrayList ( ) , JSONUtils . toList ( "" , null ) ) ;
}
@Test
public void testCheckJsonValid ( ) {
Assert . assertTrue ( JSONUtils . checkJsonValid ( "3" ) ) ;
Assert . assertFalse ( JSONUtils . checkJsonValid ( "" ) ) ;
Assertions . assertTrue ( JSONUtils . checkJsonValid ( "3" ) ) ;
Assertions . assertFalse ( JSONUtils . checkJsonValid ( "" ) ) ;
}
@Test
public void testFindValue ( ) {
Assert . assertNull ( JSONUtils . findValue (
Assertions . assertNull ( JSONUtils . findValue (
new ArrayNode ( new JsonNodeFactory ( true ) ) , null ) ) ;
}
@ -197,18 +194,18 @@ public class JSONUtilsTest {
Map < String , String > map = new HashMap < > ( ) ;
map . put ( "foo" , "bar" ) ;
Assert . assertTrue ( map . equals ( JSONUtils . toMap (
Assertions . assertTrue ( map . equals ( JSONUtils . toMap (
"{\n" + "\"foo\": \"bar\"\n" + "}" ) ) ) ;
Assert . assertFalse ( map . equals ( JSONUtils . toMap (
Assertions . assertFalse ( map . equals ( JSONUtils . toMap (
"{\n" + "\"bar\": \"foo\"\n" + "}" ) ) ) ;
Assert . assertNull ( JSONUtils . toMap ( "3" ) ) ;
Assert . assertNull ( JSONUtils . toMap ( null ) ) ;
Assertions . assertNull ( JSONUtils . toMap ( "3" ) ) ;
Assertions . assertNull ( JSONUtils . toMap ( null ) ) ;
String str = "{\"resourceList\":[],\"localParams\":[],\"rawScript\":\"#!/bin/bash\\necho \\\"shell-1\\\"\"}" ;
Map < String , String > m = JSONUtils . toMap ( str ) ;
Assert . assertNotNull ( m ) ;
Assertions . assertNotNull ( m ) ;
}
@Test
@ -216,13 +213,10 @@ public class JSONUtilsTest {
Map < String , Object > map = new HashMap < > ( ) ;
map . put ( "foo" , "bar" ) ;
Assert . assertEquals ( "{\"foo\":\"bar\"}" ,
JSONUtils . toJsonString ( map ) ) ;
Assert . assertEquals ( String . valueOf ( ( Object ) null ) ,
JSONUtils . toJsonString ( null ) ) ;
Assertions . assertEquals ( "{\"foo\":\"bar\"}" , JSONUtils . toJsonString ( map ) ) ;
Assertions . assertEquals ( String . valueOf ( ( Object ) null ) , JSONUtils . toJsonString ( null ) ) ;
Assert . assertEquals ( "{\"foo\":\"bar\"}" ,
JSONUtils . toJsonString ( map , SerializationFeature . WRITE_NULL_MAP_VALUES ) ) ;
Assertions . assertEquals ( "{\"foo\":\"bar\"}" , JSONUtils . toJsonString ( map , SerializationFeature . WRITE_NULL_MAP_VALUES ) ) ;
}
@Test
@ -230,13 +224,13 @@ public class JSONUtilsTest {
String str = "{\"color\":\"yellow\",\"type\":\"renault\"}" ;
ObjectNode node = JSONUtils . parseObject ( str ) ;
Assert . assertEquals ( "yellow" , node . path ( "color" ) . asText ( ) ) ;
Assertions . assertEquals ( "yellow" , node . path ( "color" ) . asText ( ) ) ;
node . put ( "price" , 100 ) ;
Assert . assertEquals ( 100 , node . path ( "price" ) . asInt ( ) ) ;
Assertions . assertEquals ( 100 , node . path ( "price" ) . asInt ( ) ) ;
node . put ( "color" , "red" ) ;
Assert . assertEquals ( "red" , node . path ( "color" ) . asText ( ) ) ;
Assertions . assertEquals ( "red" , node . path ( "color" ) . asText ( ) ) ;
}
@Test
@ -244,7 +238,7 @@ public class JSONUtilsTest {
String str = "[{\"color\":\"yellow\",\"type\":\"renault\"}]" ;
ArrayNode node = JSONUtils . parseArray ( str ) ;
Assert . assertEquals ( "yellow" , node . path ( 0 ) . path ( "color" ) . asText ( ) ) ;
Assertions . assertEquals ( "yellow" , node . path ( 0 ) . path ( "color" ) . asText ( ) ) ;
}
@Test
@ -256,10 +250,10 @@ public class JSONUtilsTest {
String time = "2022-02-22 13:38:24" ;
Date date = DateUtils . stringToDate ( time ) ;
String json = JSONUtils . toJsonString ( date ) ;
Assert . assertEquals ( "\"" + time + "\"" , json ) ;
Assertions . assertEquals ( "\"" + time + "\"" , json ) ;
String errorFormatTime = "Tue Feb 22 03:50:00 UTC 2022" ;
Assert . assertNull ( DateUtils . stringToDate ( errorFormatTime ) ) ;
Assertions . assertNull ( DateUtils . stringToDate ( errorFormatTime ) ) ;
}
@Test
@ -270,7 +264,7 @@ public class JSONUtilsTest {
String json = "\"2022-02-22 13:38:24\"" ;
Date date = JSONUtils . parseObject ( json , Date . class ) ;
Assert . assertEquals ( DateUtils . stringToDate ( "2022-02-22 13:38:24" ) , date ) ;
Assertions . assertEquals ( DateUtils . stringToDate ( "2022-02-22 13:38:24" ) , date ) ;
}