Search in sources :

Example 21 with TestResponse

use of com.palantir.dialogue.TestResponse 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 22 with TestResponse

use of com.palantir.dialogue.TestResponse 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 23 with TestResponse

use of com.palantir.dialogue.TestResponse 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 24 with TestResponse

use of com.palantir.dialogue.TestResponse 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)

Example 25 with TestResponse

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

the class ConjureBodySerDeTest method testErrorsDecoded.

@Test
public void testErrorsDecoded() {
    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("text/plain");
    assertThatExceptionOfType(RemoteException.class).isThrownBy(() -> serializers.deserializer(TYPE).deserialize(response));
    assertThat(response.isClosed()).describedAs("response should be closed").isTrue();
    assertThat(response.body().isClosed()).describedAs("inputstream should be closed").isTrue();
}
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)

Aggregations

TestResponse (com.palantir.dialogue.TestResponse)31 Test (org.junit.jupiter.api.Test)26 BodySerDe (com.palantir.dialogue.BodySerDe)13 Response (com.palantir.dialogue.Response)13 EndpointChannel (com.palantir.dialogue.EndpointChannel)7 CloseRecordingInputStream (com.palantir.dialogue.CloseRecordingInputStream)4 InputStream (java.io.InputStream)4 Request (com.palantir.dialogue.Request)3 TypeMarker (com.palantir.dialogue.TypeMarker)3 SafeRuntimeException (com.palantir.logsafe.exceptions.SafeRuntimeException)3 IOException (java.io.IOException)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 EnumSource (org.junit.jupiter.params.provider.EnumSource)3 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 Channel (com.palantir.dialogue.Channel)2 Endpoint (com.palantir.dialogue.Endpoint)2 Random (java.util.Random)2 Meter (com.codahale.metrics.Meter)1