21
21
22
22
import com .fasterxml .jackson .annotation .JsonCreator ;
23
23
import com .fasterxml .jackson .annotation .JsonProperty ;
24
+ import com .fasterxml .jackson .core .JsonProcessingException ;
24
25
import com .fasterxml .jackson .databind .ObjectMapper ;
25
26
import com .google .common .collect .ImmutableList ;
26
27
import com .palantir .conjure .java .api .errors .CheckedServiceException ;
27
28
import com .palantir .conjure .java .api .errors .ErrorType ;
28
29
import com .palantir .conjure .java .api .errors .RemoteException ;
29
30
import com .palantir .conjure .java .api .errors .SerializableError ;
30
31
import com .palantir .conjure .java .dialogue .serde .EndpointErrorTestUtils .ConjureError ;
32
+ import com .palantir .conjure .java .dialogue .serde .EndpointErrorTestUtils .ContentRecordingJsonDeserializer ;
31
33
import com .palantir .conjure .java .dialogue .serde .EndpointErrorTestUtils .EndpointError ;
32
34
import com .palantir .conjure .java .dialogue .serde .EndpointErrorTestUtils .TypeReturningStubEncoding ;
33
35
import com .palantir .conjure .java .serialization .ObjectMappers ;
42
44
import com .palantir .logsafe .UnsafeArg ;
43
45
import java .io .IOException ;
44
46
import java .util .Arrays ;
47
+ import java .util .List ;
45
48
import java .util .Optional ;
46
49
import javax .annotation .Nullable ;
47
50
import javax .annotation .processing .Generated ;
51
+ import org .assertj .core .api .InstanceOfAssertFactories ;
48
52
import org .junit .jupiter .api .Test ;
49
53
import org .junit .jupiter .api .extension .ExtendWith ;
54
+ import org .junit .jupiter .params .ParameterizedTest ;
55
+ import org .junit .jupiter .params .provider .ValueSource ;
50
56
import org .mockito .junit .jupiter .MockitoExtension ;
51
57
52
58
@ ExtendWith (MockitoExtension .class )
@@ -103,8 +109,10 @@ private TestEndpointError(
103
109
}
104
110
}
105
111
106
- @ Test
107
- public void testDeserializeCustomError () throws IOException {
112
+ // The error should be deserialized using Encodings.json(), when a JSON encoding is not provided.
113
+ @ ParameterizedTest
114
+ @ ValueSource (strings = {"application/json" , "text/plain" })
115
+ public void testDeserializeCustomError (String supportedContentType ) throws IOException {
108
116
// Given
109
117
TestEndpointError errorThrownByEndpoint =
110
118
new TestEndpointError ("value" , "unsafeValue" , new ComplexArg (1 , "bar" ), Optional .of (2 ), null );
@@ -114,7 +122,7 @@ public void testDeserializeCustomError() throws IOException {
114
122
TestResponse response = TestResponse .withBody (responseBody )
115
123
.contentType ("application/json" )
116
124
.code (500 );
117
- BodySerDe serializers = conjureBodySerDe ("application/json" , "text/plain" );
125
+ BodySerDe serializers = conjureBodySerDe (supportedContentType );
118
126
DeserializerArgs <EndpointReturnBaseType > deserializerArgs = DeserializerArgs .<EndpointReturnBaseType >builder ()
119
127
.baseType (new TypeMarker <>() {})
120
128
.success (new TypeMarker <ExpectedReturnValue >() {})
@@ -201,6 +209,44 @@ public void testDeserializeExpectedValue() {
201
209
assertThat (value ).isEqualTo (new ExpectedReturnValue (expectedString ));
202
210
}
203
211
212
+ // Ensure that the supplied JSON encoding is used when available.
213
+ @ Test
214
+ public void testDeserializeWithCustomEncoding () throws JsonProcessingException {
215
+ // Given
216
+ TestEndpointError errorThrownByEndpoint =
217
+ new TestEndpointError ("value" , "unsafeValue" , new ComplexArg (1 , "bar" ), Optional .of (2 ), null );
218
+ String responseBody =
219
+ MAPPER .writeValueAsString (ConjureError .fromCheckedServiceException (errorThrownByEndpoint ));
220
+
221
+ TypeReturningStubEncoding stubbingEncoding =
222
+ new TypeReturningStubEncoding ("application/json" , ContentRecordingJsonDeserializer ::new );
223
+ BodySerDe serializers = new ConjureBodySerDe (
224
+ List .of (WeightedEncoding .of (stubbingEncoding )),
225
+ Encodings .emptyContainerDeserializer (),
226
+ DefaultConjureRuntime .DEFAULT_SERDE_CACHE_SPEC );
227
+ TestResponse response = TestResponse .withBody (responseBody )
228
+ .contentType ("application/json" )
229
+ .code (500 );
230
+
231
+ TypeMarker <ErrorReturnValue > errorTypeMarker = new TypeMarker <>() {};
232
+ DeserializerArgs <EndpointReturnBaseType > deserializerArgs = DeserializerArgs .<EndpointReturnBaseType >builder ()
233
+ .baseType (new TypeMarker <>() {})
234
+ .success (new TypeMarker <ExpectedReturnValue >() {})
235
+ .error ("Default:FailedPrecondition" , errorTypeMarker )
236
+ .build ();
237
+
238
+ // When
239
+ serializers .deserializer (deserializerArgs ).deserialize (response );
240
+
241
+ // Then
242
+ assertThat (stubbingEncoding .getDeserializer (errorTypeMarker ))
243
+ .isInstanceOfSatisfying (ContentRecordingJsonDeserializer .class , deserializer -> {
244
+ assertThat (deserializer .getDeserializedContent ())
245
+ .asInstanceOf (InstanceOfAssertFactories .LIST )
246
+ .containsExactly (responseBody );
247
+ });
248
+ }
249
+
204
250
private ConjureBodySerDe conjureBodySerDe (String ... contentTypes ) {
205
251
return new ConjureBodySerDe (
206
252
Arrays .stream (contentTypes )
0 commit comments