Skip to content

Commit 514771a

Browse files
authored
Add test coverage for current SerializableError.forException() behavior (#1048)
Add test coverage for current SerializableError.forException() behavior
1 parent 4362f44 commit 514771a

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

errors/src/test/java/com/palantir/conjure/java/api/errors/SerializableErrorTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import com.palantir.logsafe.SafeArg;
2525
import com.palantir.logsafe.UnsafeArg;
2626
import java.io.IOException;
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.regex.Pattern;
2730
import org.junit.jupiter.api.Test;
2831

2932
public final class SerializableErrorTest {
@@ -65,6 +68,63 @@ public void forException_arg_key_collisions_just_use_the_last_one() {
6568
assertThat(SerializableError.forException(exception)).isEqualTo(expected);
6669
}
6770

71+
// Discussion of potentially changing the serialization format in these three _serializesWithToString tests is at
72+
// https://github.com/palantir/conjure-java/issues/1812
73+
@Test
74+
public void forException_listArgValue_serializesWithToString() {
75+
ErrorType error = ErrorType.INTERNAL;
76+
ServiceException exception = new ServiceException(
77+
error, SafeArg.of("safe-list", List.of("1", "2")), UnsafeArg.of("unsafe-list", List.of("3", "4")));
78+
79+
SerializableError expected = new SerializableError.Builder()
80+
.errorCode(error.code().name())
81+
.errorName(error.name())
82+
.errorInstanceId(exception.getErrorInstanceId())
83+
.putParameters("safe-list", "[1, 2]")
84+
.putParameters("unsafe-list", "[3, 4]")
85+
.build();
86+
assertThat(SerializableError.forException(exception)).isEqualTo(expected);
87+
}
88+
89+
@Test
90+
public void forException_mapArgValue_serializesWithToString() {
91+
ErrorType error = ErrorType.INTERNAL;
92+
ServiceException exception = new ServiceException(
93+
error, SafeArg.of("safe-map", Map.of("1", "2")), UnsafeArg.of("unsafe-map", Map.of("ABC", "DEF")));
94+
95+
SerializableError expected = new SerializableError.Builder()
96+
.errorCode(error.code().name())
97+
.errorName(error.name())
98+
.errorInstanceId(exception.getErrorInstanceId())
99+
.putParameters("safe-map", "{1=2}")
100+
.putParameters("unsafe-map", "{ABC=DEF}")
101+
.build();
102+
assertThat(SerializableError.forException(exception)).isEqualTo(expected);
103+
}
104+
105+
@Test
106+
public void forException_mapArrayArgValue_serializesWithToString() {
107+
ErrorType error = ErrorType.INTERNAL;
108+
ServiceException exception = new ServiceException(
109+
error,
110+
SafeArg.of("safe-array", new long[] {1L, 2L, 3L}),
111+
UnsafeArg.of("unsafe-array", new String[] {"A", "B", "C"}));
112+
113+
SerializableError actual = SerializableError.forException(exception);
114+
115+
assertThat(actual.parameters())
116+
.hasEntrySatisfying(
117+
"safe-array",
118+
value ->
119+
// Example: [J@714afbd4
120+
assertThat(value).matches(Pattern.quote("[J@") + "[0-9a-f]+"))
121+
.hasEntrySatisfying(
122+
"unsafe-array",
123+
value ->
124+
// Example: [Ljava.lang.String;@769a49e3
125+
assertThat(value).matches(Pattern.quote("[Ljava.lang.String;@") + "[0-9a-f]+"));
126+
}
127+
68128
@Test
69129
public void testSerializationContainsRedundantParameters() throws Exception {
70130
assertThat(mapper.writeValueAsString(ERROR))

0 commit comments

Comments
 (0)