Search in sources :

Example 11 with Request

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

the class DefaultClientsTest method testCallClosesRequestOnCompletion_success.

@ParameterizedTest
@EnumSource(CallType.class)
public void testCallClosesRequestOnCompletion_success(CallType callType) {
    RequestBody body = mock(RequestBody.class);
    Request request = Request.builder().body(body).build();
    when(stringDeserializer.deserialize(eq(response))).thenReturn("value");
    when(channel.execute(eq(endpoint), eq(request))).thenReturn(responseFuture);
    ListenableFuture<String> result = call(callType, request);
    // The request has been sent, but not yet completed
    verifyExecutionStarted(request);
    verify(body, never()).close();
    // Upon completion the request should be closed
    responseFuture.set(response);
    assertStringResult(callType, result);
    verify(body).close();
}
Also used : Request(com.palantir.dialogue.Request) RequestBody(com.palantir.dialogue.RequestBody) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 12 with Request

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

the class DefaultClientsTest method testCallBlockingPropagatesCallingThreadExecutor.

@Test
@Execution(ExecutionMode.SAME_THREAD)
public void testCallBlockingPropagatesCallingThreadExecutor() {
    Request request = Request.builder().build();
    when(stringDeserializer.deserialize(eq(response))).thenReturn(VALUE);
    when(channel.execute(eq(endpoint), requestCaptor.capture())).thenReturn(responseFuture);
    ListenableFuture<String> result = call(CallType.Blocking, request);
    assertThat(result).isNotDone();
    responseFuture.set(response);
    assertStringResult(CallType.Blocking, result);
    CallingThreadExecutorAssert.assertUsingCallingThreadExecutor(requestCaptor.getValue()).isNotNull();
}
Also used : Request(com.palantir.dialogue.Request) Execution(org.junit.jupiter.api.parallel.Execution) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 with Request

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

the class DefaultClientsTest method testCall.

@ParameterizedTest
@EnumSource(CallType.class)
public void testCall(CallType callType) {
    Request request = Request.builder().build();
    when(stringDeserializer.deserialize(eq(response))).thenReturn(VALUE);
    when(channel.execute(eq(endpoint), eq(request))).thenReturn(responseFuture);
    ListenableFuture<String> result = call(callType, request);
    assertThat(result).isNotDone();
    responseFuture.set(response);
    assertStringResult(callType, result);
}
Also used : Request(com.palantir.dialogue.Request) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with Request

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

the class ContentEncodingChannelTest method testChannelCreationWithTag.

@Test
void testChannelCreationWithTag() {
    EndpointChannel delegate = _req -> Futures.immediateCancelledFuture();
    EndpointChannel result = ContentEncodingChannel.of(delegate, new Endpoint() {

        @Override
        public HttpMethod httpMethod() {
            return HttpMethod.POST;
        }

        @Override
        public String serviceName() {
            return "service";
        }

        @Override
        public String endpointName() {
            return "endpoint";
        }

        @Override
        public String version() {
            return "1.2.3";
        }

        @Override
        public Set<String> tags() {
            return ImmutableSet.of("compress-request");
        }
    });
    assertThat(result).isNotSameAs(delegate);
}
Also used : OutputStream(java.io.OutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ImmutableSet(com.google.common.collect.ImmutableSet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RequestBody(com.palantir.dialogue.RequestBody) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Set(java.util.Set) IOException(java.io.IOException) HttpMethod(com.palantir.dialogue.HttpMethod) TestEndpoint(com.palantir.dialogue.TestEndpoint) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) OptionalLong(java.util.OptionalLong) Futures(com.google.common.util.concurrent.Futures) EndpointChannel(com.palantir.dialogue.EndpointChannel) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteStreams(com.google.common.io.ByteStreams) Endpoint(com.palantir.dialogue.Endpoint) Request(com.palantir.dialogue.Request) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) TestEndpoint(com.palantir.dialogue.TestEndpoint) Endpoint(com.palantir.dialogue.Endpoint) EndpointChannel(com.palantir.dialogue.EndpointChannel) HttpMethod(com.palantir.dialogue.HttpMethod) Test(org.junit.jupiter.api.Test)

Example 15 with Request

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

the class DialogueChannelTest method test_can_use_sticky_attachments_impl.

