|
24 | 24 | import com.palantir.logsafe.SafeArg; |
25 | 25 | import com.palantir.logsafe.UnsafeArg; |
26 | 26 | import java.io.IOException; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.regex.Pattern; |
27 | 30 | import org.junit.jupiter.api.Test; |
28 | 31 |
|
29 | 32 | public final class SerializableErrorTest { |
@@ -65,6 +68,63 @@ public void forException_arg_key_collisions_just_use_the_last_one() { |
65 | 68 | assertThat(SerializableError.forException(exception)).isEqualTo(expected); |
66 | 69 | } |
67 | 70 |
|
| 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 | + |
68 | 128 | @Test |
69 | 129 | public void testSerializationContainsRedundantParameters() throws Exception { |
70 | 130 | assertThat(mapper.writeValueAsString(ERROR)) |
|
0 commit comments