<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Duda - Medium]]></title>
        <description><![CDATA[We love working on our product and we love writing about it. You can find posts about tech, product, growth, management and dilemas written by our dudes and dudettes. - Medium]]></description>
        <link>https://medium.com/duda?source=rss----a8c59e8a5adc---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Duda - Medium</title>
            <link>https://medium.com/duda?source=rss----a8c59e8a5adc---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 13:27:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/duda" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Adding observability for web client]]></title>
            <link>https://medium.com/duda/adding-observability-for-web-client-657c25751d99?source=rss----a8c59e8a5adc---4</link>
            <guid isPermaLink="false">https://medium.com/p/657c25751d99</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[monitoring]]></category>
            <category><![CDATA[http-client]]></category>
            <category><![CDATA[actuator]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Anton Tkachenko]]></dc:creator>
            <pubDate>Tue, 11 Jun 2024 19:28:34 GMT</pubDate>
            <atom:updated>2024-06-11T19:28:34.011Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6OdRZ3zKW4hKqH3n" /></figure><h3>Introduction</h3><p>This article is part of a series of articles about a deeper dive into making HTTP calls in Spring.</p><ul><li><a href="https://medium.com/p/c3a2763e9e4c">Part 1 — Options and testing</a></li><li><a href="https://medium.com/p/10a9a79e33ff">Part 2 — Spring web client and test kit</a></li><li><a href="https://medium.com/p/135d5b0ed4e6">Part 3 — ClientHttpRequestFactory as an abstraction over the HTTP layer</a></li><li><a href="https://medium.com/p/d7df011d52ec">Part 4 — Configuring Apache Client via HttpComponentsClientHttpRequestFactory</a></li><li><a href="https://medium.com/p/65d709bb11f9">Part 5 — Setting a hard limit on a request and failing fast</a></li><li><a href="https://medium.com/p/657c25751d99">Part 6 — Adding observability for web client</a> (you’re here) <br>All source code is available in the <a href="https://github.com/tkachenkoas/spring-web-client-in-depth">GitHub repository</a>.</li></ul><h3>Spring’s default observability support</h3><p>Spring ecosystem provides a whole module dedicated to observability — spring-boot-starter-actuator. Under the hood, it uses the Micrometer library to collect metrics and also provides integration with Prometheus and other popular monitoring systems. This article will not be about how to use Prometheus and Grafana, but rather how to add observability to your web client and also how not to accidentally hurt the performance of your application.</p><p>The core component of Micrometer is MeterRegistry. If you search projects and libraries in IDE for the following @ConditionalOnBean(MeterRegistry.class), you will find a lot of systems for which Spring Boot autoconfigures monitoring. For instance, MongoMetricsAutoConfiguration and TomcatMetricsAutoConfiguration will provide you with metrics for MongoDB and Tomcat, respectively.</p><p>Also, there is HttpClientObservationsAutoConfiguration which also includes RestTemplateObservationConfiguration that defines a bean of ObservationRestTemplateCustomizer. Long story short, this means that if you have MeterRegistry bean AND you use Spring&#39;s beans to build yourRestTemplate via RestTemplateBuilder, then all the observability will be applied out of the box. This will not work if you instantiate RestTemplate manually.</p><h3>HTTP client metrics collected out of the box</h3><p>The setup for exploring rest template metrics is plain. We fully rely on Spring Boot autoconfiguration and will use the default SimpleMeterRegistry from spring-boot-starter-actuator. Also, we will use the RestTemplateBuilder from context to build RestTemplate instances.</p><pre>@SpringBootTest<br>@MockServerSettings(ports = 8090)<br>@RequiredArgsConstructor<br>@Slf4j<br>public class Part06_ObservabilityAndMonitoring {<br>    private final ClientAndServer mockServer;<br>    @SpringBootApplication<br>    static class SimpleTestApplicationForRestTemplateMonitoring {<br>    }<br>    @Autowired<br>    RestTemplateBuilder restTemplateBuilder;<br>    <br>    /**<br>    * Unless prometheus / dropwizard / other monitoring system is configured,<br>    * the default registry is SimpleMeterRegistry.<br>    */<br>    @Autowired<br>    SimpleMeterRegistry meterRegistry;<br>...</pre><p>In the first example, we make a GET request to a mock server, and then we check the metrics collected by SimpleMeterRegistry. The metrics are collected by http.client.requests meters. The metrics will contain information about executed requests, and also about active requests. They will allow you to build graphs of total requests and average request time per URI/status. Unfortunately, richer statistical information like the distribution of request time is not available.</p><pre>@Test<br>void restClient_builtViaContextBuilder_willHaveMonitoring() {<br>    RestTemplate restTemplate = restTemplateBuilder.build();<br><br>    mockServer.when(request().withMethod(&quot;GET&quot;)<br>                    .withPath(&quot;/api/v1/employees/1&quot;))<br>            .respond(response().withStatusCode(200));<br>    restTemplate.getForObject(&quot;http://localhost:8090/api/v1/employees/{id}&quot;, String.class, 1);<br>    // and now let&#39;s also make one long task<br>    mockServer.when(request().withMethod(&quot;GET&quot;)<br>                    .withPath(&quot;/api/v1/employees/2&quot;))<br>            .respond(response().withStatusCode(200).withDelay(Delay.seconds(5)));<br>    CompletableFuture.runAsync(<br>            () -&gt; restTemplate.getForObject(&quot;http://localhost:8090/api/v1/employees/{id}&quot;, String.class, 2)<br>    );<br>    /**<br>     * http.client.requests(TIMER)[client.name=&#39;localhost&#39;, error=&#39;none&#39;, exception=&#39;none&#39;, method=&#39;GET&#39;, outcome=&#39;SUCCESS&#39;,<br>     * status=&#39;200&#39;, uri=&#39;/api/v1/employees/{id}&#39;]; count=1.0, total_time=0.1194284 seconds, max=0.1194284 seconds<br>     * http.client.requests.active(LONG_TASK_TIMER)[client.name=&#39;localhost&#39;, exception=&#39;none&#39;, method=&#39;GET&#39;, outcome=&#39;UNKNOWN&#39;,<br>     * status=&#39;CLIENT_ERROR&#39;, uri=&#39;/api/v1/employees/{id}&#39;]; active_tasks=1.0, duration=0.0085765 seconds<br>     */<br>    List&lt;String&gt; relatedMetrics = Arrays.stream(meterRegistry.getMetersAsString().split(&quot;\n&quot;))<br>            .filter(name -&gt; name.startsWith(&quot;http.client.requests&quot;))<br>            .toList();<br>    // metrics contain information about executed requests: count, total_time, max<br>    // for each uri templated, target host, method, status<br>    assertThat(relatedMetrics)<br>            .anyMatch(metric -&gt; metric.contains(&quot;http.client.requests(TIMER)&quot;) &amp;&amp;<br>                    metric.contains(&quot;outcome=&#39;SUCCESS&#39;&quot;) &amp;&amp;<br>                    metric.contains(&quot;status=&#39;200&#39;&quot;) &amp;&amp;<br>                    metric.contains(&quot;count=1.0&quot;) &amp;&amp;<br>                    metric.contains(&quot;total_time=&quot;) &amp;&amp;<br>                    metric.contains(&quot;max=&quot;) &amp;&amp;<br>                    metric.contains(&quot;client.name=&#39;localhost&#39;&quot;) &amp;&amp;<br>                    metric.contains(&quot;uri=&#39;/api/v1/employees/{id}&#39;&quot;)<br>            );<br>    // metrics will also contain information about active requests<br>    assertThat(relatedMetrics)<br>            .anyMatch(metric -&gt; metric.contains(&quot;http.client.requests.active(LONG_TASK_TIMER)&quot;) &amp;&amp;<br>                    metric.contains(&quot;outcome=&#39;UNKNOWN&#39;&quot;) &amp;&amp;<br>                    metric.contains(&quot;status=&#39;CLIENT_ERROR&#39;&quot;) &amp;&amp;<br>                    metric.contains(&quot;active_tasks=1.0&quot;) &amp;&amp;<br>                    metric.contains(&quot;uri=&#39;/api/v1/employees/{id}&#39;&quot;));<br>}</pre><p>However, there is a catch. If you misuse RestTemplate&#39;s API by invoking multiple different URI templates due to String concatenation, then you will end up with a lot of metrics. This is because the UriTemplate is used as a key to store the metrics. If your application makes lots of different requests with different URIs, then you might accidentally find that you have high CPU usage and response sizes of /actuator/prometheus to be megabytes or even tens of megabytes.</p><pre>@Test<br>@SneakyThrows<br>void misusingUriTemplate_canProduceTooManyMetrics() {<br>    RestTemplate restTemplate = restTemplateBuilder.build();<br><br>    for (Integer i : List.of(1, 2, 3)) {<br>        mockServer.when(request()<br>                        .withMethod(&quot;GET&quot;)<br>                        .withPath(&quot;/api/v1/employees/&quot; + i))<br>                .respond(response()<br>                        .withStatusCode(200)<br>                        .withContentType(MediaType.APPLICATION_JSON)<br>                        .withBody(new ObjectMapper().writeValueAsString(<br>                                Map.of(&quot;id&quot;, i, &quot;name&quot;, &quot;John Doe &quot; + i))<br>                        )<br>                );<br>        restTemplate.getForObject(&quot;http://localhost:8090/api/v1/employees/&quot; + i, String.class);<br>    }<br>    List&lt;String&gt; relatedMetrics = Arrays.stream(meterRegistry.getMetersAsString().split(&quot;\n&quot;))<br>            .filter(name -&gt; name.startsWith(&quot;http.client.requests&quot;))<br>            .toList();<br>    /**<br>     * http.client.requests(TIMER)[client.name=&#39;localhost&#39;, error=&#39;none&#39;, exception=&#39;none&#39;, method=&#39;GET&#39;, outcome=&#39;SUCCESS&#39;,<br>     * status=&#39;200&#39;, uri=&#39;/api/v1/employees/1&#39;]; count=1.0, total_time=0.1304859 seconds, max=0.1304859 seconds<br>     * http.client.requests(TIMER)[client.name=&#39;localhost&#39;, error=&#39;none&#39;, exception=&#39;none&#39;, method=&#39;GET&#39;, outcome=&#39;SUCCESS&#39;,<br>     * status=&#39;200&#39;, uri=&#39;/api/v1/employees/3&#39;]; count=1.0, total_time=0.0031198 seconds, max=0.0031198 seconds<br>     * http.client.requests(TIMER)[client.name=&#39;localhost&#39;, error=&#39;none&#39;, exception=&#39;none&#39;, method=&#39;GET&#39;, outcome=&#39;SUCCESS&#39;,<br>     * status=&#39;200&#39;, uri=&#39;/api/v1/employees/2&#39;]; count=1.0, total_time=0.0055739 seconds, max=0.0055739 seconds<br>     * http.client.requests.active(LONG_TASK_TIMER)[client.name=&#39;localhost&#39;, exception=&#39;none&#39;, method=&#39;GET&#39;, outcome=&#39;UNKNOWN&#39;,<br>     * status=&#39;CLIENT_ERROR&#39;, uri=&#39;/api/v1/employees/1&#39;]; active_tasks=0.0, duration=0.0 seconds<br>     * http.client.requests.active(LONG_TASK_TIMER)[client.name=&#39;localhost&#39;, exception=&#39;none&#39;, method=&#39;GET&#39;, outcome=&#39;UNKNOWN&#39;,<br>     * status=&#39;CLIENT_ERROR&#39;, uri=&#39;/api/v1/employees/2&#39;]; active_tasks=0.0, duration=0.0 seconds<br>     * http.client.requests.active(LONG_TASK_TIMER)[client.name=&#39;localhost&#39;, exception=&#39;none&#39;, method=&#39;GET&#39;, outcome=&#39;UNKNOWN&#39;,<br>     * status=&#39;CLIENT_ERROR&#39;, uri=&#39;/api/v1/employees/3&#39;]; active_tasks=0.0, duration=0.0 seconds<br>     * http.client.requests.active(LONG_TASK_TIMER)[client.name=&#39;localhost&#39;, exception=&#39;none&#39;, method=&#39;GET&#39;, outcome=&#39;UNKNOWN&#39;,<br>     */<br>    assertThat(relatedMetrics)<br>            .hasSize(6);<br>    assertThat(relatedMetrics)<br>            .anyMatch(metric -&gt; metric.contains(&quot;uri=&#39;/api/v1/employees/1&#39;&quot;))<br>            .anyMatch(metric -&gt; metric.contains(&quot;uri=&#39;/api/v1/employees/2&#39;&quot;))<br>            .anyMatch(metric -&gt; metric.contains(&quot;uri=&#39;/api/v1/employees/3&#39;&quot;));<br>}</pre><p>Lastly, if you configured RestTemplate with Apache client powered by a connection pool, then you can also monitor its metrics. The PoolingHttpClientConnectionManager has API to get information about the pool state. And micrometer has a utility class PoolingHttpClientConnectionManagerMetricsBinder that acts as an adapter from PoolingHttpClientConnectionManager to MeterRegistry. Thus, you can get live information about the pool - how many connections are leased, how many are available, etc.</p><pre>@Test<br>@SneakyThrows<br>void connectionPool_canBeEasilyMonitored() {<br>    RestTemplate restTemplate = new RestTemplate();<br><br>    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();<br>    connectionManager.setMaxTotal(10);<br>    restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(<br>            HttpClients.custom().setConnectionManager(connectionManager).build()<br>    ));<br>    // micrometer has a built-in binder for connection pool monitoring<br>    new PoolingHttpClientConnectionManagerMetricsBinder(<br>            connectionManager, &quot;our-pool&quot;<br>    ).bindTo(meterRegistry);<br>    // now let&#39;s block a few connections in the pool with a long task<br>    mockServer.when(request()<br>                    .withMethod(&quot;GET&quot;)<br>                    .withPath(&quot;/api/v1/employees/1&quot;))<br>            .respond(response()<br>                    .withStatusCode(200)<br>                    .withContentType(MediaType.APPLICATION_JSON)<br>                    .withBody(&quot;{\&quot;id\&quot;: 1, \&quot;name\&quot;: \&quot;John Doe\&quot;}&quot;)<br>                    .withDelay(Delay.seconds(5))<br>            );<br><br>    for (int i = 0; i &lt; 3; i++) {<br>        CompletableFuture.runAsync(<br>                () -&gt; restTemplate.getForObject(&quot;http://localhost:8090/api/v1/employees/{id}&quot;, String.class, 1)<br>        );<br>    }<br>    Thread.sleep(1000);<br>    String metersAsString = meterRegistry.getMetersAsString();<br>    /**<br>     * httpcomponents.httpclient.pool.route.max.default(GAUGE)[httpclient=&#39;our-pool&#39;]; value=5.0<br>     * httpcomponents.httpclient.pool.total.connections(GAUGE)[httpclient=&#39;our-pool&#39;, state=&#39;leased&#39;]; value=3.0<br>     * httpcomponents.httpclient.pool.total.connections(GAUGE)[httpclient=&#39;our-pool&#39;, state=&#39;available&#39;]; value=0.0<br>     * httpcomponents.httpclient.pool.total.max(GAUGE)[httpclient=&#39;our-pool&#39;]; value=10.0<br>     * httpcomponents.httpclient.pool.total.pending(GAUGE)[httpclient=&#39;our-pool&#39;]; value=0.0<br>     */<br>    List&lt;String&gt; poolMetrics = Arrays.stream(metersAsString.split(&quot;\n&quot;))<br>            .filter(name -&gt; name.contains(&quot;our-pool&quot;))<br>            .toList();<br>    assertThat(poolMetrics).hasSize(5);<br>    assertThat(poolMetrics)<br>            .anyMatch(metric -&gt; metric.contains(&quot;httpcomponents.httpclient.pool.total.connections(GAUGE)&quot;) &amp;&amp;<br>                    metric.contains(&quot;state=&#39;leased&#39;&quot;) &amp;&amp;<br>                    metric.contains(&quot;value=3.0&quot;))<br>            .anyMatch(metric -&gt; metric.contains(&quot;httpcomponents.httpclient.pool.total.max(GAUGE)&quot;));<br>}</pre><h3>The short conclusion of Part 6</h3><p>Like any other part of the Spring ecosystem, Spring’s web client has built-in support for observability. You will get it out of the box if you use @Autowired RestTemplateBuilder rtb to build RestTemplate instances. If you have Prometheus or another monitoring system configured, check, how many metrics are collected from your web client, and if needed, disable it. Remember also to monitor the connection pool and adjust its size according to your needs.</p><h3>General conclusion for the series of articles</h3><p>These articles can’t be considered a complete guide to Spring’s web client or to making HTTP calls in general. Nor it can replace the effort of reading the official documentation and the source code of the libraries.</p><p>My goal was to share my personal experience in a nutshell and to show you some of the pitfalls that you might encounter. One of the core focuses was not only to mention certain feature/configuration options but also to create a reproducible example that allows one to reproduce the use case and play with it.</p><p>I hope that you found these articles useful and that you will be able to apply the knowledge to your projects. If you discovered any issues in the provided examples, feel free to create an issue in the comments or <a href="https://github.com/tkachenkoas/spring-web-client-in-depth">GitHub repository</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=657c25751d99" width="1" height="1" alt=""><hr><p><a href="https://medium.com/duda/adding-observability-for-web-client-657c25751d99">Adding observability for web client</a> was originally published in <a href="https://medium.com/duda">Duda</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting a hard limit on HTTP requests and failing fast]]></title>
            <link>https://medium.com/duda/setting-a-hard-limit-on-http-requests-and-failing-fast-65d709bb11f9?source=rss----a8c59e8a5adc---4</link>
            <guid isPermaLink="false">https://medium.com/p/65d709bb11f9</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[http-client]]></category>
            <category><![CDATA[toxiproxy]]></category>
            <dc:creator><![CDATA[Anton Tkachenko]]></dc:creator>
            <pubDate>Tue, 11 Jun 2024 19:28:28 GMT</pubDate>
            <atom:updated>2024-06-11T19:28:28.081Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4Sw9rHTXEAdEXBwn" /></figure><h3>Introduction</h3><p>This article is part of a series of articles about a deeper dive into making HTTP calls in Spring.</p><ul><li><a href="https://medium.com/p/c3a2763e9e4c">Part 1 — Options and testing</a></li><li><a href="https://medium.com/p/10a9a79e33ff">Part 2 — Spring web client and test kit</a></li><li><a href="https://medium.com/p/135d5b0ed4e6">Part 3 — ClientHttpRequestFactory as an abstraction over the HTTP layer</a></li><li><a href="https://medium.com/p/d7df011d52ec">Part 4 — Configuring Apache Client via HttpComponentsClientHttpRequestFactory</a></li><li><a href="https://medium.com/p/65d709bb11f9">Part 5 — Setting a hard limit on a request and failing fast</a> (you’re here)</li><li><a href="https://medium.com/p/657c25751d99">Part 6 — Adding observability for web client</a><br>All source code is available in the <a href="https://github.com/tkachenkoas/spring-web-client-in-depth">GitHub repository</a>.</li></ul><h3>Understanding long requests and properly imitating them</h3><p>In the previous article, we’ve only scratched the case of long requests and imitated them by setting a response delay with MockServer. In the real world, the following scenarios (and not only) can lead to long requests:</p><ul><li>slow network (e.g. low bandwidth)</li><li>bad network (e.g. data slicing with delays, packet loss…)</li><li>either-side misconfiguration not allowing use of compression</li><li>… and many more</li></ul><p>To imitate these cases, we’ll do the following:</p><ul><li>write a test application setup allows to access a controller via HTTP and Tomcat. The controller itself will just behave normally (but will allow us to configure the amount of data it will return and whether it will be compressed).</li><li>send all the requests to the controller via <a href="https://github.com/Shopify/toxiproxy">Toxiproxy</a> that will introduce network misbehaviour. This is where we’ll introduce mutations to see how the configuration of the http client can protect us.</li></ul><h3>Application &amp; test setup</h3><p>We have a simple controller that will return a list of products. The controller has two endpoints:</p><ul><li>/responses that will return a list of products as a text/plain string that is JSON (but not as application/JSON). And the only mime-type that supports compression according to server.compression.mime-types is application/json.</li><li>/compressed-responses that will return a list of products as a JSON string with application/json mime-type.</li></ul><pre>/**<br> * Calls to our application need to come via http, so we need to start a server.<br> */<br>@SpringBootTest(<br>        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,<br>        properties = {<br>                &quot;server.compression.enabled=true&quot;,<br>                &quot;server.compression.mime-types=application/json&quot;,<br>                &quot;server.compression.min-response-size=10240&quot;<br>        }<br>)<br>@Slf4j<br>public class Part05_02_SettingHardLimitAndFailingFast {<br>    /**<br>     * The excludes of auto-configurations are just to start the context,<br>     * since the repository has various spring-boot-starters for other examples.<br>     * They are not related to the content of this class.<br>     */<br>    @SpringBootApplication(<br>            exclude = {<br>                    MongoAutoConfiguration.class,<br>                    MongoDataAutoConfiguration.class,<br>                    RedisAutoConfiguration.class,<br>                    JdbcRepositoriesAutoConfiguration.class,<br>                    DataSourceAutoConfiguration.class<br>            }<br>    )<br>    static class NetworkCasesEmulationApplication {<br>        @RestController<br>        static class TheTestController {<br>            // we&#39;ll reuse some dummy data for the responses, and reusing it will ensure that data is compressable<br>            static final List&lt;String&gt; NAMES_POOL = List.of(&quot;MacBook Pro&quot;, &quot;iPhone 12&quot;, &quot;iPad Pro&quot;, &quot;Apple Watch&quot;, &quot;AirPods&quot;);<br>            static final List&lt;String&gt; DESCRIPTIONS_POOL = List.of(<br>                    &quot;The MacBook Pro is a line of Macintosh portable computers introduced in January 2006, by Apple Inc.&quot;,<br>                    &quot;The iPhone 12 and iPhone 12 Mini are smartphones designed, developed, and marketed by Apple Inc.&quot;,<br>                    &quot;The iPad Pro is a line of iPad tablet computers designed, developed, and marketed by Apple Inc.&quot;,<br>                    &quot;Apple Watch is a line of smartwatches produced by Apple Inc.&quot;,<br>                    &quot;AirPods are wireless Bluetooth earbuds created by Apple Inc.&quot;<br>            );<br>            record DummyProduct(<br>                    String id,<br>                    String name,<br>                    String description<br>            ) {<br>            }<br>            // this is a &quot;trick&quot; not to compress the response<br>            @GetMapping(value = &quot;/responses&quot;, produces = &quot;application/octet-stream&quot;)<br>            public String getResponses(<br>                    @RequestParam(required = false, defaultValue = &quot;0&quot;) int count<br>            ) throws JsonProcessingException {<br>                List&lt;DummyProduct&gt; products = generateProducts(count);<br>                return new ObjectMapper().writeValueAsString(products);<br>            }<br>            // application/json is compressible according to the configuration<br>            @GetMapping(value = &quot;/compressed-responses&quot;, produces = &quot;application/json&quot;)<br>            public List&lt;DummyProduct&gt; getCompressedResponses(<br>                    @RequestParam(required = false, defaultValue = &quot;0&quot;) int count<br>            ) {<br>                return generateProducts(count);<br>            }<br><br>            // Such single product on average will be ~100 bytes<br>            private List&lt;DummyProduct&gt; generateProducts(int count) {<br>                List&lt;DummyProduct&gt; products = new ArrayList&lt;&gt;();<br>                for (int i = 0; i &lt; count; i++) {<br>                    String id = RandomStringUtils.randomAlphanumeric(10);<br>                    int productIndex = new Random().nextInt(NAMES_POOL.size());<br>                    int descriptionIndex = new Random().nextInt(DESCRIPTIONS_POOL.size());<br>                    products.add(<br>                            new DummyProduct(<br>                                    id,<br>                                    NAMES_POOL.get(productIndex),<br>                                    DESCRIPTIONS_POOL.get(descriptionIndex)<br>                            )<br>                    );<br>                }<br>                return products;<br>            }<br>        }<br>    }<br>    <br>}</pre><h3>ToxiProxy setup</h3><p>ToxiProxy also comes as a <a href="https://java.testcontainers.org/modules/toxiproxy/">TestContainer</a>, so you can start using it pretty easy. The documentation for setup and usage is great and provides multiple examples. I will just show very basic usage of it.</p><pre>/** we need tomcat to be already running to allow testcontainers to call the port */<br>    @LocalServerPort<br>    int serverPort;<br><br>    ToxiproxyContainer toxiproxy = new ToxiproxyContainer(&quot;ghcr.io/shopify/toxiproxy:2.5.0&quot;)<br>            .withLogConsumer(new Slf4jLogConsumer(log));<br>    record ToxiProxySetup(<br>            String host,<br>            int port,<br>            eu.rekawek.toxiproxy.Proxy proxy<br>    ) {<br>    }<br>    /**<br>     * See documentation of TestContainers for more details on how to configure<br>     * networks in various cases.<br>     */<br>    @SneakyThrows<br>    private ToxiProxySetup setupToxiProxy() {<br>        Testcontainers.exposeHostPorts(serverPort);<br>        toxiproxy.start();<br>        log.info(&quot;Server port: {}&quot;, serverPort);<br>        ToxiproxyClient toxiproxyClient = new ToxiproxyClient(toxiproxy.getHost(), toxiproxy.getControlPort());<br>        Proxy proxy = toxiproxyClient.createProxy(<br>                &quot;proxy-to-local-server&quot;, &quot;0.0.0.0:8666&quot;,<br>                &quot;host.testcontainers.internal:&quot; + serverPort<br>        );<br>        String proxyHost = toxiproxy.getHost();<br>        int proxyPort = toxiproxy.getMappedPort(8666);<br>        log.info(&quot;Toxiproxy port: {} &amp; IP: {}&quot;, proxyPort, proxyHost);<br>        return new ToxiProxySetup(proxyHost, proxyPort, proxy);<br>    }</pre><h3>Exploring the performance of compressed and non-compressed endpoints</h3><p>This is what test helper methods look like:</p><pre>/** Applies timeout of 1 second to the connection pool and 2 seconds to the request. */<br>private static RestTemplate buildRestTemplateWithLimits() {<br>    return buildRestTemplateWithLimits(<br>            Timeout.ofSeconds(1), Timeout.ofSeconds(2)<br>    );<br>}<br><br>/** Will configure the connection pool&#39;s socket timeout and request&#39;s timeout. */<br>private static RestTemplate buildRestTemplateWithLimits(<br>        Timeout socketTimeout, Timeout responseTimeout<br>) {<br>    return buildRestTemplateWithLimits(<br>            connConfig -&gt; connConfig.setSocketTimeout(socketTimeout),<br>            reqConfig -&gt; reqConfig.setResponseTimeout(responseTimeout)<br>    );<br>}<br>/** Applies no customizations to the connection pool and request configuration. */<br>private static RestTemplate buildRestTemplate() {<br>    return buildRestTemplateWithLimits(<br>            __ -&gt; {<br>            }, __ -&gt; {<br>            }<br>    );<br>}<br>private static RestTemplate buildRestTemplateWithLimits(<br>        Consumer&lt;ConnectionConfig.Builder&gt; poolConfigCustomizer,<br>        Consumer&lt;RequestConfig.Builder&gt; requestConfigCustomizer<br>) {<br>    ConnectionConfig.Builder connectionConfigBuilder = ConnectionConfig.custom()<br>            .setConnectTimeout(Timeout.ofSeconds(1));<br>    poolConfigCustomizer.accept(connectionConfigBuilder);<br>    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()<br>            .setConnectionRequestTimeout(Timeout.ofSeconds(1));<br>    requestConfigCustomizer.accept(requestConfigBuilder);<br>    return new RestTemplateBuilder()<br>            .requestFactory(() -&gt; {<br>                PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()<br>                        .setDefaultConnectionConfig(connectionConfigBuilder.build())<br>                        .build();<br>                return new HttpComponentsClientHttpRequestFactory(<br>                        HttpClients.custom()<br>                                .setConnectionManager(connectionManager)<br>                                .setDefaultRequestConfig(requestConfigBuilder.build())<br>                                .build()<br>                );<br>            }).build();<br>}<br><br>private Duration timeIt(Runnable runnable) {<br>    StopWatch stopWatch = new StopWatch();<br>    stopWatch.start();<br>    runnable.run();<br>    stopWatch.stop();<br>    return Duration.ofMillis(stopWatch.getTotalTimeMillis());<br>}</pre><p>Let’s take a look at comparing the response time of fetching 100 “products” from an endpoint with limited bandwidth with compression and without compression. Pay attention to the third call that uses RestTemplate without any configuration: this can make a huge difference in the performance of fetching big chunks of data. Whether compression will actually be applied or not depends both on the client AND server configuration.</p><pre>@Test<br>void havingBandwidthLimit_compressionShouldHelp() throws Throwable {<br>    ToxiProxySetup proxySetup = setupToxiProxy();<br>    proxySetup.proxy().toxics().bandwidth(&quot;slow-down&quot;, ToxicDirection.DOWNSTREAM,<br>            // rate in kilobytes per second<br>            5);<br>    // we don&#39;t care about actual limits now<br>    RestTemplate restTemplate = buildRestTemplate();<br>    // we&#39;ll make a call to the endpoint that returns ~12-15kb of data<br>    Duration duration = timeIt(() -&gt; {<br>        ResponseEntity&lt;String&gt; response = restTemplate.getForEntity(<br>                &quot;http://{ip}:{port}/responses?count=100&quot;,<br>                String.class, proxySetup.host(), proxySetup.port()<br>        );<br>        HttpHeaders headers = response.getHeaders();<br>        assertThat(headers.getContentType().toString())<br>                .contains(&quot;text/plain&quot;);<br>        assertThat(headers.getContentLength())<br>                .isBetween(12 * 1024L, 15 * 1024L);<br>        // parse body from JSON -&gt; list will have 100 elements + some field values<br>        List&lt;DummyProduct&gt; objects = new ObjectMapper().readValue(response.getBody(), new TypeReference&lt;&gt;() {<br>        });<br>        assertThat(objects).hasSize(100);<br>        assertThat(objects.get(0).name()).isIn(TheTestController.NAMES_POOL);<br>        assertThat(objects.get(0).description()).isIn(TheTestController.DESCRIPTIONS_POOL);<br>    });<br>    assertThat(duration).isBetween(<br>            Duration.ofMillis(2500), Duration.ofMillis(3500)<br>    );<br>    // now let&#39;s see what happens when we call the endpoint eligible for compression<br>    Duration compressedDuration = timeIt(() -&gt; {<br>        ResponseEntity&lt;List&lt;DummyProduct&gt;&gt; products = restTemplate.exchange(<br>                &quot;http://{ip}:{port}/compressed-responses?count=100&quot;,<br>                GET, null,<br>                new ParameterizedTypeReference&lt;&gt;() {<br>                }, proxySetup.host(), proxySetup.port()<br>        );<br>        assertThat(products.getHeaders().getContentType())<br>                .hasToString(&quot;application/json&quot;);<br>        assertThat(products.getBody()).hasSize(100);<br>        assertThat(products.getBody().get(0).name()).isIn(TheTestController.NAMES_POOL);<br>        assertThat(products.getBody().get(0).description()).isIn(TheTestController.DESCRIPTIONS_POOL);<br>    });<br>    assertThat(compressedDuration)<br>            .isLessThan(Duration.ofSeconds(1));<br>    // btw, new RestTemplate() will not support compression<br>    RestTemplate restTemplateWithoutCompression = new RestTemplate();<br>    Duration noCompressionSupportOnClient = timeIt(() -&gt; {<br>        ResponseEntity&lt;List&lt;DummyProduct&gt;&gt; products = restTemplateWithoutCompression.exchange(<br>                &quot;http://{ip}:{port}/compressed-responses?count=100&quot;,<br>                GET, null,<br>                new ParameterizedTypeReference&lt;&gt;() {<br>                }, proxySetup.host(), proxySetup.port()<br>        );<br>        assertThat(products.getHeaders().getContentType())<br>                .hasToString(&quot;application/json&quot;);<br>        assertThat(products.getBody()).hasSize(100);<br>        assertThat(products.getBody().get(0).name()).isIn(TheTestController.NAMES_POOL);<br>        assertThat(products.getBody().get(0).description()).isIn(TheTestController.DESCRIPTIONS_POOL);<br>    });<br>    assertThat(noCompressionSupportOnClient).isBetween(<br>            Duration.ofMillis(2500), Duration.ofMillis(3500)<br>    );<br>}</pre><h3>Exploring the behaviour of timeouts in real-world scenarios</h3><p>Now let’s see how timeout configurations can behave when application meets some disturbances in the network. In the example below, we set the connection pool timeout to 1 second and request timeout to 2 seconds. The endpoint will return data with ~1 second delay between every 100 bytes of data. The overall time of the request will be way above the timeouts that we set. It is common to misunderstand the actual meaning of timeouts: timeout is the time that the client will wait for the next byte of data to arrive, not the overall maximum execution time of the request. Thus, even if your client is configured with certain timeouts, some real-world scenarios can still lead to long requests.</p><pre>@Test<br>void timeoutsOnRestTemplateMayNotBeEnough() throws Exception {<br>    ToxiProxySetup setup = setupToxiProxy();<br>    setup.proxy().toxics().slicer(<br>            &quot;slice&quot;, ToxicDirection.DOWNSTREAM,<br>            // size in bytes of the slice<br>            100,<br>            // delay between slices in microseconds, e.g. 1 second<br>            1_000_000<br>    );<br>    // socket timeout is 1 second, request timeout is 2 seconds<br>    var restTemplateWithTimeout = buildRestTemplateWithLimits();<br>    StopWatch stopWatch = new StopWatch();<br>    stopWatch.start();<br>    // the server will respond quickly, but the client will receive the response in slices<br>    String rawResponse = restTemplateWithTimeout<br>            .getForObject(&quot;http://{ip}:{port}/responses?count=4&quot;, String.class,<br>                    setup.host(), setup.port()<br>            );<br>    // due to randomness of content, we can&#39;t predict the exact length<br>    assertThat(rawResponse.length()).isBetween(400, 700);<br>    stopWatch.stop();<br>    // and overall time will be way about timeouts that we set<br>    double time = stopWatch.getTotalTime(TimeUnit.SECONDS);<br>    assertThat(time).isBetween(4.0, 8.0);<br>}</pre><h3>Configuring hard total timeout on the request</h3><p>If we want to set a real “hard” limit on the request execution, we can leverage Apache client’s HttpUriRequestBase#cancel method and schedule it to be called after a certain amount of time. However, this raises an issue reaching the actual HttpUriRequestBasefromRestTemplatehigh-level API. Below is an example of how to do it by using reflection and decorating the request fromHttpComponentsClientHttpRequestFactory. Alternatively, there is a way to useClientHttpRequestInterceptorofRestTemplateororg.apache.hc.core5.http.HttpRequestInterceptorto configureHttpClientBuilder`.</p><p>Note that aborting a request is a complex mechanism that relies on setting a “cancelled” flag in the request object, and actual cancellation will happen only after certain logic responsible for sending the request will check this flag. Think of it as something similar to “interrupting” a thread in Java: it’s not guaranteed that the running task will stop, unless certain logic in the flow will check the “interrupted” flag and throw an exception.</p><pre>@Test<br>@SneakyThrows<br>void hardLimit_canBeSetByAbortingNativeApacheRequest() {<br>    ToxiProxySetup setup = setupToxiProxy();<br>    setup.proxy().toxics().slicer(<br>            &quot;slice&quot;, ToxicDirection.DOWNSTREAM,<br>            // avg size + delay in microseconds<br>            100, 1_000_000<br>    );<br>    var restTemplate = buildRestTemplateWithLimits();<br>    HttpComponentsClientHttpRequestFactory factory = (HttpComponentsClientHttpRequestFactory)<br>            restTemplate.getRequestFactory();<br>    ClientHttpRequestFactory factoryWithHardTimeout = (uri, httpMethod) -&gt; {<br>        var request = factory.createRequest(uri, httpMethod);<br>        return requestWithHardTimeout(request, 3, TimeUnit.SECONDS);<br>    };<br>    restTemplate.setRequestFactory(factoryWithHardTimeout);<br>    // now let&#39;s see what happens when we fetch the data from &quot;slow/sliced&quot; endpoint<br>    assertThatExceptionOfType(RestClientException.class)<br>            .isThrownBy(() -&gt; restTemplate.getForObject(<br>                    &quot;http://{ip}:{port}/responses?count=4&quot;, String.class,<br>                    setup.host(), setup.port()<br>            )).havingCause()<br>            .isInstanceOf(SocketException.class)<br>            .withMessage(&quot;Socket closed&quot;);<br>}<br>@SneakyThrows<br>private static ClientHttpRequest requestWithHardTimeout(<br>        ClientHttpRequest request,<br>        int timeout, TimeUnit timeoutTimeUnit<br>) {<br>    AbstractClientHttpRequest clientRequest = (AbstractClientHttpRequest) request;<br>    // get field &quot;httpRequest&quot; from the request<br>    Field reqField = ReflectionUtils.findField(request.getClass(), &quot;httpRequest&quot;);<br>    ReflectionUtils.makeAccessible(reqField);<br>    HttpUriRequestBase apacheHttpRequest = (HttpUriRequestBase) reqField.get(clientRequest);<br>    // schedule canceling the request after provided timeout<br>    log.info(&quot;Scheduling hard cancel in {} {}&quot;, timeout, timeoutTimeUnit);<br>    Executors.newSingleThreadScheduledExecutor().schedule(() -&gt; {<br>        log.info(&quot;Hard cancelling the request&quot;);<br>        apacheHttpRequest.cancel();<br>    }, timeout, timeoutTimeUnit);<br>    return request;<br>}</pre><p>However, I do not recommend aborting requests, because:</p><ul><li>it’s relying on too many internal details of the factory powering the RestTemplate. Thus, changes in the factory implementation can break your code</li><li>it’s not guaranteed that the request object can be cast to Cancellable in all flows - thus logic will become more complex</li><li>concurrency and cancellation might mess up with some lifecycle of the request/connection — and you might end up getting strange exceptions/warnings that are not reproducible in unit tests</li><li>consistent cancellation of requests usually indicates that something goes wrong, and a better approach is to fail fast even before the request is sent</li></ul><h3>Recommended way of dealing with requests that take too long</h3><p>If you know that certain requests can take time, a good approach is usually to make them run in the background so that synchronous operations are not blocked. However, you still want to have a safety mechanism to protect your application from being overwhelmed by too many ultra-long requests that bypass your basic timeout mechanism. Setting hard limits is not a good idea also for the following reasons:</p><ul><li>suppose, you have configured general timeouts to be ~10 seconds</li><li>but you know that normally requests should not take more than 2–3 seconds, and maybe you want to also set a hard limit of 15 seconds</li><li>however, some disruptions in the network started to cause all your requests to take 25 seconds</li><li>this means that now ALL your requests will be failing, and they will also block resources for the whole duration of the ‘sufficient’ 15-second limit, and you probably don’t want this behaviour</li></ul><p>A classic approach is to use Circuit breaker pattern. The idea behind it is the following:</p><ul><li>when a certain number of requests fail or take too long, we need to stop calling the service — this is called “opening the circuit”; instead, an exception will be thrown immediately — and we’ll follow the “failing fast” principle</li><li>however, after some time (or manual action), calls should be allowed again to check if the service is back to normal</li></ul><p>Probably the best library to use for this purpose is Resilience4j. Please refer to the documentation for more details: <a href="https://resilience4j.readme.io/docs/circuitbreaker">R4J Circuit Breaker</a>. By the way, it provides implementations of many other resilience patterns like RateLimiter, Retry, Bulkhead and others.</p><p>The lifecycle and configuration of the circuit breaker are very powerful (and complex), and it’s hard to provide a “one-size-fits-all” configuration. Thus, when decorating a certain operation with a circuit breaker, you need to test its behaviour very carefully and also to have good monitoring of the circuit breaker’s state. The example below is basic, yet it shows an example of how CircuitBreaker in pair with ClientHttpRequestInterceptor to protect your application from being disrupted by too many long or failing requests.</p><pre>@Test<br>void whenSomethingGoesWrongConsistently_weShouldShortCircuitCalls() throws Exception {<br>    ToxiProxySetup setup = setupToxiProxy();<br><br>    // let&#39;s imagine that service consistently returns slow responses<br>    setup.proxy().toxics().slicer(<br>            &quot;slice&quot;, ToxicDirection.DOWNSTREAM,<br>            // avg size + delay in microseconds<br>            100, 500_000<br>    );<br>    // this operation will succeed, but it will be slow<br>    RestTemplate restTemplate = buildRestTemplateWithLimits();<br>    Duration duration = timeIt(() -&gt; restTemplate.getForObject(<br>            &quot;http://{host}:{port}/responses?count=1&quot;,<br>            String.class, setup.host(), setup.port()<br>    ));<br>    assertThat(duration).isBetween(Duration.ofMillis(1000), Duration.ofMillis(2000));<br>    CircuitBreaker circuitBreaker = CircuitBreaker.of(<br>            &quot;slow-service&quot;, CircuitBreakerConfig.custom()<br>                    // 500ms is the threshold for slow calls<br>                    .slowCallDurationThreshold(Duration.ofMillis(500))<br>                    // and 90% of calls should be slow for the circuit breaker to open<br>                    .slowCallRateThreshold(90)<br>                    // however, we&#39;ll accumulate statistics of at least 4 calls<br>                    .minimumNumberOfCalls(4)<br>                    // e.g. only 10 calls will be taken into account for calculating statistics<br>                    .slidingWindowSize(10)<br>                    .slidingWindowType(COUNT_BASED)<br>                    .waitDurationInOpenState(Duration.ofSeconds(1))<br>                    .enableAutomaticTransitionFromOpenToHalfOpen()<br>                    .permittedNumberOfCallsInHalfOpenState(1)<br>                    .maxWaitDurationInHalfOpenState(Duration.ofSeconds(2))<br>                    .build()<br>    );<br>    circuitBreaker.getEventPublisher().onStateTransition(event -&gt; {<br>        log.info(&quot;Circuit breaker state transition: {}&quot;, event);<br>    });<br>    // now we configure the rest template to use the circuit breaker via interceptor<br>    ClientHttpRequestInterceptor interceptor = (request, body, execution) -&gt; {<br>        log.info(&quot;Current state before call: {}&quot;, circuitBreaker.getState());<br>        try {<br>            return circuitBreaker.executeCheckedSupplier(() -&gt; {<br>                ClientHttpResponse executed = execution.execute(request, body);<br>                log.info(&quot;Call was executed successfully&quot;);<br>                return executed;<br>            });<br>        } catch (Throwable e) {<br>            // request interceptor&#39;s contract is to throw IOException<br>            throw e instanceof IOException<br>                    ? (IOException) e<br>                    : new IOException(e);<br>        }<br>    };<br>    restTemplate.getInterceptors().add(interceptor);<br>    // the behavior of rest template is now changed<br>    // first 3 calls will be slow, but they will succeed<br>    for (int i = 0; i &lt; 4; i++) {<br>        assertThatCode(() -&gt; restTemplate.getForObject(<br>                &quot;http://{host}:{port}/responses?count=1&quot;,<br>                String.class, setup.host(), setup.port()<br>        )).doesNotThrowAnyException();<br>    }<br>    // but if we make 4th, 5th, 6th and 7th call - they will fail<br>    for (int i = 0; i &lt; 4; i++) {<br>        assertThatExceptionOfType(ResourceAccessException.class).isThrownBy(() -&gt; {<br>                    restTemplate.getForObject(<br>                            &quot;http://{host}:{port}/responses?count=1&quot;,<br>                            String.class, setup.host(), setup.port()<br>                    );<br>                }).havingRootCause().isInstanceOf(CallNotPermittedException.class)<br>                .withMessageContaining(&quot;CircuitBreaker &#39;slow-service&#39; is OPEN&quot;);<br>    }<br>    // e.g. the circuit breaker is now in open state<br>    assertThat(circuitBreaker.getState()).isEqualTo(State.OPEN);<br>    // however, after some time (1 second according to the configuration)<br>    // the circuit breaker will allow to make calls again<br>    log.info(&quot;Waiting for the circuit breaker to switch to half-open state&quot;);<br>    await().atMost(Duration.ofMillis(1200))<br>            .until(() -&gt; circuitBreaker.getState() == State.HALF_OPEN);<br>    // and the call will succeed<br>    assertThatCode(() -&gt; restTemplate.getForObject(<br>            &quot;http://{host}:{port}/responses?count=1&quot;,<br>            String.class, setup.host(), setup.port()<br>    )).doesNotThrowAnyException();<br>    // now we remove our &quot;toxin&quot; to emulate that service is back to normal<br>    setup.proxy().toxics().get(&quot;slice&quot;).remove();<br>    // even though the target recovered, some calls may still fail because<br>    // previous calls in &quot;suspicious&quot; HALF_OPEN state were still slow<br>    assertThatExceptionOfType(ResourceAccessException.class).isThrownBy(() -&gt; {<br>                restTemplate.getForObject(<br>                        &quot;http://{host}:{port}/responses?count=1&quot;,<br>                        String.class, setup.host(), setup.port()<br>                );<br>            }).havingRootCause().isInstanceOf(CallNotPermittedException.class)<br>            .withMessageContaining(&quot;CircuitBreaker &#39;slow-service&#39; is OPEN&quot;);<br>    // now let&#39;s wait for the circuit breaker to switch to half-open state<br>    // again and make the call<br>    log.info(&quot;Waiting for the circuit breaker to switch to half-open state&quot;);<br>    await().atMost(Duration.ofMillis(1200))<br>            .until(() -&gt; circuitBreaker.getState() == State.HALF_OPEN);<br>    for (int i = 0; i &lt; 4; i++) {<br>        assertThatCode(() -&gt; restTemplate.getForObject(<br>                &quot;http://{host}:{port}/responses?count=1&quot;,<br>                String.class, setup.host(), setup.port()<br>        )).doesNotThrowAnyException();<br>    }<br>    // and once service got back to normal, circuit breaker will be closed<br>    assertThat(circuitBreaker.getState()).isEqualTo(State.CLOSED);<br>}</pre><h3>The short conclusion of Part 5</h3><p>This article illustrates how the real world can “by-pass” basic timeout configurations and how to emulate such cases in a “fair” way with ToxiProxy. It also shows how to set hard limits on the request execution, and why a better approach is to use CircuitBreaker. However, the most practical recommendation in this article is to check if your applications are using new RestTemplate() to make calls in production. If you don&#39;t have good monitoring, you might be surprised by how long some requests take, and how many resources they waste.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=65d709bb11f9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/duda/setting-a-hard-limit-on-http-requests-and-failing-fast-65d709bb11f9">Setting a hard limit on HTTP requests and failing fast</a> was originally published in <a href="https://medium.com/duda">Duda</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Configuring Apache Client via HttpComponentsClientHttpRequestFactory]]></title>
            <link>https://medium.com/duda/configuring-apache-client-via-httpcomponentsclienthttprequestfactory-d7df011d52ec?source=rss----a8c59e8a5adc---4</link>
            <guid isPermaLink="false">https://medium.com/p/d7df011d52ec</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[apache-httpclient]]></category>
            <category><![CDATA[resttemplate]]></category>
            <dc:creator><![CDATA[Anton Tkachenko]]></dc:creator>
            <pubDate>Tue, 11 Jun 2024 19:28:10 GMT</pubDate>
            <atom:updated>2024-06-11T19:28:10.832Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0RTabn2ZEx4vsZ_u" /></figure><h3>Introduction</h3><p>This article is part of a series of articles about a deeper dive into making HTTP calls in Spring.</p><ul><li><a href="https://medium.com/p/c3a2763e9e4c">Part 1 — Options and testing</a></li><li><a href="https://medium.com/p/10a9a79e33ff">Part 2 — Spring web client and test kit</a></li><li><a href="https://medium.com/p/135d5b0ed4e6">Part 3 — ClientHttpRequestFactory as an abstraction over the HTTP layer</a></li><li><a href="https://medium.com/p/d7df011d52ec">Part 4 — Configuring Apache Client via HttpComponentsClientHttpRequestFactory</a> (you’re here)</li><li><a href="https://medium.com/p/65d709bb11f9">Part 5 — Setting a hard limit on a request and failing fast</a></li><li><a href="https://medium.com/p/657c25751d99">Part 6 — Adding observability for web client</a><br>All source code is available in the <a href="https://github.com/tkachenkoas/spring-web-client-in-depth">GitHub repository</a>.</li></ul><h3>Configurations that you should have in production</h3><p>Until now, we only focused on functional happy paths — when we expected certain responses/headers/status codes. But in a production environment, we should also be prepared to meet certain non-functional requirements:</p><ul><li>ensuring certain throughput</li><li>making sure that calls are not blocked indefinitely</li><li>making sure that calls do not take too long</li><li>…</li></ul><p>Typical configurations for meeting these requirements are:</p><ul><li>reusing connections instead of creating a new connection for each request — to reduce TSL overhead</li><li>properly configuring the connection pool so as not to exhaust resources</li><li>setting certain timeouts to fail fast in case of a problem</li><li>also, we need to be able to monitor the actual behaviour of the client against the expected behaviour</li></ul><p>Base setup of mock server that we will use in the examples:</p><pre>@MockServerSettings(ports = 1100)<br>@RequiredArgsConstructor<br>@Slf4j<br>public class Part04_01_ConfiguringNativeHttpClient {<br><br>    private final MockServerClient mockServer;<br>    @BeforeEach<br>    void reset() {<br>        mockServer.reset();<br>    }<br>    <br>    /** <br>     * all the tests  <br>     */<br>    <br>}</pre><p>First, let’s take a short look at setting and testing timeout via SimpleClientHttpRequestFactory. As you can see, if the remote server is not responding in a reasonable time, we&#39;ll fail fast and not wait for all the time in the universe.</p><pre>@Test<br>void anExampleOfSettingReadTimeout_viaSimpleClientHttpRequestFactory() {<br>    // configure rest template to have a timeout<br>    RestTemplate restTemplate = new RestTemplate();<br>    SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();<br>    factory.setReadTimeout(Duration.ofSeconds(2));<br>    restTemplate.setRequestFactory(factory);<br><br>    // remote server is not responding in reasonable time<br>    mockServer.when(<br>            request()<br>                    .withMethod(&quot;GET&quot;)<br>                    .withPath(&quot;/some-endpoint&quot;)<br>    ).respond(<br>            response()<br>                    .withStatusCode(200)<br>                    .withDelay(<br>                            TimeUnit.SECONDS,<br>                            30 // 3000? :)<br>                    )<br>    );<br>    // and we&#39;d rather not wait for all the 30 seconds for the response<br>    StopWatch stopWatch = new StopWatch();<br>    stopWatch.start();<br>    assertThatThrownBy(<br>            () -&gt; restTemplate.getForObject(&quot;http://localhost:1100/some-endpoint&quot;, String.class)<br>    ).isInstanceOf(ResourceAccessException.class)<br>            .cause()<br>            .isInstanceOf(SocketTimeoutException.class);<br>    stopWatch.stop();<br>    var elapsed = stopWatch.getTotalTimeMillis();<br>    assertThat(elapsed)<br>            .isLessThan(2_300)<br>            .isGreaterThan(2_000);<br>}</pre><p>An important note about the meaning of readTimeout property. In most HTTP clients, the actual execution is delegated to JDK&#39;s HttpURLConnection, and here is the Javadoc for readTimeout property:</p><pre>Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from <br>Input stream when a connection is established to a resource. If the timeout expires before there is data available for <br>read, a java. net. SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.<br>Some non-standard implementation of this method ignores the specified timeout. To see the read timeout set, please call getReadTimeout().</pre><p>Internally, HttpURLConnection will most likely delegate the actual work to the underlying java.sun.NetworkClient and java.next.ServerSocket low-level classes. What is important to understand is that readTimeout is NOT the request execution timeout. In simple words, it&#39;s the maximum allowed time between two bytes of data being received. So, if you set readTimeout to 20 seconds, you&#39;re giving the server 20 seconds to go to DB, fetch the data, and start sending it to you. Now, if some transport-level issues with bandwidth or chunking happen in the middle, the application will wait up to 20 seconds, and in theory, the request can last MUCH longer than 20 seconds.</p><p>MockServer does not allow testing such cases in depth, but there are other tools like ToxiProxy that allow simulating such network issues - and we will cover them in the next article.</p><h3>Configuring Apache HttpClient via HttpComponentsClientHttpRequestFactory</h3><p>Apache HttpClient is an advanced HTTP client that has many features, and more configuration options, and it’s widely used in many production systems.</p><p>We’ll look into configuring two important aspects of Apache HttpClient:</p><ul><li>connection pooling and reusing connections</li><li>setting timeouts for the request and for the connection</li></ul><p>First, let’s check the behaviour of connection pooling and reusing connections. In the example below, we are making 20 concurrent requests to the same server, and we have only 10 connections in the pool. This means that the second half of the requests will have to wait, and if you want a bigger throughput, you should increase the pool size.</p><pre>@Test<br>void anExampleOfConfiguringConnectionPool_andHowItCanAffectThroughPut() {<br>    RestTemplate restTemplate = new RestTemplate();<br><br>    // pooling connections saves time on handshaking and other https steps,<br>    // and it saves resources on both caller and receiver side<br>    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();<br>    connectionManager.setMaxTotal(10);<br>    // in simple words, &quot;route&quot; is the target domain (host:port)<br>    connectionManager.setDefaultMaxPerRoute(10);<br>    restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(<br>            HttpClients.custom()<br>                    .setConnectionManager(connectionManager)<br>                    .build()<br>    ));<br>    // remote server takes 2 seconds to respond<br>    mockServer.when(<br>            request()<br>                    .withMethod(&quot;GET&quot;)<br>                    .withPath(&quot;/some-endpoint&quot;)<br>    ).respond(<br>            response()<br>                    .withStatusCode(200)<br>                    .withDelay(TimeUnit.SECONDS, 2)<br>    );<br>    StopWatch stopWatch = new StopWatch();<br>    stopWatch.start();<br>    // now let&#39;s make 20 concurrent requests and wait for all of them to finish<br>    CompletableFuture&lt;Void&gt; future = CompletableFuture.allOf(IntStream.range(0, 20)<br>            .mapToObj(i -&gt; CompletableFuture.runAsync(() -&gt; {<br>                restTemplate.getForObject(<br>                        &quot;http://localhost:1100/some-endpoint&quot;,<br>                        String.class<br>                );<br>            }))<br>            .toArray(CompletableFuture[]::new)<br>    );<br>    await().atMost(Duration.ofSeconds(1))<br>            .until(() -&gt; connectionManager.getTotalStats().getLeased() == 10);<br>    future.join();<br>    stopWatch.stop();<br>    var elapsed = stopWatch.getTotalTimeMillis();<br>    // even though we are making 20 requests concurrently,<br>    // we have only 10 connections in the pool<br>    // so the second half of the requests will have to wait<br>    assertThat(elapsed).isGreaterThan(4_000).isLessThan(4_500);<br>}</pre><p>One more example is about setting limits on the number of connections per route and choosing a pool concurrency policy. If you are using the same client to make requests to multiple hosts, you should limit the number of connections per route. This is especially important if you are making a lot of requests to a server that requires processing time, and you don’t want to exhaust the connection pool.</p><p>You can choose between LAX and STRICT pool concurrency policy. Under the hood, when a lease request is made, the pool will do some locking and unlocking, and LAX policy will allow more concurrency. This is useful when you have a lot of concurrent and short requests. With STRICT policy, you might experience some starvation and spend more time on locking and unlocking than on actual request processing.</p><p>Provided example illustrates that by limiting the number of connections per route, we can make 30 concurrent requests to a fast server and 10 requests to a slow server, and these groups of requests will not interfere with each other.</p><pre>@Test<br>void whenCallingMultipleHostsWithSameRestTemplate_connectionsShouldBePooledPerRoute() {<br>    // configure rest template with a limit of connections per route<br><br>    RestTemplate restTemplate = new RestTemplate();<br>    PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()<br>            .setMaxConnPerRoute(5)<br>            .setMaxConnTotal(10)<br>            // Higher concurrency but with lax connection max limit guarantees.<br>            // LAX,<br>            // Strict connection max limit guarantees.<br>            // STRICT<br>            .setPoolConcurrencyPolicy(LAX)<br>            .build();<br>    restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(<br>            HttpClients.custom()<br>                    .setConnectionManager(connectionManager)<br>                    .build()<br>    ));<br>    // this remote server is slow<br>    ClientAndServer slowServer = ClientAndServer.startClientAndServer(1200);<br>    slowServer.when(<br>            request()<br>                    .withMethod(&quot;GET&quot;)<br>                    .withPath(&quot;/slow-endpoint&quot;)<br>    ).respond(<br>            response().withStatusCode(200).withDelay(TimeUnit.SECONDS, 5)<br>    );<br>    // and this one is faster<br>    mockServer.when(<br>            request().withMethod(&quot;GET&quot;).withPath(&quot;/fast-endpoint&quot;)<br>    ).respond(<br>            response().withStatusCode(200).withDelay(TimeUnit.SECONDS, 1)<br>    );<br>    // now we will use same rest template to make requests to both servers<br>    // and count how many requests are made to each server<br>    AtomicInteger slowServerRequests = new AtomicInteger();<br>    AtomicInteger fastServerRequests = new AtomicInteger();<br>    // make 30 concurrent request attempts to fast server<br>    for (int i = 0; i &lt; 30; i++) {<br>        CompletableFuture.runAsync(() -&gt; {<br>            restTemplate.getForObject(&quot;http://localhost:1100/fast-endpoint&quot;, String.class);<br>            log.info(&quot;fastServerRequests = &quot; + fastServerRequests.incrementAndGet());<br>        });<br>    }<br>    // and also 10 requests to slow server<br>    for (int i = 0; i &lt; 10; i++) {<br>        CompletableFuture.runAsync(() -&gt; {<br>            restTemplate.getForObject(&quot;http://localhost:1200/slow-endpoint&quot;, String.class);<br>            log.info(&quot;slowServerRequests = &quot; + slowServerRequests.incrementAndGet());<br>        });<br>    }<br>    // fast server should have 30 requests in ~10-12 seconds<br>    await().atMost(Duration.ofSeconds(12))<br>            .until(() -&gt; fastServerRequests.get() == 30);<br>    // wait till slow server is done<br>    await().atMost(Duration.ofSeconds(10))<br>            .until(() -&gt; slowServerRequests.get() == 10);<br>}</pre><p>The Next configuration example is about setting timeouts for the request. When building an Apache HTTP client with PoolingHttpClientConnectionManager, you can set multiple parameters that will affect overall duration. The configuration might be a bit misguiding since you can configure ConnectionConfig and RequestConfig with similar parameters, and you won&#39;t have any exceptions with the feeling that you&#39;ve set inconsistent values.</p><p>The comments in the code are copy-pasted from corresponding javadocs. Pay attention that library defaults are usually not the configuration that you want to have in production. Not all the parameters can be properly unit-tested with MockServer or any other tools (for instance, I don&#39;t know a way to test connectTimeout).</p><ul><li>Parameters of org.apache.hc.client5.http.config.ConnectionConfig: <br>— socketTimeout - Determines the default socket timeout value for I/ O operations. Default: null (undefined) <br>— connectTimeout - Determines the timeout until a new connection is fully established. A timeout value of zero is interpreted as an infinite timeout. Default: 3 minutes. <br>— timeToLive - Defines the total span of time connections can be kept alive or execute requests. Default: null (undefined)</li><li>Parameters of org.apache.hc.client5.http.config.RequestConfig:<br> — connectionRequestTimeout - connection lease request timeout used when requesting a connection from the connection manager.<br> — responseTimeout - Determines the timeout until arrival of a response from the opposite endpoint. A timeout value of zero is interpreted as an infinite timeout. Please note that response timeout may be unsupported by HTTP transports with message multiplexing.</li></ul><p>First, we’ll illustrate the behaviour of connectionRequestTimeout: if all connections are already in lease by the pool manager, we should fail fast: the value of this parameter should not be too high.</p><pre>@Test<br>void limitedConnectionRequestTime_willThrowOnAllConnectionsBeingBlocked() {<br>    // we&#39;ll configure a single connection pool<br>    RestTemplate restTemplate = new RestTemplate();<br>    var manager = PoolingHttpClientConnectionManagerBuilder.create()<br>            .setMaxConnTotal(1)<br>            .build();<br>    restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(<br>            HttpClients.custom()<br>                    .setDefaultRequestConfig(<br>                            RequestConfig.custom()<br>                                    .setConnectionRequestTimeout(Timeout.of(2, TimeUnit.SECONDS))<br>                                    .build()<br>                    ).setConnectionManager(manager)<br>                    .build()<br>    ));<br>    // remote server is not responding in reasonable time<br>    mockServer.when(request().withMethod(&quot;GET&quot;).withPath(&quot;/some-endpoint&quot;))<br>            .respond(response().withStatusCode(200).withDelay(TimeUnit.SECONDS, 5));<br>    // this async operation will block the only connection in the pool<br>    CompletableFuture.runAsync(() -&gt; restTemplate.getForObject(&quot;http://localhost:1100/some-endpoint&quot;, String.class));<br>    // connection manager has api that can be used to monitor the pool<br>    await()<br>            .atMost(Duration.ofSeconds(1))<br>            .untilAsserted(() -&gt; {<br>                PoolStats totalStats = manager.getTotalStats();<br>                assertThat(totalStats.getPending()).isEqualTo(0);<br>                assertThat(totalStats.getLeased()).isEqualTo(1);<br>                assertThat(totalStats.getAvailable()).isEqualTo(0);<br>                assertThat(totalStats.getMax()).isEqualTo(1);<br>            });<br>    assertThatThrownBy(() -&gt; restTemplate.getForObject(&quot;http://localhost:1100/some-endpoint&quot;, String.class))<br>            .isInstanceOf(ResourceAccessException.class)<br>            .hasCauseInstanceOf(ConnectionRequestTimeoutException.class)<br>            .hasMessageContaining(&quot;Timeout deadline: 2000 MILLISECONDS, actual:&quot;);<br>}</pre><p>Now, let’s take a look at the “conflicting” parameter of socketTimeout of the connection pool and responseTimeout of the client&#39;s request config. The actual behaviour is that the responseTimeout will be used and will override the socketTimeout of the connection pool. If you want to put a breakpoint, here is the place: org.apache.hc.client5.http.impl.classic.InternalExecRuntime#execute(String, ClassicHttpRequest, HttpClientContext)</p><p>Here is a detailed example that illustrates various cases when an exception will be thrown, and when — it won’t</p><pre>private RestTemplate buildWithTimeouts(<br>            Timeout socketTimeout,<br>            Timeout responseTimeout<br>    ) {<br>    RestTemplate restTemplate = new RestTemplate();<br><br>    var manager = PoolingHttpClientConnectionManagerBuilder.create()<br>                .setDefaultConnectionConfig(<br>                        ConnectionConfig.custom()<br>                                .setSocketTimeout(socketTimeout)<br>                                .build()<br>                )<br>                .build();<br>    restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(<br>            HttpClients.custom()<br>                    .setDefaultRequestConfig(<br>                            RequestConfig.custom()<br>                                    .setResponseTimeout(responseTimeout)<br>                                    .build()<br>                    ).setConnectionManager(manager)<br>                    .build()<br>    ));<br>    return restTemplate;<br>}<br><br>@Test<br>void requestTimeoutOfTheClient_willOverrideSocketTimeoutOfConnectionPool() {<br>    // configure endpoint to respond in 2 seconds<br>    mockServer.when(request().withMethod(&quot;GET&quot;).withPath(&quot;/some-endpoint&quot;))<br>            .respond(response()<br>                    .withBody(&quot;hello&quot;)<br>                    .withStatusCode(200).withDelay(TimeUnit.SECONDS, 2));<br>    // set only socket timeout<br>    RestTemplate onlySocketAndAbove = buildWithTimeouts(<br>            Timeout.ofSeconds(5), null<br>    );<br>    assertThat(onlySocketAndAbove.getForObject(&quot;http://localhost:1100/some-endpoint&quot;, String.class))<br>            .isEqualTo(&quot;hello&quot;);<br>    // if socket timeout is less than server delay time, it will get an exception<br>    RestTemplate onlySocketAndBelow = buildWithTimeouts(<br>            Timeout.ofSeconds(1), null<br>    );<br>    assertThatThrownBy(() -&gt; onlySocketAndBelow.getForObject(&quot;http://localhost:1100/some-endpoint&quot;, String.class))<br>            .isInstanceOf(ResourceAccessException.class)<br>            .hasCauseInstanceOf(SocketTimeoutException.class);<br>    // set only response timeout<br>    RestTemplate onlyResponseAndAbove = buildWithTimeouts(<br>            null, Timeout.ofSeconds(5)<br>    );<br>    assertThat(onlyResponseAndAbove.getForObject(&quot;http://localhost:1100/some-endpoint&quot;, String.class))<br>            .isEqualTo(&quot;hello&quot;);<br>    // if response timeout is less than server delay time, it will get an exception<br>    RestTemplate onlyResponseAndBelow = buildWithTimeouts(<br>            null, Timeout.ofSeconds(1)<br>    );<br>    assertThatThrownBy(() -&gt; onlyResponseAndBelow.getForObject(&quot;http://localhost:1100/some-endpoint&quot;, String.class))<br>            .isInstanceOf(ResourceAccessException.class)<br>            .hasCauseInstanceOf(SocketTimeoutException.class);<br>    // set both socket and response timeout, but socket timeout is less than server delay time<br>    RestTemplate bothAndSocketIsBelow = buildWithTimeouts(<br>            Timeout.ofSeconds(1), Timeout.ofSeconds(5)<br>    );<br>    // response timeout wins<br>    assertThat(bothAndSocketIsBelow.getForObject(&quot;http://localhost:1100/some-endpoint&quot;, String.class))<br>            .isEqualTo(&quot;hello&quot;);<br>    // not response timeout is less than server delay time<br>    RestTemplate bothAndResponseIsBelow = buildWithTimeouts(<br>            Timeout.ofSeconds(5), Timeout.ofSeconds(1)<br>    );<br>    assertThatThrownBy(() -&gt; bothAndResponseIsBelow.getForObject(&quot;http://localhost:1100/some-endpoint&quot;, String.class))<br>            .isInstanceOf(ResourceAccessException.class)<br>            .hasCauseInstanceOf(SocketTimeoutException.class);<br>}</pre><p>I’ll continue with a more complex case of limiting full request execution time in the next article.</p><h3>The short conclusion of Part 4</h3><p>In this article, we’ve seen how to use Apache HttpClient in RestTemplate via HttpComponentsClientHttpRequestFactory, how and why connection pooling is important, and how to set socket timeouts for the request and connection.</p><p>My advice regarding these configs is to have tests for them independently of the business logic tests to be able to verify expected behaviour on simple examples and to make sure that you deliver the expected behaviour in production.</p><p>In case you hesitate whether to use MockServer or MockRestServiceServer, I suggest the following rule of thumb:</p><ul><li>if you’re testing the logic/interaction of your component with a remote API via RestTemplate, use MockRestServiceServer. It&#39;s very flexible and even allows you to test the &quot;real&quot; URL.</li><li>if you’re testing how your rest client makes HTTP calls, use MockServer. In contrast to MockRestServiceServer, it&#39;s not replacing a real layer of your application (e.g. HTTP client), thus making tests more honest.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d7df011d52ec" width="1" height="1" alt=""><hr><p><a href="https://medium.com/duda/configuring-apache-client-via-httpcomponentsclienthttprequestfactory-d7df011d52ec">Configuring Apache Client via HttpComponentsClientHttpRequestFactory</a> was originally published in <a href="https://medium.com/duda">Duda</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ClientHttpRequestFactory asфт  abstraction over the HTTP layer]]></title>
            <link>https://medium.com/duda/clienthttprequestfactory-as%D1%84%D1%82-abstraction-over-the-http-layer-135d5b0ed4e6?source=rss----a8c59e8a5adc---4</link>
            <guid isPermaLink="false">https://medium.com/p/135d5b0ed4e6</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[http-client]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[resttemplate]]></category>
            <dc:creator><![CDATA[Anton Tkachenko]]></dc:creator>
            <pubDate>Tue, 11 Jun 2024 19:27:56 GMT</pubDate>
            <atom:updated>2024-06-11T19:27:56.028Z</atom:updated>
            <content:encoded><![CDATA[<h3>ClientHttpRequestFactory as abstraction over the HTTP layer</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CpEjWGpbGo1rumSf" /></figure><h3>Introduction</h3><p>This article is part of a series of articles about a deeper dive into making HTTP calls in Spring.</p><ul><li><a href="https://medium.com/p/c3a2763e9e4c">Part 1 — Options and testing</a></li><li><a href="https://medium.com/p/10a9a79e33ff">Part 2 — Spring web client and test kit</a></li><li><a href="https://medium.com/p/135d5b0ed4e6">Part 3 — ClientHttpRequestFactory as an abstraction over the HTTP layer </a>(you’re here)</li><li><a href="https://medium.com/p/d7df011d52ec">Part 4 — Configuring Apache Client via HttpComponentsClientHttpRequestFactory</a></li><li><a href="https://medium.com/p/65d709bb11f9">Part 5 — Setting a hard limit on a request and failing fast</a></li><li><a href="https://medium.com/p/657c25751d99">Part 6 — Adding observability for web client</a><br>All source code is available in the <a href="https://github.com/tkachenkoas/spring-web-client-in-depth">GitHub repository</a>.</li></ul><h3>What’s under the facade of RestTemplate</h3><p>As I mentioned earlier, RestTemplate does not have its own implementation of the HTTP client. Instead, it uses the ClientHttpRequestFactory abstraction to delegate the actual HTTP calls to some &quot;native&quot; client.</p><p>For instance, the behaviour of MockRestServiceServer is based on the MockClientHttpRequestFactory that is set to the rest template instead of the &quot;real&quot; ClientHttpRequestFactory that will make the actual HTTP calls.</p><pre>@Test<br>void aGlimpseIntoMockRestServiceServer() {<br>    RestTemplate restTemplate = new RestTemplate();<br>    // This logic is executed as part of @RestClientTest bootstrapping.<br>    MockRestServiceServer server = MockRestServiceServer.createServer(restTemplate);<br>    Assertions.assertThat(restTemplate.getRequestFactory().getClass().getSimpleName())<br>            /**<br>             * @{@link org.springframework.test.web.client.MockRestServiceServer#MockClientHttpRequestFactory}<br>             * // no actual HTTP request is made, so we do whatever we want<br>             * with expected &quot;requests&quot; and &quot;responses&quot;<br>             */<br>            .isEqualTo(&quot;MockClientHttpRequestFactory&quot;);<br>}</pre><p>Here is a visual comparison of what making a call to MockServer (or a remote server) looks in comparison with MockRestServiceServer:</p><ul><li><a href="https://i.imgflip.com/8gm1t9.jpg">https://i.imgflip.com/8gm1t9.jpg</a></li><li><a href="https://i.imgflip.com/8gm2io.jpg">https://i.imgflip.com/8gm2io.jpg</a></li></ul><p>In many examples on the internet, you will see the new RestTemplate() being used, and I do not recommend doing it (unless you are writing a small script or a prototype). One of the reasons is that the default request factory of RestTemplate is SimpleClientHttpRequestFactory that is a bit limited</p><pre>@Test<br>void theDefaultRequestFactoryOfRestTemplateBehavior() {<br>    RestTemplate restTemplate = new RestTemplate();<br><br>    Assertions.assertThat(restTemplate.getRequestFactory())<br>            .isInstanceOf(org.springframework.http.client.SimpleClientHttpRequestFactory.class);<br>    // try and guess what will happen here and why the test will fail<br>    restTemplate.patchForObject(&quot;https://api.example.com/v1/some-endpoint&quot;, null, String.class);<br>}</pre><h3>The beauty of RestTemplate and ClientHttpRequestFactory abstractions</h3><p>Since RestTemplate and ClientHttpRequestFactory are &#39;natural&#39; Spring-web abstractions, you can even integrate them in testing your server-side code. I&#39;m pretty sure that you are familiar with the @WebMvcTest and MockMvc that allows you to test your controllers without starting the whole application context and underlying servers. But you might have noticed that MockMvc is often quite verbose when you need to test a simple HTTP call:</p><pre>mockMvc.perform(<br>                MockMvcRequestBuilders.get(&quot;/some-endpoint&quot;)<br>                        .accept(MediaType.APPLICATION_JSON)<br>        )<br>                .andExpect(MockMvcResultMatchers.status().isOk())<br>                .andExpect(MockMvcResultMatchers.content().json(&quot;{\&quot;name\&quot;:\&quot;John\&quot;,\&quot;age\&quot;:25}&quot;));</pre><p>I bet that when writing any test from scratch, you always search for the class that provides all the get(...), status() static methods. Also, since we delegate assertions to MockMvcResultMatchers, we lose the convenience of debugging that we have when we use assertThat from org.assertj.core.api.Assertions on the response.</p><p>Another example of MockMvc verbosity when you need to make a call with a request body and then read the response body into a POJO:</p><pre>record ResApiResponse(String name) {<br>}<br><br>RestApiResponse response = new com.fasterxml.jackson.databind.ObjectMapper()<br>              .readValue(<br>                      mockMvc.perform(<br>                              MockMvcRequestBuilders.post(&quot;/some-endpoint&quot;)<br>                                      .contentType(MediaType.APPLICATION_JSON)<br>                                      .content(&quot;{\&quot;name\&quot;:\&quot;John\&quot;}&quot;)<br>                      )<br>                              .andExpect(MockMvcResultMatchers.status().isOk())<br>                              .andReturn()<br>                              .getResponse()<br>                              .getContentAsString(),<br>                      ResApiResponse.class<br>              );</pre><p>And now multiply this by the number of endpoints/cases that you usually need to test. And the sad fact is that many developers also do not know about TestRestTemplate / WebTestClient (but it&#39;s a different story).</p><p>But the good news is that you can use RestTemplate / RestClient as a client to make calls to your MockMvc-covered server-side code in the same way as you make calls to external services.</p><p>The trick is to define a RestTemplate bean in your test configuration that uses MockMvcClientHttpRequestFactory and the MockMvc instance that you get from the context</p><pre>@Bean<br>RestOperations mockMvcRestOperations(MockMvc mockMvc) {<br>    RestTemplate restTemplate = new RestTemplate();<br>    restTemplate.setRequestFactory(new MockMvcClientHttpRequestFactory(mockMvc));<br>    return restTemplate;<br>}</pre><p>The benefit of this approach will show itself when you need to debug an environment-sensitive production issue covered by a long cross-endpoint behavioural unit test that is green. You can adjust the configuration of the RestTemplate bean to use the &quot;real&quot; HTTP client, do some tricks with API authentication - and voilà, a cross-environment test case is ready in a few minutes.</p><pre>/**<br> * Illustrates how to use {@link MockMvcClientHttpRequestFactory} to test the client-side<br> * with RestTemplate, and not MockMvc.<br> */<br>@WebMvcTest<br>public class Part03_02_ClientHttpRequestFactory {<br>    // this is the &quot;inlined&quot; application we&#39;re going to test<br>    @SpringBootApplication<br>    static class TheSpringBootApplication {<br>        record ResponseModel(String name, int age) {<br>        }<br>        // this is our server-side controller<br>        @RestController<br>        static class TheRestController {<br>            @GetMapping(&quot;/api/v1/some-endpoint/{name}/{age}&quot;)<br>            public ResponseModel getResponse(@PathVariable Integer age, @PathVariable String name) {<br>                return new ResponseModel(name, age);<br>            }<br>        }<br>        @Qualifier(&quot;mockMvcRestOperations&quot;)<br>        @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})<br>        @Retention(RetentionPolicy.RUNTIME)<br>        @interface MockMvcRestTemplate {<br>        }<br>        @MockMvcRestTemplate<br>        @Bean<br>        RestOperations mockMvcRestOperations(MockMvc mockMvc) {<br>            RestTemplate restTemplate = new RestTemplate();<br>            restTemplate.setRequestFactory(new MockMvcClientHttpRequestFactory(mockMvc));<br>            return restTemplate;<br>        }<br>        // by default, RestTemplate throws exceptions when 4xx or 5xx status codes are returned<br>        // but when we know that we expect such status codes, we can tweak the RestTemplate<br>        // to not throw exceptions, so that to assert the response status ourselves<br>        @Qualifier(&quot;nonThrowingRestOperations&quot;)<br>        @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})<br>        @Retention(RetentionPolicy.RUNTIME)<br>        @interface NonThrowingMockMvcRestTemplate {<br>        }<br>        @NonThrowingMockMvcRestTemplate<br>        @Bean<br>        RestOperations nonThrowingRestOperations(MockMvc mockMvc) {<br>            RestTemplate restTemplate = new RestTemplate();<br>            restTemplate.setRequestFactory(new MockMvcClientHttpRequestFactory(mockMvc));<br>            restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {<br>                @Override<br>                protected void handleError(@NotNull ClientHttpResponse response, @NotNull HttpStatusCode statusCode) {<br>                    // we won&#39;t throw exceptions here, and instead we will let the flow handle the response status<br>                }<br>            });<br>            return restTemplate;<br>        }<br>    }<br>    @MockMvcRestTemplate<br>    @Autowired<br>    RestOperations restOperations;<br>    @NonThrowingMockMvcRestTemplate<br>    @Autowired<br>    RestOperations nonThrowingRestOperations;<br>    @Test<br>    void callOurGetEndpoint_shouldReturnOurResponse() {<br>        record ClientSideResponse(String name, int age) {<br>        }<br>        var response = restOperations.getForObject(<br>                &quot;/api/v1/some-endpoint/{name}/{age}&quot;,<br>                ClientSideResponse.class,<br>                &quot;John&quot;, 25<br>        );<br>        assertThat(response.name()).isEqualTo(&quot;John&quot;);<br>        assertThat(response.age()).isEqualTo(25);<br>        assertThatExceptionOfType(HttpClientErrorException.NotFound.class)<br>                .isThrownBy(() -&gt; restOperations.getForObject(&quot;/api/v1/non-existing-endpoint&quot;, ClientSideResponse.class));<br>        // now we can make a request to a non-existing endpoint without causing an exception<br>        var fourOhFour = nonThrowingRestOperations.getForEntity(<br>                &quot;/api/v1/non-existing-endpoint&quot;, String.class<br>        );<br>        assertThat(fourOhFour.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);<br>        assertThat(fourOhFour.getBody()).isNull();<br>    }<br>}</pre><h3>The short conclusion of Part 3</h3><p>In this article, we’ve seen how RestTemplate uses ClientHttpRequestFactory to delegate the actual calls to a real HTTP client or some mock server from test-kit. Also, now you know how you can use RestTemplate or RestClient test your controllers if you like its API more than MockMvc.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=135d5b0ed4e6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/duda/clienthttprequestfactory-as%D1%84%D1%82-abstraction-over-the-http-layer-135d5b0ed4e6">ClientHttpRequestFactory asфт  abstraction over the HTTP layer</a> was originally published in <a href="https://medium.com/duda">Duda</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spring’s HTTP client and test-kit]]></title>
            <link>https://medium.com/duda/springs-http-client-and-test-kit-10a9a79e33ff?source=rss----a8c59e8a5adc---4</link>
            <guid isPermaLink="false">https://medium.com/p/10a9a79e33ff</guid>
            <category><![CDATA[http-client]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Anton Tkachenko]]></dc:creator>
            <pubDate>Tue, 11 Jun 2024 19:27:44 GMT</pubDate>
            <atom:updated>2024-06-11T19:27:44.389Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ufCOaskBma-adoLJ" /></figure><h3>Introduction</h3><p>This article is part of a series of articles about a deeper dive into making HTTP calls in Spring.</p><ul><li><a href="https://medium.com/p/c3a2763e9e4c">Part 1 — Options and testing</a></li><li><a href="https://medium.com/p/10a9a79e33ff">Part 2 — Spring web client and test kit</a> (you’re here)</li><li><a href="https://medium.com/p/135d5b0ed4e6">Part 3 — ClientHttpRequestFactory as an abstraction over the HTTP layer</a></li><li><a href="https://medium.com/p/d7df011d52ec">Part 4 — Configuring Apache Client via HttpComponentsClientHttpRequestFactory</a></li><li><a href="https://medium.com/p/65d709bb11f9">Part 5 — Setting a hard limit on a request and failing fast</a></li><li><a href="https://medium.com/p/657c25751d99">Part 6 — Adding observability for web client</a> <br>All source code is available in the <a href="https://github.com/tkachenkoas/spring-web-client-in-depth">GitHub repository</a>.</li></ul><h3>The Template and Operations pattern in Spring</h3><p>If you are using Spring ecosystem for web, DB, and other integrations, you might have noticed that it has a lot of classes named SomethingTemplate or SomethingOperations. For example, JdbcTemplate, MongoTemplate, RestTemplate, TransactionTemplate (and they implement JdbcOperations, ... interfaces). This is a pattern that Spring uses to provide a convenient way to work with some external system (like a database, a web service, etc.). Generally, the Template classes follow a similar strategy:</p><ul><li>they are thread-safe, so you can use the instance across your application</li><li>they act as facades — e.g., provide a nice and convenient API to work with the external system, but hide the complexity</li><li>they also support rich customization and extendability via interceptors, callbacks, customizers, event listeners, etc. Thus, your business logic will depend only on relatively simple Operations interface, but full-context implementation can be done in configuration classes</li><li>another important benefit of using the Template classes is that they often throw Spring&#39;s runtime exceptions no matter what the underlying implementation is.</li></ul><p>Usually, the Template classes are built on top of some &quot;native&quot; client (like java.net.HttpURLConnection, mongo-java-driver, jedis etc.), instead of providing a new implementation from scratch. This means that you can always use the &quot;native&quot; client if you need to - but you will lose the convenience and the consistency that the Template provides. In my opinion, whenever you need to work with some external system, you should first check if Spring has a Template for it, and only then consider using a &quot;native&quot; client.</p><pre>/**<br> * YYYTemplate and YYYOperations are common names for classes that encapsulate the operations<br> * with a specific service. For example:<br> */<br>RestOperations restOperationsIsAnInterfaceForMakingHttpRequests;<br><br><br>/**<br> * In data-jpa world, we have JdbcOperations and NamedParameterJdbcOperations and their implementations<br> */<br>JdbcOperations jdbcOperations;<br><br>NamedParameterJdbcOperations namedParameterJdbcOperations;<br><br>JdbcTemplate jdbcTemplate;<br><br>NamedParameterJdbcTemplate namedParameterJdbcTemplate;<br><br>/**<br> * Alternative to {@link org.springframework.transaction.annotation.Transactional}<br> */<br>TransactionOperations transactionOperations;<br><br>TransactionTemplate transactionTemplate;<br><br>/**<br> * Mongo and redis have their own templates.<br> */<br><br>MongoOperations mongoOperations;<br><br>MongoTemplate mongoTemplate;<br><br>/**<br> * &lt;K&gt; – the Redis key type against which the template works (usually a String)<br> * &lt;V&gt; – the Redis value type against which the template works<br> */<br>RedisOperations&lt;String, String&gt; redisOperations;<br><br>RedisTemplate&lt;String, String&gt; redisTemplate;</pre><h3>But RestTemplate is deprecated! Don’t you know how to google?</h3><p>Well, as of the date of writing this article, RestTemplate is not deprecated. It&#39;s true that Spring 6.1 / Spring Boot 3.2 has introduced a new interface RestClient for synchronous operations, and it&#39;s true that Spring suggests using it instead of RestTemplate.</p><p>This is written in the documentation of RestTemplate:</p><pre>NOTE: As of 6.1, RestClient offers a more modern API for synchronous HTTP access.<br>     For asynchronous and streaming scenarios, consider the reactive<br>     org.springframework.web.reactive.function.client.WebClient.</pre><p>So essentially, the RestClient is just another interface that provides the same functionality as RestTemplate. But you can still use RestTemplate and will be just fine.</p><p>If you check the package org.springframework.web.client you will also see that it has RestOperationsExtensionsKt class that provides kotlin extension functions to make RestTemplate even more convenient to use.</p><h3>Sample usage of RestTemplate</h3><p>Here is an example of just making a simple GET request with RestTemplate:</p><pre>@Test<br>void sampleUsageOfRestTemplate() {<br>    var restTemplate = new RestTemplate();<br>    String url = &quot;http://localhost:1090/some-endpoint&quot;;<br>    SampleResponseModel getResponse = restTemplate.getForObject(url, SampleResponseModel.class);<br>    assertThat(getResponse).isEqualTo(new SampleResponseModel(&quot;John&quot;, 25));<br>}</pre><p>As you can see, RestTemplate is very convenient to use. It also has built-in support for parsing the response body into an object. In general, you SHOULD NOT just use new RestTemplate() in production (and to make additional configs), and it will be covered later in part 5 of this series.</p><p>Here is a more complex example of setting headers/cookies. In this case, we have to use RequestEntity to set the headers, and then make a call with RestTemplate.exchange that returns ResponseEntity with the response body and headers</p><pre>@Test<br>void sampleUsageOfSettingHeaders() throws Exception {<br>    // example of setting cookies / headers<br>    String url = &quot;http://localhost:1090/some-endpoint-with-cookies&quot;;<br><br>    mockServer.when(<br>            org.mockserver.model.HttpRequest.request()<br>                    .withMethod(&quot;GET&quot;)<br>                    .withHeader(&quot;Cookie&quot;, &quot;name=value&quot;)<br>                    .withHeader(&quot;Authorization&quot;, &quot;Bearer token&quot;)<br>    ).respond(<br>            org.mockserver.model.HttpResponse.response()<br>                    .withStatusCode(200)<br>                    .withBody(&quot;{\&quot;name\&quot;:\&quot;John\&quot;,\&quot;age\&quot;:25}&quot;)<br>                    .withHeader(&quot;Content-Type&quot;, &quot;application/json&quot;)<br>                    .withHeader(&quot;Set-Cookie&quot;, &quot;name=value&quot;)<br>    );<br><br>    HttpHeaders httpHeaders = new HttpHeaders();<br>    httpHeaders.add(&quot;Cookie&quot;, &quot;name=value&quot;);<br>    httpHeaders.add(&quot;Authorization&quot;, &quot;Bearer token&quot;);<br>    RequestEntity&lt;?&gt; request = new RequestEntity&lt;&gt;(<br>            httpHeaders, HttpMethod.GET, new URI(url)<br>    );<br>    ResponseEntity&lt;SampleResponseModel&gt; response = new RestTemplate().exchange(request, SampleResponseModel.class);<br>    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);<br>    assertThat(response.getBody()).isNotNull();<br>    assertThat(response.getBody().name()).isEqualTo(&quot;John&quot;);<br>    assertThat(response.getBody().age()).isEqualTo(25);<br>    assertThat(response.getHeaders().get(&quot;Set-Cookie&quot;)).contains(&quot;name=value&quot;);<br>}</pre><p>But one of my favourite features of RestTemplate is the ability to use URL patterns with path variables or query parameters. As you can see, this API satisfies the criteria of one line of your action in human-readable language is represented by one line of code in the library.</p><pre>@Test<br>void sampleUsageOfMakingAPostWithPathVariables() {<br>    RestTemplate restTemplate = new RestTemplate();<br>    mockServer.when(<br>            org.mockserver.model.HttpRequest.request()<br>                    .withMethod(&quot;POST&quot;)<br>                    .withPath(&quot;/some-endpoint/John&quot;)<br>                    .withQueryStringParameter(&quot;age&quot;, &quot;25&quot;)<br>    ).respond(<br>            org.mockserver.model.HttpResponse.response()<br>                    .withStatusCode(200)<br>                    .withHeader(&quot;Content-Type&quot;, &quot;application/json&quot;)<br>                    .withBody(&quot;{\&quot;name\&quot;:\&quot;John\&quot;,\&quot;age\&quot;:25}&quot;)<br>    );<br>    String urlWithTemplate = &quot;http://localhost:1090/some-endpoint/{name}?age={age}&quot;;<br>    SampleResponseModel someResponse = restTemplate.postForObject(<br>            urkWithTemplate, new SampleResponseModel(&quot;John&quot;, 25),<br>            SampleResponseModel.class,<br>            &quot;John&quot;, 25<br>    );<br>    assertThat(someResponse).isEqualTo(new SampleResponseModel(&quot;John&quot;, 25));<br>}</pre><h3>Ok, and what about RestClient?</h3><p>Under the hood the implementation of RestClient (DefaultRestClient) uses the same abstractions as RestTemplate, and it can be built on top of existing RestTemplate. This means that after you upgrade to Spring 6.1, you can reuse benefits from using the new API, but still use your properly configured RestTemplate instance. However, everywhere in the article, I will use RestTemplate because its API is still a current default standard.</p><p>Here is an example of how to use RestClient:</p><pre>@Test<br>void sampleUsageOfRestClientInterface() {<br>    // the way to adapt your existing RestTemplate to RestClient<br>    RestClient restClient = RestClient.builder(new RestTemplate()).build();<br>    mockServer.when(<br>            org.mockserver.model.HttpRequest.request()<br>                    .withMethod(&quot;GET&quot;)<br>                    .withHeader(&quot;Accept&quot;, &quot;application/json&quot;)<br>                    .withPath(&quot;/some-endpoint/John&quot;)<br>                    .withQueryStringParameter(&quot;age&quot;, &quot;25&quot;)<br>    ).respond(<br>            org.mockserver.model.HttpResponse.response()<br>                    .withStatusCode(200)<br>                    .withHeader(&quot;Content-Type&quot;, &quot;application/json&quot;)<br>                    .withBody(&quot;{\&quot;name\&quot;:\&quot;John\&quot;,\&quot;age\&quot;:25}&quot;)<br>    );<br>    // now we can use the new interface<br>    String url = &quot;http://localhost:1090/some-endpoint/{name}?age={age}&quot;;<br>    var getResponse = restClient.get()<br>            .uri(url, &quot;John&quot;, 25)<br>            .accept(MediaType.APPLICATION_JSON)<br>            .retrieve().body(SampleResponseModel.class);<br>    assertThat(getResponse).isEqualTo(new SampleResponseModel(&quot;John&quot;, 25));<br>}</pre><p>As you can see, the RestClient&#39;s API is more fluent and convenient to use. It also provides support for creating a proxy for a &quot;typed&quot; interface that represents the API. Here is an example:</p><pre>@Test<br>void usingRestClientWithInterfacedDefinedProxy() {<br>    interface RemoteServiceAsInterface {<br>        @org.springframework.web.service.annotation.GetExchange(&quot;/some-endpoint/{name}&quot;)<br>        SampleResponseModel getSampleResponseModel(<br>                @org.springframework.web.bind.annotation.PathVariable String name,<br>                @org.springframework.web.bind.annotation.RequestParam Integer age<br>        );<br>    }<br>    mockServer.when(<br>            org.mockserver.model.HttpRequest.request()<br>                    .withMethod(&quot;GET&quot;)<br>                    .withPath(&quot;/some-endpoint/John&quot;)<br>                    .withQueryStringParameter(&quot;age&quot;, &quot;25&quot;)<br>    ).respond(<br>            org.mockserver.model.HttpResponse.response()<br>                    .withStatusCode(200)<br>                    .withHeader(&quot;Content-Type&quot;, &quot;application/json&quot;)<br>                    .withBody(&quot;{\&quot;name\&quot;:\&quot;John\&quot;,\&quot;age\&quot;:25}&quot;)<br>    );<br><br>    HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder()<br>            // supports both RestTemplate and RestClient<br>            /*.exchangeAdapter(RestTemplateAdapter.create(new RestTemplateBuilder()<br>                    .rootUri(&quot;http://localhost:1090&quot;).build()<br>            ))*/<br>            .exchangeAdapter(RestClientAdapter.create(RestClient.builder()<br>                    .baseUrl(&quot;http://localhost:1090&quot;)<br>                    .build()))<br>            .build();<br>    RemoteServiceAsInterface remoteServiceProxy = factory.createClient(RemoteServiceAsInterface.class);<br>    SampleResponseModel responseModel = remoteServiceProxy.getSampleResponseModel(&quot;John&quot;, 25);<br>    assertThat(responseModel).isEqualTo(new SampleResponseModel(&quot;John&quot;, 25));<br>}</pre><p>But even though this “typed” proxy is very convenient to use, please don’t write tests that use some mock implementation or Mockito.when().thenReturn() to test the interaction with the proxy.</p><h3>A better alternative to a type-safe proxy for the API</h3><p>The interface-based proxy that is available in Retorfit, Jersey and RestClient is a very convenient way to work with the API. However, it&#39;s not always the best way to do it. Lots of APIs are documented with OpenAPI/Swagger, and some tools can generate the client code for you.</p><p>Some time ago I wrote an article about how to use openapi-generator to generate the client code for testing your server-side code. You can find it here: <a href="https://medium.com/duda/cleaner-spring-boot-it-rest-tests-with-client-generation-cc3ac880d9ec">https://medium.com/duda/cleaner-spring-boot-it-rest-tests-with-client-generation-cc3ac880d9ec</a></p><p>The main benefit of using the generated client code is zero manually written code. The components will be generated from the API definition that the server-side team provides for you. This means that you can replace tests with mock-server with tests on Mockito mocks, or even subclassed reimplementations to support integration cases. In this case, it is a “legal” action because it’s not your code, and it is as reliable as its OpenAPI definition is.</p><p>Suppose you are the one who is responsible for the API. In that case, you should also consider investing time into properly documenting the API so that its consumers can generate the client code in any language they want. Also, a good practice for maintaining the generated schema is this:</p><ul><li>store the schema in the version control system</li><li>update the schema as part of the build process (and fail the build if the schema is not up to date)</li><li>verify that changes in code are correctly reflected in the schema and that you don’t introduce unexpected breaking changes</li></ul><h3>Spring’s test kit for RestTemplate / RestClient</h3><p>A significant advantage of using RestTemplate or RestClient is that Spring provides built-in support for testing the code that uses these classes. This is done via MockRestServiceServer, which is a part of spring-test module, and also via @RestClientTest annotation that is a part of spring-boot-test and allows you to start only a slice of all auto-configurations available in your project, but to configure the context to work with the REST clients.</p><p>Now let’s take a look at the tiny application and the test that focuses on the RestTemplate</p><p>First, I need to remind you that for demonstration purposes, I’ve got a pretty “fat” build.gradle file:</p><pre>implementation &#39;org.springframework.boot:spring-boot-starter-data-jpa&#39;<br>implementation &#39;org.springframework.boot:spring-boot-starter-data-redis&#39;<br>implementation &#39;org.springframework.boot:spring-boot-starter-data-mongodb&#39;</pre><p>If I were to write a @SpringBootTest test, it would load all the auto-configurations for all the databases, and since I have not configured any mandatory properties for them (like database driver, URL, ...), the context would fail.</p><p>However, if I use @RestClientTest, only the autoconfiguration for the REST clients will be loaded, and as a free additional benefit, MockRestServiceServer will be autoconfigured for the rest-template. Be careful, though, because Spring will try to initialize all your beans that are in the scope of @SpringBootApplication class, thus typically you would want to use @ContextConfiguration to limit the scope of the context to the class under test.</p><pre>@RestClientTest<br>public class Part02_02_SpringWebClientTestKit {<br>    @SpringBootApplication<br>    static class SpringBootApplicationForTheCurrentTest {<br>        record OurDomainModel(<br>                String name,<br>                int age<br>        ) {<br>        }<br>        @Component<br>        static class OurService {<br>            private final RestTemplate restTemplate;<br>            @Autowired<br>            public OurService(RestTemplateBuilder builder) {<br>                this.restTemplate = builder.build();<br>            }<br>            /**<br>             * You might be wondering why we make a call to the &quot;real API&quot; endpoint and how exactly it<br>             * is possible to imitate the real API endpoint in the test.<br>             */<br>            public OurDomainModel getOurDomainModel() {<br>                return restTemplate.getForObject(<br>                        &quot;https://api.example.com/v1/some-endpoint&quot;,<br>                        OurDomainModel.class<br>                );<br>            }<br>            public OurDomainModel postOurDomainModel(<br>                    OurDomainModel request<br>            ) {<br>                return restTemplate.postForObject(<br>                        &quot;https://api.example.com/v1/some-endpoint&quot;,<br>                        request,<br>                        OurDomainModel.class<br>                );<br>            }<br>        }<br>    }<br>    @Autowired<br>    private ApplicationContext applicationContext;<br>    /**<br>     * This is our server-side component that we are going to test.<br>     */<br>    @Autowired<br>    private SpringBootApplicationForTheCurrentTest.OurService underTest;<br>    /**<br>     * This is the mock server that comes out of the box with Spring Boot.<br>     */<br>    @Autowired<br>    private MockRestServiceServer mockServer;<br>}</pre><p>Now let’s take a look at the test that verifies the behavior of the OurService class:</p><pre>@Test<br>void getDomainModel_happyPath() {<br>    mockServer.expect(<br>                    MockRestRequestMatchers.requestTo(&quot;https://api.example.com/v1/some-endpoint&quot;)<br>            )<br>            .andExpect(<br>                    MockRestRequestMatchers.header(&quot;Accept&quot;,<br>                            org.hamcrest.CoreMatchers.containsString(MediaType.APPLICATION_JSON_VALUE)<br>                    )<br>            )<br>            .andRespond(withSuccess(<br>                    &quot;{\&quot;name\&quot;:\&quot;John\&quot;,\&quot;age\&quot;:25}&quot;,<br>                    MediaType.APPLICATION_JSON<br>            ));<br>    var response = underTest.getOurDomainModel();<br>    assertThat(response).isNotNull();<br>    assertThat(response.name()).isEqualTo(&quot;John&quot;);<br>    assertThat(response.age()).isEqualTo(25);<br>    server.verify();<br>}</pre><p>One of the benefits of using MockRestServiceServer is that it allows you to verify that the request was made to a &quot;real&quot; endpoint, while all the previous examples with MockServer were limited to making calls to localhost. Imagine a situation when you need to write an app-level test for the following use case: <br>— user specifies a URL in the UI from which the app should fetch some data <br>— the service layer of the application verifies that the URL is valid, that it&#39;s public (and not a private/localhost IP address) <br> — and then some data is fetched from the URL, transformed/stored/validated and returned to the UI</p><p>When using MockServer for such a test, you would have to make some tricks to make it work, like using a remote mock server or intercepting the request and forwarding it to localhost. But with MockRestServiceServer, you can specify the real URL and it will work. However, keep in mind that this is not a 100% &quot;end-to-end&quot; test, since MockRestServiceServer does not make a real HTTP call (and we&#39;ll get to it later).</p><p>Here is another example of making a call with a request body. I also suggest that you pay attention to the matcher API that is used by MockRestServiceServer - it&#39;s powerful and flexible, and in my opinion, gives you more freedom in comparison with MockServer&#39;s api.</p><pre>@Test<br>void postDomainModel_happyPath() {<br>    mockServer.expect(<br>                    MockRestRequestMatchers.requestTo(&quot;https://api.example.com/v1/some-endpoint&quot;)<br>            )<br>            .andExpect(<br>                    MockRestRequestMatchers.header(&quot;Content-Type&quot;,<br>                            org.hamcrest.CoreMatchers.containsString(MediaType.APPLICATION_JSON_VALUE)<br>                    )<br>            )<br>            .andRespond(withSuccess(<br>                    &quot;{\&quot;name\&quot;:\&quot;John\&quot;,\&quot;age\&quot;:25}&quot;,<br>                    MediaType.APPLICATION_JSON<br>            ));<br>    var response = underTest.postOurDomainModel(new SpringBootApplicationForTheCurrentTest.OurDomainModel(&quot;John&quot;, 25));<br>    assertThat(response).isNotNull();<br>    assertThat(response.name()).isEqualTo(&quot;John&quot;);<br>    assertThat(response.age()).isEqualTo(25);<br>    server.verify();<br>}</pre><p>Also, the default behaviour of RestTemplate is to throw an exception if the response status code is not 2xx - and spring-web defines a whole family of exceptions for different status codes. This means that you usually don&#39;t have to check the status code manually: exceptional cases will result in an exception being thrown</p><pre>@Test<br>void getDomainModel_404ThrowsException() {<br>    mockServer.expect(<br>                    MockRestRequestMatchers.requestTo(&quot;https://api.example.com/v1/some-endpoint&quot;)<br>            )<br>            .andRespond(MockRestResponseCreators.withResourceNotFound()<br>                    .body(&quot;Not found&quot;));<br><br>    assertThatThrownBy(() -&gt; underTest.getOurDomainModel())<br>            .withFailMessage(&quot;404 Not Found: [Not found]&quot;)<br>            .isInstanceOf(org.springframework.web.client.HttpClientErrorException.NotFound.class);<br>}</pre><h3>The short conclusion of Part 2</h3><p>Spring ecosystem is very mature and provides various solutions for accessing external systems more easily. If you check the source of any Spring Project (like Spring Security), you will see that they rely on these solutions. And as a good boy scout, Spring also provides a built-in test kit that you can use instead of re-inventing the wheel or relying on 3rd party solutions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10a9a79e33ff" width="1" height="1" alt=""><hr><p><a href="https://medium.com/duda/springs-http-client-and-test-kit-10a9a79e33ff">Spring’s HTTP client and test-kit</a> was originally published in <a href="https://medium.com/duda">Duda</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making HTTP calls in Java & Spring: overview, tips and pitfalls]]></title>
            <link>https://medium.com/duda/making-http-calls-in-java-spring-overview-tips-and-pitfalls-c3a2763e9e4c?source=rss----a8c59e8a5adc---4</link>
            <guid isPermaLink="false">https://medium.com/p/c3a2763e9e4c</guid>
            <category><![CDATA[http-client]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Anton Tkachenko]]></dc:creator>
            <pubDate>Tue, 11 Jun 2024 19:27:17 GMT</pubDate>
            <atom:updated>2024-06-11T19:27:17.580Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cGk6sDrZepUAtoMW" /></figure><h3>Introduction</h3><p>The majority of modern applications make HTTP calls to other systems. In this series of articles, I want to share my thoughts and experience (positive and negative) on what should be the approach of choosing the library to make HTTP calls in your Spring Boot-based projects.</p><p>Here are the links to the articles on Medium:</p><ul><li><a href="https://medium.com/p/c3a2763e9e4c">Part 1 — Options and testing</a> (you’re here)</li><li><a href="https://medium.com/p/10a9a79e33ff">Part 2 — Spring web client and test kit</a></li><li><a href="https://medium.com/p/135d5b0ed4e6">Part 3 — ClientHttpRequestFactory as an abstraction over the HTTP layer</a></li><li><a href="https://medium.com/p/d7df011d52ec">Part 4 — Configuring Apache Client via HttpComponentsClientHttpRequestFactory</a></li><li><a href="https://medium.com/p/65d709bb11f9">Part 5 — Setting a hard limit on a request and failing fast</a></li><li><a href="https://medium.com/p/657c25751d99">Part 6 — Adding observability for web client</a><br> All source code is available in the <a href="https://github.com/tkachenkoas/spring-web-client-in-depth">GitHub repository</a>.</li></ul><p>Specifically, I am targeting the case of a big project with multiple teams and multiple services. In such a case, it is important to have a consistent approach to common tasks like making HTTP calls so that the code is straightforward to understand and maintain by all the teams.</p><p>In this article, I will cover high-level topics with occasional dive-ins and provide examples of various approaches to making HTTP calls in Java, and how to test them. If you are going to check out the source code, please don’t blame me for the fact that .gradlew test the task fails: I did not invest time into making the project&#39;s build green (mostly because I reused the same ports). But every individual test will pass if you run it.</p><p>I will not be covering the topic of reactive/async clients, as it’s a separate topic. Also, most applications still use synchronous clients (and other blocking processing). And as Java 21 is already out, it might be that virtual threads will be a game-changer for choosing sync vs. async clients.</p><h3>General agenda for the series:</h3><ul><li>Options for making HTTP calls in Java<br> — Code examples for each option<br> — Testing performed HTTP calls</li><li>Spring’s interfaces for making HTTP calls<br> — RestTemplate, RestClient, ***Template and ***Operations patterns<br> — Spring’s test kit for RestTemplate / RestClient</li><li>What’s under the facade of RestTemplate <br> — ClientHttpRequestFactory abstractions<br> — How ClientHttpRequestFactory is used in Spring&#39;s mocks/test-kits</li><li>Configuring Apache Client as an implementation of ClientHttpRequestFactory <br> — Connection pooling<br> — Timeouts</li><li>Adding observability for HTTP client<br> — Monitoring connection pool<br> — Monitoring core metrics of outgoing requests</li><li>A deeper glance into non-happy-path scenarios:<br> — handling unexpected network behavior<br> — setting hard timeouts, failing fast</li></ul><h3>Part 1 — Options and testing</h3><p>Java has several libraries to make HTTP calls. Here are some of them:</p><ul><li>java.net.HttpURLConnection - the standard JDK means for making HTTP calls. it&#39;s unlikely that you will use it in a modern application, but some legacy applications might still use it.</li><li>Java 11 HttpClient — a relatively new addition to the JDK. It&#39;s a modern and flexible solution</li><li>Apache HttpClient (org.apache.httpcomponents.client5:httpclient5) - a popular library that has been around for a long time</li><li>OkHttp (com.squareup.okhttp3:okhttp) - a modern and efficient library</li><li>Jersey (org.glassfish.jersey.core:jersey-client) - a JAX-RS implementation of HTTP client</li><li>Retrofit (com.squareup.retrofit2:retrofit) - a type-safe HTTP client for Android and Java</li><li>… and some others that I will not cover here</li></ul><p>Further, I’ll show quick code examples for each of the libraries.</p><p>Also, spring web module provides a RestTemplate class that can be used to make HTTP calls with convenient &quot;template&quot; methods. Most of the article will be dedicated to Spring&#39;s built-in API for making HTTP calls.</p><h3>Testing making HTTP calls</h3><p>No matter which library you choose, it’s important to test that it works as expected (and also non-happy-path scenarios).</p><p>First, a crucial rule on how NOT to test HTTP calls:</p><pre>DO NOT USE MOCKITO TO MOCK INVOCATIONS OF HTTP CLIENTS</pre><p>Few reasons for this:</p><ul><li>It’s generally not a good idea to mock something you don’t own. Libraries/components that are designed to be used by other developers should either be tested in integration with FULL real implementation and infrastructure or should provide mock implementations that are compatible with the real one in most cases.</li><li>It’s hard to maintain such tests: in many cases, you can do a legal refactoring that will break the tests because of the way how the mocks are set up to expect specific methods with specific arguments (e.g. if API has multiple ways to achieve the same result, you might need to change the test to reflect the change)</li><li>such tests are very limited in exploring the real behaviour of the system.</li><li>as a quick example, when you bypass the real deserialization logic, bugs in production may happen because you forgot to add no-arg constructor to the model class, or forgot to add annotation for naming strategy, etc.</li><li>also, exploring non-2xx responses may become a guessing game: do I need to check the status code, or does the client throw an exception (and what kind of exception)?</li><li>some behaviour is just too painful to mock via Mockito (like content-type that comes from annotations)</li></ul><p>A good way to test HTTP calls is to use a mock server. Two popular options are WireMock and MockServer, in this article, I&#39;ll use <a href="https://www.mock-server.com/">https://www.mock-server.com/</a></p><p>This is a very convenient library that also integrates with JUnit 5 to reduce the boilerplate code.</p><pre>// https://mvnrepository.com/artifact/org.mock-server/mockserver-netty<br>testImplementation &#39;org.mock-server:mockserver-junit-jupiter:5.15.0&#39;</pre><p>Here is the example of the base-test class that is used in the examples below:</p><pre>import lombok.RequiredArgsConstructor;<br>import org.junit.jupiter.api.BeforeEach;<br>import org.mockserver.client.MockServerClient;<br>import org.mockserver.junit.jupiter.MockServerSettings;<br><br>@MockServerSettings(ports = 1090)<br>@RequiredArgsConstructor<br>public class TestWithMockServer {<br>    protected final MockServerClient mockServer;<br>    protected record SampleResponseModel(String name, int age) {<br>    }<br>    <br>    protected record ErrorResponseModel(String error) {<br>    }<br>    @BeforeEach<br>    void reset() {<br>        mockServer.reset();<br>        // expect GET /some-endpoint<br>        // and respond with 200 OK + response body<br>        mockServer.when(<br>                org.mockserver.model.HttpRequest.request()<br>                        .withMethod(&quot;GET&quot;)<br>                        .withPath(&quot;/some-endpoint&quot;)<br>        ).respond(<br>                org.mockserver.model.HttpResponse.response()<br>                        .withStatusCode(200)<br>                        .withHeader(&quot;Content-Type&quot;, &quot;application/json&quot;)<br>                        .withBody(&quot;{\&quot;name\&quot;:\&quot;John\&quot;,\&quot;age\&quot;:25}&quot;)<br>        );<br>        // + configure non-ok responses<br>        mockServer.when(<br>                org.mockserver.model.HttpRequest.request()<br>                        .withMethod(&quot;GET&quot;)<br>                        .withPath(&quot;/bad-req-endpoint&quot;)<br>        ).respond(<br>                org.mockserver.model.HttpResponse.response()<br>                        .withStatusCode(400)<br>                        .withHeader(&quot;Content-Type&quot;, &quot;application/json&quot;)<br>                        .withBody(&quot;{\&quot;error\&quot;:\&quot;Bad request\&quot;}&quot;)<br>        );<br>    }<br>}</pre><p>The general explanation of the setup:</p><ul><li>@MockServerSettings(ports = 1090) - will start the mock server (via MockServerExtension that is used under the hood) on port 1090</li><li>@RequiredArgsConstructor + protected final MockServerClient mockServer - will inject the mock server client into the test class</li><li>@BeforeEach void reset() - will reset the mock server before each test</li><li>mockServer.when(...).respond(...) - will set up the expectation for the mock server for a simple call</li><li>each test might also add additional expectations when needed</li></ul><h3>Code examples for each option</h3><h4>java.net.HttpURLConnection</h4><pre>@Test<br>void goodOldDirectViaJavaConnection() throws Exception {<br>    URL url = new URL(&quot;http://localhost:1090/some-endpoint&quot;);<br>    java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();<br>    connection.setRequestMethod(&quot;GET&quot;);<br>    connection.setRequestProperty(&quot;Accept&quot;, &quot;application/json&quot;);<br>    connection.connect();<br>    int responseCode = connection.getResponseCode();<br>    assertThat(responseCode).isEqualTo(200);<br>    String responseBody = new String(connection.getInputStream().readAllBytes());<br>    connection.disconnect();<br>    var asObject = new com.fasterxml.jackson.databind.ObjectMapper()<br>            .readValue(responseBody, SampleResponseModel.class);<br>    assertThat(asObject).isEqualTo(new SampleResponseModel(&quot;John&quot;, 25));<br>}</pre><p>Note that we have to manually parse the response body into an object by using any JSON library (in this case, Jackson).</p><h4>Java 11 HttpClient</h4><pre>@Test<br>void viaJava11HttpClient() throws Exception {<br>    var client = java.net.http.HttpClient.newHttpClient();<br>    var request = java.net.http.HttpRequest.newBuilder()<br>            .uri(java.net.URI.create(&quot;http://localhost:1090/some-endpoint&quot;))<br>            .header(&quot;Accept&quot;, &quot;application/json&quot;)<br>            .build();<br>    HttpResponse&lt;String&gt; response = client.send(request, java.net.http.HttpResponse.BodyHandlers.ofString());<br>    assertThat(response.statusCode()).isEqualTo(200);<br>    var asObject = new com.fasterxml.jackson.databind.ObjectMapper()<br>            .readValue(response.body(), SampleResponseModel.class);<br>    assertThat(asObject).isEqualTo(new SampleResponseModel(&quot;John&quot;, 25));<br>    // now making the call to the bad-req endpoint<br>    HttpRequest badRequest = java.net.http.HttpRequest.newBuilder()<br>            .uri(java.net.URI.create(&quot;http://localhost:1090/bad-req-endpoint&quot;))<br>            .header(&quot;Accept&quot;, &quot;application/json&quot;)<br>            .build();<br>    HttpResponse&lt;String&gt; badResponse = client.send(badRequest, java.net.http.HttpResponse.BodyHandlers.ofString());<br>    assertThat(badResponse.statusCode()).isEqualTo(400);<br>    assertThat(badResponse.body()).isEqualTo(&quot;{\&quot;error\&quot;:\&quot;Bad request\&quot;}&quot;);<br>}</pre><p>In comparison with java.net.HttpURLConnection, the java.net.http.HttpClient requires less boilerplate code. However, you still need to manually parse the response body into an object and handle non-2xx responses.</p><h4>Apache HttpClient</h4><pre>@Test<br>void withApacheHttpClient() throws Exception {<br>    try (CloseableHttpClient client = HttpClientBuilder.create().build()) {<br>        org.apache.hc.client5.http.classic.methods.HttpGet request = new org.apache.hc.client5.http.classic.methods.HttpGet(&quot;http://localhost:1090/some-endpoint&quot;);<br>        request.addHeader(&quot;Accept&quot;, &quot;application/json&quot;);<br>        CloseableHttpResponse response = client.execute(request);<br>        assertThat(response.getCode()).isEqualTo(200);<br>        var asObject = new com.fasterxml.jackson.databind.ObjectMapper()<br>                .readValue(response.getEntity().getContent(), SampleResponseModel.class);<br>        assertThat(asObject).isEqualTo(new SampleResponseModel(&quot;John&quot;, 25));<br>        // now making the call to the bad-req endpoint<br>        org.apache.hc.client5.http.classic.methods.HttpGet badRequest = new org.apache.hc.client5.http.classic.methods.HttpGet(&quot;http://localhost:1090/bad-req-endpoint&quot;);<br>        CloseableHttpResponse badResponse = client.execute(badRequest);<br>        assertThat(badResponse.getCode()).isEqualTo(400);<br>        assertThat(new com.fasterxml.jackson.databind.ObjectMapper()<br>                .readTree(badResponse.getEntity().getContent()).get(&quot;error&quot;).asText()).isEqualTo(&quot;Bad response for bad request&quot;);<br>    }<br>}</pre><p>Here I will not dive into the Apache HttpClient, as it’s a big library with a lot of features, but you can see that it adds more abstractions over the HTTP protocol (like CloseableHttpClient, HttpGet, CloseableHttpResponse).</p><h3>OkHttp</h3><pre>@Test<br>void withOkHttpClient() throws Exception {<br>    OkHttpClient client = new okhttp3.OkHttpClient();<br><br>    okhttp3.Request request = new okhttp3.Request.Builder()<br>            .url(https://medium.com/&quot;http://localhost:1090/some-endpoint&quot;)<br>            .addHeader(&quot;Accept&quot;, &quot;application/json&quot;)<br>            .build();<br><br>    okhttp3.Response response = client.newCall(request).execute();<br>    assertThat(response.code()).isEqualTo(200);<br><br>    var asObject = new com.fasterxml.jackson.databind.ObjectMapper()<br>            .readValue(response.body().string(), SampleResponseModel.class);<br>    assertThat(asObject).isEqualTo(new SampleResponseModel(&quot;John&quot;, 25));<br><br>    // now making the call to the bad-req endpoint<br>    okhttp3.Request badRequest = new okhttp3.Request.Builder()<br>            .url(https://medium.com/&quot;http://localhost:1090/bad-req-endpoint&quot;)<br>            .build();<br>    okhttp3.Response badResponse = client.newCall(badRequest).execute();<br>    assertThat(badResponse.code()).isEqualTo(400);<br>    assertThat(new com.fasterxml.jackson.databind.ObjectMapper()<br>            .readTree(badResponse.body().string()).get(&quot;error&quot;).asText()).isEqualTo(&quot;Bad response for bad request&quot;);<br>}</pre><p>As you can see, OkHttp is very similar to Java 11 HttpClient and Apache HttpClient in terms of the amount of boilerplate code.</p><h4>Jersey</h4><pre>@Test<br>void withJerseyClient() {<br>    try (var client = jakarta.ws.rs.client.ClientBuilder.newClient()) {<br>        SampleResponseModel response = client.target(&quot;http://localhost:1090/some-endpoint&quot;)<br>                .request()<br>                .get(SampleResponseModel.class);<br><br>        assertThat(response).isEqualTo(new SampleResponseModel(&quot;John&quot;, 25));<br><br>        // making the call to the bad-req endpoint will throw  jakarta.ws.rs.BadRequestException<br>        Assertions.assertThatExceptionOfType(<br>                        jakarta.ws.rs.BadRequestException.class<br>                ).isThrownBy(<br>                        () -&gt; client.target(&quot;http://localhost:1090/bad-req-endpoint&quot;)<br>                                .request()<br>                                .get(ErrorResponseModel.class)<br>                ).withMessage(&quot;HTTP 400 Bad Request&quot;)<br>                .extracting(e -&gt; e.getResponse().readEntity(ErrorResponseModel.class))<br>                .isEqualTo(new ErrorResponseModel(&quot;Bad response for bad request&quot;));<br>    }<br>}</pre><p>Jersey is a JAX-RS implementation, so it’s very convenient to use if you are already using JAX-RS for the web layer (and not using Spring-Web). Also, unlike the previous examples, it has built-in support for parsing the response body into an object, which is very convenient. Also unlike most of the previous examples, it throws an exception for non-2xx responses, and this provides some consistency when writing application code. E.g., you write your code to expect a successful outcome, and if something does not go as expected, you will get an exception that you can handle (and not a silent swallowed failure).</p><p>Also, it’s worth mentioning that Jersey has proxy-extension implementation &#39;org.glassfish.jersey.ext:jersey-proxy-client&#39; for creating a proxy for an interface that represents the API. Here is an example of how to use it:</p><pre>@Test<br>void withJerseyClient_viaApiInterfaceProxy() {<br><br>    @Produces(&quot;application/json&quot;)<br>    @Consumes(&quot;application/json&quot;)<br>    interface RemoteApi {<br>        @GET<br>        @Path(&quot;/some-endpoint&quot;)<br>        SampleResponseModel getOurDomainModel();<br><br>        @GET<br>        @Path(&quot;/bad-req-endpoint&quot;)<br>        SampleResponseModel getBadResponse();<br>    }<br><br>    var target = jakarta.ws.rs.client.ClientBuilder.newClient().target(&quot;http://localhost:1090&quot;);<br><br>    var ourServiceProxy = WebResourceFactory.newResource(RemoteApi.class, target);<br><br>    SampleResponseModel response = ourServiceProxy.getOurDomainModel();<br><br>    assertThat(response).isEqualTo(new SampleResponseModel(&quot;John&quot;, 25));<br><br>    // making the call to the bad-req endpoint will throw  jakarta.ws.rs.BadRequestException<br>    Assertions.assertThatExceptionOfType(<br>                    jakarta.ws.rs.BadRequestException.class<br>            ).isThrownBy(<br>                    () -&gt; ourServiceProxy.getBadResponse()<br>            ).withMessage(&quot;HTTP 400 Bad Request&quot;)<br>            .extracting(e -&gt; e.getResponse().readEntity(ErrorResponseModel.class))<br>            .isEqualTo(new ErrorResponseModel(&quot;Bad response for bad request&quot;));<br>}</pre><h4>Retrofit</h4><pre>@Test<br>void withRetrofit() throws Exception {<br>    interface RemoteRetrofitService {<br>        @retrofit2.http.GET(&quot;/some-endpoint&quot;)<br>        retrofit2.Call&lt;SampleResponseModel&gt; getOurDomainModel();<br><br>        @retrofit2.http.GET(&quot;/bad-req-endpoint&quot;)<br>        retrofit2.Call&lt;SampleResponseModel&gt; getBadResponse();<br>    }<br><br>    retrofit2.Retrofit retrofit = new retrofit2.Retrofit.Builder()<br>            .baseUrl(&quot;http://localhost:1090&quot;)<br>            .addConverterFactory(GsonConverterFactory.create())<br>            .build();<br>    RemoteRetrofitService service = retrofit.create(RemoteRetrofitService.class);<br>    retrofit2.Call&lt;SampleResponseModel&gt; call = service.getOurDomainModel();<br>    retrofit2.Response&lt;SampleResponseModel&gt; response = call.execute();<br>    assertThat(response.code()).isEqualTo(200);<br>    assertThat(response.body()).isEqualTo(new SampleResponseModel(&quot;John&quot;, 25));<br><br>    // non-ok responses still need to be handled<br>    retrofit2.Call&lt;SampleResponseModel&gt; badCall = service.getBadResponse();<br>    retrofit2.Response&lt;SampleResponseModel&gt; badResponse = badCall.execute();<br>    assertThat(badResponse.code()).isEqualTo(400);<br>}</pre><p>Like Jersey, Retrofit has a type-safe HTTP client, which creates a type-safe proxy for the API you define. As you can see, it’s very convenient to use, but it also requires some additional setup (like defining the interface and the Retrofit object which acts as a factory for creating the actual HTTP client).</p><h3>The short conclusion of Part 1</h3><p>No matter which library you choose (except for direct usage of java.net.HttpURLConnection :) ), you can achieve the expected behaviour of your system. Some libraries have more complicated features and APIs, and some are more lightweight, but for the simple cases, they all work well. Today the internet is full of examples and tutorials for each of the libraries, so you will find something that matches your case. Also, many developers, use AI tools (or at least ChatGPT)— it&#39;s not a big deal to achieve your goal with any of the libraries.</p><p>However, there are some things to consider when choosing the solution:</p><ul><li>whether one line of your action in human-readable language is represented by one line of code in the library</li><li>whether the library is actively maintained (all the mentioned libraries are, but it’s not always the case)</li><li>whether it supports serialization/deserialization of the request/response bodies as part of the API</li><li>whether it has additional test-kit features to make testing easier</li><li>whether it supports interface/”typed” proxy APIs</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c3a2763e9e4c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/duda/making-http-calls-in-java-spring-overview-tips-and-pitfalls-c3a2763e9e4c">Making HTTP calls in Java &amp; Spring: overview, tips and pitfalls</a> was originally published in <a href="https://medium.com/duda">Duda</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Improving CLS with fallback fonts]]></title>
            <link>https://medium.com/duda/improving-cls-with-fallback-fonts-5ac1b0c81c29?source=rss----a8c59e8a5adc---4</link>
            <guid isPermaLink="false">https://medium.com/p/5ac1b0c81c29</guid>
            <category><![CDATA[core-web-vitals]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[web-performance]]></category>
            <category><![CDATA[seo]]></category>
            <category><![CDATA[ux]]></category>
            <dc:creator><![CDATA[Ronny Shapiro]]></dc:creator>
            <pubDate>Wed, 29 Nov 2023 19:24:21 GMT</pubDate>
            <atom:updated>2023-11-29T19:24:21.322Z</atom:updated>
            <content:encoded><![CDATA[<h4>We were able to improve Duda’s sites CLS scores by 30% using fallback fonts. Here’s how …</h4><p>Cumulative Layout Shift (<a href="https://web.dev/articles/cls">CLS</a>) is one of Core Web Vitals metrics that, in simple terms, measures unexpected movement of elements on a web page. For example, when you’re reading an article and suddenly an ad loads, pushing the content down, that’s a layout shift.</p><p>There are many recommended strategies for optimizing CLS, such as specifying media dimensions, prioritizing server-side rendering, and inlining critical CSS. In this post, I will elaborate on our successful approach to improving CLS by leveraging fallback fonts with metrics overrides.</p><h4>Why are fonts important?</h4><p>In many webpages, the most important piece of content (LCP element) we want folks to see earliest is text, and it’s our job to ensure we show it as quickly as we can, but there’s a challenge: most sites use web fonts that the browser need to download. So, what can we do for our users while they wait for the fonts to arrive fashionably late?</p><p>Thanks to <em>`font-display: swap`</em> CSS property, we’re able to instruct the browser to display a fallback font (system font) while the fancy one is being downloaded and swap the two once the font is ready. This technique (also known as <strong>font swapping)</strong> is considered a best practice since users don’t need to wait before being able to see content.</p><h4>The CLS problem</h4><p>Font swapping is actually a great way to improve LCP scores of text elements as it ensures texts appear as soon as possible, however, this solution may introduce a CLS problem since fonts differ quite significantly from one another, resulting in text elements changing their size and position when their font changes, shifting surrounding elements around.</p><figure><img alt="Font metrics differences" src="https://cdn-images-1.medium.com/max/1024/1*X0jskITINENY7aWH_Wzujw.png" /></figure><p>In the image above we can see Pacifico font rendered on top of Times New Roman, even though they have the exact same font-size. It’s easy to spot the Pacifico font, marked in blue, is narrower and higher than Times New Roman, marked in red.</p><figure><img alt="CLS during font swap" src="https://cdn-images-1.medium.com/max/320/1*8Xl8viaVXPvWvHLkXCsnUQ.gif" /><figcaption>CLS during font swap</figcaption></figure><h4>Font metrics and overrides</h4><p>To better understand why the above happens, we first need the better understand the font metrics system. Font metrics are like a set of measurements for fonts. For example, the <em>width</em> metric is how much space a letter takes horizontally, <em>height</em> is the vertical space, <em>ascent</em> is how high letters go above the line and <em>descent</em> is the space below the line. These and other measurements help make sure letters fit and line up correctly when you’re reading text.</p><figure><img alt="font metrics" src="https://cdn-images-1.medium.com/max/360/1*3ULKxG5I3qEHx9-CYhc0Ow.png" /></figure><p>Thankfully, using the font-face CSS rule, we’re able to declare custom fonts and set font metrics overrides. These overrides allows designers and developers to fine-tune the appearance of text by adjusting the font’s ascent, descent, and line-gap and size metrics.</p><pre>@font-face { <br>  font-family: pacifico-fallback; <br>  src: local(&quot;Times New Roman&quot;); <br>  size-adjust: 99.19745862%; <br>  ascent-override: 131.3541716%; <br>  descent-override: 45.6664925%; <br>  line-gap-override: 0%; <br>}</pre><p>In the snippet above, there’s an adjusted Times New Roman font that now matches the Pacifico dimensions. This results in something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8DBGfCyP204R3VE3NrJMvg.png" /></figure><p>Now, using font fallbacks we can simply instruct the browser to load our perfectly adjusted font while Pacifico is loading:</p><pre>p.main {<br>    font-weight: normal;<br>    font-size: 14px;<br>    font-family: Pacifico, &#39;pacifico-fallback&#39;;<br>}</pre><p>This CSS basically tells the browser to use Pacifico, but if it isn’t available, use the next font on the list instead.</p><p>And this is how it actually affects CLS in real web pages:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*DBurH-MTSfStYCAznRemww.gif" /></figure><h4>Automation and scale</h4><p>Ok cool, we now know how to take a single site using a single particular font and a adjust it perfectly. But how do we automate this process for over 1 million sites hosted on Duda?</p><p>Fortunately, for the majority of fonts, including popular ones like Google’s webfonts, font metrics are well-documented and accessible in repositories such as: <a href="https://github.com/khempenius/font-fallbacks-dataset">https://github.com/khempenius/font-fallbacks-dataset</a></p><p>With the help of tools like <a href="https://github.com/seek-oss/capsize">Capszie</a> built on top of such datasets, developers can effortlessly extract precise mappings from one font to another.</p><p>So what we’ve done at Duda was to generate a mapping of all fonts supported by the Duda editor to a perfectly adjusted Arial font, a system font supported by all operating systems.</p><p>Once we have these mappings available, we identify for each site which fonts it uses and simply append their fallback font-face definitions to the sites’ &lt;head&gt; section.</p><p>The final touch involved an automatic scan and modification of CSS rules and style attributes, and adding our generated fallback font to existing font-family properties.</p><p>And that’s it! This sweet improvement has recently been rolled out to all Duda sites without any manual intervention or designer involvement.</p><h4>Initial results</h4><p>At Duda we have been investing heavily in web performance and CWV for a long time and already achieve great results with over 75% of Duda sites pass all 3 web vitals metrics.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xheP0g_I-IRscLWBmMwsxQ.png" /><figcaption>Duda is a CWV industry leader</figcaption></figure><p>After many rounds of optimizations, identifying opportunities for across-the-board improvements has become quite challenging. This is why we are particularly excited to witness the significant impact this approach has shown, resulting in an improvement of approximately 30% in average scores.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NNBbGAqmsgm7Qq2lwu7jxA.png" /><figcaption>Duda’s avg CLS scores</figcaption></figure><p>It’s worth noting that the effectiveness of this enhancement may be more marginal for websites facing more severe CLS issues, such as disruptive ads or images not taking designated space before loading. So this optimization technique is better suited for sites that have already undergone multiple optimization phases and can be considered a more advanced strategy in the optimization process.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5ac1b0c81c29" width="1" height="1" alt=""><hr><p><a href="https://medium.com/duda/improving-cls-with-fallback-fonts-5ac1b0c81c29">Improving CLS with fallback fonts</a> was originally published in <a href="https://medium.com/duda">Duda</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Multi-Tenant Debugging with Spring Boot: Optimizing Log Levels for Specific Tenants]]></title>
            <link>https://medium.com/duda/multi-tenant-debugging-with-spring-boot-optimizing-log-levels-for-specific-tenants-874112eec1c8?source=rss----a8c59e8a5adc---4</link>
            <guid isPermaLink="false">https://medium.com/p/874112eec1c8</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[logging-and-monitoring]]></category>
            <category><![CDATA[debugging]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Anton Tkachenko]]></dc:creator>
            <pubDate>Wed, 29 Nov 2023 17:22:32 GMT</pubDate>
            <atom:updated>2023-04-23T19:54:12.720Z</atom:updated>
            <content:encoded><![CDATA[<p>In enterprise applications, logs play an important role. They allow you to investigate failures and understand sequences of events and flows.</p><p>The most widely used are the following log levels:</p><ol><li><strong>DEBUG</strong>: The DEBUG level designates fine-grained informational events that are most useful to debug an application.</li><li><strong>INFO</strong>: The INFO level designates informational messages that highlight the application&#39;s progress at a coarse-grained level.</li><li><strong>WARN</strong>: The WARN level designates potentially harmful situations.</li></ol><p>The default log level for most applications is INFO — which means that DEBUG (and TRACE) logs will not reach the log file. And sometimes when the application doesn’t behave as expected for specific inputs (and also doesn’t fail), we need to have more granular logs. A typical way of changing log levels is per class or per package. For instance, you can set the following values in<em> application.properties</em> file to enable debugging:</p><pre>logging.level.org.springframework.web=debug<br>logging.level.com.example.you_app.YourClass=debug</pre><h3>The problem</h3><p>If we have an application that serves lots of requests, enabling debug per package or even in a specific class can produce lots of unnecessary I/O or even hit your monthly quota if you’re using external services like Logz.io. When debugging a specific case (specific request, specific tenant), we would love to enable debug ONLY for it.</p><p>In this article, we will see how to build a Spring Boot application that will allow setting log level to DEBUG only for specific tenants in a multi-tenant application by updating properties. Technical issues that we will face:</p><ul><li>controlling log level per-thread (in addition to per class/per-package)</li><li>accessing template/path variables in request URL before controller</li><li>unit-testing logs output</li></ul><h3>The setup</h3><p>Application source code is available <a href="https://github.com/tkachenkoas/debugging-spring">on github</a></p><p>The application will need a minimum set of dependencies:</p><pre>dependencies {<br>    // for starting web context<br>    implementation &#39;org.springframework.boot:spring-boot-starter-web&#39;<br><br>    // we&#39;ll need this to update props at runtime<br>    implementation &#39;org.springframework.boot:spring-boot-starter-actuator&#39;<br>    implementation &#39;org.springframework.cloud:spring-cloud-context&#39;<br>}</pre><h3>Configuring Logback filter</h3><p>Logback, a logging framework used in this application, uses the <a href="https://logback.qos.ch/manual/filters.html">mechanism of filters</a> in order to decide whether specific log event should be logged or ignored.</p><p>Also, <em>logback-classic </em>defines class <strong>ch.qos.logback.classic.turbo.MDCFilter</strong> that relies on thread-local MDC (<strong>Mapping Diagnostic Context</strong>) to determine whether event should be logged or not.</p><pre>public class MDCFilter extends MatchingFilter {<br><br>    String MDCKey;<br>    String value;<br><br>    @Override<br>    public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {<br>        if (MDCKey == null) {<br>            return FilterReply.NEUTRAL;<br>        }<br><br>        String value = MDC.get(MDCKey);<br>        if (this.value.equals(value)) {<br>            return onMatch;<br>        }<br>        return onMismatch;<br>    }<br><br>    public void setValue(String value) {<br>        this.value = value;<br>    }<br><br>    public void setMDCKey(String MDCKey) {<br>        this.MDCKey = MDCKey;<br>    }<br><br>}</pre><p>We can extend it and add some logic specific to our use case:</p><pre>public class TenantDebugMdcFilter extends MDCFilter {<br><br>    @Override<br>    public FilterReply decide(<br>           Marker marker, Logger logger, Level level, <br>           String format, Object[] params, Throwable t<br>    ) {<br>        if (level != Level.DEBUG) {<br>            return FilterReply.NEUTRAL;<br>        }<br>        if (logger.getName().startsWith(&quot;examples.debuggingtenant&quot;)) {<br>            return super.decide(marker, logger, level, format, params, t);<br>        }<br>        return FilterReply.NEUTRAL;<br>    }<br>}</pre><p>E.g. we’re only interested in allowing thread-local debugging for debug logs in our application.</p><p>Now we need to configure it and add it to our application</p><pre>@Configuration<br>class TenantDebuggingConfiguration {<br>    public static final String TENANT_DEBUGGING_MDC_KEY = &quot;tenant-log-level&quot;;<br>    public static final String TENANT_DEBUG_LEVEL = &quot;enabled&quot;;<br><br>    @Autowired<br>    public TenantDebuggingConfiguration() {<br>        LoggerContext loggerContext = <br>                   (LoggerContext) LoggerFactory.getILoggerFactory();<br><br>        MDCFilter filter = new TenantDebugMdcFilter();<br>        filter.setMDCKey(TENANT_DEBUGGING_MDC_KEY);<br>        filter.setValue(TENANT_DEBUG_LEVEL);<br>        filter.setOnMatch(&quot;ACCEPT&quot;);<br><br>        loggerContext.addTurboFilter(filter);<br>    }<br>}</pre><p>This configuration means the following: if any part of our application will set key <em>tenant-log-level </em>(can be any by your choice) with value <em>enabled </em>(also can be anything), then TenantDebugMdcFilter will indicate that all debug events should be logged.</p><h3>Resolving current tenant from request</h3><p>The logic of setting MDC flag can be any. Let’s just assume that all our tenant-specific endpoints have a “tenant-id” path variable in URL. E.g., our controller will look like this:</p><pre>@RestController<br>@Slf4j<br>public class ControllerForTenantWithDebug {<br>    @GetMapping(&quot;/api/tenants/{&quot; + TENANT_ID_TEMPLATE_VAR + &quot;}/action&quot;)<br>    public void doLogSomething(<br>            @PathVariable(TENANT_ID_TEMPLATE_VAR) String tenantId<br>    ) {<br>        log.info(&quot;Info log that we got request from tenant {}&quot;, tenantId);<br>        log.debug(&quot;And here is some debug data for tenant {}&quot;, tenantId);<br>    }<br><br>}</pre><p>And now we’ll need to create a component that will set MDC property before flow reaches our controller. Logic will be following:</p><ul><li>we need the environment to contain something in <em>tenants-for-debugging</em> property</li><li>next, we extract framework-provided attribute from request (HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE) and check the value of “tenant-id” path variable before reaching the controller</li></ul><pre>@Component<br>@RequiredArgsConstructor<br>public class TenantDebuggingHandlerInterceptor implements HandlerInterceptor {<br><br>    public static final String TENANT_ID_TEMPLATE_VAR = &quot;tenant-id&quot;;<br><br>    private final PropertyResolver propertyResolver;<br><br>    @Override<br>    public boolean preHandle(<br>          HttpServletRequest request, <br>          HttpServletResponse response, Object handler) throws Exception {<br>        Set tenantsForDebugging = propertyResolver.getProperty(<br>                &quot;tenants-for-debugging&quot;, Set.class, Set.of()<br>        );<br>        if (tenantsForDebugging.isEmpty()) {<br>            return true;<br>        }<br>        Map&lt;String, String&gt; templateVariables = <br>            (Map&lt;String, String&gt;) request.getAttribute(<br>                  HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE<br>            );<br>        if (ObjectUtils.isEmpty(templateVariables)) {<br>            return true;<br>        }<br>        if (!templateVariables.containsKey(TENANT_ID_TEMPLATE_VAR)) {<br>            return true;<br>        }<br>        String pathVarValue = templateVariables.get(TENANT_ID_TEMPLATE_VAR);<br>        if (tenantsForDebugging.contains(pathVarValue)) {<br>            MDC.put(TENANT_DEBUGGING_MDC_KEY, TENANT_DEBUG_LEVEL);<br>        }<br>        return true;<br>    }<br><br>    @Override<br>    public void postHandle(<br>          HttpServletRequest request, HttpServletResponse response, <br>          Object handler, ModelAndView modelAndView) throws Exception {<br>        MDC.remove(TENANT_DEBUGGING_MDC_KEY);<br>    }<br><br>}</pre><p>And lastly, this bean needs to be added to Spring’s interceptor registry via registering WebMvcConfigurer bean</p><pre>@Bean<br>public WebMvcConfigurer interceptorsConfigurer(TenantDebuggingHandlerInterceptor debuggingHandlerInterceptor) {<br>    return new WebMvcConfigurer() {<br>        @Override<br>        public void addInterceptors(InterceptorRegistry registry) {<br>            registry.addInterceptor(debuggingHandlerInterceptor);<br>        }<br>    };<br>}</pre><h3>Verifying setup</h3><p>In order to make sure that this setup works, we’ll write two simple tests and verify the console output</p><pre>@SpringBootTest<br>@ExtendWith(OutputCaptureExtension.class)<br>@AutoConfigureMockMvc<br>public class TenantScopedDebugTest {<br><br>    @Autowired<br>    private MockMvc mockMvc;<br><br>    @Test<br>    void byDefaultWeWillOnlyGetInfoLogs(CapturedOutput output) throws Exception {<br>        mockMvc.perform(get(&quot;/api/tenants/some-tenant/action&quot;))<br>                .andExpect(status().isOk());<br>        String out = output.getOut();<br>        assertThat(out).contains(&quot;Info log that we got request from tenant some-tenant&quot;);<br>        assertThat(out).doesNotContain(&quot;And here is some debug data for this request&quot;);<br>    }<br><br>    @Test<br>    void enableTenantDebug_willProduceLogs(CapturedOutput output) throws Exception {<br>        // first we need to set property that will allow debugging for specific tenant<br>        mockMvc.perform(post(&quot;/actuator/env&quot;).contentType(MediaType.APPLICATION_JSON)<br>                .content(<br>                        &quot;&quot;&quot;<br>                                {<br>                                &quot;name&quot;: &quot;tenants-for-debugging&quot;,<br>                                &quot;value&quot;: &quot;32167&quot;<br>                                }<br>                                &quot;&quot;&quot;<br>                )).andExpect(status().isOk());<br><br>        mockMvc.perform(get(&quot;/api/tenants/32167/action&quot;))<br>                .andExpect(status().isOk());<br>        String out = output.getOut();<br>        assertThat(out).contains(&quot;Info log that we got request from tenant 32167&quot;);<br>        assertThat(out).contains(&quot;And here is some debug data for tenant 32167&quot;);<br>    }<br><br>}</pre><p>And this is how logs produced by running this test will look like — you can see that we’ll get 2 INFO logs and one DEBUG</p><pre>2023-03-18 19:54:59.442  INFO 6684 --- [    Test worker] e.d.c.ControllerForTenantWithDebug       : Info log that we got request from tenant some-tenant<br>2023-03-18 19:55:04.300  INFO 6684 --- [    Test worker] e.d.c.ControllerForTenantWithDebug       : Info log that we got request from tenant 32167<br>2023-03-18 19:55:04.300 DEBUG 6684 --- [    Test worker] e.d.c.ControllerForTenantWithDebug       : And here is some debug data for tenant 32167</pre><h3>Conclusion</h3><p>In this article, I have demonstrated how to configure a small Spring Boot application to allow setting the log level to DEBUG only for specific tenants by updating properties. By extending the Logback filter and creating a component that sets the MDC property, we can enable thread-local debugging in a tenant-specific manner. This approach offers a more efficient and targeted way of debugging, avoiding the need to enable debugging for the entire application or specific packages/classes.</p><p>I hope that this article provides useful insights and guidance for developers working on large-scale, multi-tenant applications and helps them optimize their debugging process.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=874112eec1c8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/duda/multi-tenant-debugging-with-spring-boot-optimizing-log-levels-for-specific-tenants-874112eec1c8">Multi-Tenant Debugging with Spring Boot: Optimizing Log Levels for Specific Tenants</a> was originally published in <a href="https://medium.com/duda">Duda</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cleaner Spring Boot IT Rest tests]]></title>
            <link>https://medium.com/duda/cleaner-spring-boot-it-rest-tests-56ce71d64236?source=rss----a8c59e8a5adc---4</link>
            <guid isPermaLink="false">https://medium.com/p/56ce71d64236</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[integration-testing]]></category>
            <dc:creator><![CDATA[Anton Tkachenko]]></dc:creator>
            <pubDate>Wed, 29 Nov 2023 17:22:26 GMT</pubDate>
            <atom:updated>2023-04-23T19:48:44.380Z</atom:updated>
            <content:encoded><![CDATA[<p>It’s pretty common for developers to hear that test code is something that doesn’t require much attention. You usually write it once and read it only when tests fail. As a result, copying and pasting test code seem like a quick and easy solution. While I agree with this approach for unit tests of a compact class, “dirty” test code is usually compensated by the small amount of context covered, so it’s relatively easy to keep the full picture in mind.</p><p>However, when we move from class-level unit tests to the application or system-level end-to-end (e2e) feature testing, the amount of code that is executed in a scenario is usually much larger. The typical flow goes through web, service validation, and persistency layers, and in-application or even cross-application messages can be published. If on top of this, you also add dirty copy-pasted test code, you’ll get yourself in trouble in the not-so-distant future.</p><p>In this article, I want to target a narrow topic related to integrational testing: how to make test code compact, readable, and reusable. We’ll take a look at a small demo application that allows the user to manage cooking recipes and create integrational tests to verify that CRUD (create-read-update-delete) operations work as expected.</p><p>The source code can be found on <a href="https://github.com/tkachenkoas/cleaner-int-tests">github</a>, and in the article, I’ll go over main points and comment them. This article is split into two parts for readability — and part 2 is <a href="https://medium.com/p/cc3ac880d9ec">here</a></p><h3>Application Overview</h3><p>Simple application with web layer that goes directly to in-memory h2 database. <br>Main <a href="https://github.com/tkachenkoas/cleaner-int-tests/blob/main/build.gradle"><strong><em>build.gradle </em></strong></a>dependencies:</p><pre>implementation &#39;org.springframework.boot:spring-boot-starter-web&#39;<br>implementation &#39;org.springframework.boot:spring-boot-starter-data-jpa&#39;<br>runtimeOnly &#39;com.h2database:h2&#39;</pre><p>And “standard” h2 configuration in <strong><em>application.properties</em></strong> file</p><pre>spring.datasource.url=jdbc:h2:mem:testdb<br>spring.datasource.driverClassName=org.h2.Driver<br>spring.jpa.database-platform=org.hibernate.dialect.H2Dialect</pre><p>Here’s the jpa <a href="https://github.com/tkachenkoas/cleaner-int-tests/blob/main/src/main/java/com/example/cleaner_int_tests/data/RecipeEntity.java">RecipeEntity</a> that also defines the recipe model with 3 simple fields (<strong><em>ingredients </em></strong>are de-facto a list, but for simplicity, we’ll save it in DB as a comma-separated string)</p><pre>@Entity<br>@Data<br>@Builder<br>@NoArgsConstructor<br>@AllArgsConstructor<br>public class RecipeEntity {<br>    @Id<br>    @GeneratedValue(generator = &quot;uuid&quot;)<br>    @GenericGenerator(name = &quot;uuid&quot;, strategy = &quot;uuid2&quot;)<br>    private String id;<br>    private String name;<br>    private String instructions;<br>    private String ingredients;<br><br>    @Transient<br>    public List&lt;String&gt; getIngredientsList() {<br>        return asList(ingredients.split(&quot;,&quot;));<br>    }<br><br>    @Transient<br>    public void setIngredientsList(List&lt;String&gt; ingredients) {<br>        this.ingredients = String.join(&quot;,&quot;, ingredients);<br>    }<br>}</pre><p>And here is the <a href="https://github.com/tkachenkoas/cleaner-int-tests/blob/main/src/main/java/com/example/cleaner_int_tests/web/RecipesRestApi.java">rest API</a> for CRUD operations</p><pre>@RequestMapping(&quot;/recipes&quot;)<br>public interface RecipesRestApi {<br><br>    @GetMapping<br>    Page&lt;RecipeResponse&gt; getRecipesPage();<br><br>    @PostMapping<br>    RecipeResponse createRecipe(<br>            @RequestBody RecipeRequest request<br>    );<br><br>    @GetMapping(&quot;/{id}&quot;)<br>    RecipeResponse getRecipe(@PathVariable(&quot;id&quot;) String id);<br><br>    @PatchMapping(&quot;/{id}&quot;)<br>    RecipeResponse updateRecipe(<br>            @PathVariable(&quot;id&quot;) String id,<br>            @RequestBody RecipeRequest request<br>    );<br><br>    @DeleteMapping(&quot;/{id}&quot;)<br>    void deleteRecipe(@PathVariable(&quot;id&quot;) String id);<br><br>}</pre><p>Request &amp; response objects: similar to the entity. Classes are package-private so that NOT to accidentally use them in tests</p><pre>class RecipeRequest {<br>    private String name;<br>    private String instructions;<br>    private List&lt;String&gt; ingredients;<br>}</pre><pre>class RecipeResponse extends RecipeRequest {<br>    private String id;<br>}</pre><p><a href="https://github.com/tkachenkoas/cleaner-int-tests/blob/main/src/main/java/com/example/cleaner_int_tests/web/RecipesController.java">Controller</a> only contains web-to-entity mapping, so I won’t even specify its code here. If “recipe-by-id” doesn’t exist, it throws an exception that is mapped into 404 status in trivial exception handler</p><pre>@ControllerAdvice<br>public class ControllerExceptionsHandler {<br><br>    @ExceptionHandler(ResourceNotExistException.class)<br>    public final ResponseEntity handleException(Exception ex) {<br>        return new ResponseEntity&lt;&gt;(<br>                Map.of(&quot;message&quot;, ex.getMessage()),<br>                HttpStatus.NOT_FOUND<br>        );<br>    }<br><br>}</pre><h3>Test flow that verifies main use-cases</h3><p>We’ll take a look at tests that cover the following flow:</p><ul><li>creating a recipe</li><li>retrieving existing recipes / single recipe by id</li><li>updating recipe</li><li>deleting recipe</li><li>(and verifying that it was deleted)</li></ul><p>I will show 5 different variations of a test class that verifies this flow, and with every “version increase”, there will be less and less code to read.</p><h3>Test Configuration</h3><pre>@TestInstance(TestInstance.Lifecycle.PER_CLASS)<br>@SpringBootTest(webEnvironment = DEFINED_PORT)<br>@AutoConfigureMockMvc<br>@DirtiesContext(classMode = BEFORE_CLASS)<br>@ActiveProfiles(&quot;test&quot;)<br>public class BaseFullContextTest {<br>    // will be used to save nerves while jsonifying requests and responses<br>    public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();<br><br>}</pre><ul><li>@TestInstance(PER_CLASS) — tests will “stateful”, and next steps will depend on the outcome of the previous (test will be @Ordered). This will save “setup” code, and instead of having “test per single case”, we’ll have “TEST CLASS per scenario”</li><li>@DirtiesContext(BEFORE_CLASS) — recreating context is the simplest way to clean h2 db between test classes for such a tiny app.</li><li>@SpringBootTest(DEFINED_PORT) &amp; @AutoConfigureMockMvc —some tests will use mock-web-environment, others will make real HTTP calls to embedded tomcat hosting our web-application</li><li>@ActiveProfiles(“test”) — for spring-boot to pick up configs from <em>application-test.properties</em></li></ul><h3>Version 0: “MockMvc boilerplate”</h3><p>Corresponds to test class <a href="https://github.com/tkachenkoas/cleaner-int-tests/blob/main/src/test/java/com/example/cleaner_int_tests/CrudFlowTest_v0_MockMvcBoilerplate.java"><strong><em>CrudFlowTest_v0_MockMvcBoilerplate</em></strong></a><strong><em>. </em></strong>This test variation is the longest and the most verbose of all.</p><p>Creating recipe and making all assertions:</p><pre>@Test<br>@Order(0)<br>void createNewRecipe() throws Exception {<br>    mockMvc.perform(<br>                    post(&quot;/recipes&quot;)<br>                            .contentType(MediaType.APPLICATION_JSON)<br>                            .content(&quot;&quot;&quot;<br>                                    {<br>                                    &quot;name&quot; : &quot;Man sushi&quot;,<br>                                    &quot;instructions&quot; : &quot;Call sushi bar&quot;,<br>                                    &quot;ingredients&quot; : [ &quot;Phone&quot;, &quot;Sushi bar&quot;, &quot;Money&quot; ]<br>                                    }<br>                                    &quot;&quot;&quot;<br>                            )<br>            ).andExpect(status().isOk())<br>            .andExpect(content().contentType(MediaType.APPLICATION_JSON))<br>            .andExpect(<br>                    jsonPath(&quot;name&quot;).value(&quot;Man sushi&quot;)<br>            )<br>            .andExpect(<br>                    jsonPath(&quot;instructions&quot;).value(&quot;Call sushi bar&quot;)<br>            )<br>            .andExpect(<br>                    jsonPath(&quot;ingredients[0]&quot;).value(&quot;Phone&quot;)<br>            )<br>            .andExpect(<br>                    jsonPath(&quot;ingredients[1]&quot;).value(&quot;Sushi bar&quot;)<br>            )<br>            .andExpect(<br>                    jsonPath(&quot;ingredients[2]&quot;).value(&quot;Money&quot;)<br>            )<br>            .andReturn();<br>}</pre><p>MockMvc has a pretty rich, readable and flexible DSL that allows to make calls and assertions about all attributes of response — from status and headers to specific paths. But there is a flaw — this code is very verbose. Also, it relies on lots of static imports from various RequestBuilders and ResultMathchers classes that you need to know and remember</p><pre>import static org.springframework.test.---.MockMvcRequestBuilders.get;<br>import static org.springframework.test.---.MockMvcRequestBuilders.post;<br>import static org.springframework.test.---.MockMvcResultMatchers.content;<br>import static org.springframework.test.---.MockMvcResultMatchers.jsonPath;<br>import static org.springframework.test.---.MockMvcResultMatchers.status;</pre><p>Also sometimes we need to directly access properties of response to make some manipulations in test — and this is when ObjectMapper can help us.</p><pre>String recipeId;<br><br>@Test<br>@Order(1)<br>void createdRecipe_shouldBeAvailable_viaGetPage_andViaGetById() throws Exception {<br>    String recipesPageResponseString = mockMvc.perform(<br>                    get(&quot;/recipes&quot;)<br>            ).andExpect(status().isOk())<br>            .andExpect(content().contentType(MediaType.APPLICATION_JSON))<br>            .andReturn().getResponse().getContentAsString();<br><br>    Map responseAsMap = OBJECT_MAPPER.readValue(recipesPageResponseString, Map.class);<br><br>    List&lt;Map&lt;String, Object&gt;&gt; content = (List) responseAsMap.get(&quot;content&quot;);<br>    assertThat(content).hasSize(1);<br>    var recipe = content.get(0);<br>    assertThat(recipe.get(&quot;name&quot;)).isEqualTo(&quot;Man sushi&quot;);<br>    assertThat(recipe.get(&quot;instructions&quot;)).isEqualTo(&quot;Call sushi bar&quot;);<br>    assertThat((List) recipe.get(&quot;ingredients&quot;))<br>            .hasSize(3)<br>            .containsExactly(&quot;Phone&quot;, &quot;Sushi bar&quot;, &quot;Money&quot;);<br><br>    recipeId = (String) recipe.get(&quot;id&quot;);<br><br>    String byIdRecipeJson = mockMvc.perform(<br>                    get(&quot;/recipes/&quot; + recipeId)<br>            ).andExpect(status().isOk())<br>            .andExpect(content().contentType(MediaType.APPLICATION_JSON))<br>            .andReturn().getResponse().getContentAsString();<br><br>    Map byIdRecipe = OBJECT_MAPPER.readValue(byIdRecipeJson, Map.class);<br>    assertThat(byIdRecipe).isEqualTo(recipe);<br>}</pre><p>Unfortunately, Java is a strongly typed language —and in order to make similar json-assumptions, we need to write lots and lots of code.</p><p>Repository source code also contains three other steps — but clearly, using “raw” MockMvc apis results in writing (and reading) lots of code.</p><h3>Version 1 -&gt; test-utils and mvc-client</h3><p>As you might have noticed, there is a lot of repeated code that can be reused instead of being copied</p><ul><li><strong><em>andExpect(status().isOk()) </em></strong>/ <strong><em>content().contentType(APPLICATION_JSON) </em></strong>— 95% of test cases expect the server to respond with “ok” — and most applications use json format — so it’s not worth mentioning every time (and should be encapsulated somewhere)</li><li>integrational testing is mostly about making sure that your application performs for the main declared behavior, not for all the billion possible non-happy-path-scenarios</li><li>there are lots of libraries for making http calls with simple API for frequent cases, so why not have some “wrapper” around MockMvc that can reduce the amount of code and be reused for testing all APIs in the project?</li></ul><pre>/**<br> * Simplified test-version of org.springframework.web.client.RestOperations<br> */<br>public interface TestRestOperations {<br><br>    /**<br>     * Accepts &quot;raw&quot; json content, expects 200 status and returns mockmvc result<br>     * actions for response assertions<br>     */<br>    ResultActions postActions(String path, String content);<br><br>    /**<br>     * Performs mockmvc-get, expects 200 status and reads response into Map / List object<br>     */<br>    &lt;T&gt; T doGet(String path);<br><br>    /**<br>     * Performs mockmvc-get, expects given status and parses response into object<br>     */<br>    &lt;T&gt; T doGet(HttpStatus expectedStatus, String path);<br>    <br>    ... any other helper operations that you may need<br>}</pre><p>link to repo: <a href="https://github.com/tkachenkoas/cleaner-int-tests/blob/main/src/test/java/com/example/cleaner_int_tests/test_utils/TestRestOperations.java">TestRestOperations</a></p><p>And sample implementation that contains all the boilerplate:<br>(yes, some private methods can be extracted to reuse assertions)…</p><pre>@Component<br>@RequiredArgsConstructor<br>class TestRestOperationsImpl implements TestRestOperations {<br><br>    private final MockMvc mockMvc;<br><br>    @Override<br>    @SneakyThrows<br>    public ResultActions postActions(String path, String content) {<br>        MockHttpServletRequestBuilder requestBuilder = post(path)<br>                .contentType(MediaType.APPLICATION_JSON)<br>                .content(content);<br>        return mockMvc.perform(requestBuilder)<br>                .andExpect(status().isOk())<br>                .andExpect(content().contentType(MediaType.APPLICATION_JSON));<br>    }<br><br>    @Override<br>    @SneakyThrows<br>    public &lt;T&gt; T doGet(String path) {<br>        String responseJson = mockMvc.perform(get(path))<br>                .andExpect(status().isOk())<br>                .andExpect(content().contentType(MediaType.APPLICATION_JSON))<br>                .andReturn().getResponse().getContentAsString();<br>        return readResponse(responseJson);<br>    }<br>    <br>    @Override<br>    @SneakyThrows<br>    public &lt;T&gt; T doGet(HttpStatus expectedStatus, String path) {<br>        String responseJson = mockMvc.perform(get(path))<br>                .andExpect(status().is(expectedStatus.value()))<br>                .andExpect(content().contentType(MediaType.APPLICATION_JSON))<br>                .andReturn().getResponse().getContentAsString();<br>        return readResponse(responseJson);<br>    }<br><br>    ...<br><br>    @SneakyThrows<br>    private static &lt;T&gt; T readResponse(String response) {<br>        return (T) OBJECT_MAPPER.readValue(response, Object.class);<br>    }<br>}</pre><ul><li>repeating/chaining of “<strong><em>andExpect( jsonPath…)</em></strong>” can be extracted. Btw, adding @SneakyThrows helps us get rid of checked exceptions<br>link to repo: <a href="https://github.com/tkachenkoas/cleaner-int-tests/blob/main/src/test/java/com/example/cleaner_int_tests/test_utils/MockMvcOperationsUtils.java">MockMvcOperationsUtils</a></li></ul><pre>/**<br> * Contains various helper methods that help reduce amount of copy-paste<br> */<br>public class MockMvcOperationsUtils {<br>    @SneakyThrows<br>    public static void asserJsonPaths(<br>            ResultActions resultActions,<br>            Map&lt;String, String&gt; pathValues<br>    ) {<br>        for (Map.Entry&lt;String, String&gt; entry : pathValues.entrySet()) {<br>            resultActions.andExpect(<br>                    jsonPath(entry.getKey()).value(entry.getValue())<br>            );<br>        }<br>    }<br>}</pre><p>General idea is — to prefer composition and reusable classes/components over private methods in test classes that are not related to flow. Now, the test code will be shorter:</p><p><a href="https://github.com/tkachenkoas/cleaner-int-tests/blob/main/src/test/java/com/example/cleaner_int_tests/CrudFlowTest_v1_WithUtilsAndTestRestOperations.java"><strong><em>CrudFlowTest_v1_WithUtilsAndTestRestOperations</em></strong></a></p><pre>    @Autowired<br>    private TestRestOperations testRestOperations;<br><br>    @Test<br>    @Order(0)slo<br>    void createNewRecipe() {<br>        ResultActions postActions = testRestOperations.postActions(<br>                &quot;/recipes&quot;,<br>                &quot;&quot;&quot;<br>                        {<br>                        &quot;name&quot; : &quot;Man sushi&quot;,<br>                        &quot;instructions&quot; : &quot;Call sushi bar&quot;,<br>                        &quot;ingredients&quot; : [ &quot;Phone&quot;, &quot;Sushi bar&quot;, &quot;Money&quot; ]<br>                        }<br>                &quot;&quot;&quot;<br>        );<br><br>        asserJsonPaths(postActions, Map.of(<br>                &quot;name&quot;, &quot;Man sushi&quot;,<br>                &quot;instructions&quot;, &quot;Call sushi bar&quot;,<br>                &quot;ingredients[0]&quot;, &quot;Phone&quot;,<br>                &quot;ingredients[1]&quot;, &quot;Sushi bar&quot;,<br>                &quot;ingredients[2]&quot;, &quot;Money&quot;<br>        ));<br>    }</pre><p>Also, if your scenario requires some authentication, then you can apply it per class on <strong><em>TestRestOperations</em></strong> in any convenient way</p><p>By reusing code in tests, we make them easier to read. Also, we can use “java-lang-assertions” over Assertj’s <em>assertThat </em>to have even less code to read.</p><pre>@Test<br>@Order(2)<br>void shouldUpdateRecipeById_inPatchManner() {<br>    Map&lt;String, Object&gt; patchResponse = testRestOperations.doPatch(<br>            &quot;/recipes/&quot; + recipeId,<br>            Map.of(&quot;name&quot;, &quot;Lazy sushi&quot;)<br>    );<br><br>    assertThat(patchResponse.get(&quot;name&quot;)).isEqualTo(&quot;Lazy sushi&quot;);<br>    assertThat(patchResponse.get(&quot;instructions&quot;)).isEqualTo(&quot;Call sushi bar&quot;);<br>}<br><br>@Test<br>@Order(3)<br>void reloadById_verifyNameUpdate() {<br>    Map byIdRecipe = testRestOperations.doGet(&quot;/recipes/&quot; + recipeId);<br><br>    assert byIdRecipe.get(&quot;name&quot;).equals(&quot;Lazy sushi&quot;);<br>    assert byIdRecipe.get(&quot;instructions&quot;).equals(&quot;Call sushi bar&quot;);<br>}</pre><p>However, Java’s <em>assert is not </em>very helpful when it fails:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/920/1*KFEpIQP4me_OQ7U75fRmUw.png" /></figure><h3>Version 2 — wrapping operations into “test client” interfaces</h3><p>Corresponds to <a href="https://github.com/tkachenkoas/cleaner-int-tests/blob/main/src/test/java/com/example/cleaner_int_tests/CrudFlowTest_v2_WithTestClient.java">CrudFlowTest_v2_WithTestClient</a></p><p>If your application has lots of cross-api scenarios, at some point you will get tired of rememberhing / specifying all the endpoints and request/response models — and this is another area to reuse code.<br>To simplify it, we can “mirror” our server-side api to encapsulate endpoints and arguments, json-mapping. Also we’ll leave some javadoc how request/response body looks like.</p><p>Implementation will rely on existing “mock-mvc client”</p><pre>@Component<br>@RequiredArgsConstructor<br>class RecipesRestApiTestClientImpl implements RecipesRestApiTestClient {<br><br>    private final TestRestOperations restOperations;<br><br>    @Override<br>    public Map getRecipesPage() {<br>        return restOperations.doGet(&quot;/recipes&quot;);<br>    }<br><br>    @Override<br>    public Map createRecipe(Map request) {<br>        return restOperations.doPost(&quot;/recipes&quot;, request);<br>    }<br>...<br>}</pre><p>In this iteration, test code will be even easier to read than in previous, but it still requires some strong-typing</p><pre>@Test<br>@Order(3)<br>void reloadById_verifyNameUpdate() {<br>    Map byIdRecipe = apiClient.getRecipe(recipeId);<br><br>    assert byIdRecipe.get(&quot;name&quot;).equals(&quot;Lazy sushi&quot;);<br>    assert byIdRecipe.get(&quot;instructions&quot;).equals(&quot;Call sushi bar&quot;);<br>}<br><br>@Test<br>@Order(4)<br>void deleteById_shouldRemoveFrom_getPage() {<br>    apiClient.deleteRecipe(recipeId);<br><br>    Map pageResponse = apiClient.getRecipesPage();<br>    List&lt;Map&lt;String, Object&gt;&gt; content = (List) pageResponse.get(&quot;content&quot;);<br>    assert content.isEmpty();<br>}<br><br>@Test<br>@Order(5)<br>void getMissingRecipeById_shouldReturn404() {<br>    Map responseAsMap = apiClient.getRecipe(NOT_FOUND, recipeId);<br>    assert responseAsMap.get(&quot;message&quot;).equals(&quot;No recipe for id &quot; + recipeId);<br>}</pre><h3>Version 3 — using better syntax for testing</h3><p>Java is not the only programming language that works on JVM platform. We also have Groovy — a dynamically-typed language that has syntax similar to JavaScript. Groovy also is fully compatible with all your existing Java codebase. If you’re writing gradle buildscripts from time to time, then you are already familiar with Groovy. In order to add it to your project, you’ll need the following setup:</p><pre>plugins { id &#39;groovy&#39; }</pre><pre>dependencies { testImplementation &#39;org.codehaus.groovy:groovy-all:3.0.13&#39; }</pre><p>And then to create <em>.groovy </em>classes under <strong><em>groovy </em></strong>namespace</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/391/1*ElQsBdwpA73uYU9VVjQ2gQ.png" /></figure><p>If you already know Java, learning groovy won’t be a big issue — 99% of Java code is accepted by Groovy compiler. There are some minor differences that you will pick up easily ( for instance, <em>def</em> is identical to Java’s <em>var</em>)</p><p>This is how test code will look now:<br><a href="https://github.com/tkachenkoas/cleaner-int-tests/blob/main/src/test/groovy/com/example/cleaner_int_tests/CrudFlowTest_v3_WithTestClientInGroovy.groovy">CrudFlowTest_v3_WithTestClientInGroovy</a></p><pre>@Test<br>@Order(0)<br>void createNewRecipe() {<br>    def response = apiClient.createRecipe([<br>            &quot;name&quot;        : &quot;Man sushi&quot;,<br>            &quot;instructions&quot;: &quot;Call sushi bar&quot;,<br>            &quot;ingredients&quot; : [&quot;Phone&quot;, &quot;Sushi bar&quot;, &quot;Money&quot;]<br>    ])<br><br>    assert response[&quot;name&quot;] == &quot;Man sushi&quot;<br>    assert response[&quot;instructions&quot;] == &quot;Call sushi bar&quot;<br>    assert response[&quot;ingredients&quot;] == [&quot;Phone&quot;, &quot;Sushi bar&quot;, &quot;Money&quot;]<br>    assert response.ingredients[0] == &#39;Phone&#39;<br>}</pre><p>Note that <strong><em>[ ‘key’ : ‘value’ ] </em></strong>defines a Map, but <strong><em>[‘first’, ‘second’] </em></strong>is a List.</p><p>Single-quotes define regular <strong><em>‘string’</em></strong>, and double-quotes define parametrized string <strong><em>“recipe id $recipeId”</em></strong> — like in most modern languages</p><p>Groovy supports dynamic field accessors like javascript. If you use them, IDE will not help you with compilation &amp; hints (and code will fail in runtime), but your tests will be short and readable</p><p>Also, Groovy’s <em>assert </em>is much better than Java’s and gives a lot of information about failure (nearly as much as the AssertJ library):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hB2PTpdcydHQzcC0vq9rUg.png" /></figure><pre>@Test<br>@Order(2)<br>void shouldUpdateRecipeById_inPatchManner() {<br>    def patchResponse = apiClient.updateRecipe(<br>            recipeId, [&quot;name&quot;: &quot;Lazy sushi&quot;]<br>    )<br><br>    assert patchResponse.name == &quot;Lazy sushi&quot;<br>    assert patchResponse.instructions == &quot;Call sushi bar&quot;<br>}</pre><pre>@Test<br>@Order(5)<br>void getMissingRecipeById_shouldReturn404() {<br>    def response = apiClient.getRecipe(NOT_FOUND, recipeId)<br>    assert response.message == &quot;No recipe for id $recipeId&quot;<br>}</pre><p>One more trick that reduces amount of test code is covered in <a href="https://medium.com/p/cc3ac880d9ec">Part 2</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=56ce71d64236" width="1" height="1" alt=""><hr><p><a href="https://medium.com/duda/cleaner-spring-boot-it-rest-tests-56ce71d64236">Cleaner Spring Boot IT Rest tests</a> was originally published in <a href="https://medium.com/duda">Duda</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cleaner Spring Boot IT Rest tests with client generation]]></title>
            <link>https://medium.com/duda/cleaner-spring-boot-it-rest-tests-with-client-generation-cc3ac880d9ec?source=rss----a8c59e8a5adc---4</link>
            <guid isPermaLink="false">https://medium.com/p/cc3ac880d9ec</guid>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[integration-testing]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[code-generation]]></category>
            <dc:creator><![CDATA[Anton Tkachenko]]></dc:creator>
            <pubDate>Wed, 29 Nov 2023 17:22:03 GMT</pubDate>
            <atom:updated>2023-04-23T19:51:12.077Z</atom:updated>
            <content:encoded><![CDATA[<p>This is part 2 of <a href="https://medium.com/p/56ce71d64236">the article</a>; it uses the same application code and test setup as Part 1. You can find the source code on <a href="https://github.com/tkachenkoas/cleaner-int-tests">GitHub</a>.</p><h3>OpenAPI documentation and its usage</h3><p>The <a href="https://www.openapis.org/">OpenAPI </a>specification covers a standard way of documenting REST endpoints, including the request/response body, query parameters, headers, and more.</p><p>The typical flow of using OpenAPI is as follows:</p><ol><li>You create a JSON/YAML documentation of your application’s REST APIs using one of the doc-generation tools. For instance, <a href="https://stripe.com/docs/api">Stripe’s </a>API schema is documented using OpenAPI.</li><li>The API consumer needs a user interface to browse the API. Several solutions provide user interfaces based on YAML/JSON schema, such as Stripe API Docs. The default one is <a href="https://editor.swagger.io/">https://editor.swagger.io/</a>.</li><li>To invoke application endpoints from the consumer app, you can generate an SDK using one of the tools, such as <a href="https://github.com/OpenAPITools/openapi-generator">https://github.com/OpenAPITools/openapi-generator</a>.</li></ol><p>Code generation can save you a lot of time. You won’t need to specify all the endpoints and request/response objects, as everything can be generated for you. If your application maintains API docs in order, you can benefit a lot from it when writing tests.</p><h3>How to generate open API schema for endpoints of your application</h3><p><a href="https://springdoc.org/">Springdoc</a> can be used to create REST API docs in a few clicks. Here’s the Gradle script:</p><pre>plugins {<br>   id &#39;org.springdoc.openapi-gradle-plugin&#39; version &#39;1.6.0&#39;<br>}</pre><pre>dependencies {<br>   implementation &#39;org.springdoc:springdoc-openapi-ui:1.6.14&#39;<br>}</pre><pre>// <a href="https://github.com/springdoc/springdoc-openapi-gradle-plugin">https://github.com/springdoc/springdoc-openapi-gradle-plugin</a><br>openApi {<br>    apiDocsUrl.set(&quot;http://localhost:8080/v3/api-docs.yaml&quot;)<br>    outputDir.set(file(&quot;./&quot;))<br>    outputFileName.set(&quot;api-schema.yaml&quot;)<br>}</pre><p>Now you can use this Gradle task to produce an OpenAPI schema of your controllers. It will take some time because it actually starts your application. The schema should be regenerated after any change in the controller so that in a pull request, you would see how API consumers are affected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/206/1*WcxULLm7BWCb0xCDDN3Adw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5Be34Clh-AntgWd09lDU3g.png" /></figure><p>Schema should be regenerated after any change in controller — so that in pull request you would see how api consumers are affected.</p><h3>Generating client sdk from openapi-schema</h3><p>The next step is to turn API docs schema into generated code:</p><pre>plugins { id &quot;org.openapi.generator&quot; version &quot;6.3.0&quot; }</pre><pre>task generateClient(<br>        type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask,<br>        group: &#39;openapi tools&#39;<br>){<br>    generatorName = &quot;java&quot;<br>    inputSpec = &quot;$rootDir/api-schema.yaml&quot;<br>    outputDir = &quot;$buildDir/generated&quot;<br>    apiPackage = &quot;com.example.apiclient.api&quot;<br>    library = &#39;resttemplate&#39;<br>    invokerPackage = &quot;com.example.apiclient.invoker&quot;<br>    modelPackage = &quot;com.example.apiclient.model&quot;<br>    configOptions = [<br>            dateLibrary: &quot;java8&quot;<br>    ]<br>}</pre><pre>// this code is needed for generated code to be available in <br>// test-namespace of our application</pre><pre>sourceSets.test.java.srcDirs += [&#39;build/generated/src/main/java&#39;]<br>compileTestJava.dependsOn generateClient</pre><p>The client will be regenerated before compiling main code, and it will also be added to the test-classpath, so our tests can use it, and we won’t need to commit it to the repo.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/1*qHhZzpSheCFt6xKiGfcHug.png" /></figure><h3>Using generated client-sdk in IT tests</h3><p>In order to use generated API accessors as Spring beans in our tests, we need to write a small config and define a bean of com.example.apiclient.invoker.ApiClient, which is the main component used by generated code to invoke endpoints.</p><pre>@Configuration<br>@ComponentScan(&quot;com.example.apiclient.api&quot;)<br>public class GeneratedApiClientConfiguration {<br>    @Bean<br>    public ApiClient apiClient(@Value(&quot;${server.port}&quot;) Integer serverPort) {<br>        RestTemplate restTemplate = new RestTemplate();<br>        // to support PATCH requests<br>        restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());<br>        ApiClient apiClient = new ApiClient(restTemplate);<br>        apiClient.setBasePath(&quot;http://localhost:&quot; + serverPort);<br>        return apiClient;<br>    }<br>}</pre><p>Now we can enjoy strong typing of generated request &amp; response classes.</p><p><a href="https://github.com/tkachenkoas/cleaner-int-tests/blob/main/src/test/groovy/com/example/cleaner_int_tests/CrudFlowTest_v4_WithGeneratedTestClientInGroovy.groovy">CrudFlowTest_v4_WithGeneratedTestClientInGroovy</a></p><pre>import com.example.apiclient.api.RecipesControllerApi<br>import com.example.apiclient.model.RecipeRequest<br><br>@Autowired<br>private RecipesControllerApi apiClient<br>@Test<br>@Order(0)<br>void createNewRecipe() {<br>    def response = apiClient.createRecipe(new RecipeRequest()<br>            .name(&quot;Man sushi&quot;).instructions(&quot;Call sushi bar&quot;)<br>            .ingredients([&quot;Phone&quot;, &quot;Sushi bar&quot;, &quot;Money&quot;])<br>    )<br>    assert response.name == &quot;Man sushi&quot;<br>    assert response.instructions == &quot;Call sushi bar&quot;<br>    assert response.ingredients == [&quot;Phone&quot;, &quot;Sushi bar&quot;, &quot;Money&quot;]<br>}</pre><p>Although it may seem like we are injecting the controller and calling it like a service, that’s not true. The only difference between this and the production environment is that here we are calling the server directly by localhost-ip, not via load-balancer and all other infrastructure. However, the requests go through all layers of Tomcat, Spring web, serialize and deserialize.</p><p>Furthermore, if we change any server code (for example, rename a request field), our tests will fail (as they should) because the test code doesn’t reuse any “production” classes — it relies only on the generated API schema.</p><p>Using Groovy as the test language is preferred over Java because it’s more compact. However, pay attention that in this example, <strong>response.name </strong>and <strong>response.instructions</strong> are actual compiler-checked and IDE-suggested properties (.name is identical to .getName() in Groovy).</p><p>In order to make non-200 requests, we’ll need a small helper utility method to read RestTemplate’s exceptions into a strong-typed record class.</p><pre>@Test<br>@Order(5)<br>void getMissingRecipeById_shouldReturn404() {<br>    def failureResponse = expectHttpFailure(NOT_FOUND, () -&gt; apiClient.getRecipe(recipeId))<br>    assert failureResponse.message() == &quot;No recipe for id $recipeId&quot;<br>}</pre><pre>public class ApiClientUtils {<br>    <br>    record FailureResponse(String message) {<br>    }<br><br>    @SneakyThrows<br>    public static FailureResponse expectHttpFailure(<br>            HttpStatus status, Runnable operation<br>    ) {<br>        try {<br>            operation.run();<br>            throw new RuntimeException(&quot;Code is expected to throw HttpStatusCodeException with status &quot; + status);<br>        } catch (HttpStatusCodeException e) {<br>            assert e.getStatusCode() == status;<br>            return OBJECT_MAPPER.readValue(e.getResponseBodyAsString(), FailureResponse.class);<br>        }<br>    }<br>}</pre><h3>Using generated clients for “shallow” web layer tests (@WebMvcTest)</h3><p>Spring Boot provides capabilities for convenient testing only of selected application layer (only db/web/ … ) with set of <a href="https://docs.spring.io/spring-boot/docs/2.7.9/reference/html/test-auto-configuration.html#appendix.test-auto-configuration">special annotations</a>.</p><p>The benefit of such tests is that the context starts much faster than with @SpringBootTest, and it’s beneficial if we want to target some security rules or servlet-filters. As in the previous example, we can save ourselves lots of effort by using generated code to invoke our APIs:</p><p>Source code: <a href="https://github.com/tkachenkoas/cleaner-int-tests/blob/main/src/test/groovy/com/example/cleaner_int_tests/ShallowControllerTest_WithGeneratedClient.groovy">ShallowControllerTest_WithGeneratedClient</a></p><p>Key configuration points:</p><ul><li>@WebMvcTest — starts mock-web environment, filters, http-conterters, security (if applicable) and all components that normally are provided by spring-boot-web-starter</li><li><a href="http://twitter.com/ContextConfiguration">@ContextConfiguration</a>(classes = MockMvcShallowTestContext) + <a href="http://twitter.com/ComponentScan">@ComponentScan</a>([“com.example.cleaner_int_tests.web”]) + <a href="http://twitter.com/MockBean">@MockBean</a> RecipeEntityRepository mockRepository</li></ul><p>This is context of our application’s components that we want to have in our “shallow” test context. E.g. — controllers &amp; request/response-converter, as well as Mockito’s mockbeans to imitate our db/service layer.</p><ul><li>Finally, we need @ComponentScan for “com.example.apiclient.api” with generated clients — and also to define ApiClient bean with RestTemplate that is based on MockMvcClientHttpRequestFactory (and MockMvc will be provided by WebMvcTest)</li></ul><pre>@Bean<br>ApiClient apiClient(MockMvc mockMvc) {<br>    RestTemplate restTemplate = new RestTemplate(<br>            new MockMvcClientHttpRequestFactory(mockMvc)<br>    );<br>    return new ApiClient(restTemplate)<br>}</pre><p>This test verifies the following points in integration:</p><ul><li>The request will reach the controller through all web layers.</li><li>The request will be converted to an entity.</li><li>The mock-repository will be called with the correct parameters.</li><li>The entity returned by the mock-repository will be correctly transformed into a response. And since we fully control the values received and returned by mocks, this gives us great freedom in making assertions that target specific details of our shallow context.</li></ul><pre>@Autowired<br>RecipesControllerApi apiClient<br><br>@Autowired<br>RecipeEntityRepository mockRepository<br><br>@Test<br>void createNewRecipe_willCallRepoAndMakeAllConversions() {<br>    when(mockRepository.save(argThat({ RecipeEntity it -&gt;<br>        assert it.name == &quot;Man sushi&quot;<br>        assert it.instructions == &quot;Call sushi bar&quot;<br>        assert it.getIngredientsList() == [&quot;Phone&quot;, &quot;Sushi bar&quot;, &quot;Money&quot;]<br>        return true<br>    }))).thenReturn(new RecipeEntity(<br>            &quot;123&quot;, &quot;Man sushi&quot;, &quot;Call sushi bar&quot;,<br>            &quot;Phone,Sushi bar,Money&quot;<br>    ))<br><br>    def response = apiClient.createRecipe(new RecipeRequest()<br>            .name(&quot;Man sushi&quot;).instructions(&quot;Call sushi bar&quot;)<br>            .ingredients([&quot;Phone&quot;, &quot;Sushi bar&quot;, &quot;Money&quot;])<br>    )<br><br>    assert response.id == &quot;123&quot;<br>    assert response.name == &quot;Man sushi&quot;<br>    assert response.instructions == &quot;Call sushi bar&quot;<br>    assert response.ingredients == [&quot;Phone&quot;, &quot;Sushi bar&quot;, &quot;Money&quot;]<br><br>    verify(mockRepository).save(any())<br>}</pre><h3>Final step — using clean generated groovy models</h3><p>If you look into the <em>build/generated/src/main/java/com/example/apiclient/model</em> folder and open any class, you will see lots of annotations, public-static-constants, getters and setters, and even with the project structure on the left, it’s not easy to guess what the model fields are.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DaW7rQ8aBP8dYokZjTvEDQ.png" /></figure><p>In contrast, if we change generation language from java to groovy, this is what we’ll see:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*ay9ms5RjAQyDlRQWYDJ5ug.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7-OKpMpr7VgsP_FPoPW0hA.png" /></figure><p>Groovy sources are very compact, and the compiler does lots of work for you out of the box, such as multiple constructors, toString &amp; hashCode, getter-setters, and many other things. In order to make our generated classes more readable, we can use Groovy models instead of Java models. But we need to keep Java invokers (e.g., ApiClient) because we can’t use Groovy with RestTemplate that we configured. To achieve this, we will:</p><ul><li>generate two clients — for Java and Groovy, with identical parameters.</li></ul><pre>[generateGroovyModels, generateJavaInvoker].each {<br>    it.inputSpec = &quot;$rootDir/api-schema.yaml&quot;<br>    it.outputDir = &quot;$buildDir/generated&quot;<br>    it.apiPackage = &quot;com.example.apiclient.api&quot;<br>    it.invokerPackage = &quot;com.example.apiclient.invoker&quot;<br>    it.modelPackage = &quot;com.example.apiclient.model&quot;<br>}</pre><ul><li>in groovy client, we will delete “…apiclient/api” package</li></ul><pre>task generateGroovyModels(<br>        type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask,<br>        group: &#39;openapi tools&#39;<br>){<br>    generatorName = &quot;groovy&quot;<br>    doLast {<br>        delete &quot;build/generated/src/main/groovy/com/example/apiclient/api&quot;<br>    }<br>}</pre><ul><li>in Java client, we will delete verbose “…apiclient/model” package and copy the rest into groovy namespace.</li></ul><pre>task generateJavaInvoker(<br>        type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask,<br>        group: &#39;openapi tools&#39;<br>){<br>    generatorName = &quot;java&quot;<br>    library = &#39;resttemplate&#39;<br>    doLast {<br>        delete &quot;build/generated/src/main/java/com/example/apiclient/model&quot;<br>        copy {<br>            from &#39;build/generated/src/main/java&#39;<br>            into &quot;build/generated/src/main/groovy&quot;<br>        }<br>    }<br>}</pre><ul><li>we will put all our test code (java and groovy classes) under /groovy namespace. Groovy compiler knows how to compile both groovy and java classes</li><li>last, we’ll need to make a small compilation change in tests when creating request object</li></ul><pre>// before - with Java&#39;s generated &quot;chained-setters&quot;<br>def response = apiClient.createRecipe(new RecipeRequest()<br>                .name(&quot;Man sushi&quot;).instructions(&quot;Call sushi bar&quot;)<br>                .ingredients([&quot;Phone&quot;, &quot;Sushi bar&quot;, &quot;Money&quot;])<br>)<br>// after - with Groovy&#39;s all-args-constructor with named params<br>def response = apiClient.createRecipe(new RecipeRequest(<br>        name: &quot;Man sushi&quot;,<br>        instructions: &quot;Call sushi bar&quot;,<br>        ingredients: [&quot;Phone&quot;, &quot;Sushi bar&quot;, &quot;Money&quot;]<br>))<br><br>def patchResponse = apiClient.updateRecipe(<br>      recipeId, new RecipeRequest(name: &quot;Lazy sushi&quot;)<br>)</pre><p>This change is not merged into main branch because some people can think that this is an overkill — but you can see the diff here: <a href="https://github.com/tkachenkoas/cleaner-int-tests/pull/6/files">https://github.com/tkachenkoas/cleaner-int-tests/pull/6/files</a></p><h3>Summary and general thoughts</h3><p>In conclusion, the article highlights the importance of maintaining a clean and efficient test suite for web applications. The use of generated client code and Groovy as a test language can greatly simplify the testing process and ensure stronger type checking of requests and responses. The article emphasizes the benefits of testing only selected layers of the application, which can speed up the testing process while still providing thorough coverage. The use of Groovy models can also make generated code more readable and easier to work with. Overall while test code may not be running in a production environment, it is still important to maintain it in good shape and apply best practices for clean and efficient code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc3ac880d9ec" width="1" height="1" alt=""><hr><p><a href="https://medium.com/duda/cleaner-spring-boot-it-rest-tests-with-client-generation-cc3ac880d9ec">Cleaner Spring Boot IT Rest tests with client generation</a> was originally published in <a href="https://medium.com/duda">Duda</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>