Skip to content

Commit f9a8e71

Browse files
authored
Improve test coverage (#239)
* Improve test coverage * Improve jackson module
1 parent deab584 commit f9a8e71

12 files changed

+233
-49
lines changed

frameworks/jackson/src/main/java/com/github/skjolber/jsonfilter/jackson/DefaultJacksonJsonFilter.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
package com.github.skjolber.jsonfilter.jackson;
2+
import java.nio.charset.StandardCharsets;
3+
24
import com.fasterxml.jackson.core.JsonFactory;
35
import com.fasterxml.jackson.core.JsonParser;
46
import com.github.skjolber.jsonfilter.JsonFilterMetrics;
@@ -45,6 +47,19 @@ public boolean process(byte[] bytes, int offset, int length, ResizableByteArrayO
4547
return false;
4648
}
4749
}
50+
51+
public boolean process(byte[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
52+
try (JsonParser parser = jsonFactory.createParser(chars, offset, length)) {
53+
if(parse(parser)) {
54+
output.ensureCapacity(output.length() + length);
55+
output.append(new String(chars, offset, length, StandardCharsets.UTF_8));
56+
return true;
57+
}
58+
return false;
59+
} catch(final Exception e) {
60+
return false;
61+
}
62+
}
4863

4964
protected boolean parse(JsonParser parser) {
5065
try {

frameworks/jackson/src/main/java/com/github/skjolber/jsonfilter/jackson/JacksonMaxSizeJsonFilter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ public boolean process(byte[] bytes, int offset, int length, StringBuilder outpu
7272
}
7373

7474
if(maxSize >= length) {
75-
output.append(new String(bytes, offset, length));
76-
return true;
75+
return super.process(bytes, offset, length, output, metrics);
7776
}
7877
output.ensureCapacity(output.length() + length);
7978

frameworks/jackson/src/main/java/com/github/skjolber/jsonfilter/jackson/JacksonMaxSizeMaxStringLengthJsonFilter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public JacksonMaxSizeMaxStringLengthJsonFilter(int maxStringLength, int maxSize,
3030
super(maxStringLength, maxSize, pruneMessage, anonymizeMessage, truncateMessage, jsonFactory);
3131
}
3232

33+
@Override
3334
public boolean process(char[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics metrics) {
3435
if(maxSize >= length) {
3536
return super.process(chars, offset, length, output, metrics);
@@ -46,6 +47,7 @@ public boolean process(char[] chars, int offset, int length, StringBuilder outpu
4647
}
4748
}
4849

50+
@Override
4951
public boolean process(byte[] bytes, int offset, int length, StringBuilder output, JsonFilterMetrics metrics) {
5052
if(maxSize >= length) {
5153
return super.process(bytes, offset, length, output, metrics);
@@ -62,6 +64,7 @@ public boolean process(byte[] bytes, int offset, int length, StringBuilder outpu
6264
}
6365
}
6466

67+
@Override
6568
public boolean process(byte[] bytes, int offset, int length, ResizableByteArrayOutputStream output, JsonFilterMetrics metrics) {
6669
if(maxSize >= length) {
6770
return super.process(bytes, offset, length, output, metrics);

frameworks/jackson/src/main/java/com/github/skjolber/jsonfilter/jackson/JacksonMaxStringLengthJsonFilter.java

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,40 +47,6 @@ public static void writeMaxStringLength(final JsonParser parser, JsonGenerator g
4747
}
4848
}
4949

50-
public static void skipMaxStringLength(final JsonParser parser, JsonGenerator generator, int maxStringLength, StringBuilder builder, JsonFilterMetrics metrics, char[] truncateStringValue) throws IOException {
51-
int level = 1;
52-
53-
while(level > 0) {
54-
JsonToken nextToken = parser.nextToken();
55-
if(nextToken == null) {
56-
break;
57-
}
58-
59-
switch(nextToken) {
60-
case START_OBJECT:
61-
case START_ARRAY:
62-
level++;
63-
break;
64-
case END_OBJECT:
65-
case END_ARRAY:
66-
level--;
67-
break;
68-
case VALUE_STRING:
69-
if(parser.getTextLength() > maxStringLength) {
70-
writeMaxStringLength(parser, generator, builder, maxStringLength, truncateStringValue);
71-
72-
if(metrics != null) {
73-
metrics.onMaxStringLength(1);
74-
}
75-
76-
continue;
77-
}
78-
}
79-
80-
generator.copyCurrentEvent(parser);
81-
}
82-
}
83-
8450
protected final JsonFactory jsonFactory;
8551

8652
public JacksonMaxStringLengthJsonFilter(int maxStringLength) {
@@ -132,6 +98,7 @@ public boolean process(char[] chars, int offset, int length, StringBuilder outpu
13298
}
13399
}
134100

101+
@Override
135102
public boolean process(byte[] bytes, int offset, int length, StringBuilder output, JsonFilterMetrics metrics) {
136103
if(bytes.length < offset + length) {
137104
return false;

frameworks/jackson/src/main/java/com/github/skjolber/jsonfilter/jackson/JacksonPathMaxSizeMaxStringLengthJsonFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected JacksonPathMaxSizeMaxStringLengthJsonFilter(int maxStringLength, int m
3232

3333
public boolean process(char[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics metrics) {
3434
if(!mustConstrainMaxSize(length)) {
35-
return super.process(chars, offset, length, output);
35+
return super.process(chars, offset, length, output, metrics);
3636
}
3737
output.ensureCapacity(output.length() + length);
3838

frameworks/jackson/src/test/java/com/github/skjolber/jsonfilter/jackson/AbstractDefaultJacksonJsonFilterTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.io.IOException;
1010

11+
import com.github.skjolber.jsonfilter.base.DefaultJsonFilterMetrics;
1112
import com.github.skjolber.jsonfilter.test.DefaultJsonFilterTest;
1213

1314
public abstract class AbstractDefaultJacksonJsonFilterTest extends DefaultJsonFilterTest {
@@ -24,6 +25,9 @@ public void testConvenienceMethods(JacksonJsonFilter successFilter, JacksonJsonF
2425
assertTrue(successFilter.process(jsonBytes, 0, 2, new StringBuilder()));
2526
assertTrue(successFilter.process(jsonChars, 0, 2, new StringBuilder()));
2627

28+
assertTrue(successFilter.process(jsonBytes, new StringBuilder()));
29+
assertTrue(successFilter.process(jsonBytes, new StringBuilder(), new DefaultJsonFilterMetrics()));
30+
2731
assertFalse(failureFilter.process(jsonBytes, 0, 2, new StringBuilder()));
2832
assertFalse(failureFilter.process(jsonChars, 0, 2, new StringBuilder()));
2933

frameworks/jackson/src/test/java/com/github/skjolber/jsonfilter/jackson/DefaultJacksonJsonFilterTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
import static org.junit.jupiter.api.Assertions.assertFalse;
44
import static org.junit.jupiter.api.Assertions.assertNull;
55

6+
import java.nio.charset.StandardCharsets;
7+
68
import org.junit.jupiter.api.Test;
79

10+
import com.fasterxml.jackson.core.JsonFactory;
11+
import com.github.skjolber.jsonfilter.ResizableByteArrayOutputStream;
12+
813
public class DefaultJacksonJsonFilterTest {
914

1015
@Test
@@ -14,5 +19,19 @@ public void testException() throws Exception {
1419

1520
assertNull(filter.process(new byte[] {}, 0, 100));
1621
assertFalse(filter.process(new char[] {}, 0, 100, new StringBuilder()));
22+
23+
String illegalJson = "{abcdef}";
24+
assertFalse(filter.process(illegalJson.toCharArray(), 0, 3, new StringBuilder()));
25+
assertFalse(filter.process(illegalJson.getBytes(StandardCharsets.UTF_8), 0, 3, new ResizableByteArrayOutputStream(1024)));
26+
}
27+
28+
@Test
29+
public void testExceptionConstructor() throws Exception {
30+
31+
JsonFactory factory = new JsonFactory();
32+
DefaultJacksonJsonFilter filter = new DefaultJacksonJsonFilter(factory);
33+
34+
assertNull(filter.process(new byte[] {}, 0, 100));
35+
assertFalse(filter.process(new char[] {}, 0, 100, new StringBuilder()));
1736
}
1837
}

frameworks/jackson/src/test/java/com/github/skjolber/jsonfilter/jackson/JacksonMaxSizeJsonFilterTest.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22

33
import static org.junit.Assert.assertNull;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.mockito.ArgumentMatchers.any;
6+
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.when;
58

9+
import java.io.ByteArrayOutputStream;
610
import java.io.IOException;
711
import java.nio.charset.StandardCharsets;
12+
import java.util.function.LongSupplier;
813

14+
import org.apache.commons.io.output.StringBuilderWriter;
915
import org.junit.jupiter.api.Test;
1016

17+
import com.fasterxml.jackson.core.JsonFactory;
18+
import com.fasterxml.jackson.core.JsonGenerator;
19+
import com.fasterxml.jackson.core.JsonParser;
20+
import com.github.skjolber.jsonfilter.JsonFilterMetrics;
21+
import com.github.skjolber.jsonfilter.ResizableByteArrayOutputStream;
22+
1123
public class JacksonMaxSizeJsonFilterTest extends AbstractDefaultJacksonJsonFilterTest {
1224

1325
public JacksonMaxSizeJsonFilterTest() throws Exception {
@@ -41,4 +53,94 @@ public void maxSize() throws Exception {
4153
assertThat(new JacksonMaxSizeJsonFilter(DEFAULT_MAX_SIZE)).hasMaxSize(DEFAULT_MAX_SIZE);
4254
}
4355

56+
@Test
57+
public void testConvenienceMethods() throws IOException {
58+
JsonFactory jsonFactory = mock(JsonFactory.class);
59+
when(jsonFactory.createGenerator(any(StringBuilderWriter.class))).thenThrow(new RuntimeException());
60+
when(jsonFactory.createGenerator(any(ByteArrayOutputStream.class))).thenThrow(new RuntimeException());
61+
62+
testConvenienceMethods(
63+
new JacksonMaxSizeJsonFilter(1) {
64+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
65+
return true;
66+
}
67+
public boolean process(char[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
68+
return true;
69+
}
70+
},
71+
new JacksonMaxSizeJsonFilter(1) {
72+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
73+
return false;
74+
}
75+
public boolean process(char[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
76+
return false;
77+
}
78+
},
79+
new JacksonMaxSizeJsonFilter(1, jsonFactory) {
80+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
81+
throw new RuntimeException();
82+
}
83+
@Override
84+
public boolean process(byte[] bytes, int offset, int length, ResizableByteArrayOutputStream output, JsonFilterMetrics filterMetrics) {
85+
throw new RuntimeException();
86+
}
87+
}
88+
);
89+
90+
testConvenienceMethods(
91+
new JacksonMaxSizeJsonFilter(1024) {
92+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
93+
return true;
94+
}
95+
public boolean process(char[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
96+
return true;
97+
}
98+
99+
@Override
100+
public boolean process(byte[] bytes, int offset, int length, ResizableByteArrayOutputStream output, JsonFilterMetrics filterMetrics) {
101+
return true;
102+
}
103+
104+
public boolean process(byte[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
105+
return true;
106+
}
107+
},
108+
new JacksonMaxSizeJsonFilter(1024) {
109+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
110+
return false;
111+
}
112+
public boolean process(char[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
113+
return false;
114+
}
115+
116+
@Override
117+
public boolean process(byte[] bytes, int offset, int length, ResizableByteArrayOutputStream output, JsonFilterMetrics filterMetrics) {
118+
return false;
119+
}
120+
121+
public boolean process(byte[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
122+
return false;
123+
}
124+
},
125+
new JacksonMaxSizeJsonFilter(1024, jsonFactory) {
126+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
127+
throw new RuntimeException();
128+
}
129+
130+
@Override
131+
public boolean process(byte[] bytes, int offset, int length, ResizableByteArrayOutputStream output, JsonFilterMetrics filterMetrics) {
132+
throw new RuntimeException();
133+
}
134+
135+
136+
}
137+
);
138+
}
139+
140+
@Test
141+
public void testConstructor() {
142+
new JacksonMaxSizeJsonFilter(1024, "XXX", "YYY", "ZZZ");
143+
}
144+
145+
44146
}

frameworks/jackson/src/test/java/com/github/skjolber/jsonfilter/jackson/JacksonMaxSizeMaxStringLengthJsonFilterTest.java

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.io.ByteArrayOutputStream;
88
import java.io.IOException;
9+
import java.util.function.LongSupplier;
910

1011
import org.apache.commons.io.output.StringBuilderWriter;
1112
import org.junit.jupiter.api.Test;
@@ -41,24 +42,59 @@ public void maxStringLength() throws Exception {
4142

4243
@Test
4344
public void testConvenienceMethods() throws IOException {
44-
4545
JsonFactory jsonFactory = mock(JsonFactory.class);
4646
when(jsonFactory.createGenerator(any(StringBuilderWriter.class))).thenThrow(new RuntimeException());
4747
when(jsonFactory.createGenerator(any(ByteArrayOutputStream.class))).thenThrow(new RuntimeException());
4848

4949
testConvenienceMethods(
50-
new JacksonMaxStringLengthJsonFilter(-1) {
51-
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) {
50+
new JacksonMaxSizeMaxStringLengthJsonFilter(512, 1) {
51+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
52+
return true;
53+
}
54+
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
5255
return true;
5356
}
5457
},
55-
new JacksonMaxStringLengthJsonFilter(-1) {
56-
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) {
58+
new JacksonMaxSizeMaxStringLengthJsonFilter(512, 1) {
59+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
60+
return false;
61+
}
62+
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
63+
return false;
64+
}
65+
},
66+
new JacksonMaxSizeMaxStringLengthJsonFilter(512, 1) {
67+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
5768
throw new RuntimeException();
5869
}
70+
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
71+
throw new RuntimeException();
72+
}
73+
}
74+
);
75+
76+
testConvenienceMethods(
77+
new JacksonMaxSizeMaxStringLengthJsonFilter(512, 1024) {
78+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
79+
return true;
80+
}
81+
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
82+
return true;
83+
}
84+
},
85+
new JacksonMaxSizeMaxStringLengthJsonFilter(512, 1024) {
86+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
87+
return false;
88+
}
89+
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
90+
return false;
91+
}
5992
},
60-
new JacksonMaxStringLengthJsonFilter(-1, jsonFactory) {
61-
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) {
93+
new JacksonMaxSizeMaxStringLengthJsonFilter(512, 1024) {
94+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
95+
throw new RuntimeException();
96+
}
97+
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
6298
throw new RuntimeException();
6399
}
64100
}

frameworks/jackson/src/test/java/com/github/skjolber/jsonfilter/jackson/JacksonMaxStringLengthJsonFilterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public boolean process(final JsonParser parser, JsonGenerator generator, JsonFil
4646
},
4747
new JacksonMaxStringLengthJsonFilter(-1) {
4848
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) {
49-
throw new RuntimeException();
49+
return false;
5050
}
5151
},
5252
new JacksonMaxStringLengthJsonFilter(-1, jsonFactory) {

0 commit comments

Comments
 (0)