From 03c47a905b9643bccea089b755c3c5dfcdacdee9 Mon Sep 17 00:00:00 2001 From: alzimmermsft <48699787+alzimmermsft@users.noreply.github.com> Date: Fri, 10 May 2019 11:08:25 -0700 Subject: [PATCH 1/7] Cleaned up constructors, removed newContext methods --- .../ConfigurationAsyncClientBuilder.java | 6 +- .../com/azure/core/http/HttpPipeline.java | 61 +++---------------- .../azure/core/implementation/RestProxy.java | 2 +- .../azure/core/http/HttpPipelineTests.java | 2 +- 4 files changed, 15 insertions(+), 56 deletions(-) diff --git a/applicationconfig/client/src/main/java/com/azure/applicationconfig/ConfigurationAsyncClientBuilder.java b/applicationconfig/client/src/main/java/com/azure/applicationconfig/ConfigurationAsyncClientBuilder.java index f5e94e0715ab..e4ec8b2427c4 100644 --- a/applicationconfig/client/src/main/java/com/azure/applicationconfig/ConfigurationAsyncClientBuilder.java +++ b/applicationconfig/client/src/main/java/com/azure/applicationconfig/ConfigurationAsyncClientBuilder.java @@ -128,9 +128,11 @@ public ConfigurationAsyncClient build() { policies.add(new HttpLoggingPolicy(httpLogDetailLevel)); + HttpPipelinePolicy[] pipelinePolicies = policies.toArray(new HttpPipelinePolicy[0]); + HttpPipeline pipeline = httpClient == null - ? new HttpPipeline(policies) - : new HttpPipeline(httpClient, policies); + ? new HttpPipeline(pipelinePolicies) + : new HttpPipeline(httpClient, pipelinePolicies); return new ConfigurationAsyncClient(serviceEndpoint, pipeline); } diff --git a/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java b/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java index 33aa268e1c42..cb898f25ec73 100644 --- a/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java +++ b/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java @@ -7,7 +7,6 @@ import reactor.core.publisher.Mono; import java.util.Arrays; -import java.util.List; import java.util.Objects; /** @@ -48,37 +47,6 @@ public HttpPipeline(HttpPipelinePolicy... pipelinePolicies) { this(HttpClient.createDefault(), pipelinePolicies); } - /** - * Creates a HttpPipeline holding array of policies that gets applied to all request initiated through - * {@link HttpPipeline#send(HttpPipelineCallContext)} and it's response. - * - * @param httpClient the http client to write request to wire and receive response from wire. - * @param pipelinePolicies pipeline policies in the order they need to applied, a copy of this list - * will be made so changing the original list after the creation of pipeline - * will not mutate the pipeline - */ - public HttpPipeline(HttpClient httpClient, List pipelinePolicies) { - Objects.requireNonNull(httpClient); - Objects.requireNonNull(pipelinePolicies); - this.pipelinePolicies = pipelinePolicies.toArray(new HttpPipelinePolicy[0]); - this.httpClient = httpClient; - } - - /** - * Creates a HttpPipeline holding array of policies that gets applied all request initiated through - * {@link HttpPipeline#send(HttpPipelineCallContext)} and it's response. - * - * The default HttpClient {@link HttpClient#createDefault()} will be used to write request to wire and - * receive response from wire. - * - * @param pipelinePolicies pipeline policies in the order they need to applied, a copy of this list - * will be made so changing the original list after the creation of pipeline - * will not mutate the pipeline - */ - public HttpPipeline(List pipelinePolicies) { - this(HttpClient.createDefault(), pipelinePolicies); - } - /** * Get the policies in the pipeline. * @@ -98,34 +66,23 @@ public HttpClient httpClient() { } /** - * Creates a new context local to the provided http request. - * - * @param httpRequest the request for a context needs to be created - * @return the request context - */ - public HttpPipelineCallContext newContext(HttpRequest httpRequest) { - return new HttpPipelineCallContext(httpRequest); - } - - /** - * Creates a new context local to the provided http request. + * Wraps the request in a context and send it through pipeline. * - * @param httpRequest the request for a context needs to be created - * @param data the data to associate with the context - * @return the request context + * @param request the request + * @return a publisher upon subscription flows the context through policies, sends the request and emits response upon completion */ - public HttpPipelineCallContext newContext(HttpRequest httpRequest, ContextData data) { - return new HttpPipelineCallContext(httpRequest, data); + public Mono send(HttpRequest request) { + return this.send(new HttpPipelineCallContext(request)); } /** - * Wraps the request in a context and send it through pipeline. - * + * Wraps the request in a context with additional metadata and sends it through the pipeline. * @param request the request + * @param data additional metadata to pass along in the request * @return a publisher upon subscription flows the context through policies, sends the request and emits response upon completion */ - public Mono send(HttpRequest request) { - return this.send(this.newContext(request)); + public Mono send(HttpRequest request, ContextData data) { + return this.send(new HttpPipelineCallContext(request, data)); } /** diff --git a/core/azure-core/src/main/java/com/azure/core/implementation/RestProxy.java b/core/azure-core/src/main/java/com/azure/core/implementation/RestProxy.java index 545f620cd804..b36a920de676 100644 --- a/core/azure-core/src/main/java/com/azure/core/implementation/RestProxy.java +++ b/core/azure-core/src/main/java/com/azure/core/implementation/RestProxy.java @@ -111,7 +111,7 @@ public SerializerAdapter serializer() { * @return a {@link Mono} that emits HttpResponse asynchronously */ public Mono send(HttpRequest request, ContextData contextData) { - return httpPipeline.send(httpPipeline.newContext(request, contextData)); + return httpPipeline.send(request, contextData); } @Override diff --git a/core/azure-core/src/test/java/com/azure/core/http/HttpPipelineTests.java b/core/azure-core/src/test/java/com/azure/core/http/HttpPipelineTests.java index 9561cdd6f79e..afcc187ec1f2 100644 --- a/core/azure-core/src/test/java/com/azure/core/http/HttpPipelineTests.java +++ b/core/azure-core/src/test/java/com/azure/core/http/HttpPipelineTests.java @@ -49,7 +49,7 @@ public void withRequestOptions() throws MalformedURLException { new ProtocolPolicy("ftp", true), new RetryPolicy()); - HttpPipelineCallContext context = pipeline.newContext(new HttpRequest(HttpMethod.GET, new URL("http://foo.com"))); + HttpPipelineCallContext context = new HttpPipelineCallContext(new HttpRequest(HttpMethod.GET, new URL("http://foo.com"))); assertNotNull(context); assertNotNull(pipeline.httpClient()); assertTrue(pipeline.httpClient() instanceof ReactorNettyClient); From 84f0d31b0b5311fc9164cb7e6491466768715b99 Mon Sep 17 00:00:00 2001 From: alzimmermsft <48699787+alzimmermsft@users.noreply.github.com> Date: Fri, 10 May 2019 14:01:26 -0700 Subject: [PATCH 2/7] Removed pipelinePolicy method and replace it with getPolicy and getPolicyCount --- .../java/com/azure/core/http/HttpPipeline.java | 18 +++++++++++++----- .../core/http/HttpPipelineNextPolicy.java | 14 +++++++------- .../com/azure/core/http/HttpPipelineTests.java | 10 +++++----- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java b/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java index cb898f25ec73..f8d812a85d5d 100644 --- a/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java +++ b/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java @@ -48,12 +48,20 @@ public HttpPipeline(HttpPipelinePolicy... pipelinePolicies) { } /** - * Get the policies in the pipeline. - * - * @return policies in the pipeline + * Get the policy at the passed index in the pipeline. + * @param index index of the the policy to retrieve. + * @return the policy stored at that index. + */ + public HttpPipelinePolicy getPolicy(int index) { + return this.pipelinePolicies[index]; + } + + /** + * Get the count of policies in the pipeline. + * @return count of policies. */ - public HttpPipelinePolicy[] pipelinePolicies() { - return Arrays.copyOf(this.pipelinePolicies, this.pipelinePolicies.length); + public int getPolicyCount() { + return this.pipelinePolicies.length; } /** diff --git a/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineNextPolicy.java b/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineNextPolicy.java index c56e2c833465..9d7a54e1792d 100644 --- a/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineNextPolicy.java +++ b/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineNextPolicy.java @@ -34,16 +34,16 @@ public class HttpPipelineNextPolicy { * @return a publisher upon subscription invokes next policy and emits response from the policy. */ public Mono process() { - final int size = this.pipeline.pipelinePolicies().length; + final int size = this.pipeline.getPolicyCount(); if (this.currentPolicyIndex > size) { return Mono.error(new IllegalStateException("There is no more policies to execute.")); + } + + this.currentPolicyIndex++; + if (this.currentPolicyIndex == size) { + return this.pipeline.httpClient().send(this.context.httpRequest()); } else { - this.currentPolicyIndex++; - if (this.currentPolicyIndex == size) { - return this.pipeline.httpClient().send(this.context.httpRequest()); - } else { - return this.pipeline.pipelinePolicies()[this.currentPolicyIndex].process(this.context, this); - } + return this.pipeline.getPolicy(this.currentPolicyIndex).process(this.context, this); } } diff --git a/core/azure-core/src/test/java/com/azure/core/http/HttpPipelineTests.java b/core/azure-core/src/test/java/com/azure/core/http/HttpPipelineTests.java index afcc187ec1f2..3502699b82aa 100644 --- a/core/azure-core/src/test/java/com/azure/core/http/HttpPipelineTests.java +++ b/core/azure-core/src/test/java/com/azure/core/http/HttpPipelineTests.java @@ -24,7 +24,7 @@ public class HttpPipelineTests { @Test public void constructorWithNoArguments() { HttpPipeline pipeline = new HttpPipeline(); - assertEquals(0, pipeline.pipelinePolicies().length); + assertEquals(0, pipeline.getPolicyCount()); assertNotNull(pipeline.httpClient()); assertTrue(pipeline.httpClient() instanceof ReactorNettyClient); } @@ -35,10 +35,10 @@ public void withRequestPolicy() { new ProtocolPolicy("ftp", true), new RetryPolicy()); - assertEquals(3, pipeline.pipelinePolicies().length); - assertEquals(PortPolicy.class, pipeline.pipelinePolicies()[0].getClass()); - assertEquals(ProtocolPolicy.class, pipeline.pipelinePolicies()[1].getClass()); - assertEquals(RetryPolicy.class, pipeline.pipelinePolicies()[2].getClass()); + assertEquals(3, pipeline.getPolicyCount()); + assertEquals(PortPolicy.class, pipeline.getPolicy(0).getClass()); + assertEquals(ProtocolPolicy.class, pipeline.getPolicy(1).getClass()); + assertEquals(RetryPolicy.class, pipeline.getPolicy(2).getClass()); assertNotNull(pipeline.httpClient()); assertTrue(pipeline.httpClient() instanceof ReactorNettyClient); } From 3892e45ebaf7830188d52eb818d4d574b5d54c66 Mon Sep 17 00:00:00 2001 From: alzimmermsft <48699787+alzimmermsft@users.noreply.github.com> Date: Tue, 14 May 2019 13:02:28 -0700 Subject: [PATCH 3/7] Made index parameter final --- .../src/main/java/com/azure/core/http/HttpPipeline.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java b/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java index f8d812a85d5d..3b9802163cbc 100644 --- a/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java +++ b/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java @@ -52,7 +52,7 @@ public HttpPipeline(HttpPipelinePolicy... pipelinePolicies) { * @param index index of the the policy to retrieve. * @return the policy stored at that index. */ - public HttpPipelinePolicy getPolicy(int index) { + public HttpPipelinePolicy getPolicy(final int index) { return this.pipelinePolicies[index]; } From 10a262572b95e61bafdfdfab4ce0f62d69f219ee Mon Sep 17 00:00:00 2001 From: alzimmermsft <48699787+alzimmermsft@users.noreply.github.com> Date: Thu, 16 May 2019 13:09:26 -0700 Subject: [PATCH 4/7] HttpPipeline uses the builder pattern now --- .../ConfigurationAsyncClientBuilder.java | 9 +- .../com/azure/core/management/AzureProxy.java | 5 +- .../core/management/AzureProxyTests.java | 4 +- .../AzureProxyToRestProxyTests.java | 4 +- .../com/azure/core/http/HttpPipeline.java | 30 +++---- .../azure/core/http/HttpPipelineBuilder.java | 84 +++++++++++++++++++ .../azure/core/implementation/RestProxy.java | 5 +- .../java/com/azure/core/CredentialsTests.java | 16 ++-- .../java/com/azure/core/UserAgentTests.java | 18 ++-- .../azure/core/http/HttpPipelineTests.java | 52 +++++++----- .../core/http/policy/HostPolicyTests.java | 20 +++-- .../core/http/policy/ProtocolPolicyTests.java | 40 +++++---- .../ProxyAuthenticationPolicyTests.java | 10 ++- .../http/policy/RequestIdPolicyTests.java | 73 ++++++++-------- .../core/http/policy/RetryPolicyTests.java | 43 +++++----- .../implementation/RestProxyStressTests.java | 9 +- .../core/implementation/RestProxyTests.java | 18 ++-- .../RestProxyWithMockTests.java | 16 ++-- .../implementation/RestProxyXMLTests.java | 12 ++- 19 files changed, 304 insertions(+), 164 deletions(-) create mode 100644 core/azure-core/src/main/java/com/azure/core/http/HttpPipelineBuilder.java diff --git a/applicationconfig/client/src/main/java/com/azure/applicationconfig/ConfigurationAsyncClientBuilder.java b/applicationconfig/client/src/main/java/com/azure/applicationconfig/ConfigurationAsyncClientBuilder.java index e4ec8b2427c4..f66d8b345619 100644 --- a/applicationconfig/client/src/main/java/com/azure/applicationconfig/ConfigurationAsyncClientBuilder.java +++ b/applicationconfig/client/src/main/java/com/azure/applicationconfig/ConfigurationAsyncClientBuilder.java @@ -128,11 +128,10 @@ public ConfigurationAsyncClient build() { policies.add(new HttpLoggingPolicy(httpLogDetailLevel)); - HttpPipelinePolicy[] pipelinePolicies = policies.toArray(new HttpPipelinePolicy[0]); - - HttpPipeline pipeline = httpClient == null - ? new HttpPipeline(pipelinePolicies) - : new HttpPipeline(httpClient, pipelinePolicies); + HttpPipeline pipeline = HttpPipeline.builder() + .setPolicies(policies) + .httpClient(httpClient) + .build(); return new ConfigurationAsyncClient(serviceEndpoint, pipeline); } diff --git a/core/azure-core-management/src/main/java/com/azure/core/management/AzureProxy.java b/core/azure-core-management/src/main/java/com/azure/core/management/AzureProxy.java index a8440d1bcae2..4a0d07a97023 100644 --- a/core/azure-core-management/src/main/java/com/azure/core/management/AzureProxy.java +++ b/core/azure-core-management/src/main/java/com/azure/core/management/AzureProxy.java @@ -199,7 +199,10 @@ public static HttpPipeline createDefaultPipeline(Class swaggerInterface, Http if (credentialsPolicy != null) { policies.add(credentialsPolicy); } - return new HttpPipeline(policies.toArray(new HttpPipelinePolicy[policies.size()])); + + return HttpPipeline.builder() + .setPolicies(policies) + .build(); } /** diff --git a/core/azure-core-management/src/test/java/com/azure/core/management/AzureProxyTests.java b/core/azure-core-management/src/test/java/com/azure/core/management/AzureProxyTests.java index dd275d21bce6..d75f53894eee 100644 --- a/core/azure-core-management/src/test/java/com/azure/core/management/AzureProxyTests.java +++ b/core/azure-core-management/src/test/java/com/azure/core/management/AzureProxyTests.java @@ -850,7 +850,9 @@ public Mono send(HttpRequest request) { } private static T createMockService(Class serviceClass, MockAzureHttpClient httpClient) { - HttpPipeline pipeline = new HttpPipeline(httpClient); + HttpPipeline pipeline = HttpPipeline.builder() + .httpClient(httpClient) + .build(); return AzureProxy.create(serviceClass, null, pipeline, SERIALIZER); } diff --git a/core/azure-core-management/src/test/java/com/azure/core/management/AzureProxyToRestProxyTests.java b/core/azure-core-management/src/test/java/com/azure/core/management/AzureProxyToRestProxyTests.java index 2ab4376459b0..a5826149f8cb 100644 --- a/core/azure-core-management/src/test/java/com/azure/core/management/AzureProxyToRestProxyTests.java +++ b/core/azure-core-management/src/test/java/com/azure/core/management/AzureProxyToRestProxyTests.java @@ -745,7 +745,9 @@ public void service18GetStatus500WithExpectedResponse500() { } private T createService(Class serviceClass) { - HttpPipeline pipeline = new HttpPipeline(createHttpClient()); + HttpPipeline pipeline = HttpPipeline.builder() + .httpClient(createHttpClient()) + .build(); // return AzureProxy.create(serviceClass, null, pipeline, SERIALIZER); } diff --git a/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java b/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java index 7eefd8c0a2ab..fe00a17557bd 100644 --- a/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java +++ b/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java @@ -8,6 +8,7 @@ import reactor.core.publisher.Mono; import java.util.Arrays; +import java.util.List; import java.util.Objects; /** @@ -17,35 +18,30 @@ public final class HttpPipeline { private final HttpClient httpClient; private final HttpPipelinePolicy[] pipelinePolicies; + /** - * Creates a HttpPipeline holding array of policies that gets applied to all request initiated through - * {@link HttpPipeline#send(HttpPipelineCallContext)} and it's response. + * Creates a builder that can configure options for the HttpPipeline before creating an instance of it. * - * @param httpClient the http client to write request to wire and receive response from wire. - * @param pipelinePolicies pipeline policies in the order they need to applied, a copy of this array will - * be made hence changing the original array after the creation of pipeline - * will not mutate the pipeline + * @return A new {@link HttpPipelineBuilder} to create a HttpPipeline from. */ - public HttpPipeline(HttpClient httpClient, HttpPipelinePolicy... pipelinePolicies) { - Objects.requireNonNull(httpClient); - Objects.requireNonNull(pipelinePolicies); - this.pipelinePolicies = Arrays.copyOf(pipelinePolicies, pipelinePolicies.length); - this.httpClient = httpClient; + public static HttpPipelineBuilder builder() { + return new HttpPipelineBuilder(); } /** - * Creates a HttpPipeline holding array of policies that gets applied all request initiated through + * Creates a HttpPipeline holding array of policies that gets applied to all request initiated through * {@link HttpPipeline#send(HttpPipelineCallContext)} and it's response. * - * The default HttpClient {@link HttpClient#createDefault()} will be used to write request to wire and - * receive response from wire. - * + * @param httpClient the http client to write request to wire and receive response from wire. * @param pipelinePolicies pipeline policies in the order they need to applied, a copy of this array will * be made hence changing the original array after the creation of pipeline * will not mutate the pipeline */ - public HttpPipeline(HttpPipelinePolicy... pipelinePolicies) { - this(HttpClient.createDefault(), pipelinePolicies); + HttpPipeline(HttpClient httpClient, List pipelinePolicies) { + Objects.requireNonNull(httpClient); + Objects.requireNonNull(pipelinePolicies); + this.httpClient = httpClient; + this.pipelinePolicies = pipelinePolicies.toArray(new HttpPipelinePolicy[0]); } /** diff --git a/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineBuilder.java b/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineBuilder.java new file mode 100644 index 000000000000..fcee453e8a61 --- /dev/null +++ b/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineBuilder.java @@ -0,0 +1,84 @@ +package com.azure.core.http; + +import com.azure.core.http.policy.HttpPipelinePolicy; + +import java.util.ArrayList; +import java.util.List; + +/** + * This class provides a fluent builder API to help aid the configuration and instantiation of the {@link HttpPipeline}, + * calling {@link HttpPipelineBuilder#build() build} constructs an instance of the pipeline. + * + *
+ * HttpPipeline.builder()
+ *     .httpClient(httpClient)
+ *     .addPolicy(httpPipelinePolicy)
+ *     .build();
+ * 
+ * + *
+ * HttpPipeline.builder()
+ *     .httpClient(httpClient)
+ *     .setPolicies(httpPipelinePolicies)
+ *     .build();
+ * 
+ * + * @see HttpPipeline + */ +public class HttpPipelineBuilder { + private HttpClient httpClient; + private List pipelinePolicies = new ArrayList<>(); + + + HttpPipelineBuilder() { + } + + /** + * Creates a {@link HttpPipeline} based on options set in the Builder. Every time {@code build()} is + * called, a new instance of {@link HttpPipeline} is created. + * + * If HttpClient is not set then the default HttpClient is used. + * + * @return A HttpPipeline with the options set from the builder. + */ + public HttpPipeline build() { + if (httpClient == null) { + return new HttpPipeline(HttpClient.createDefault(), pipelinePolicies); + } else { + return new HttpPipeline(httpClient, pipelinePolicies); + } + } + + /** + * Sets the HttpClient for the pipeline instance. + * + * @param httpClient The HttpClient the pipeline will use when sending requests. + * @return The updated HttpPipelineBuilder object. + */ + public HttpPipelineBuilder httpClient(HttpClient httpClient) { + this.httpClient = httpClient; + return this; + } + + /** + * Adds a {@link HttpPipelinePolicy} to the list of policies that the pipeline will use. + * + * @param policy Policy to add to the policy set. + * @return The updated HttpPipelineBuilder object. + */ + public HttpPipelineBuilder addPolicy(HttpPipelinePolicy policy) { + this.pipelinePolicies.add(policy); + return this; + } + + /** + * Sets the policies that the pipeline will use. + * + * @param policies List of policies + * @return The updated HttpPipelineBuilder object. + */ + public HttpPipelineBuilder setPolicies(List policies) { + this.pipelinePolicies = new ArrayList<>(policies); + return this; + } +} diff --git a/core/azure-core/src/main/java/com/azure/core/implementation/RestProxy.java b/core/azure-core/src/main/java/com/azure/core/implementation/RestProxy.java index 4bf69f1c8b73..38748fa5aa53 100644 --- a/core/azure-core/src/main/java/com/azure/core/implementation/RestProxy.java +++ b/core/azure-core/src/main/java/com/azure/core/implementation/RestProxy.java @@ -558,7 +558,10 @@ public static HttpPipeline createDefaultPipeline(HttpPipelinePolicy credentialsP if (credentialsPolicy != null) { policies.add(credentialsPolicy); } - return new HttpPipeline(policies.toArray(new HttpPipelinePolicy[policies.size()])); + + return HttpPipeline.builder() + .setPolicies(policies) + .build(); } /** diff --git a/core/azure-core/src/test/java/com/azure/core/CredentialsTests.java b/core/azure-core/src/test/java/com/azure/core/CredentialsTests.java index 1b0c1c1c19ac..23ff591bea8b 100644 --- a/core/azure-core/src/test/java/com/azure/core/CredentialsTests.java +++ b/core/azure-core/src/test/java/com/azure/core/CredentialsTests.java @@ -28,9 +28,11 @@ public void basicCredentialsTest() throws Exception { return next.process(); }; // - final HttpPipeline pipeline = new HttpPipeline(new MockHttpClient(), - new CredentialsPolicy(credentials), - auditorPolicy); + final HttpPipeline pipeline = HttpPipeline.builder() + .httpClient(new MockHttpClient()) + .addPolicy(new CredentialsPolicy(credentials)) + .addPolicy(auditorPolicy) + .build(); HttpRequest request = new HttpRequest(HttpMethod.GET, new URL("http://localhost")); @@ -47,9 +49,11 @@ public void tokenCredentialsTest() throws Exception { return next.process(); }; - final HttpPipeline pipeline = new HttpPipeline(new MockHttpClient(), - new CredentialsPolicy(credentials), - auditorPolicy); + final HttpPipeline pipeline = HttpPipeline.builder() + .httpClient(new MockHttpClient()) + .addPolicy(new CredentialsPolicy(credentials)) + .addPolicy(auditorPolicy) + .build(); HttpRequest request = new HttpRequest(HttpMethod.GET, new URL("http://localhost")); pipeline.send(request).block(); diff --git a/core/azure-core/src/test/java/com/azure/core/UserAgentTests.java b/core/azure-core/src/test/java/com/azure/core/UserAgentTests.java index 86ba18720c4a..f554fbe2c046 100644 --- a/core/azure-core/src/test/java/com/azure/core/UserAgentTests.java +++ b/core/azure-core/src/test/java/com/azure/core/UserAgentTests.java @@ -19,7 +19,8 @@ public class UserAgentTests { @Test public void defaultUserAgentTests() throws Exception { - final HttpPipeline pipeline = new HttpPipeline(new MockHttpClient() { + final HttpPipeline pipeline = HttpPipeline.builder() + .httpClient(new MockHttpClient() { @Override public Mono send(HttpRequest request) { Assert.assertEquals( @@ -27,8 +28,9 @@ public Mono send(HttpRequest request) { "AutoRest-Java"); return Mono.just(new MockHttpResponse(request, 200)); } - }, - new UserAgentPolicy("AutoRest-Java")); + }) + .addPolicy(new UserAgentPolicy("AutoRest-Java")) + .build(); HttpResponse response = pipeline.send(new HttpRequest( HttpMethod.GET, new URL("http://localhost"))).block(); @@ -38,15 +40,17 @@ public Mono send(HttpRequest request) { @Test public void customUserAgentTests() throws Exception { - final HttpPipeline pipeline = new HttpPipeline(new MockHttpClient() { - @Override + final HttpPipeline pipeline = HttpPipeline.builder() + .httpClient(new MockHttpClient() { + @Override public Mono send(HttpRequest request) { String header = request.headers().value("User-Agent"); Assert.assertEquals("Awesome", header); return Mono.just(new MockHttpResponse(request, 200)); } - }, - new UserAgentPolicy("Awesome")); + }) + .addPolicy(new UserAgentPolicy("Awesome")) + .build(); HttpResponse response = pipeline.send(new HttpRequest(HttpMethod.GET, new URL("http://localhost"))).block(); diff --git a/core/azure-core/src/test/java/com/azure/core/http/HttpPipelineTests.java b/core/azure-core/src/test/java/com/azure/core/http/HttpPipelineTests.java index 3502699b82aa..80d558084792 100644 --- a/core/azure-core/src/test/java/com/azure/core/http/HttpPipelineTests.java +++ b/core/azure-core/src/test/java/com/azure/core/http/HttpPipelineTests.java @@ -23,7 +23,7 @@ public class HttpPipelineTests { @Test public void constructorWithNoArguments() { - HttpPipeline pipeline = new HttpPipeline(); + HttpPipeline pipeline = HttpPipeline.builder().build(); assertEquals(0, pipeline.getPolicyCount()); assertNotNull(pipeline.httpClient()); assertTrue(pipeline.httpClient() instanceof ReactorNettyClient); @@ -31,9 +31,11 @@ public void constructorWithNoArguments() { @Test public void withRequestPolicy() { - HttpPipeline pipeline = new HttpPipeline(new PortPolicy(80, true), - new ProtocolPolicy("ftp", true), - new RetryPolicy()); + HttpPipeline pipeline = HttpPipeline.builder() + .addPolicy(new PortPolicy(80, true)) + .addPolicy(new ProtocolPolicy("ftp", true)) + .addPolicy(new RetryPolicy()) + .build(); assertEquals(3, pipeline.getPolicyCount()); assertEquals(PortPolicy.class, pipeline.getPolicy(0).getClass()); @@ -45,9 +47,11 @@ public void withRequestPolicy() { @Test public void withRequestOptions() throws MalformedURLException { - HttpPipeline pipeline = new HttpPipeline(new PortPolicy(80, true), - new ProtocolPolicy("ftp", true), - new RetryPolicy()); + HttpPipeline pipeline = HttpPipeline.builder() + .addPolicy(new PortPolicy(80, true)) + .addPolicy(new ProtocolPolicy("ftp", true)) + .addPolicy(new RetryPolicy()) + .build(); HttpPipelineCallContext context = new HttpPipelineCallContext(new HttpRequest(HttpMethod.GET, new URL("http://foo.com"))); assertNotNull(context); @@ -59,15 +63,17 @@ public void withRequestOptions() throws MalformedURLException { public void withNoRequestPolicies() throws MalformedURLException { final HttpMethod expectedHttpMethod = HttpMethod.GET; final URL expectedUrl = new URL("http://my.site.com"); - final HttpPipeline httpPipeline = new HttpPipeline(new MockHttpClient() { - @Override - public Mono send(HttpRequest request) { - assertEquals(0, request.headers().size()); - assertEquals(expectedHttpMethod, request.httpMethod()); - assertEquals(expectedUrl, request.url()); - return Mono.just(new MockHttpResponse(request, 200)); - } - }); + final HttpPipeline httpPipeline = HttpPipeline.builder() + .httpClient(new MockHttpClient() { + @Override + public Mono send(HttpRequest request) { + assertEquals(0, request.headers().size()); + assertEquals(expectedHttpMethod, request.httpMethod()); + assertEquals(expectedUrl, request.url()); + return Mono.just(new MockHttpResponse(request, 200)); + } + }) + .build(); final HttpResponse response = httpPipeline.send(new HttpRequest(expectedHttpMethod, expectedUrl)).block(); assertNotNull(response); @@ -90,8 +96,10 @@ public Mono send(HttpRequest request) { } }; - final HttpPipeline httpPipeline = new HttpPipeline(httpClient, - new UserAgentPolicy(expectedUserAgent)); + final HttpPipeline httpPipeline = HttpPipeline.builder() + .httpClient(httpClient) + .addPolicy(new UserAgentPolicy(expectedUserAgent)) + .build(); final HttpResponse response = httpPipeline.send(new HttpRequest(expectedHttpMethod, expectedUrl)).block(); assertNotNull(response); @@ -102,7 +110,8 @@ public Mono send(HttpRequest request) { public void withRequestIdRequestPolicy() throws MalformedURLException { final HttpMethod expectedHttpMethod = HttpMethod.GET; final URL expectedUrl = new URL("http://my.site.com/1"); - final HttpPipeline httpPipeline = new HttpPipeline(new MockHttpClient() { + final HttpPipeline httpPipeline = HttpPipeline.builder() + .httpClient(new MockHttpClient() { @Override public Mono send(HttpRequest request) { assertEquals(1, request.headers().size()); @@ -114,8 +123,9 @@ public Mono send(HttpRequest request) { assertEquals(expectedUrl, request.url()); return Mono.just(new MockHttpResponse(request, 200)); } - }, - new RequestIdPolicy()); + }) + .addPolicy(new RequestIdPolicy()) + .build(); final HttpResponse response = httpPipeline.send(new HttpRequest(expectedHttpMethod, expectedUrl)).block(); assertNotNull(response); diff --git a/core/azure-core/src/test/java/com/azure/core/http/policy/HostPolicyTests.java b/core/azure-core/src/test/java/com/azure/core/http/policy/HostPolicyTests.java index 1feec4572b12..7dfd8b6c8437 100644 --- a/core/azure-core/src/test/java/com/azure/core/http/policy/HostPolicyTests.java +++ b/core/azure-core/src/test/java/com/azure/core/http/policy/HostPolicyTests.java @@ -32,17 +32,19 @@ public void withPort() throws MalformedURLException { } private static HttpPipeline createPipeline(String host, String expectedUrl) { - return new HttpPipeline(new MockHttpClient() { - @Override - public Mono send(HttpRequest request) { - return Mono.empty(); // NOP - } - }, - new HostPolicy(host), - (context, next) -> { + return HttpPipeline.builder() + .httpClient(new MockHttpClient() { + @Override + public Mono send(HttpRequest request) { + return Mono.empty(); // NOP + } + }) + .addPolicy(new HostPolicy(host)) + .addPolicy((context, next) -> { assertEquals(expectedUrl, context.httpRequest().url().toString()); return next.process(); - }); + }) + .build(); } private static HttpRequest createHttpRequest(String url) throws MalformedURLException { diff --git a/core/azure-core/src/test/java/com/azure/core/http/policy/ProtocolPolicyTests.java b/core/azure-core/src/test/java/com/azure/core/http/policy/ProtocolPolicyTests.java index 9f0c11807d6b..ca535fe804b2 100644 --- a/core/azure-core/src/test/java/com/azure/core/http/policy/ProtocolPolicyTests.java +++ b/core/azure-core/src/test/java/com/azure/core/http/policy/ProtocolPolicyTests.java @@ -32,31 +32,35 @@ public void withNoOverwrite() throws MalformedURLException { pipeline.send(createHttpRequest("https://www.bing.com")); } private static HttpPipeline createPipeline(String protocol, String expectedUrl) { - return new HttpPipeline(new MockHttpClient() { - @Override - public Mono send(HttpRequest request) { - return Mono.empty(); // NOP - } - }, - new ProtocolPolicy(protocol, true), - (context, next) -> { + return HttpPipeline.builder() + .httpClient(new MockHttpClient() { + @Override + public Mono send(HttpRequest request) { + return Mono.empty(); // NOP + } + }) + .addPolicy(new ProtocolPolicy(protocol, true)) + .addPolicy((context, next) -> { assertEquals(expectedUrl, context.httpRequest().url().toString()); return next.process(); - }); + }) + .build(); } private static HttpPipeline createPipeline(String protocol, boolean overwrite, String expectedUrl) { - return new HttpPipeline(new MockHttpClient() { - @Override - public Mono send(HttpRequest request) { - return Mono.empty(); // NOP - } - }, - new ProtocolPolicy(protocol, overwrite), - (context, next) -> { + return HttpPipeline.builder() + .httpClient(new MockHttpClient() { + @Override + public Mono send(HttpRequest request) { + return Mono.empty(); // NOP + } + }) + .addPolicy(new ProtocolPolicy(protocol, overwrite)) + .addPolicy((context, next) -> { assertEquals(expectedUrl, context.httpRequest().url().toString()); return next.process(); - }); + }) + .build(); } private static HttpRequest createHttpRequest(String url) throws MalformedURLException { diff --git a/core/azure-core/src/test/java/com/azure/core/http/policy/ProxyAuthenticationPolicyTests.java b/core/azure-core/src/test/java/com/azure/core/http/policy/ProxyAuthenticationPolicyTests.java index 1b6369793df5..5ac253ead6b7 100644 --- a/core/azure-core/src/test/java/com/azure/core/http/policy/ProxyAuthenticationPolicyTests.java +++ b/core/azure-core/src/test/java/com/azure/core/http/policy/ProxyAuthenticationPolicyTests.java @@ -23,13 +23,15 @@ public void test() throws MalformedURLException { final String username = "testuser"; final String password = "testpass"; // - final HttpPipeline pipeline = new HttpPipeline(new MockHttpClient(), - new ProxyAuthenticationPolicy(username, password), - (context, next) -> { + final HttpPipeline pipeline = HttpPipeline.builder() + .httpClient(new MockHttpClient()) + .addPolicy(new ProxyAuthenticationPolicy(username, password)) + .addPolicy((context, next) -> { assertEquals("Basic dGVzdHVzZXI6dGVzdHBhc3M=", context.httpRequest().headers().value("Proxy-Authentication")); auditorVisited.set(true); return next.process(); - }); + }) + .build(); pipeline.send(new HttpRequest(HttpMethod.GET, new URL("http://localhost"))) .block(); diff --git a/core/azure-core/src/test/java/com/azure/core/http/policy/RequestIdPolicyTests.java b/core/azure-core/src/test/java/com/azure/core/http/policy/RequestIdPolicyTests.java index f67e324b2de0..b7366e3c2ff6 100644 --- a/core/azure-core/src/test/java/com/azure/core/http/policy/RequestIdPolicyTests.java +++ b/core/azure-core/src/test/java/com/azure/core/http/policy/RequestIdPolicyTests.java @@ -62,23 +62,26 @@ public Mono bodyAsString(Charset charset) { @Test public void newRequestIdForEachCall() throws Exception { - HttpPipeline pipeline = new HttpPipeline(new MockHttpClient() { - String firstRequestId = null; - @Override - public Mono send(HttpRequest request) { - if (firstRequestId != null) { - String newRequestId = request.headers().value(REQUEST_ID_HEADER); - Assert.assertNotNull(newRequestId); - Assert.assertNotEquals(newRequestId, firstRequestId); + HttpPipeline pipeline = HttpPipeline.builder() + .httpClient(new MockHttpClient() { + String firstRequestId = null; + @Override + public Mono send(HttpRequest request) { + if (firstRequestId != null) { + String newRequestId = request.headers().value(REQUEST_ID_HEADER); + Assert.assertNotNull(newRequestId); + Assert.assertNotEquals(newRequestId, firstRequestId); + } + + firstRequestId = request.headers().value(REQUEST_ID_HEADER); + if (firstRequestId == null) { + Assert.fail(); + } + return Mono.just(mockResponse); } - - firstRequestId = request.headers().value(REQUEST_ID_HEADER); - if (firstRequestId == null) { - Assert.fail(); - } - return Mono.just(mockResponse); - } - }, new RequestIdPolicy()); + }) + .addPolicy(new RequestIdPolicy()) + .build(); pipeline.send(new HttpRequest(HttpMethod.GET, new URL("http://localhost/"))).block(); pipeline.send(new HttpRequest(HttpMethod.GET, new URL("http://localhost/"))).block(); @@ -86,25 +89,27 @@ public Mono send(HttpRequest request) { @Test public void sameRequestIdForRetry() throws Exception { - final HttpPipeline pipeline = new HttpPipeline(new MockHttpClient() { - String firstRequestId = null; - - @Override - public Mono send(HttpRequest request) { - if (firstRequestId != null) { - String newRequestId = request.headers().value(REQUEST_ID_HEADER); - Assert.assertNotNull(newRequestId); - Assert.assertEquals(newRequestId, firstRequestId); - } - firstRequestId = request.headers().value(REQUEST_ID_HEADER); - if (firstRequestId == null) { - Assert.fail(); + final HttpPipeline pipeline = HttpPipeline.builder() + .httpClient(new MockHttpClient() { + String firstRequestId = null; + + @Override + public Mono send(HttpRequest request) { + if (firstRequestId != null) { + String newRequestId = request.headers().value(REQUEST_ID_HEADER); + Assert.assertNotNull(newRequestId); + Assert.assertEquals(newRequestId, firstRequestId); + } + firstRequestId = request.headers().value(REQUEST_ID_HEADER); + if (firstRequestId == null) { + Assert.fail(); + } + return Mono.just(mockResponse); } - return Mono.just(mockResponse); - } - }, - new RequestIdPolicy(), - new RetryPolicy(1, Duration.of(0, ChronoUnit.SECONDS))); + }) + .addPolicy(new RequestIdPolicy()) + .addPolicy(new RetryPolicy(1, Duration.of(0, ChronoUnit.SECONDS))) + .build(); pipeline.send(new HttpRequest(HttpMethod.GET, new URL("http://localhost/"))).block(); } diff --git a/core/azure-core/src/test/java/com/azure/core/http/policy/RetryPolicyTests.java b/core/azure-core/src/test/java/com/azure/core/http/policy/RetryPolicyTests.java index f2ee21cfabca..5c83318836a0 100644 --- a/core/azure-core/src/test/java/com/azure/core/http/policy/RetryPolicyTests.java +++ b/core/azure-core/src/test/java/com/azure/core/http/policy/RetryPolicyTests.java @@ -20,16 +20,19 @@ public class RetryPolicyTests { @Test public void exponentialRetryEndOn501() throws Exception { - final HttpPipeline pipeline = new HttpPipeline(new MockHttpClient() { - // Send 408, 500, 502, all retried, with a 501 ending - private final int[] codes = new int[]{408, 500, 502, 501}; - private int count = 0; + final HttpPipeline pipeline = HttpPipeline.builder() + .httpClient(new MockHttpClient() { + // Send 408, 500, 502, all retried, with a 501 ending + private final int[] codes = new int[]{408, 500, 502, 501}; + private int count = 0; - @Override - public Mono send(HttpRequest request) { - return Mono.just(new MockHttpResponse(request, codes[count++])); - } - }, new RetryPolicy(3, Duration.of(0, ChronoUnit.MILLIS))); + @Override + public Mono send(HttpRequest request) { + return Mono.just(new MockHttpResponse(request, codes[count++])); + } + }) + .addPolicy(new RetryPolicy(3, Duration.of(0, ChronoUnit.MILLIS))) + .build(); HttpResponse response = pipeline.send(new HttpRequest(HttpMethod.GET, new URL("http://localhost/"))).block(); @@ -40,16 +43,18 @@ public Mono send(HttpRequest request) { @Test public void exponentialRetryMax() throws Exception { final int maxRetries = 5; - final HttpPipeline pipeline = new HttpPipeline(new MockHttpClient() { - int count = -1; - - @Override - public Mono send(HttpRequest request) { - Assert.assertTrue(count++ < maxRetries); - return Mono.just(new MockHttpResponse(request, 500)); - } - }, - new RetryPolicy(maxRetries, Duration.of(0, ChronoUnit.MILLIS))); + final HttpPipeline pipeline = HttpPipeline.builder() + .httpClient(new MockHttpClient() { + int count = -1; + + @Override + public Mono send(HttpRequest request) { + Assert.assertTrue(count++ < maxRetries); + return Mono.just(new MockHttpResponse(request, 500)); + } + }) + .addPolicy(new RetryPolicy(maxRetries, Duration.of(0, ChronoUnit.MILLIS))) + .build(); HttpResponse response = pipeline.send(new HttpRequest(HttpMethod.GET, diff --git a/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyStressTests.java b/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyStressTests.java index bec39a8c33d7..44134c4cee0c 100644 --- a/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyStressTests.java +++ b/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyStressTests.java @@ -115,7 +115,9 @@ public static void beforeClass() throws IOException { polices.add(new HttpLoggingPolicy(HttpLogDetailLevel.BASIC, false)); // service = RestProxy.create(IOService.class, - new HttpPipeline(polices.toArray(new HttpPipelinePolicy[polices.size()]))); + HttpPipeline.builder() + .setPolicies(polices) + .build()); RestProxyStressTests.tempFolderPath = Paths.get(tempFolderPath); create100MFiles(false); @@ -510,7 +512,10 @@ public void testHighParallelism() { } final IOService innerService = RestProxy.create(IOService.class, - new HttpPipeline(policies.toArray(new HttpPipelinePolicy[policies.size()]))); + HttpPipeline + .builder() + .setPolicies(policies) + .build()); // When running with MockServer, connections sometimes get dropped, // but this doesn't seem to result in any bad behavior as long as we retry. diff --git a/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyTests.java b/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyTests.java index ad01770018c2..610e531ecc41 100644 --- a/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyTests.java +++ b/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyTests.java @@ -1499,8 +1499,10 @@ public void flowableUploadTest() throws Exception { // // Order in which policies applied will be the order in which they added to builder // - final HttpPipeline httpPipeline = new HttpPipeline(httpClient, - new HttpLoggingPolicy(HttpLogDetailLevel.BODY_AND_HEADERS, true)); + final HttpPipeline httpPipeline = HttpPipeline.builder() + .httpClient(httpClient) + .addPolicy(new HttpLoggingPolicy(HttpLogDetailLevel.BODY_AND_HEADERS, true)) + .build(); // Response response = RestProxy.create(FlowableUploadService.class, httpPipeline, SERIALIZER).put(stream, Files.size(filePath)); @@ -1578,14 +1580,14 @@ interface Service25 { @Test(expected = HttpRequestException.class) @Ignore("Decoding is not a policy anymore") public void testMissingDecodingPolicyCausesException() { - Service25 service = RestProxy.create(Service25.class, new HttpPipeline()); + Service25 service = RestProxy.create(Service25.class, HttpPipeline.builder().build()); service.get(); } @Test(expected = HttpRequestException.class) @Ignore("Decoding is not a policy anymore") public void testSingleMissingDecodingPolicyCausesException() { - Service25 service = RestProxy.create(Service25.class, new HttpPipeline()); + Service25 service = RestProxy.create(Service25.class, HttpPipeline.builder().build()); service.getAsync().block(); service.getBodyResponseAsync().block(); } @@ -1593,7 +1595,7 @@ public void testSingleMissingDecodingPolicyCausesException() { @Test(expected = HttpRequestException.class) @Ignore("Decoding is not a policy anymore") public void testSingleBodyResponseMissingDecodingPolicyCausesException() { - Service25 service = RestProxy.create(Service25.class, new HttpPipeline()); + Service25 service = RestProxy.create(Service25.class, HttpPipeline.builder().build()); service.getBodyResponseAsync().block(); } @@ -1605,7 +1607,7 @@ interface Service26 { @Test public void postUrlFormEncoded() { - Service26 service = RestProxy.create(Service26.class, new HttpPipeline()); + Service26 service = RestProxy.create(Service26.class, HttpPipeline.builder().build()); HttpBinFormDataJSON response = service.postForm("Foo", "123", "foo@bar.com", PizzaSize.LARGE, Arrays.asList("Bacon", "Onion")); assertNotNull(response); assertNotNull(response.form()); @@ -1626,7 +1628,9 @@ protected T createService(Class serviceClass) { } protected T createService(Class serviceClass, HttpClient httpClient) { - final HttpPipeline httpPipeline = new HttpPipeline(httpClient); + final HttpPipeline httpPipeline = HttpPipeline.builder() + .httpClient(httpClient) + .build(); return RestProxy.create(serviceClass, httpPipeline, SERIALIZER); } diff --git a/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyWithMockTests.java b/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyWithMockTests.java index d20ec50af243..1e5a6a7f2626 100644 --- a/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyWithMockTests.java +++ b/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyWithMockTests.java @@ -176,7 +176,7 @@ interface ServiceErrorWithCharsetService { public void serviceErrorWithResponseContentType() { ServiceErrorWithCharsetService service = RestProxy.create( ServiceErrorWithCharsetService.class, - new HttpPipeline(new SimpleMockHttpClient() { + HttpPipeline.builder().httpClient(new SimpleMockHttpClient() { @Override public Mono send(HttpRequest request) { HttpHeaders headers = new HttpHeaders(); @@ -186,7 +186,7 @@ public Mono send(HttpRequest request) { "{ \"error\": \"Something went wrong, but at least this JSON is valid.\"}".getBytes(StandardCharsets.UTF_8)); return Mono.just(response); } - })); + }).build()); try { service.get(); @@ -200,7 +200,7 @@ public Mono send(HttpRequest request) { public void serviceErrorWithResponseContentTypeBadJSON() { ServiceErrorWithCharsetService service = RestProxy.create( ServiceErrorWithCharsetService.class, - new HttpPipeline(new SimpleMockHttpClient() { + HttpPipeline.builder().httpClient(new SimpleMockHttpClient() { @Override public Mono send(HttpRequest request) { HttpHeaders headers = new HttpHeaders(); @@ -209,7 +209,7 @@ public Mono send(HttpRequest request) { HttpResponse response = new MockHttpResponse(request, 200, headers, "BAD JSON".getBytes(StandardCharsets.UTF_8)); return Mono.just(response); } - })); + }).build()); try { service.get(); @@ -224,7 +224,7 @@ public Mono send(HttpRequest request) { public void serviceErrorWithResponseContentTypeCharset() { ServiceErrorWithCharsetService service = RestProxy.create( ServiceErrorWithCharsetService.class, - new HttpPipeline(new SimpleMockHttpClient() { + HttpPipeline.builder().httpClient(new SimpleMockHttpClient() { @Override public Mono send(HttpRequest request) { HttpHeaders headers = new HttpHeaders(); @@ -234,7 +234,7 @@ public Mono send(HttpRequest request) { "{ \"error\": \"Something went wrong, but at least this JSON is valid.\"}".getBytes(StandardCharsets.UTF_8)); return Mono.just(response); } - })); + }).build()); try { service.get(); @@ -248,7 +248,7 @@ public Mono send(HttpRequest request) { public void serviceErrorWithResponseContentTypeCharsetBadJSON() { ServiceErrorWithCharsetService service = RestProxy.create( ServiceErrorWithCharsetService.class, - new HttpPipeline(new SimpleMockHttpClient() { + HttpPipeline.builder().httpClient(new SimpleMockHttpClient() { @Override public Mono send(HttpRequest request) { HttpHeaders headers = new HttpHeaders(); @@ -257,7 +257,7 @@ public Mono send(HttpRequest request) { HttpResponse response = new MockHttpResponse(request, 200, headers, "BAD JSON".getBytes(StandardCharsets.UTF_8)); return Mono.just(response); } - })); + }).build()); try { service.get(); diff --git a/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyXMLTests.java b/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyXMLTests.java index 2f3a910aa03f..5519b67f6066 100644 --- a/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyXMLTests.java +++ b/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyXMLTests.java @@ -95,7 +95,9 @@ interface MyXMLService { @Test public void canReadXMLResponse() throws Exception { // - final HttpPipeline pipeline = new HttpPipeline(new MockXMLHTTPClient()); + final HttpPipeline pipeline = HttpPipeline.builder() + .httpClient(new MockXMLHTTPClient()) + .build(); // MyXMLService myXMLService = RestProxy.create(MyXMLService.class, @@ -159,7 +161,9 @@ public void canWriteXMLRequest() throws Exception { JacksonAdapter serializer = new JacksonAdapter(); MockXMLReceiverClient httpClient = new MockXMLReceiverClient(); // - final HttpPipeline pipeline = new HttpPipeline(httpClient); + final HttpPipeline pipeline = HttpPipeline.builder() + .httpClient(httpClient) + .build(); // MyXMLService myXMLService = RestProxy.create(MyXMLService.class, pipeline, @@ -193,7 +197,9 @@ public interface MyXMLServiceWithAttributes { public void canDeserializeXMLWithAttributes() throws Exception { JacksonAdapter serializer = new JacksonAdapter(); // - final HttpPipeline pipeline = new HttpPipeline(new MockXMLHTTPClient()); + final HttpPipeline pipeline = HttpPipeline.builder() + .httpClient(new MockXMLHTTPClient()) + .build(); // MyXMLServiceWithAttributes myXMLService = RestProxy.create( From 9c0517236598ba253573e046bf952f8aa1e5b8ef Mon Sep 17 00:00:00 2001 From: alzimmermsft <48699787+alzimmermsft@users.noreply.github.com> Date: Thu, 16 May 2019 13:16:30 -0700 Subject: [PATCH 5/7] Fixing checkstyle issues --- .../src/main/java/com/azure/core/http/HttpPipeline.java | 1 - .../src/main/java/com/azure/core/http/HttpPipelineBuilder.java | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java b/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java index fe00a17557bd..7ee93824740a 100644 --- a/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java +++ b/core/azure-core/src/main/java/com/azure/core/http/HttpPipeline.java @@ -7,7 +7,6 @@ import com.azure.core.util.Context; import reactor.core.publisher.Mono; -import java.util.Arrays; import java.util.List; import java.util.Objects; diff --git a/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineBuilder.java b/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineBuilder.java index fcee453e8a61..15d9766e5682 100644 --- a/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineBuilder.java +++ b/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineBuilder.java @@ -1,3 +1,6 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + package com.azure.core.http; import com.azure.core.http.policy.HttpPipelinePolicy; From afe778923668fb917a6b2bbd489c0dd0a8ffec72 Mon Sep 17 00:00:00 2001 From: alzimmermsft <48699787+alzimmermsft@users.noreply.github.com> Date: Thu, 16 May 2019 16:52:46 -0700 Subject: [PATCH 6/7] Cleaned up setting policies --- .../ConfigurationAsyncClientBuilder.java | 2 +- .../com/azure/core/management/AzureProxy.java | 4 +- .../azure/core/http/HttpPipelineBuilder.java | 48 +++++++++---------- .../azure/core/implementation/RestProxy.java | 2 +- .../java/com/azure/core/CredentialsTests.java | 6 +-- .../java/com/azure/core/UserAgentTests.java | 4 +- .../azure/core/http/HttpPipelineTests.java | 16 +++---- .../core/http/policy/HostPolicyTests.java | 23 ++++----- .../core/http/policy/ProtocolPolicyTests.java | 40 +++++++--------- .../ProxyAuthenticationPolicyTests.java | 12 ++--- .../http/policy/RequestIdPolicyTests.java | 5 +- .../core/http/policy/RetryPolicyTests.java | 4 +- .../implementation/RestProxyStressTests.java | 7 ++- .../core/implementation/RestProxyTests.java | 2 +- 14 files changed, 80 insertions(+), 95 deletions(-) diff --git a/applicationconfig/client/src/main/java/com/azure/applicationconfig/ConfigurationAsyncClientBuilder.java b/applicationconfig/client/src/main/java/com/azure/applicationconfig/ConfigurationAsyncClientBuilder.java index f66d8b345619..8f8b2efb44ac 100644 --- a/applicationconfig/client/src/main/java/com/azure/applicationconfig/ConfigurationAsyncClientBuilder.java +++ b/applicationconfig/client/src/main/java/com/azure/applicationconfig/ConfigurationAsyncClientBuilder.java @@ -129,7 +129,7 @@ public ConfigurationAsyncClient build() { policies.add(new HttpLoggingPolicy(httpLogDetailLevel)); HttpPipeline pipeline = HttpPipeline.builder() - .setPolicies(policies) + .policies(policies.toArray(new HttpPipelinePolicy[0])) .httpClient(httpClient) .build(); diff --git a/core/azure-core-management/src/main/java/com/azure/core/management/AzureProxy.java b/core/azure-core-management/src/main/java/com/azure/core/management/AzureProxy.java index 4a0d07a97023..5e0adaf16e48 100644 --- a/core/azure-core-management/src/main/java/com/azure/core/management/AzureProxy.java +++ b/core/azure-core-management/src/main/java/com/azure/core/management/AzureProxy.java @@ -192,7 +192,7 @@ public static HttpPipeline createDefaultPipeline(Class swaggerInterface, Asyn public static HttpPipeline createDefaultPipeline(Class swaggerInterface, HttpPipelinePolicy credentialsPolicy) { // Order in which policies applied will be the order in which they appear in the array // - List policies = new ArrayList(); + List policies = new ArrayList<>(); policies.add(new UserAgentPolicy(getDefaultUserAgentString(swaggerInterface))); policies.add(new RetryPolicy()); policies.add(new CookiePolicy()); @@ -201,7 +201,7 @@ public static HttpPipeline createDefaultPipeline(Class swaggerInterface, Http } return HttpPipeline.builder() - .setPolicies(policies) + .policies(policies.toArray(new HttpPipelinePolicy[0])) .build(); } diff --git a/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineBuilder.java b/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineBuilder.java index 15d9766e5682..23f9cc189668 100644 --- a/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineBuilder.java +++ b/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineBuilder.java @@ -6,23 +6,31 @@ import com.azure.core.http.policy.HttpPipelinePolicy; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /** * This class provides a fluent builder API to help aid the configuration and instantiation of the {@link HttpPipeline}, * calling {@link HttpPipelineBuilder#build() build} constructs an instance of the pipeline. * + *

A pipeline uses a HttpClient to send requests, if no client is configured a default client will be used. + * A pipeline may also contain a list of policies that are applied to each service request that is sent.

+ * + *

Code Samples

+ * + *

Create a pipeline without configuration

+ * *
  * HttpPipeline.builder()
- *     .httpClient(httpClient)
- *     .addPolicy(httpPipelinePolicy)
  *     .build();
  * 
* + *

Create a pipeline using the default HTTP client and a retry policy

+ * *
  * HttpPipeline.builder()
- *     .httpClient(httpClient)
- *     .setPolicies(httpPipelinePolicies)
+ *     .httpClient(HttpClient.createDefault())
+ *     .policies(new RetryPolicy())
  *     .build();
  * 
* @@ -30,7 +38,7 @@ */ public class HttpPipelineBuilder { private HttpClient httpClient; - private List pipelinePolicies = new ArrayList<>(); + private List pipelinePolicies; HttpPipelineBuilder() { @@ -40,16 +48,15 @@ public class HttpPipelineBuilder { * Creates a {@link HttpPipeline} based on options set in the Builder. Every time {@code build()} is * called, a new instance of {@link HttpPipeline} is created. * - * If HttpClient is not set then the default HttpClient is used. + * If HttpClient is not set then the {@link HttpClient#createDefault() default HttpClient} is used. * * @return A HttpPipeline with the options set from the builder. */ public HttpPipeline build() { - if (httpClient == null) { - return new HttpPipeline(HttpClient.createDefault(), pipelinePolicies); - } else { - return new HttpPipeline(httpClient, pipelinePolicies); - } + List policies = (pipelinePolicies == null) ? new ArrayList<>() : pipelinePolicies; + HttpClient client = (httpClient == null) ? HttpClient.createDefault() : httpClient; + + return new HttpPipeline(client, policies); } /** @@ -66,22 +73,15 @@ public HttpPipelineBuilder httpClient(HttpClient httpClient) { /** * Adds a {@link HttpPipelinePolicy} to the list of policies that the pipeline will use. * - * @param policy Policy to add to the policy set. + * @param policies Policies to add to the policy set. * @return The updated HttpPipelineBuilder object. */ - public HttpPipelineBuilder addPolicy(HttpPipelinePolicy policy) { - this.pipelinePolicies.add(policy); - return this; - } + public HttpPipelineBuilder policies(HttpPipelinePolicy... policies) { + if (pipelinePolicies == null) { + pipelinePolicies = new ArrayList<>(); + } - /** - * Sets the policies that the pipeline will use. - * - * @param policies List of policies - * @return The updated HttpPipelineBuilder object. - */ - public HttpPipelineBuilder setPolicies(List policies) { - this.pipelinePolicies = new ArrayList<>(policies); + this.pipelinePolicies.addAll(Arrays.asList(policies)); return this; } } diff --git a/core/azure-core/src/main/java/com/azure/core/implementation/RestProxy.java b/core/azure-core/src/main/java/com/azure/core/implementation/RestProxy.java index 38748fa5aa53..384398d644c1 100644 --- a/core/azure-core/src/main/java/com/azure/core/implementation/RestProxy.java +++ b/core/azure-core/src/main/java/com/azure/core/implementation/RestProxy.java @@ -560,7 +560,7 @@ public static HttpPipeline createDefaultPipeline(HttpPipelinePolicy credentialsP } return HttpPipeline.builder() - .setPolicies(policies) + .policies(policies.toArray(new HttpPipelinePolicy[0])) .build(); } diff --git a/core/azure-core/src/test/java/com/azure/core/CredentialsTests.java b/core/azure-core/src/test/java/com/azure/core/CredentialsTests.java index 23ff591bea8b..933dcf64ac2f 100644 --- a/core/azure-core/src/test/java/com/azure/core/CredentialsTests.java +++ b/core/azure-core/src/test/java/com/azure/core/CredentialsTests.java @@ -30,8 +30,7 @@ public void basicCredentialsTest() throws Exception { // final HttpPipeline pipeline = HttpPipeline.builder() .httpClient(new MockHttpClient()) - .addPolicy(new CredentialsPolicy(credentials)) - .addPolicy(auditorPolicy) + .policies(new CredentialsPolicy(credentials), auditorPolicy) .build(); @@ -51,8 +50,7 @@ public void tokenCredentialsTest() throws Exception { final HttpPipeline pipeline = HttpPipeline.builder() .httpClient(new MockHttpClient()) - .addPolicy(new CredentialsPolicy(credentials)) - .addPolicy(auditorPolicy) + .policies(new CredentialsPolicy(credentials), auditorPolicy) .build(); HttpRequest request = new HttpRequest(HttpMethod.GET, new URL("http://localhost")); diff --git a/core/azure-core/src/test/java/com/azure/core/UserAgentTests.java b/core/azure-core/src/test/java/com/azure/core/UserAgentTests.java index f554fbe2c046..404631e808c4 100644 --- a/core/azure-core/src/test/java/com/azure/core/UserAgentTests.java +++ b/core/azure-core/src/test/java/com/azure/core/UserAgentTests.java @@ -29,7 +29,7 @@ public Mono send(HttpRequest request) { return Mono.just(new MockHttpResponse(request, 200)); } }) - .addPolicy(new UserAgentPolicy("AutoRest-Java")) + .policies(new UserAgentPolicy("AutoRest-Java")) .build(); HttpResponse response = pipeline.send(new HttpRequest( @@ -49,7 +49,7 @@ public Mono send(HttpRequest request) { return Mono.just(new MockHttpResponse(request, 200)); } }) - .addPolicy(new UserAgentPolicy("Awesome")) + .policies(new UserAgentPolicy("Awesome")) .build(); HttpResponse response = pipeline.send(new HttpRequest(HttpMethod.GET, diff --git a/core/azure-core/src/test/java/com/azure/core/http/HttpPipelineTests.java b/core/azure-core/src/test/java/com/azure/core/http/HttpPipelineTests.java index 80d558084792..07f2e164317e 100644 --- a/core/azure-core/src/test/java/com/azure/core/http/HttpPipelineTests.java +++ b/core/azure-core/src/test/java/com/azure/core/http/HttpPipelineTests.java @@ -32,9 +32,9 @@ public void constructorWithNoArguments() { @Test public void withRequestPolicy() { HttpPipeline pipeline = HttpPipeline.builder() - .addPolicy(new PortPolicy(80, true)) - .addPolicy(new ProtocolPolicy("ftp", true)) - .addPolicy(new RetryPolicy()) + .policies(new PortPolicy(80, true), + new ProtocolPolicy("ftp", true), + new RetryPolicy()) .build(); assertEquals(3, pipeline.getPolicyCount()); @@ -48,9 +48,9 @@ public void withRequestPolicy() { @Test public void withRequestOptions() throws MalformedURLException { HttpPipeline pipeline = HttpPipeline.builder() - .addPolicy(new PortPolicy(80, true)) - .addPolicy(new ProtocolPolicy("ftp", true)) - .addPolicy(new RetryPolicy()) + .policies(new PortPolicy(80, true), + new ProtocolPolicy("ftp", true), + new RetryPolicy()) .build(); HttpPipelineCallContext context = new HttpPipelineCallContext(new HttpRequest(HttpMethod.GET, new URL("http://foo.com"))); @@ -98,7 +98,7 @@ public Mono send(HttpRequest request) { final HttpPipeline httpPipeline = HttpPipeline.builder() .httpClient(httpClient) - .addPolicy(new UserAgentPolicy(expectedUserAgent)) + .policies(new UserAgentPolicy(expectedUserAgent)) .build(); final HttpResponse response = httpPipeline.send(new HttpRequest(expectedHttpMethod, expectedUrl)).block(); @@ -124,7 +124,7 @@ public Mono send(HttpRequest request) { return Mono.just(new MockHttpResponse(request, 200)); } }) - .addPolicy(new RequestIdPolicy()) + .policies(new RequestIdPolicy()) .build(); final HttpResponse response = httpPipeline.send(new HttpRequest(expectedHttpMethod, expectedUrl)).block(); diff --git a/core/azure-core/src/test/java/com/azure/core/http/policy/HostPolicyTests.java b/core/azure-core/src/test/java/com/azure/core/http/policy/HostPolicyTests.java index 7dfd8b6c8437..8c209c4817c1 100644 --- a/core/azure-core/src/test/java/com/azure/core/http/policy/HostPolicyTests.java +++ b/core/azure-core/src/test/java/com/azure/core/http/policy/HostPolicyTests.java @@ -33,17 +33,12 @@ public void withPort() throws MalformedURLException { private static HttpPipeline createPipeline(String host, String expectedUrl) { return HttpPipeline.builder() - .httpClient(new MockHttpClient() { - @Override - public Mono send(HttpRequest request) { - return Mono.empty(); // NOP - } - }) - .addPolicy(new HostPolicy(host)) - .addPolicy((context, next) -> { - assertEquals(expectedUrl, context.httpRequest().url().toString()); - return next.process(); - }) + .httpClient(new MockHttpClient()) + .policies(new HostPolicy(host), + (context, next) -> { + assertEquals(expectedUrl, context.httpRequest().url().toString()); + return next.process(); + }) .build(); } @@ -51,10 +46,12 @@ private static HttpRequest createHttpRequest(String url) throws MalformedURLExce return new HttpRequest(HttpMethod.GET, new URL(url)); } - private abstract static class MockHttpClient implements HttpClient { + private static class MockHttpClient implements HttpClient { @Override - public abstract Mono send(HttpRequest request); + public Mono send(HttpRequest request) { + return Mono.empty(); // NOP + } @Override public HttpClient proxy(Supplier proxyOptions) { diff --git a/core/azure-core/src/test/java/com/azure/core/http/policy/ProtocolPolicyTests.java b/core/azure-core/src/test/java/com/azure/core/http/policy/ProtocolPolicyTests.java index ca535fe804b2..c581c598b71a 100644 --- a/core/azure-core/src/test/java/com/azure/core/http/policy/ProtocolPolicyTests.java +++ b/core/azure-core/src/test/java/com/azure/core/http/policy/ProtocolPolicyTests.java @@ -33,33 +33,23 @@ public void withNoOverwrite() throws MalformedURLException { } private static HttpPipeline createPipeline(String protocol, String expectedUrl) { return HttpPipeline.builder() - .httpClient(new MockHttpClient() { - @Override - public Mono send(HttpRequest request) { - return Mono.empty(); // NOP - } - }) - .addPolicy(new ProtocolPolicy(protocol, true)) - .addPolicy((context, next) -> { - assertEquals(expectedUrl, context.httpRequest().url().toString()); - return next.process(); - }) + .httpClient(new MockHttpClient()) + .policies(new ProtocolPolicy(protocol, true), + (context, next) -> { + assertEquals(expectedUrl, context.httpRequest().url().toString()); + return next.process(); + }) .build(); } private static HttpPipeline createPipeline(String protocol, boolean overwrite, String expectedUrl) { return HttpPipeline.builder() - .httpClient(new MockHttpClient() { - @Override - public Mono send(HttpRequest request) { - return Mono.empty(); // NOP - } - }) - .addPolicy(new ProtocolPolicy(protocol, overwrite)) - .addPolicy((context, next) -> { - assertEquals(expectedUrl, context.httpRequest().url().toString()); - return next.process(); - }) + .httpClient(new MockHttpClient()) + .policies(new ProtocolPolicy(protocol, overwrite), + (context, next) -> { + assertEquals(expectedUrl, context.httpRequest().url().toString()); + return next.process(); + }) .build(); } @@ -67,10 +57,12 @@ private static HttpRequest createHttpRequest(String url) throws MalformedURLExce return new HttpRequest(HttpMethod.GET, new URL(url)); } - private abstract static class MockHttpClient implements HttpClient { + private static class MockHttpClient implements HttpClient { @Override - public abstract Mono send(HttpRequest request); + public Mono send(HttpRequest request) { + return Mono.empty(); + } @Override public HttpClient proxy(Supplier proxyOptions) { diff --git a/core/azure-core/src/test/java/com/azure/core/http/policy/ProxyAuthenticationPolicyTests.java b/core/azure-core/src/test/java/com/azure/core/http/policy/ProxyAuthenticationPolicyTests.java index 5ac253ead6b7..2832ab41e820 100644 --- a/core/azure-core/src/test/java/com/azure/core/http/policy/ProxyAuthenticationPolicyTests.java +++ b/core/azure-core/src/test/java/com/azure/core/http/policy/ProxyAuthenticationPolicyTests.java @@ -25,12 +25,12 @@ public void test() throws MalformedURLException { // final HttpPipeline pipeline = HttpPipeline.builder() .httpClient(new MockHttpClient()) - .addPolicy(new ProxyAuthenticationPolicy(username, password)) - .addPolicy((context, next) -> { - assertEquals("Basic dGVzdHVzZXI6dGVzdHBhc3M=", context.httpRequest().headers().value("Proxy-Authentication")); - auditorVisited.set(true); - return next.process(); - }) + .policies(new ProxyAuthenticationPolicy(username, password), + (context, next) -> { + assertEquals("Basic dGVzdHVzZXI6dGVzdHBhc3M=", context.httpRequest().headers().value("Proxy-Authentication")); + auditorVisited.set(true); + return next.process(); + }) .build(); pipeline.send(new HttpRequest(HttpMethod.GET, new URL("http://localhost"))) diff --git a/core/azure-core/src/test/java/com/azure/core/http/policy/RequestIdPolicyTests.java b/core/azure-core/src/test/java/com/azure/core/http/policy/RequestIdPolicyTests.java index b7366e3c2ff6..e044f7d1ea5b 100644 --- a/core/azure-core/src/test/java/com/azure/core/http/policy/RequestIdPolicyTests.java +++ b/core/azure-core/src/test/java/com/azure/core/http/policy/RequestIdPolicyTests.java @@ -80,7 +80,7 @@ public Mono send(HttpRequest request) { return Mono.just(mockResponse); } }) - .addPolicy(new RequestIdPolicy()) + .policies(new RequestIdPolicy()) .build(); pipeline.send(new HttpRequest(HttpMethod.GET, new URL("http://localhost/"))).block(); @@ -107,8 +107,7 @@ public Mono send(HttpRequest request) { return Mono.just(mockResponse); } }) - .addPolicy(new RequestIdPolicy()) - .addPolicy(new RetryPolicy(1, Duration.of(0, ChronoUnit.SECONDS))) + .policies(new RequestIdPolicy(), new RetryPolicy(1, Duration.of(0, ChronoUnit.SECONDS))) .build(); pipeline.send(new HttpRequest(HttpMethod.GET, new URL("http://localhost/"))).block(); diff --git a/core/azure-core/src/test/java/com/azure/core/http/policy/RetryPolicyTests.java b/core/azure-core/src/test/java/com/azure/core/http/policy/RetryPolicyTests.java index 5c83318836a0..7a20532fa07c 100644 --- a/core/azure-core/src/test/java/com/azure/core/http/policy/RetryPolicyTests.java +++ b/core/azure-core/src/test/java/com/azure/core/http/policy/RetryPolicyTests.java @@ -31,7 +31,7 @@ public Mono send(HttpRequest request) { return Mono.just(new MockHttpResponse(request, codes[count++])); } }) - .addPolicy(new RetryPolicy(3, Duration.of(0, ChronoUnit.MILLIS))) + .policies(new RetryPolicy(3, Duration.of(0, ChronoUnit.MILLIS))) .build(); HttpResponse response = pipeline.send(new HttpRequest(HttpMethod.GET, @@ -53,7 +53,7 @@ public Mono send(HttpRequest request) { return Mono.just(new MockHttpResponse(request, 500)); } }) - .addPolicy(new RetryPolicy(maxRetries, Duration.of(0, ChronoUnit.MILLIS))) + .policies(new RetryPolicy(maxRetries, Duration.of(0, ChronoUnit.MILLIS))) .build(); diff --git a/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyStressTests.java b/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyStressTests.java index 44134c4cee0c..bff69ccf195d 100644 --- a/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyStressTests.java +++ b/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyStressTests.java @@ -116,7 +116,7 @@ public static void beforeClass() throws IOException { // service = RestProxy.create(IOService.class, HttpPipeline.builder() - .setPolicies(polices) + .policies(polices.toArray(new HttpPipelinePolicy[0])) .build()); RestProxyStressTests.tempFolderPath = Paths.get(tempFolderPath); @@ -512,9 +512,8 @@ public void testHighParallelism() { } final IOService innerService = RestProxy.create(IOService.class, - HttpPipeline - .builder() - .setPolicies(policies) + HttpPipeline.builder() + .policies(policies.toArray(new HttpPipelinePolicy[0])) .build()); // When running with MockServer, connections sometimes get dropped, diff --git a/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyTests.java b/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyTests.java index 610e531ecc41..12f2968a880d 100644 --- a/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyTests.java +++ b/core/azure-core/src/test/java/com/azure/core/implementation/RestProxyTests.java @@ -1501,7 +1501,7 @@ public void flowableUploadTest() throws Exception { // final HttpPipeline httpPipeline = HttpPipeline.builder() .httpClient(httpClient) - .addPolicy(new HttpLoggingPolicy(HttpLogDetailLevel.BODY_AND_HEADERS, true)) + .policies(new HttpLoggingPolicy(HttpLogDetailLevel.BODY_AND_HEADERS, true)) .build(); // Response response = RestProxy.create(FlowableUploadService.class, httpPipeline, SERIALIZER).put(stream, Files.size(filePath)); From caebdcd3a3da3b55243aff9f636fac657e7be657 Mon Sep 17 00:00:00 2001 From: alzimmermsft <48699787+alzimmermsft@users.noreply.github.com> Date: Thu, 16 May 2019 21:59:18 -0700 Subject: [PATCH 7/7] Javadoc cleanup --- .../java/com/azure/core/http/HttpPipelineBuilder.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineBuilder.java b/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineBuilder.java index 23f9cc189668..14f7f6ab53e5 100644 --- a/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineBuilder.java +++ b/core/azure-core/src/main/java/com/azure/core/http/HttpPipelineBuilder.java @@ -13,8 +13,8 @@ * This class provides a fluent builder API to help aid the configuration and instantiation of the {@link HttpPipeline}, * calling {@link HttpPipelineBuilder#build() build} constructs an instance of the pipeline. * - *

A pipeline uses a HttpClient to send requests, if no client is configured a default client will be used. - * A pipeline may also contain a list of policies that are applied to each service request that is sent.

+ *

A pipeline is configured with a HttpClient that sends the request, if no client is set a default is used. + * A pipeline may be configured with a list of policies that are applied to each request.

* *

Code Samples

* @@ -60,7 +60,7 @@ public HttpPipeline build() { } /** - * Sets the HttpClient for the pipeline instance. + * Sets the HttpClient that the pipeline will use to send requests. * * @param httpClient The HttpClient the pipeline will use when sending requests. * @return The updated HttpPipelineBuilder object. @@ -71,7 +71,8 @@ public HttpPipelineBuilder httpClient(HttpClient httpClient) { } /** - * Adds a {@link HttpPipelinePolicy} to the list of policies that the pipeline will use. + * Adds {@link HttpPipelinePolicy policies} to the set of policies that the pipeline will use + * when sending requests. * * @param policies Policies to add to the policy set. * @return The updated HttpPipelineBuilder object.