Search in sources :

Example 11 with BodySerDe

use of com.palantir.dialogue.BodySerDe in project dialogue by palantir.

the class BinaryEncodingTest method testBinary.

@Test
public void testBinary() throws IOException {
    TestResponse response = new TestResponse().code(200).contentType("application/octet-stream");
    BodySerDe serializers = new ConjureBodySerDe(ImmutableList.of(WeightedEncoding.of(new ConjureBodySerDeTest.StubEncoding("application/json"))), ErrorDecoder.INSTANCE, Encodings.emptyContainerDeserializer(), DefaultConjureRuntime.DEFAULT_SERDE_CACHE_SPEC);
    InputStream deserialized = serializers.inputStreamDeserializer().deserialize(response);
    assertThat(deserialized.available()).isEqualTo(0);
    CloseRecordingInputStream rawInputStream = response.body();
    rawInputStream.assertNotClosed();
    assertThat(response.isClosed()).describedAs("response is unclosed initially").isFalse();
    deserialized.close();
    assertThat(response.isClosed()).describedAs("Response#close was never called, but no big deal because the body is the only resource worth" + " closing").isFalse();
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) CloseRecordingInputStream(com.palantir.dialogue.CloseRecordingInputStream) InputStream(java.io.InputStream) BodySerDe(com.palantir.dialogue.BodySerDe) CloseRecordingInputStream(com.palantir.dialogue.CloseRecordingInputStream) Test(org.junit.jupiter.api.Test)

Example 12 with BodySerDe

use of com.palantir.dialogue.BodySerDe in project dialogue by palantir.

the class BinaryEncodingTest method testBinary_optional_present.

@Test
public void testBinary_optional_present() throws IOException {
    TestResponse response = new TestResponse().code(200).contentType("application/octet-stream");
    BodySerDe serializers = new ConjureBodySerDe(ImmutableList.of(WeightedEncoding.of(new ConjureBodySerDeTest.StubEncoding("application/json"))), ErrorDecoder.INSTANCE, Encodings.emptyContainerDeserializer(), DefaultConjureRuntime.DEFAULT_SERDE_CACHE_SPEC);
    Optional<InputStream> maybe = serializers.optionalInputStreamDeserializer().deserialize(response);
    assertThat(maybe).isPresent();
    InputStream deserialized = maybe.get();
    assertThat(deserialized.available()).isEqualTo(0);
    CloseRecordingInputStream rawInputStream = response.body();
    rawInputStream.assertNotClosed();
    assertThat(response.isClosed()).describedAs("response is unclosed initially").isFalse();
    deserialized.close();
    assertThat(response.isClosed()).describedAs("Response#close was never called, but no big deal because the body is the only resource worth" + " closing").isFalse();
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) CloseRecordingInputStream(com.palantir.dialogue.CloseRecordingInputStream) InputStream(java.io.InputStream) BodySerDe(com.palantir.dialogue.BodySerDe) CloseRecordingInputStream(com.palantir.dialogue.CloseRecordingInputStream) Test(org.junit.jupiter.api.Test)

Example 13 with BodySerDe

use of com.palantir.dialogue.BodySerDe in project dialogue by palantir.

the class ConjureBodySerDeTest method testEmptyResponse_failure.

@Test
public void testEmptyResponse_failure() {
    TestResponse response = new TestResponse().code(400);
    ServiceException serviceException = new ServiceException(ErrorType.INVALID_ARGUMENT);
    SerializableError serialized = SerializableError.forException(serviceException);
    errorDecoder = mock(ErrorDecoder.class);
    when(errorDecoder.isError(response)).thenReturn(true);
    when(errorDecoder.decode(response)).thenReturn(new RemoteException(serialized, 400));
    BodySerDe serializers = conjureBodySerDe("application/json");
    assertThatExceptionOfType(RemoteException.class).isThrownBy(() -> serializers.emptyBodyDeserializer().deserialize(response));
}
Also used : ServiceException(com.palantir.conjure.java.api.errors.ServiceException) TestResponse(com.palantir.dialogue.TestResponse) SerializableError(com.palantir.conjure.java.api.errors.SerializableError) RemoteException(com.palantir.conjure.java.api.errors.RemoteException) BodySerDe(com.palantir.dialogue.BodySerDe) Test(org.junit.jupiter.api.Test)

Example 14 with BodySerDe

use of com.palantir.dialogue.BodySerDe in project dialogue by palantir.

the class ConjureBodySerDeTest method testDefaultBinaryRequestBodyProducesNonRepeatableRequestBody.

@Test
public void testDefaultBinaryRequestBodyProducesNonRepeatableRequestBody() {
    BodySerDe serde = DefaultConjureRuntime.builder().build().bodySerDe();
    RequestBody requestBody = serde.serialize(_requestBody -> {
    });
    assertThat(requestBody.repeatable()).isFalse();
}
Also used : BodySerDe(com.palantir.dialogue.BodySerDe) BinaryRequestBody(com.palantir.dialogue.BinaryRequestBody) RequestBody(com.palantir.dialogue.RequestBody) Test(org.junit.jupiter.api.Test)

Example 15 with BodySerDe

use of com.palantir.dialogue.BodySerDe in project dialogue by palantir.

the class ConjureBodySerDeTest method testRequestOptionalEmpty.

@Test
public void testRequestOptionalEmpty() {
    TestResponse response = new TestResponse().code(204);
    BodySerDe serializers = conjureBodySerDe("application/json");
    Optional<String> value = serializers.deserializer(OPTIONAL_TYPE).deserialize(response);
    assertThat(value).isEmpty();
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) BodySerDe(com.palantir.dialogue.BodySerDe) Test(org.junit.jupiter.api.Test)

Aggregations

BodySerDe (com.palantir.dialogue.BodySerDe)20 Test (org.junit.jupiter.api.Test)20 TestResponse (com.palantir.dialogue.TestResponse)13 BinaryRequestBody (com.palantir.dialogue.BinaryRequestBody)7 RequestBody (com.palantir.dialogue.RequestBody)7 RemoteException (com.palantir.conjure.java.api.errors.RemoteException)2 SerializableError (com.palantir.conjure.java.api.errors.SerializableError)2 ServiceException (com.palantir.conjure.java.api.errors.ServiceException)2 CloseRecordingInputStream (com.palantir.dialogue.CloseRecordingInputStream)2 TypeMarker (com.palantir.dialogue.TypeMarker)2 SafeRuntimeException (com.palantir.logsafe.exceptions.SafeRuntimeException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ImmutableList (com.google.common.collect.ImmutableList)1 SafeIllegalArgumentException (com.palantir.logsafe.exceptions.SafeIllegalArgumentException)1 List (java.util.List)1