private void test_can_use_sticky_attachments_impl(NodeSelectionStrategy nodeSelectionStrategy, ImmutableList<String> uris) throws ExecutionException {
    String uriHeader = "uri";
    DialogueChannelFactory factory = args -> {
        mockChannel = Mockito.mock(Channel.class);
        lenient().when(mockChannel.execute(eq(endpoint), any())).thenAnswer((Answer<ListenableFuture<Response>>) _invocation -> Futures.immediateFuture(TestResponse.withBody(null).withHeader(uriHeader, args.uri())));
        return mockChannel;
    };
    channel = DialogueChannel.builder().channelName("my-channel").clientConfiguration(ClientConfiguration.builder().uris(uris).from(stubConfig).nodeSelectionStrategy(nodeSelectionStrategy).build()).factory(factory).random(new Random(1L)).build();
    request = createRequestWithAddExecutedOnAttachment();
    response = Futures.getDone(channel.execute(endpoint, request));
    String expectedUri = response.getFirstHeader(uriHeader).get();
    Consumer<Request> stickyTarget = StickyAttachments.copyStickyTarget(response);
    request = Request.builder().build();
    stickyTarget.accept(request);
    response = Futures.getDone(channel.execute(endpoint, request));
    assertThat(response.getFirstHeader(uriHeader)).hasValue(expectedUri);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SslConfiguration(com.palantir.conjure.java.api.config.ssl.SslConfiguration) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) Random(java.util.Random) SettableFuture(com.google.common.util.concurrent.SettableFuture) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) NodeSelectionStrategy(com.palantir.conjure.java.client.config.NodeSelectionStrategy) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) UserAgent(com.palantir.conjure.java.api.config.service.UserAgent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) RequestBody(com.palantir.dialogue.RequestBody) Channel(com.palantir.dialogue.Channel) Test(org.junit.jupiter.api.Test) TestResponse(com.palantir.dialogue.TestResponse) List(java.util.List) Optional(java.util.Optional) TypeMarker(com.palantir.dialogue.TypeMarker) Response(com.palantir.dialogue.Response) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Mock(org.mockito.Mock) EnumSource(org.junit.jupiter.params.provider.EnumSource) ClientConfiguration(com.palantir.conjure.java.client.config.ClientConfiguration) Mockito.lenient(org.mockito.Mockito.lenient) TestEndpoint(com.palantir.dialogue.TestEndpoint) Supplier(java.util.function.Supplier) Answer(org.mockito.stubbing.Answer) SocketException(java.net.SocketException) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ImmutableList(com.google.common.collect.ImmutableList) SocketTimeoutException(java.net.SocketTimeoutException) DefaultConjureRuntime(com.palantir.conjure.java.dialogue.serde.DefaultConjureRuntime) Endpoint(com.palantir.dialogue.Endpoint) ServiceConfiguration(com.palantir.conjure.java.api.config.service.ServiceConfiguration) Request(com.palantir.dialogue.Request) ClientConfigurations(com.palantir.conjure.java.client.config.ClientConfigurations) OutputStream(java.io.OutputStream) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) TestTracing(com.palantir.tracing.TestTracing) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Mockito(org.mockito.Mockito) Futures(com.google.common.util.concurrent.Futures) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Paths(java.nio.file.Paths) Collections(java.util.Collections) TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) Answer(org.mockito.stubbing.Answer) Random(java.util.Random) Request(com.palantir.dialogue.Request)

Aggregations

Request (com.palantir.dialogue.Request)40 Test (org.junit.jupiter.api.Test)21 Channel (com.palantir.dialogue.Channel)14 Endpoint (com.palantir.dialogue.Endpoint)14 Response (com.palantir.dialogue.Response)13 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 TestResponse (com.palantir.dialogue.TestResponse)8 Test (org.junit.Test)8 TestEndpoint (com.palantir.dialogue.TestEndpoint)6 Optional (java.util.Optional)6 Futures (com.google.common.util.concurrent.Futures)5 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)5 RequestBody (com.palantir.dialogue.RequestBody)5 UrlBuilder (com.palantir.dialogue.UrlBuilder)5 OutputStream (java.io.OutputStream)5 Duration (java.time.Duration)5 ExecutionException (java.util.concurrent.ExecutionException)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 EnumSource (org.junit.jupiter.params.provider.EnumSource)5 ImmutableList (com.google.common.collect.ImmutableList)4