Search in sources :

Example 11 with Endpoint

use of com.palantir.dialogue.Endpoint 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 12 with Endpoint

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

Example 13 with Endpoint

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

the class QueuedChannelTest method testQueuedResponseClosedOnCancel.

@Test
public void testQueuedResponseClosedOnCancel() {
    Request queuedRequest = Request.builder().pathParams(ImmutableMap.of("foo", "bar")).build();
    when(delegate.maybeExecute(endpoint, queuedRequest, DO_NOT_SKIP_LIMITS)).thenReturn(Optional.empty());
    ListenableFuture<Response> result = queuedChannel.maybeExecute(endpoint, queuedRequest).get();
    verify(delegate, times(2)).maybeExecute(endpoint, queuedRequest, DO_NOT_SKIP_LIMITS);
    when(delegate.maybeExecute(endpoint, request, DO_NOT_SKIP_LIMITS)).thenReturn(Optional.of(Futures.immediateFuture(Mockito.mock(Response.class))));
    when(delegate.maybeExecute(endpoint, queuedRequest, DO_NOT_SKIP_LIMITS)).thenAnswer((Answer<Optional<ListenableFuture<Response>>>) _invocation -> {
        assertThat(result.cancel(true)).isTrue();
        return Optional.of(Futures.immediateFuture(mockResponse));
    });
    // Force scheduling
    queuedChannel.maybeExecute(endpoint, request);
    assertThat(result).isCancelled();
    verify(delegate, times(1)).maybeExecute(endpoint, request, DO_NOT_SKIP_LIMITS);
    verify(mockResponse, times(1)).close();
}
Also used : Response(com.palantir.dialogue.Response) BeforeEach(org.junit.jupiter.api.BeforeEach) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SettableFuture(com.google.common.util.concurrent.SettableFuture) Answer(org.mockito.stubbing.Answer) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Endpoint(com.palantir.dialogue.Endpoint) Request(com.palantir.dialogue.Request) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ImmutableMap(com.google.common.collect.ImmutableMap) LimitEnforcement(com.palantir.dialogue.core.LimitedChannel.LimitEnforcement) OngoingStubbing(org.mockito.stubbing.OngoingStubbing) Mockito.times(org.mockito.Mockito.times) TestTracing(com.palantir.tracing.TestTracing) Mockito.when(org.mockito.Mockito.when) DefaultTaggedMetricRegistry(com.palantir.tritium.metrics.registry.DefaultTaggedMetricRegistry) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Futures(com.google.common.util.concurrent.Futures) Timer(com.codahale.metrics.Timer) Optional(java.util.Optional) Response(com.palantir.dialogue.Response) Optional(java.util.Optional) Request(com.palantir.dialogue.Request) Test(org.junit.jupiter.api.Test)

Example 14 with Endpoint

use of com.palantir.dialogue.Endpoint in project conjure-java-runtime by palantir.

the class JaxRsClientDialogueEndpointTest method testPostWithBody_defaultContentType.

@Test
public void testPostWithBody_defaultContentType() {
    Channel channel = stubNoContentResponseChannel();
    StubServiceWithoutContentType service = JaxRsClient.create(StubServiceWithoutContentType.class, channel, runtime);
    service.post("Hello, World!");
    ArgumentCaptor<Endpoint> endpointCaptor = ArgumentCaptor.forClass(Endpoint.class);
    ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
    verify(channel).execute(endpointCaptor.capture(), requestCaptor.capture());
    Endpoint endpoint = endpointCaptor.getValue();
    assertThat(endpoint.serviceName()).isEqualTo("StubServiceWithoutContentType");
    assertThat(endpoint.endpointName()).isEqualTo("post");
    assertThat(endpoint.httpMethod()).isEqualTo(HttpMethod.POST);
    Request request = requestCaptor.getValue();
    assertThat(request.body()).isPresent();
    assertThat(request.body().get().contentType()).isEqualTo("application/json");
    assertThat(request.headerParams().asMap()).containsExactly(new AbstractMap.SimpleImmutableEntry<>("Content-Length", ImmutableList.of("15")));
}
Also used : AbstractMap(java.util.AbstractMap) Endpoint(com.palantir.dialogue.Endpoint) Channel(com.palantir.dialogue.Channel) Request(com.palantir.dialogue.Request) Test(org.junit.Test)

Example 15 with Endpoint

use of com.palantir.dialogue.Endpoint in project conjure-java-runtime by palantir.

the class JaxRsClientDialogueEndpointTest method testTrailingWildcardParameter_emptyString.

@Test
public void testTrailingWildcardParameter_emptyString() {
    Channel channel = stubNoContentResponseChannel();
    StubService service = JaxRsClient.create(StubService.class, channel, runtime);
    service.complexPath("dynamic0", "");
    ArgumentCaptor<Endpoint> endpointCaptor = ArgumentCaptor.forClass(Endpoint.class);
    ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
    verify(channel).execute(endpointCaptor.capture(), requestCaptor.capture());
    UrlBuilder urlBuilder = mock(UrlBuilder.class);
    endpointCaptor.getValue().renderPath(ImmutableMap.of(), urlBuilder);
    // context path
    verify(urlBuilder).pathSegment("foo");
    verify(urlBuilder).pathSegment("static0");
    verify(urlBuilder).pathSegment("dynamic0");
    verify(urlBuilder).pathSegment("static1");
    // Empty string must be included
    verify(urlBuilder).pathSegment("");
}
Also used : Endpoint(com.palantir.dialogue.Endpoint) Channel(com.palantir.dialogue.Channel) Request(com.palantir.dialogue.Request) UrlBuilder(com.palantir.dialogue.UrlBuilder) Test(org.junit.Test)

Aggregations

Endpoint (com.palantir.dialogue.Endpoint)30 Channel (com.palantir.dialogue.Channel)17 Request (com.palantir.dialogue.Request)15 Response (com.palantir.dialogue.Response)14 Test (org.junit.jupiter.api.Test)11 TestEndpoint (com.palantir.dialogue.TestEndpoint)10 TestResponse (com.palantir.dialogue.TestResponse)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)8 Test (org.junit.Test)8 EndpointChannel (com.palantir.dialogue.EndpointChannel)6 UrlBuilder (com.palantir.dialogue.UrlBuilder)6 Optional (java.util.Optional)6 Random (java.util.Random)6 Futures (com.google.common.util.concurrent.Futures)5 HttpMethod (com.palantir.dialogue.HttpMethod)5 IOException (java.io.IOException)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Meter (com.codahale.metrics.Meter)4 SafeRuntimeException (com.palantir.logsafe.exceptions.SafeRuntimeException)4