1+ package org .zalando .logbook .json ;
2+
3+ import com .jayway .jsonpath .Configuration ;
4+ import com .jayway .jsonpath .Option ;
5+ import com .jayway .jsonpath .spi .json .JacksonJsonProvider ;
6+ import com .jayway .jsonpath .spi .json .JsonProvider ;
7+ import com .jayway .jsonpath .spi .mapper .JacksonMappingProvider ;
8+ import com .jayway .jsonpath .spi .mapper .MappingProvider ;
9+ import org .junit .jupiter .api .BeforeAll ;
10+ import org .junit .jupiter .api .Test ;
11+ import org .zalando .logbook .BodyFilter ;
12+
13+ import java .util .HashSet ;
14+ import java .util .Set ;
15+
16+ import static org .assertj .core .api .Assertions .assertThat ;
17+ import static org .zalando .logbook .json .JsonPathBodyFilters .jsonPath ;
18+
19+ class JsonPathBodyFiltersTest {
20+
21+ public static final String CONTENT_TYPE = "application/json" ;
22+
23+ @ Test
24+ public void deleteObjectTest () {
25+ BodyFilter filter = jsonPath ("$.test.test123" ).delete ();
26+ String result = filter .filter (CONTENT_TYPE , "{\" test\" : {\" test123\" :\" testvalue\" }, \" test2\" :\" value\" }" );
27+ assertThat (result ).isEqualTo ("{\" test\" :{},\" test2\" :\" value\" }" );
28+ }
29+
30+ @ Test
31+ public void replaceArrayWithStringTest () {
32+ BodyFilter filter = jsonPath ("$.test" ).replace ("XXX" );
33+ String result = filter .filter (CONTENT_TYPE , "{\" test\" : [\" test123\" , \" test321\" , 1.0], \" test123\" : \" testing\" }" );
34+ assertThat (result ).isEqualTo ("{\" test\" :\" XXX\" ,\" test123\" :\" testing\" }" );
35+ }
36+
37+ @ Test
38+ public void replaceNumberWithStringTest () {
39+ BodyFilter filter = jsonPath ("$.test" ).replace ("XXX" );
40+ String result = filter .filter (CONTENT_TYPE , "{\" test\" : 1, \" test123\" : \" testing\" }" );
41+ assertThat (result ).isEqualTo ("{\" test\" :\" XXX\" ,\" test123\" :\" testing\" }" );
42+ }
43+
44+ @ Test
45+ public void replaceArrayWithNumberTest () {
46+ BodyFilter filter = jsonPath ("$.test" ).replace (10.0 );
47+ String result = filter .filter (CONTENT_TYPE , "{\" test\" : [\" test123\" , \" test321\" , 1.0], \" test123\" : \" testing\" }" );
48+ assertThat (result ).isEqualTo ("{\" test\" :10.0,\" test123\" :\" testing\" }" );
49+ }
50+
51+ @ Test
52+ public void replaceNumberWithNumberTest () {
53+ BodyFilter filter = jsonPath ("$.test" ).replace (10.0 );
54+ String result = filter .filter (CONTENT_TYPE , "{\" test\" : 1, \" test123\" : \" testing\" }" );
55+ assertThat (result ).isEqualTo ("{\" test\" :10.0,\" test123\" :\" testing\" }" );
56+ }
57+
58+ @ Test
59+ public void replaceArrayWithBooleanTest () {
60+ BodyFilter filter = jsonPath ("$.test" ).replace (true );
61+ String result = filter .filter (CONTENT_TYPE , "{\" test\" : [\" test123\" , \" test321\" , 1.0], \" test123\" : \" testing\" }" );
62+ assertThat (result ).isEqualTo ("{\" test\" :true,\" test123\" :\" testing\" }" );
63+ }
64+
65+ @ Test
66+ public void replaceNumberWithBooleanTest () {
67+ BodyFilter filter = jsonPath ("$.test" ).replace (true );
68+ String result = filter .filter (CONTENT_TYPE , "{\" test\" : 1, \" test123\" : \" testing\" }" );
69+ assertThat (result ).isEqualTo ("{\" test\" :true,\" test123\" :\" testing\" }" );
70+ }
71+
72+ @ Test
73+ public void replaceStringDynamicallyTest () {
74+ BodyFilter filter = jsonPath ("$.test" ).replace ("(\\ d{6})\\ d+(\\ d{4})" , "$1******$2" );
75+ String result = filter .filter (CONTENT_TYPE , "{\" test\" : \" 5213486633218931\" , \" test123\" : \" 5213486633218931\" }" );
76+ assertThat (result ).isEqualTo ("{\" test\" :\" 521348******8931\" ,\" test123\" :\" 5213486633218931\" }" );
77+ }
78+
79+ @ Test
80+ public void replaceArrayDynamicallyTest () {
81+ BodyFilter filter = jsonPath ("$.test" ).replace ("(\\ d{6})\\ d+(\\ d{4})" , "$1******$2" );
82+ String result = filter .filter (CONTENT_TYPE , "{\" test\" : [\" 5213486633218931\" , \" 123\" , {}, true], \" test123\" : \" 5213486633218931\" }" );
83+ assertThat (result ).isEqualTo ("{\" test\" :\" [\\ \" 521348******8931\\ \" ,\\ \" 123\\ \" ,{},true]\" ,\" test123\" :\" 5213486633218931\" }" );
84+ }
85+
86+ @ Test
87+ public void replaceArrayInRightWayDynamicallyTest () {
88+ BodyFilter filter = jsonPath ("$.test.*" ).replace ("(\\ d{6})\\ d+(\\ d{4})" , "$1******$2" );
89+ String result = filter .filter (CONTENT_TYPE , "{\" test\" : [\" 5213486633218931\" , \" 123\" , {}, true], \" test123\" : \" 5213486633218931\" }" );
90+ assertThat (result ).isEqualTo ("{\" test\" :[\" 521348******8931\" ,\" 123\" ,{},true],\" test123\" :\" 5213486633218931\" }" );
91+ }
92+
93+ @ Test
94+ public void replaceObjectDynamicallyTest () {
95+ BodyFilter filter = jsonPath ("$.test" ).replace ("(\\ d{6})\\ d+(\\ d{4})" , "$1******$2" );
96+ String result = filter .filter (CONTENT_TYPE , "{\" test\" : {\" 321test\" : \" 5213486633218931\" }, \" test123\" : \" 5213486633218931\" }" );
97+ assertThat (result ).isEqualTo ("{\" test\" :\" {321test=521348******8931}\" ,\" test123\" :\" 5213486633218931\" }" );
98+ }
99+
100+ @ Test
101+ public void unsuccessfullReplaceStringDynamicallyTest () {
102+ BodyFilter filter = jsonPath ("$.test" ).replace ("\\ s+" , "$1********$2" );
103+ String result = filter .filter (CONTENT_TYPE , "{\" test\" :5213, \" test123\" : \" 5213486633218931\" }" );
104+ assertThat (result ).isEqualTo ("{\" test\" :5213,\" test123\" :\" 5213486633218931\" }" );
105+ }
106+
107+ @ Test
108+ public void unsuccessfullReplaceNumberDynamicallyTest () {
109+ BodyFilter filter = jsonPath ("$.test" ).replace ("\\ s+" , "$1********$2" );
110+ String result = filter .filter (CONTENT_TYPE , "{\" test\" : \" 5213486633218931\" , \" test123\" : \" 5213486633218931\" }" );
111+ assertThat (result ).isEqualTo ("{\" test\" :\" 5213486633218931\" ,\" test123\" :\" 5213486633218931\" }" );
112+ }
113+
114+ @ Test
115+ public void contentTypeTest () {
116+ BodyFilter filter = jsonPath ("$.test" ).replace ("XXX" );
117+ String result = filter .filter ("application/xml" , "{\" test\" : \" value\" }" );
118+ assertThat (result ).isEqualTo ("{\" test\" : \" value\" }" );
119+ }
120+ }
0 commit comments