<?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[Stories by Daniel Willian on Medium]]></title>
        <description><![CDATA[Stories by Daniel Willian on Medium]]></description>
        <link>https://medium.com/@danielwilliansc?source=rss-a6217f88c40d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*38TipfsKhqabUiUNm-0VCw.jpeg</url>
            <title>Stories by Daniel Willian on Medium</title>
            <link>https://medium.com/@danielwilliansc?source=rss-a6217f88c40d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 01:20:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@danielwilliansc/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Executable Specifications Beyond Acceptance Tests]]></title>
            <link>https://danielwilliansc.medium.com/executable-specifications-beyond-acceptance-tests-2b3f86c30ab4?source=rss-a6217f88c40d------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b3f86c30ab4</guid>
            <category><![CDATA[test-automation]]></category>
            <category><![CDATA[continuous-integration]]></category>
            <category><![CDATA[test-driven-development]]></category>
            <category><![CDATA[software-testing]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Daniel Willian]]></dc:creator>
            <pubDate>Mon, 10 Mar 2025 10:58:09 GMT</pubDate>
            <atom:updated>2025-03-10T11:02:47.966Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*Pi8vj0T4l_3VdYWnPp8HMw.png" /><figcaption>Four-layer approach for executable specification</figcaption></figure><p><a href="https://www.linkedin.com/in/dave-farley-a67927">Dave Farley</a>’s videos on <strong>acceptance testing</strong> and <strong>deployment pipelines</strong> changed how I think about testing. Even though they were posted over four years ago, they remain highly relevant today.</p><p>One of the biggest takeaways was Executable Specifications, particularly <a href="https://www.youtube.com/watch?v=JDD5EEJgpHU">Dave Farley’s brilliant Four-Layer Approach</a> to writing them, which is most often discussed in the context of acceptance tests. But why stop there? What if we reused the same test cases across unit, integration, and acceptance tests to gain confidence earlier in the pipeline?</p><p>TL;DR: Running Executable Specifications at unit and integration levels provides the<strong> highest confidence</strong> and <strong>fastest feedback</strong> possible from <strong>commit time</strong> to <strong>test completion</strong>, long before acceptance tests run. While acceptance tests offer the highest overall confidence, they are slower and more expensive to run. By validating the same business rules earlier, we catch issues sooner, reduce debugging time, and prevent costly late-stage failures. The trade-off is that maintaining multiple test layers requires effort, but if fast feedback and early confidence matter, this is one of the best investments you can make in your deployment pipeline.</p><h4>Defining Some Terms</h4><p>People define these concepts differently, so here’s what I mean when I refer to them:</p><ul><li><strong>Unit tests</strong>: These test only your own code, using mocks or test doubles when needed. The number of classes tested doesn’t matter, as long as there are no external dependencies (like databases or frameworks), <strong>they remain fast</strong>.</li><li><strong>Integration tests</strong>: These test interactions with third-party code, such as databases, libraries, or frameworks.</li><li><strong>Acceptance tests</strong>: <a href="https://youtu.be/knB4jBafR_M?t=165">As Dave Farley describes</a>, these verify <strong>business rules</strong> in a <strong>production-like environment</strong>.</li><li><strong>Executable Specification (ES)</strong>: There are many definitions for it, but I’ll refer to ES specifically as <a href="https://www.youtube.com/watch?v=JDD5EEJgpHU">Dave Farley’s Four-Layer approach</a>, a brilliant model for writing and reusing tests aimed at verifying business rules. It separates test logic from system implementation by using clear test cases, a readable abstraction (DSL), a protocol for interacting with the system (e.g., HTTP, direct calls), and the actual system under test. This structure allows tests to be reused, targeting different entry points of a system through different protocols, but also targeting different systems, which we will explore later.</li><li><strong>Deployment pipeline</strong>: a pipeline that goes from commit to releasable code, <a href="https://youtu.be/x9l6yw1PFbs?t=133">as Dave Farley describes</a>. In this post, I’ll focus on the <strong>commit phase</strong>, where tests must be <strong>fast</strong> but <strong>reliable</strong> and provide <strong>high confidence</strong> for the release candidate.</li></ul><h3>Why Limit ES to Acceptance Tests?</h3><p>From what I’ve seen, Dave Farley primarily discusses ES in the context of acceptance tests. While he may have addressed their use at other levels, I haven’t come across it and I believe that limiting ES to acceptance tests is a missed opportunity. Acceptance tests provide the highest confidence in business rules before deployment, but they happen much later in the pipeline. The same principles can be applied earlier for faster feedback and the highest confidence possible from commit time to test completion.</p><p>We can apply ES in <strong>integration tests</strong> by spinning up test containers for databases, APIs, and external services while running a test version of our system locally within the test environment. This setup allows us to validate functional requirements without needing a full production-like environment. These tests provide the <strong>best confidence we can get for business rules without a deployment</strong>, often completing in just a few minutes. The protocol layer can even be reused as you interface with your system just like you would with a live environment.</p><p>Even when ES is applied in integration tests, applying it to <strong>unit tests</strong> is valuable. Here, we initialize the system under test much like we would in a dependency injection framework, but we replace external dependencies with test doubles (mocks, fakes).<br>In unit tests, the protocol layer, instead of HTTP requests to WireMock, sets expectations on mocks. Instead of interacting with the system via an API request, we directly invoke the responsible methods.<br>These tests provide the <strong>best possible confidence in business rules at unit test speed</strong>, ensuring that even before integration tests run, we already have high confidence in our changes. When paired with lightweight integration tests, this combination offers the <strong>highest confidence possible in seconds</strong>, making it a perfect candidate for fast feedback before committing changes.</p><h3>Conclusion</h3><p>Running Executable Specifications at the integration or even unit test level provides <strong>the best possible confidence in business rules at the earliest stage of the pipeline</strong>.</p><p>Unit tests complete <strong>almost immediately</strong> after commit, offering <strong>the fastest feedback</strong>. Integration tests take longer due to container and service initialization but provide <strong>greater</strong> <strong>confidence</strong>. Acceptance tests take the longest, as they require full deployment, but they offer <strong>the highest overall confidence before release</strong>.</p><p>By validating the same business rules earlier in the pipeline, we catch issues sooner, reduce debugging time, and prevent costly failures later on. This ensures that feedback is received at the fastest possible stage, allowing developers to make corrections before issues escalate.</p><p>Maintaining multiple test layers requires effort, and if ES in acceptance tests is already efficient, running them at lower levels might not add significant value. But for teams that prioritize fast feedback and early confidence, bringing these tests earlier in the pipeline is one of the smartest investments you can make in your deployment pipeline.</p><p>What Do You Think? Do you use Executable Specifications in your testing strategy? At what layer do you run them, and why?</p><p>I’d love to hear your thoughts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b3f86c30ab4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Winning Strategy for Unit Testing]]></title>
            <link>https://danielwilliansc.medium.com/winning-strategy-for-unit-testing-1fbb8bcdb972?source=rss-a6217f88c40d------2</link>
            <guid isPermaLink="false">https://medium.com/p/1fbb8bcdb972</guid>
            <category><![CDATA[domain-driven-design]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[test-driven-development]]></category>
            <dc:creator><![CDATA[Daniel Willian]]></dc:creator>
            <pubDate>Fri, 05 Jul 2024 02:14:47 GMT</pubDate>
            <atom:updated>2024-07-05T02:14:47.289Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*DK3z-Tmo7Ms1YXG4qh2E5g.png" /></figure><p>Testing is a powerful tool to assert the correctness of a system. Although time-consuming, it is usually worth the effort to maintain system stability. Unit testing is a part of the testing process and is sometimes overused, with highly detailed tests and mocks that, if not careful, end up merely confirming the code you wrote is indeed the code you wrote.</p><h4>What is a Unit Test?</h4><p>Unit test boundaries may shift depending on whom you ask. Some may say it tests a single method, others a single class, or even some classes required to fulfill a requirement of the system. I believe it falls somewhere in between — not as low as a single method but not as high as loading the whole application to check if an API request results in something saved to the database. The appropriate boundaries for unit tests, in my opinion, are any combination of classes that do not require setup for third-party code or systems. Need to spin up Spring Boot to run the test? Then it is not a unit test. Need to connect to a database? Then it is not a unit test. Only need to orchestrate some of your classes? Then it is a unit test. I think this for a few reasons:</p><ul><li><strong>Speed</strong>: Unit tests should be fast. Instantiating classes is quick. Even if a test needs 100 objects (it should never need that many), it is at least an order of magnitude faster than initializing a framework or connecting to an external system.</li><li><strong>Behavior Verification</strong>: The test should verify the behavior of the unit under test. Internal refactors should not break the test if they do not change the behavior. Some refactors might move methods around, delegate computations to other classes, or consolidate common behavior in a utility class. If unit tests were restricted to a single method or class, extensive mocking would be needed, and most tests would just assert that the code you wrote is indeed the code you wrote.</li></ul><h4>When to Write a Unit Test?</h4><p>Tests guarantee the behavior of a system. As the code changes, tests assert that the system’s intended behavior has not changed. Additionally, before they are completed, tests can guide the design of the code, making it possible and effective to test. This is powerful and one of the main benefits of Test-Driven Development (TDD).</p><p>Writing tests before any code helps to keep the requirements in mind and think of the necessary interfaces to fulfill those requirements, without worrying about implementation details.</p><h4>What to Write in a Unit Test?</h4><p>In software testing, there are concepts of black box testing and white box testing:</p><ul><li><strong>Black Box Tests</strong>: Treat the subject under test as a black box, only observing the behavior without worrying about implementation details.</li><li><strong>White Box Tests</strong>: Have knowledge of the implementation details, often asserting that certain internal states were achieved or specific computations occurred during the test.</li><li><strong>Gray Box Tests</strong>: A mix of the two approaches, including assertions on behavior and internal state.</li></ul><p><strong>Write tests as close to black box as possible, including unit tests</strong>. Because unit tests are so close to the implementation, it is tempting to validate the internal parts of the system, especially protected or private methods, but you should resist this temptation for a few reasons:</p><ol><li><strong>Brittleness</strong>: Tests become brittle, failing due to changes in internal details even if the behavior remains unchanged.</li><li><strong>Unused Code</strong>: Tests might execute code paths that are otherwise unreachable in normal execution. If some internal part of the test subject can’t be reached by its public interface, it will never run outside of tests. In such cases, the code should be deleted, so no need to test it.</li><li><strong>Ease of Writing Tests</strong>: Tests for validating the interfaces are easier to write, especially before any code is written for the functionality, as TDD suggests.</li></ol><h4>How to Design Code with Testing in Mind</h4><p>Applications have various concerns, with business logic being the most important. However, they also need to consider performance, observability, and consistency, to name a few. Often, these concerns are handled by third-party libraries or systems, with only configuration on the application side (e.g., frameworks, database calls, third-party APIs, message brokers). Let’s call these code and configuration infrastructure code.</p><p><strong>Do not write unit tests for infrastructure code.</strong> Integration tests are more suitable for these interactions. If you don’t spin up a database, message broker, or third-party server, as suggested, unit tests for these would merely verify that you called the library in a particular way, which is not very helpful. Assert your configuration and calls with a real connection to your dependency, even if it is an in-memory database or a simulator for third-party systems. With <a href="https://testcontainers.com/">Testcontainers</a>, this is easier than ever.</p><p>It is crucial to write code in a testable way. Mixing infrastructure code with business logic means that business logic can only be tested in integration tests or with complicated mocks. However, this is unnecessary; business logic does not require any third-party code, so unit tests are sufficient. Separating business logic from infrastructure code is necessary for effective unit testing.</p><p>This approach aligns with the layered architecture of Domain-Driven Design (DDD). The system should be divided into layers: the domain layer (full of business logic) should contain as few third-party dependencies as possible and can thus be highly unit tested. The application layer is responsible for use cases and interfacing the necessary I/O with the domain to keep it isolated.</p><p><strong>Code with business logic should not perform any I/O and should avoid invoking methods that perform I/O.</strong> I/O should be built around the business logic. Let me demonstrate:</p><p>Consider a delivery application where the system needs to move an existing order to an order ready to be delivered, polling an external system depending on the product being ordered to check if it is ready. Doing everything in a single class might look like this:</p><pre>class OrderControllerAllInOne(<br>    private val jdbcTemplate: JdbcTemplate,<br>    @Value(&quot;\${api.grocery.url}&quot;) private val groceryUrl: String,<br>    @Value(&quot;\${api.supermarket.url}&quot;) private val supermarketUrl: String<br>) {<br>  @PostMapping(&quot;/{id}/all-in-one-set-ready&quot;)<br>  fun setOrderReady(@PathVariable id: Long): ResponseEntity&lt;Order&gt; {<br>    val order =<br>        jdbcTemplate.query(<br>            &quot;SELECT order_id, status, product FROM orders WHERE order_id = ?&quot;,<br>            PreparedStatementSetter { it.setLong(1, id) },<br>            ResultSetExtractor { toOrder(it) })<br>    if (order == null) return ResponseEntity.notFound().build()<br><br>    val response =<br>        when (order.product) {<br>          Product.APPLE -&gt; &quot;$groceryUrl/apples/$id&quot;.httpGet()<br>          Product.BANANA -&gt; &quot;$supermarketUrl/bananas/$id&quot;.httpGet()<br>        }<br>    if (response.code != 200) return ResponseEntity.ok(order)<br><br>    val updatedOrder = order.copy(status = OrderStatus.READY)<br><br>    jdbcTemplate.update(&quot;UPDATE orders SET status = ? WHERE order_id = ?&quot;) {<br>      it.setString(1, updatedOrder.status.toString())<br>      it.setLong(2, updatedOrder.orderId.id)<br>    }<br><br>    return ResponseEntity.ok(updatedOrder)<br>  }<br><br>  private fun toOrder(resultSet: ResultSet): Order? {<br>    resultSet.use {<br>      if (!resultSet.next()) return null<br><br>      val orderId = resultSet.getLong(&quot;order_id&quot;)<br>      val status = resultSet.getString(&quot;status&quot;)<br>      val product = resultSet.getString(&quot;product&quot;)<br>      return Order(OrderId(orderId), Product.valueOf(product), OrderStatus.valueOf(status))<br>    }<br>  }<br>}</pre><p>This code is hard to test without external systems. The HTTP request is sent to a configured URL, so something needs to respond to it. Mocking the database interaction would be a lot of work, just to test if the query you are sending is correct.</p><p>Here, the business logic is deciding which system to call depending on the product and whether the order should be set to ready depending on the response. I decided to add these to the Order class itself since it is related to it.</p><pre>enum class Product {<br>  APPLE,<br>  BANANA<br>}<br><br>data class OrderId(val id: Long)<br><br>enum class OrderStatus {<br>  CREATED,<br>  READY<br>}<br><br>class SupplierResponse(val code: Int)<br><br>data class Order(val orderId: OrderId, val product: Product, val status: OrderStatus) {<br>  fun getProductSupplier(): ProductSupplier =<br>      when (product) {<br>        Product.APPLE -&gt; ProductSupplier.GROCERY<br>        Product.BANANA -&gt; ProductSupplier.SUPERMARKET<br>      }<br><br>  fun trySetOrderToReady(response: SupplierResponse): Order =<br>      if (response.code == 200) copy(status = OrderStatus.READY) else this<br>}</pre><p>These methods are easily unit tested, creating an order in a particular state and asserting the response. Tests were created using <a href="https://kotest.io/">Kotest</a>.</p><pre>class OrderTest :<br>    StringSpec({<br>      &quot;Order for Apples has grocery supplier&quot; {<br>        val order = Order(OrderId(1), Product.APPLE, OrderStatus.CREATED)<br>        val supplier = order.getProductSupplier()<br>        supplier shouldBe ProductSupplier.GROCERY<br>      }<br><br>      &quot;Order for Bananas has supermarket supplier&quot; {<br>        val order = Order(OrderId(1), Product.BANANA, OrderStatus.CREATED)<br>        val supplier = order.getProductSupplier()<br>        supplier shouldBe ProductSupplier.SUPERMARKET<br>      }<br><br>      &quot;Order remains created after a non 200 response from supplier&quot; {<br>        val order = Order(OrderId(1), Product.BANANA, OrderStatus.CREATED)<br>        val updatedOrder = order.trySetOrderToReady(SupplierResponse(404))<br>        updatedOrder.status shouldBe OrderStatus.CREATED<br>      }<br><br>      &quot;Order ready after a 200 response from supplier&quot; {<br>        val order = Order(OrderId(1), Product.BANANA, OrderStatus.CREATED)<br>        val updatedOrder = order.trySetOrderToReady(SupplierResponse(200))<br>        updatedOrder.status shouldBe OrderStatus.READY<br>      }<br>    })</pre><p>The wiring logic may go into an OrderService.</p><pre>class OrderServiceImpl(<br>    private val repository: OrderRepository,<br>    private val supplierService: SupplierService<br>) : OrderService {<br>  override fun trySetOrderToReady(orderId: OrderId): Order {<br>    val order =<br>        repository.getOrder(orderId)<br>            ?: throw OrderNotFoundException(&quot;Could find order with id: ${orderId.id}&quot;)<br>    val productSupplier = order.getProductSupplier()<br>    val response = supplierService.getProductStatus(orderId, productSupplier) ?: return order<br>    val updatedOrder = order.trySetOrderToReady(response)<br>    if (order == updatedOrder) return order<br>    repository.updateOrder(updatedOrder)<br>    return updatedOrder<br>  }<br>}</pre><p>OrderRepository and SupplierService abstract the I/O operations. Notice that the business logic (Order class) is given what it needs to execute without worrying about I/O. Logs, database transactions, telemetry data, and retries could be inserted without ever impacting the business logic.</p><p>OrderService is reasonably easy to unit test as well. By mocking the I/O interfaces, scenarios can be easily set up.</p><p>The implementations for OrderRepository and SupplierService are not worth unit testing. Set up the required infrastructure and create integration tests for these classes with the database or a mocked external API if necessary. Alternatively, these implementations can be tested with OrderService calling them or with the whole system.</p><h3>Conclusion</h3><p>For effective unit testing, remember:</p><ol><li>A unit test is not only for a class or method; related classes may be grouped and tested together, but without setting up infrastructure for them.</li><li>Write tests before writing any code. This helps to focus on the requirements.</li><li>Test the interface, not the underlying implementation. Lean more towards black box testing.</li><li>Separate business logic from I/O. Create interfaces for I/O so the logic can be unit tested effectively.</li></ol><p>All code can be found on <a href="https://github.com/DanielWillian/winning-testing-strategy">GitHub</a>. Feel free to comment/discuss on this post or send me a message.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1fbb8bcdb972" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Observability with OpenTelemetry]]></title>
            <link>https://danielwilliansc.medium.com/observability-with-opentelemetry-6a8de88e042f?source=rss-a6217f88c40d------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a8de88e042f</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[observability]]></category>
            <category><![CDATA[opentelemetry]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[Daniel Willian]]></dc:creator>
            <pubDate>Fri, 29 Mar 2024 21:58:07 GMT</pubDate>
            <atom:updated>2024-07-05T12:11:39.680Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*5N7WeNyGMcBRcOnxERBb1g.png" /></figure><p>Observability is an important aspect of any application, especially when dealing with microservices. Seeing what happens between them helps identify issues and provides insight into their usage. <a href="https://opentelemetry.io/">OpenTelemetry</a> offers APIs and tools to instrument the application and increase overall confidence in the system.</p><h4><a href="https://opentelemetry.io/docs/concepts/signals/traces/">Traces</a></h4><p>Traces are a sequence of spans representing an operation, each representing a step of the operation.</p><p>The most interesting feature of it is the propagation of traces, with it you may observe an operation through all the microservices and identify bottlenecks and areas of improvement.</p><h4><a href="https://opentelemetry.io/docs/concepts/signals/metrics/">Metrics</a></h4><p>While traces allow a more low-level description of each operation, metrics are used to have a better overview of the system as it combines individual measurements into aggregates.</p><h4><a href="https://opentelemetry.io/docs/concepts/signals/logs/">Logs</a></h4><p>OpenTelemetry provides tools to enhance logs from existing popular log frameworks (SLF4J, Logback, and others) with the OpenTelemetry ecosystem. Logs may enhanced with context information from traces and forwarded to the collector or the telemetry backend.</p><h3>OpenTelemetry in Java</h3><p>OpenTelemetry provides an API, SDK, and tools to help generate and collect these telemetry data. You may use an agent to generate them automatically without any code, instrument the code yourself, or both.</p><h4>Java agent</h4><p>The <a href="https://github.com/open-telemetry/opentelemetry-java-instrumentation">OpenTelemetry Java agent</a> collects general metrics automatically, like JVM memory and CPU usage, and generates traces with spans from popular frameworks, see this <a href="https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/docs/supported-libraries.md">list</a> for the supported ones. It is <a href="https://opentelemetry.io/docs/languages/java/automatic/configuration/">highly configurable</a> and telemetry data may be exported in different formats, by default it is the <a href="https://opentelemetry.io/docs/specs/otlp/">OpenTelemetry Protocol (OTLP)</a>.</p><h4>Manual instrumentation</h4><p>There’s an API library to add manual instrumentation. Instrumentation should only be added through the API. For the API to function it must be <a href="https://opentelemetry.io/docs/languages/java/instrumentation/#initialize-the-sdk">initialized by the SDK</a> once in your application, which may also be <a href="https://opentelemetry.io/docs/languages/java/instrumentation/#automatic-configuration">autoconfigured</a> based on environment variables.</p><p>Classes wanting to instrument may be injected with an <strong><em>OpenTelemetry</em></strong> object and create the Traces and Metrics from there. When doing it manually, context propagation also needs to be done manually.</p><p>There’s a <a href="https://opentelemetry.io/docs/languages/java/automatic/annotations/">middle ground</a> between fully manual and fully automatic instrumentation which is to annotate the methods you want to create spans with annotations from the annotations library, the agent recognizes these annotations and creates spans around the methods.</p><h3><strong>OpenTelemetry backend</strong></h3><p>There are several open-source and commercial backends to choose from, they may have OTLP support out of the box or at least some parts of it, and when that is the case data can be sent directly to them.</p><p>A <a href="https://opentelemetry.io/docs/collector/">collector</a> may be used between the application and the backend to unburden the application. It may filter telemetry data and export it to multiple backend endpoints. There are several deployment methods with their pros and cons, more details are in the <a href="https://opentelemetry.io/docs/collector/deployment/">documentation</a>.</p><h3>Conclusion</h3><p>OpenTelemetry provides vendor-agnostic API, SDK, and tools to instrument your code. This allows flexibility in handling telemetry data, not only to change vendors in production but also to be able to explore it locally or in dev environments.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a8de88e042f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Idempotency in event processing]]></title>
            <link>https://danielwilliansc.medium.com/idempotency-in-event-processing-744b2b19fb55?source=rss-a6217f88c40d------2</link>
            <guid isPermaLink="false">https://medium.com/p/744b2b19fb55</guid>
            <category><![CDATA[domain-driven-design]]></category>
            <category><![CDATA[event-driven-architecture]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[quarkus]]></category>
            <dc:creator><![CDATA[Daniel Willian]]></dc:creator>
            <pubDate>Wed, 31 Jan 2024 18:39:25 GMT</pubDate>
            <atom:updated>2024-01-31T19:16:31.743Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*Qh52neUoHrb2JenVDKdbrQ.png" /></figure><p>In <a href="https://danielwilliansc.medium.com/exploring-domain-events-implementations-d389538e2385">my previous blog</a>, I presented how you could implement Domain Events using an outbox pattern to guarantee at least once delivery in the processing of events. In that particular implementation, the publishing and consumption of events happened on the same transaction, guaranteeing exactly once processing.</p><p>Alternatively, you may use a message broker for delivering these events, when that is the case, you need more to guarantee exactly once processing as publishers may fail to delete the message from the outbox after publishing to the message broker and consumers may complete their processing then fail to reach the message broker to acknowledge successful processing of the event. In this scenario, consumers need to be idempotent.</p><h4>Idempotency</h4><p>To recognize duplicate events, the events need to have a unique identifier associated with them. Luckily, events have an <em>eventId</em> field that holds this identifier. In the same transaction, after processing an event, its ID is also saved to the database in a separate table. An idempotency repository was created to save and retrieve the IDs.</p><pre>public interface IdempotencyRepository {<br>    Optional&lt;Idempotency&gt; getIdempotency(IdempotencyId id);<br>    void saveIdempotency(IdempotencyId id);<br>}</pre><pre>public class IdempotencyId {<br>    private final String id;<br>}</pre><pre>public class Idempotency {<br>    private final IdempotencyId idempotencyId;<br>    private final LocalDateTime creationDate;<br>}</pre><p>To identify the event has been processed it suffices to store its ID in the database, a date is stored with the ID to make it easier to clean up old records. If we had to keep any result of the processing, say that instead of processing an event, we were handling an HTTP request, a payload may be stored alongside the ID to respond to the request, but it is not needed here.</p><p>Before processing an event, a consumer checks if the event’s ID has already been saved to the idempotency table, halting processing if it is there.</p><pre>public void process(DomainEvent event) {<br>    IdempotencyId idempotencyId = IdempotencyId.of(event.getEventId().getId());<br>    if (idempotencyRepository.getIdempotency(idempotencyId).isPresent()) {<br>        log.info(&quot;Already processed event: {}&quot;, event);<br>        return;<br>    }<br><br>    // event processing<br><br>    idempotencyRepository.saveIdempotency(idempotencyId);<br>}</pre><h4>Demonstration</h4><p>To demonstrate a failure to acknowledge the message does not lead to processing more than once, the message broker listener has a chance to throw an exception after the transaction:</p><pre>@Incoming(&quot;user-created&quot;)<br>@Blocking<br>@Retry<br>public void userCreated(EventMessage message) {<br>    QuarkusTransaction.requiringNew().run(() -&gt; processEvent(Topic.USER_CREATED, message));<br><br>    if (Math.random() &gt;= 0.5) throw new RuntimeException(&quot;Simulated error after transaction&quot;);<br>}</pre><p>The application uses <a href="https://quarkus.io/">Quarkus</a> and it uses <a href="https://kafka.apache.org/">Kafka</a> as the message broker, by running <em>$ ./gradlew quarkusDev</em> Quarkus stands up a Kafka server and the application for us.</p><p>The following API call was sent to the server:</p><pre>$ curl -H &quot;Content-Type: application/json&quot; -d &#39;{&quot;name&quot;:&quot;James Smith&quot;}&#39; localhost:8080/users/outbox-immediate</pre><p>Logs show that even if the message gets consumed twice, the application recognizes it already processed the event, guaranteeing exactly once processing.</p><pre>     [org.dom.eve.inf.UserRepositoryImpl] (executor-thread-3) Creating user: User(userId=UserId(id=ac56d6fb-24d3-4cd2-91c0-f636d782c44c), name=James Smith)<br>[org.dom.eve.app.OutboxServiceImpl] (executor-thread-3) Saving outbox message: OutboxMessage(eventId=EventId(id=a2f1c339-7ad0-42d8-b641-0c16835b09dd), topic=USER_CREATED, message={&quot;@class&quot;:&quot;org.domain.events.application.message.UserCreatedMessage&quot;,&quot;messageId&quot;:&quot;a2f1c339-7ad0-42d8-b641-0c16835b09dd&quot;,&quot;userId&quot;:&quot;ac56d6fb-24d3-4cd2-91c0-f636d782c44c&quot;,&quot;name&quot;:&quot;James Smith&quot;}, updateDate=2024-01-29T00:51:30.331432086)<br>[org.dom.eve.inf.EventChannels] (executor-thread-1) Publishing message: UserCreatedMessage(userId=ac56d6fb-24d3-4cd2-91c0-f636d782c44c, name=James Smith)<br>[org.dom.eve.app.OutboxServiceImpl] (executor-thread-1) Deleting outbox message with id: EventId(id=a2f1c339-7ad0-42d8-b641-0c16835b09dd)<br>[org.dom.eve.inf.EventChannels] (vert.x-worker-thread-1) Consuming message: UserCreatedMessage(userId=ac56d6fb-24d3-4cd2-91c0-f636d782c44c, name=James Smith)<br>[org.dom.eve.dom.LoggingListener] (vert.x-worker-thread-1) Processing event: UserCreatedEvent(user=User(userId=UserId(id=ac56d6fb-24d3-4cd2-91c0-f636d782c44c), name=James Smith))<br>[org.dom.eve.inf.ide.IdempotencyRepositoryImpl] (vert.x-worker-thread-1) Saving idempotency, id: a2f1c339-7ad0-42d8-b641-0c16835b09dd<br>     [org.dom.eve.inf.EventChannels] (vert.x-worker-thread-1) Consuming message: UserCreatedMessage(userId=ac56d6fb-24d3-4cd2-91c0-f636d782c44c, name=James Smith)<br>[org.dom.eve.dom.LoggingListener] (vert.x-worker-thread-1) Already processed event: UserCreatedEvent(user=User(userId=UserId(id=ac56d6fb-24d3-4cd2-91c0-f636d782c44c), name=James Smith))</pre><h4>Conclusion</h4><p>Using an outbox pattern and an idempotency repository guarantees exactly once processing of domain events, improving the reliability of the system. Some implementation details were left out of the blog so be free to explore the code on <a href="https://github.com/DanielWillian/idempotent-events">GitHub</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=744b2b19fb55" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploring Domain Events implementations]]></title>
            <link>https://danielwilliansc.medium.com/exploring-domain-events-implementations-d389538e2385?source=rss-a6217f88c40d------2</link>
            <guid isPermaLink="false">https://medium.com/p/d389538e2385</guid>
            <category><![CDATA[event-driven-architecture]]></category>
            <category><![CDATA[domain-driven-design]]></category>
            <category><![CDATA[quarkus]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Daniel Willian]]></dc:creator>
            <pubDate>Sun, 31 Dec 2023 15:08:44 GMT</pubDate>
            <atom:updated>2023-12-31T15:08:44.303Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*-7maht-XZ2IPXsTLol3XUA.png" /></figure><p>In <a href="https://en.wikipedia.org/wiki/Domain-driven_design">Domain Driven Design</a>, aggregates are the boundary for consistency. When you need to update multiple aggregates you should rely on eventual consistency. Domain Events are a way to achieve this. They represent something that happened in the system that is valuable to other parts of the system. Interested parties can listen to these events and take the appropriate measures. There are a couple of options for implementing domain events, this blog presents some of these approaches.</p><h4>Publishing and subscribing</h4><p>I will not go over in detail the ways of publishing the events, be it by using a lightweight publish-subscribe or a specific message broker. For this blog, publishing and subscribing to events will be done through a simple <a href="https://en.wikipedia.org/wiki/Observer_pattern">observer</a>.</p><p>Subscribers would need to implement <em>EventListener</em>:</p><pre>@FunctionalInterface<br>public interface EventListener&lt;T extends DomainEvent&gt; {<br>    void process(T event);<br>    @SuppressWarnings(&quot;unchecked&quot;)<br>    default void processEvent(DomainEvent event) {<br>        process((T) event);<br>    }<br>}</pre><p>A simple logging subscriber will be set up:</p><pre>@Slf4j<br>public class LoggingListener implements EventListener&lt;DomainEvent&gt; {<br>    @Override<br>    public void process(DomainEvent event) {<br>        log.info(&quot;Processing event: {}&quot;, event);<br>    }<br>}</pre><p>And be set to listen to a specific topic:</p><pre>public enum Topic {<br>    USER_CREATED<br>}</pre><pre>public interface EventSubscriber {<br>    void addListener(Topic topic, EventListener&lt;?&gt; listener);<br>}</pre><pre>addListener(Topic.USER_CREATED, new LoggingListener());</pre><p>A publisher will publish events:</p><pre>public interface EventPublisher {<br>    void publish(Topic topic, DomainEvent event);<br>}</pre><pre>@Singleton<br>@Slf4j<br>public class EventChannels implements EventSubscriber, EventPublisher {<br>    private final Map&lt;Topic, List&lt;EventListener&lt;?&gt;&gt;&gt; listeners;<br><br>    public EventChannels() {<br>        listeners = new EnumMap&lt;&gt;(Topic.class);<br>        addListener(Topic.USER_CREATED, new LoggingListener());<br>    }<br><br>    public EventChannels(Map&lt;Topic, List&lt;EventListener&lt;?&gt;&gt;&gt; listeners) {<br>        this.listeners = new EnumMap&lt;&gt;(listeners);<br>    }<br><br>    @Override<br>    public void addListener(Topic topic, EventListener&lt;?&gt; listener) {<br>        if (!listeners.containsKey(topic)) listeners.put(topic, new ArrayList&lt;&gt;());<br>        listeners.get(topic).add(listener);<br>    }<br><br>    @Override<br>    public void publish(Topic topic, DomainEvent event) {<br>        if (!listeners.containsKey(topic)) return;<br><br>        log.info(&quot;Publishing event: {}&quot;, event);<br><br>        for (EventListener&lt;?&gt; listener : listeners.get(topic)) {<br>            listener.processEvent(event);<br>        }<br>    }<br>}</pre><h4>Best effort</h4><p>A naive approach to publishing the event would be to do it without considering failures.</p><pre>public interface UserService {<br>    User createUserBestEffort(String name);<br>}</pre><pre>    @Transactional<br>    @Override<br>    public User createUserBestEffort(String name) {<br>        User user = userFactory.createUser(name);<br>        userRepository.saveUser(user);<br><br>        UserCreatedEvent userCreatedEvent = UserCreatedEvent.of(user);<br>        eventPublisher.publish(Topic.USER_CREATED, userCreatedEvent);<br><br>        return user;<br>    }</pre><p>Doing it this way, there are no guarantees that the changes to the database and publishing of the event will happen when the other does.</p><p>If you simply publish the event before committing the transaction, there’s a chance the transaction fails and the message gets sent without anything happening on the database. Or if the publishing happens after the transaction is committed, the message may fail to be sent even when the changes already happened.</p><p>While these failures may not happen often, when they do, they leave the system in an inconsistent state.</p><h4>Outbox Pattern</h4><p>One way to guarantee at least once delivery would be to use an <a href="https://en.wikipedia.org/wiki/Inbox_and_outbox_pattern#The_outbox_pattern">outbox</a>. This pattern saves both the original changes and the information required by the message to the database in the same transaction. Later, the message can be retrieved from the outbox and sent.</p><p>This guarantees at least once delivery of the event because as the message will only be marked as processed or deleted from the outbox after the message gets sent, there’s a chance of failure when deleting after the message is sent so it may be sent multiple times. To guarantee exactly once, consumers would need to be idempotent.</p><p>So instead of publishing the event directly, we save the event on the outbox in the same transaction.</p><pre>public interface UserService {<br>    User createUserOutboxPoll(String name);<br>}</pre><pre>    @Transactional<br>    @Override<br>    public User createUserOutboxPoll(String name) {<br>        User user = userFactory.createUser(name);<br>        userRepository.saveUser(user);<br><br>        UserCreatedEvent userCreatedEvent = UserCreatedEvent.of(user);<br>        outboxService.saveEvent(Topic.USER_CREATED, userCreatedEvent);<br><br>        return user;<br>    }</pre><h4>Multiple pollers</h4><p>In this implementation, a single process may pick up messages once, but when multiple pollers are in place messages may be picked up before others get to update, and the message may be processed more than once potentially.</p><p>Ownership of the message alleviates this problem, to each process is given a name, and the name gets saved in the same row in the outbox. Pollers query their messages at the regular interval but query messages from other owners at a much higher interval, enough time for the owner to have a chance to pick up its message. This covers the scenario of owners dying before they get to send their message.</p><h4>Retries</h4><p>Retry count may also be saved on the outbox, this may be used to exponentially back off the message, for example, instead of updating the last executed time to now it may be saved to the future based on the retry count.</p><h4>Send messages right after the transaction</h4><p>Another approach would be to process the message right after the transaction gets committed. In this implementation, an interceptor was added right before the transactional interceptor so the message gets processed right after the transaction gets committed.</p><p>An annotation was created for the interceptor:</p><pre>@InterceptorBinding<br>@Retention(RetentionPolicy.RUNTIME)<br>@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR})<br>@Inherited<br>public @interface EarlyEventDelivery {<br>}</pre><p>The interceptor searches the returned object for outbox messages and processes them in a different thread.</p><pre>@Value<br>@AllArgsConstructor(staticName = &quot;of&quot;)<br>@Builder(builderClassName = &quot;Builder&quot;)<br>public class OutboxWrapper&lt;T&gt; {<br>    List&lt;OutboxMessage&gt; messages;<br>    T payload;<br>}</pre><pre>@EarlyEventDelivery<br>@Priority(Interceptor.Priority.PLATFORM_BEFORE + 100)<br>@Interceptor<br>public class EventInterceptor {<br>    @Inject<br>    OutboxService outboxService;<br><br>    @SneakyThrows<br>    @AroundInvoke<br>    Object sendMessages(InvocationContext context) {<br>        Object ret = context.proceed();<br>        if (ret instanceof OutboxMessage message) {<br>            deliverMessage(message);<br>        }<br>        if (ret instanceof Collection&lt;?&gt; collection) {<br>            for (Object o : collection) {<br>                if (o instanceof OutboxMessage message) {<br>                    deliverMessage(message);<br>                }<br>            }<br>        }<br>        if (ret instanceof OutboxWrapper wrapper) {<br>            for (Object o : wrapper.getMessages()) {<br>                if (o instanceof OutboxMessage message) {<br>                    deliverMessage(message);<br>                }<br>            }<br>        }<br>        return ret;<br>    }<br><br>    private void deliverMessage(OutboxMessage message) {<br>        Uni.createFrom()<br>                .item(message)<br>                .emitOn(Infrastructure.getDefaultWorkerPool())<br>                .subscribe()<br>                .with(outboxService::deliverMessage);<br>    }<br>}</pre><p>Methods saving to the outbox may be annotated and return either the outbox message or a wrapper with the messages saved.</p><pre>public interface UserService {<br>    OutboxWrapper&lt;User&gt; createUserOutboxImmediate(String name);<br>}</pre><pre>    @EarlyEventDelivery<br>    @Transactional<br>    @Override<br>    public OutboxWrapper&lt;User&gt; createUserOutboxImmediate(String name) {<br>        User user = userFactory.createUser(name);<br>        userRepository.saveUser(user);<br><br>        UserCreatedEvent userCreatedEvent = UserCreatedEvent.of(user);<br>        OutboxMessage message = outboxService.saveEvent(Topic.USER_CREATED, userCreatedEvent);<br><br>        return OutboxWrapper.&lt;User&gt;builder()<br>                .messages(List.of(message))<br>                .payload(user)<br>                .build();<br>    }</pre><p>This alleviates the poller as when everything goes right, the message gets saved and processed almost immediately. Without it ever being polled.</p><h4>Implementation</h4><p><a href="https://quarkus.io/">Quarkus</a> is being used and APIs were set up to demonstrate these approaches. Executing <em>$ ./gradlew quarkusDev</em> creates a dev environment that has everything set up. Application logs show us what is happening on each approach.</p><h4>Best effort</h4><p>This approach seems fine when everything works, but when failure happens it may leave the system in an inconsistent state.</p><pre>$ curl -H &quot;Content-Type: application/json&quot; -d &#39;{&quot;name&quot;:&quot;James Smith&quot;}&#39; \<br>    localhost:8080/users/best-effort<br>{&quot;id&quot;:&quot;bee5b8ca-eabf-4476-9de1-c913f5607a2e&quot;,&quot;name&quot;:&quot;James Smith&quot;}</pre><pre>2023-12-30 23:21:03,505 INFO  [org.dom.eve.inf.UserRepositoryImpl] (executor-thread-1) Creating user: User(userId=UserId(id=bee5b8ca-eabf-4476-9de1-c913f5607a2e), name=James Smith)<br>2023-12-30 23:21:03,506 INFO  [org.dom.eve.inf.EventChannels] (executor-thread-1) Publishing event: UserCreatedEvent(user=User(userId=UserId(id=bee5b8ca-eabf-4476-9de1-c913f5607a2e), name=James Smith))<br>2023-12-30 23:21:03,507 INFO  [org.dom.eve.dom.LoggingListener] (executor-thread-1) Processing event: UserCreatedEvent(user=User(userId=UserId(id=bee5b8ca-eabf-4476-9de1-c913f5607a2e), name=James Smith)</pre><h4>Outbox and poll</h4><p>Processing of the message only happens later, when the poller picks up the message.</p><pre>$ curl -H &quot;Content-Type: application/json&quot; -d &#39;{&quot;name&quot;:&quot;James Smith&quot;}&#39; \<br>    localhost:8080/users/outbox-poll<br>{&quot;id&quot;:&quot;d24a8e88-0768-4afd-8360-fe5591fbca82&quot;,&quot;name&quot;:&quot;James Smith&quot;}</pre><pre>2023-12-30 23:22:10,804 INFO  [org.dom.eve.inf.UserRepositoryImpl] (executor-thread-1) Creating user: User(userId=UserId(id=d24a8e88-0768-4afd-8360-fe5591fbca82), name=James Smith)<br>2023-12-30 23:22:11,110 INFO  [org.dom.eve.app.OutboxServiceImpl] (executor-thread-1) Saving outbox message: OutboxMessage(eventId=EventId(id=8791509d-abb4-43c7-bb49-903fc27ec68a), topic=USER_CREATED, message={&quot;@class&quot;:&quot;org.domain.events.application.message.UserCreatedMessage&quot;,&quot;messageId&quot;:&quot;8791509d-abb4-43c7-bb49-903fc27ec68a&quot;,&quot;userId&quot;:&quot;d24a8e88-0768-4afd-8360-fe5591fbca82&quot;,&quot;name&quot;:&quot;James Smith&quot;}, updateDate=2023-12-31T02:22:11.109621486)<br>2023-12-30 23:22:18,078 INFO  [org.dom.eve.inf.OutboxPoller] (vert.x-worker-thread-1) Outbox poll fetched 1 messages<br>2023-12-30 23:22:18,154 INFO  [org.dom.eve.inf.EventChannels] (vert.x-worker-thread-1) Publishing event: UserCreatedEvent(user=User(userId=UserId(id=d24a8e88-0768-4afd-8360-fe5591fbca82), name=James Smith))<br>2023-12-30 23:22:18,155 INFO  [org.dom.eve.dom.LoggingListener] (vert.x-worker-thread-1) Processing event: UserCreatedEvent(user=User(userId=UserId(id=d24a8e88-0768-4afd-8360-fe5591fbca82), name=James Smith))<br>2023-12-30 23:22:18,156 INFO  [org.dom.eve.app.OutboxServiceImpl] (vert.x-worker-thread-1) Deleting outbox message with id: EventId(id=8791509d-abb4-43c7-bb49-903fc27ec68a)</pre><h4>Outbox and send the message immediately</h4><p>Processing of the message happens a little bit after getting saved, in a different thread. The poller never picks it up.</p><pre>$ curl -H &quot;Content-Type: application/json&quot; -d &#39;{&quot;name&quot;:&quot;James Smith&quot;}&#39; \<br>    localhost:8080/users/outbox-immediate<br>{&quot;id&quot;:&quot;dProcessing of the message only happens later, when the poller picks up the message.d92fd7e-e833-4892-b309-e7fb3e955d12&quot;,&quot;name&quot;:&quot;James Smith&quot;}</pre><pre>2023-12-30 23:22:28,085 INFO  [org.dom.eve.inf.UserRepositoryImpl] (executor-thread-1) Creating user: User(userId=UserId(id=dd92fd7e-e833-4892-b309-e7fb3e955d12), name=James Smith)<br>2023-12-30 23:22:28,087 INFO  [org.dom.eve.app.OutboxServiceImpl] (executor-thread-1) Saving outbox message: OutboxMessage(eventId=EventId(id=4e224630-668c-40b8-90e9-5e97ee0e04eb), topic=USER_CREATED, message={&quot;@class&quot;:&quot;org.domain.events.application.message.UserCreatedMessage&quot;,&quot;messageId&quot;:&quot;4e224630-668c-40b8-90e9-5e97ee0e04eb&quot;,&quot;userId&quot;:&quot;dd92fd7e-e833-4892-b309-e7fb3e955d12&quot;,&quot;name&quot;:&quot;James Smith&quot;}, updateDate=2023-12-31T02:22:28.087703373)<br>2023-12-30 23:22:28,177 INFO  [org.dom.eve.inf.EventChannels] (executor-thread-2) Publishing event: UserCreatedEvent(user=User(userId=UserId(id=dd92fd7e-e833-4892-b309-e7fb3e955d12), name=James Smith))<br>2023-12-30 23:22:28,178 INFO  [org.dom.eve.dom.LoggingListener] (executor-thread-2) Processing event: UserCreatedEvent(user=User(userId=UserId(id=dd92fd7e-e833-4892-b309-e7fb3e955d12), name=James Smith))<br>2023-12-30 23:22:28,178 INFO  [org.dom.eve.app.OutboxServiceImpl] (executor-thread-2) Deleting outbox message with id: EventId(id=4e224630-668c-40b8-90e9-5e97ee0e04eb)</pre><h4>Conclusion</h4><p>Domain events allow coordination between aggregates but need some wiring to guarantee eventual consistency. Using an outbox pattern helps with this goal as it provides at least once guarantee for delivering the events. Exactly once delivery requires consumers to be idempotent.</p><p>The code for this blog can be found on <a href="https://github.com/DanielWillian/domain-events">GitHub</a>. Some quirks were left out of this blog, like keeping the domain classes immutable and handling serialization of the domain events. Feel free to explore and experiment there.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d389538e2385" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MapStruct enhances object mapping]]></title>
            <link>https://danielwilliansc.medium.com/mapstruct-enhances-object-mapping-17f53d583ab9?source=rss-a6217f88c40d------2</link>
            <guid isPermaLink="false">https://medium.com/p/17f53d583ab9</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[domain-driven-design]]></category>
            <category><![CDATA[backend]]></category>
            <dc:creator><![CDATA[Daniel Willian]]></dc:creator>
            <pubDate>Wed, 22 Nov 2023 03:00:09 GMT</pubDate>
            <atom:updated>2023-11-22T03:08:30.348Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*9SrdxVLJSEuR5iMAuBEYJQ.png" /></figure><p>Mapping between objects is a common activity in many applications, especially when separating domain code from the edges of the system. Usually, mappings are straightforward, renaming a property or converting to a different type. It can be tiring to handwrite them so some people may be tempted to skip defining these different representations altogether, coupling a lot of functionalities to a few classes. <a href="https://mapstruct.org/">MapStruct</a> allows you to easily create these mappings.</p><h3>What is MapStruct</h3><p>MapStruct is a code generator for mappers between classes. It favors convention over configuration so a lot of classes can be mapped with little configuration. It creates mappers on compile time via annotation processors and uses plain method invocation when mapping. By convention, fields are mapped from source to target if they have the same name. Some type mapping happens implicitly, most commonly between primitive types, but some of the standard library classes have mapping between them, like between Java 8’s Date and Time APIs, see the <a href="https://mapstruct.org/documentation/stable/reference/html/#implicit-type-conversions">documentation</a> for a full list.</p><h4>Mapper definition</h4><p>Mappers are defined through interfaces and annotations. The generator may create the implementation annotated to better integrate with CDI frameworks, see the <a href="https://mapstruct.org/documentation/stable/reference/html/#using-dependency-injection">documentation</a> for a list of supported ones.</p><p>The methods in the interfaces specify the mappings, annotating a method configures the generator to deviate from convention. Combining methods and annotations allows you to:</p><ul><li>Map to properties with different names</li><li>Map multiple objects into one</li><li>Map properties of inner objects</li><li>Define an explicit type conversion</li></ul><p>See the <a href="https://mapstruct.org/documentation/stable/reference/html/#defining-mapper">documentation</a> for more.</p><h4>Example</h4><p>I created a simple<a href="https://www.jbang.dev/"> jBang</a> showing how a mapper can be defined. The scenario is mapping a Post request for a user into a domain representation and mapping the domain into a response for that request.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d11ab0b8501a1e3177a9036c6a5718b/href">https://medium.com/media/3d11ab0b8501a1e3177a9036c6a5718b/href</a></iframe><p>The output when running is the following:</p><pre>post: MapStructExample.UserPost(nameFirst=first, nameLast=last)<br>user: MapStructExample.User(userId=MapStructExample.UserId(id=123), firstName=first, lastName=last, creationDate=2023-11-12)<br>response: MapStructExample.UserResponse(firstName=first, lastName=last, createdDate=2023-11-12)</pre><p>Feel free to explore the code on <a href="https://github.com/DanielWillian/mapstruct-example">GitHub</a>.</p><h4>Conclusion</h4><p>MapStruct makes mapping between classes easier and helps in keeping layers in code separated while not spending time coding boilerplate.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17f53d583ab9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ensuring Order in Chaos: Leader Election with Zookeeper]]></title>
            <link>https://danielwilliansc.medium.com/ensuring-order-in-chaos-leader-election-with-zookeeper-c67f52d5320a?source=rss-a6217f88c40d------2</link>
            <guid isPermaLink="false">https://medium.com/p/c67f52d5320a</guid>
            <category><![CDATA[zookeeper]]></category>
            <category><![CDATA[jbang]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[leader-election]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <dc:creator><![CDATA[Daniel Willian]]></dc:creator>
            <pubDate>Sun, 15 Oct 2023 17:46:00 GMT</pubDate>
            <atom:updated>2023-11-22T03:07:53.756Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*LTBNrBxWxGsebaFTUh6few.png" /><figcaption>Leader election illustration</figcaption></figure><p>Applications usually require their microservices to have multiple replicas running, be it for resilience to failures or elasticity for bursts in load. Sometimes it is necessary to have only one of these replicas doing a specific task, for example, for triggering jobs based on a timer or coordinating some processing. In these scenarios, you can have a leader election and have these tasks only run on the leader.</p><h4>Zookeeper and leader election</h4><p>Zookeeper helps you coordinate distributed systems. Using its nodes it is possible to coordinate a leader election, the details for it can be found in the <a href="https://zookeeper.apache.org/doc/current/recipes.html#:~:text=own%20ephemeral%20node.-,Leader%20Election,with%20both%20flags%20SEQUENCE%7CEPHEMERAL.">documentation</a> but to put it briefly, participants of the election register themselves for it by creating a child node on an election node, whoever is the owner of the child node whose id is the lowest is the leader and the other participants watch for removal of the node with the closest lower id to figure out if it became the leader.<br><a href="https://curator.apache.org/docs/about/">Curator</a> provides an <a href="https://curator.apache.org/curator-recipes/leader-latch.html">implementation</a> for leader election using Zookeeper. It abstracts the algorithm through simple interfaces and participants are notified through callbacks whenever they become the leader.</p><h4>Demonstration</h4><p>To conclude I’ll demonstrate how a leader election can be done.<br>First start a Zookeeper server with <a href="https://hub.docker.com/_/zookeeper">Docker</a>:</p><pre>$ docker run -p 2181:2181 --name zookeeper --restart always -d zookeeper</pre><p>The following <a href="https://www.jbang.dev/">jBang</a> script registers itself for election and shuts itself down after 5 seconds of becoming the leader:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa7ce5192bc35d44964e5e14db0ee88a/href">https://medium.com/media/aa7ce5192bc35d44964e5e14db0ee88a/href</a></iframe><p>I created scripts for two scenarios, the first one demonstrates an instance becoming a leader, shutting itself down letting another instance become the leader:</p><pre>#!/bin/sh<br><br>main() {<br>  jbang run ZookeeperLeader.java --node-name=first &amp;<br>  FIRST_PID=$!<br>  sleep 2<br>  jbang run ZookeeperLeader.java --node-name=second &amp;<br>  SECOND_PID=$!<br>  wait ${FIRST_PID}<br>  wait ${SECOND_PID}<br>}<br><br>main</pre><p>Upon execution it shows the following output:</p><pre>$ ./run-election.sh<br>first: trying to connect to Zookeeper in localhost:2181<br>first: Connected to Zookeeper<br>first: sleeping waiting on election<br>first: is leader!<br>second: trying to connect to Zookeeper in localhost:2181<br>second: Connected to Zookeeper<br>second: sleeping waiting on election<br>first: removing ourselves from the election!<br>first: exiting!<br>second: is leader!<br>second: removing ourselves from the election!<br>second: exiting!</pre><p>The second example shows what happens when the leader gets shutdown through failure, after becoming the leader, the process gets killed:</p><pre>#!/bin/sh<br><br>main() {<br>  jbang run ZookeeperLeader.java --node-name=first &amp;<br>  FIRST_PID=$!<br>  sleep 1<br>  jbang run ZookeeperLeader.java --node-name=second &amp;<br>  SECOND_PID=$!<br>  sleep 2<br>  echo &quot;Killing first process!&quot;<br>  kill ${FIRST_PID}<br>  wait ${SECOND_PID}<br>}<br><br>main</pre><p>The second instance eventually becomes the leader:</p><pre>$ ./run-election-exit-abruptly.sh<br>first: trying to connect to Zookeeper in localhost:2181<br>first: Connected to Zookeeper<br>first: sleeping waiting on election<br>first: is leader!<br>second: trying to connect to Zookeeper in localhost:2181<br>second: Connected to Zookeeper<br>second: sleeping waiting on election<br>Killing first process!<br>second: sleeping waiting on election<br>second: sleeping waiting on election<br>second: sleeping waiting on election<br>second: sleeping waiting on election<br>second: is leader!<br>second: removing ourselves from the election!<br>second: exiting!</pre><p>Feel free to explore the code on <a href="https://github.com/DanielWillian/zookeeper-leader">GitHub</a> and try these scenarios yourself. Leader election might sound complex, but with Zookeeper and Curator, it’s a manageable task. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c67f52d5320a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MockServer is great for simulating external APIs]]></title>
            <link>https://danielwilliansc.medium.com/mockserver-is-great-for-simulating-external-apis-36b20222878b?source=rss-a6217f88c40d------2</link>
            <guid isPermaLink="false">https://medium.com/p/36b20222878b</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[software-testing]]></category>
            <category><![CDATA[api-testing]]></category>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[integration-testing]]></category>
            <dc:creator><![CDATA[Daniel Willian]]></dc:creator>
            <pubDate>Sat, 16 Sep 2023 00:03:09 GMT</pubDate>
            <atom:updated>2023-09-16T00:03:09.208Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*GJnf6-c90tcb43MzfHic5Q.png" /></figure><p>MockServer allows you to easily mock APIs from external systems. It has a lot of functionalities but I want to focus on how it can help you in testing your application, both during integration tests locally or on a running server doing end-to-end testing, and how you can configure MockServer for your needs.</p><h4>Running in a Docker container</h4><p>For end-to-end testing you can start a mock server through docker, you can deploy it in your infrastructure on test environments to help simulate external APIs.<br>When using a docker engine locally, you can do it by running:</p><pre>$ docker run -d --rm -p 1080:1080 mockserver/mockserver:5.15.0</pre><p>Configuring expectations can be done through requests to it:</p><pre>$ curl -X PUT &#39;localhost:1080/mockserver/expectation&#39; \<br>-d &#39;{<br>  &quot;httpRequest&quot;: {<br>    &quot;method&quot;: &quot;GET&quot;,<br>    &quot;path&quot;: &quot;/users/1&quot;<br>  },<br>  &quot;httpResponse&quot;: {<br>    &quot;body&quot;: &quot;{\&quot;firstName\&quot;: \&quot;foo\&quot;, \&quot;lastName\&quot;: \&quot;bar\&quot;}&quot;<br>  }<br>}&#39;</pre><p>Hitting the API configured gives the wanted response:</p><pre>$ curl localhost:1080/users/1<br>{&quot;firstName&quot;: &quot;foo&quot;, &quot;lastName&quot;: &quot;bar&quot;}</pre><p>More info on running with docker can be found in the <a href="https://www.mock-server.com/where/docker.html">documentation</a>.</p><h4>Integration tests when building</h4><p>You can add the dependency for starting a MockServer inside a test. In this case, it will be in a gradle project with Junit5.</p><pre>dependencies {<br>    ...<br>    testImplementation &#39;org.junit.jupiter:junit-jupiter:5.9.2&#39;<br>    testImplementation &#39;org.mock-server:mockserver-junit-jupiter-no-dependencies:5.14.0&#39;<br>    testRuntimeOnly &#39;org.junit.platform:junit-platform-launcher&#39;<br>    ...<br>}</pre><p>Configuring requests could be done like so:</p><pre>private final ClientAndServer clientAndServer;<br>...<br>@Test<br>void testMethodAndPath() throws URISyntaxException, IOException, InterruptedException {<br>    clientAndServer<br>            .when(request()<br>                    .withMethod(&quot;GET&quot;)<br>                    .withPath(&quot;/users&quot;))<br>            .respond(response()<br>                    .withStatusCode(200)<br>                    .withBody(&quot;[{\&quot;firstName\&quot;: \&quot;foo\&quot;, \&quot;lastName\&quot;: \&quot;bar\&quot;}]&quot;));<br>    ...<br>}</pre><h4>Configuration</h4><p>Expectations can be very descriptive, you can configure the HTTP method, the path that gets hit, specify the headers of the request, the body, and more.<br>Here are tests with a few examples on how to do it, you can also get the code on <a href="https://github.com/DanielWillian/mock-server-intro">GitHub</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc1a650733339d9313d07473fc571f6b/href">https://medium.com/media/bc1a650733339d9313d07473fc571f6b/href</a></iframe><p>You can also set the response in various ways, check the <a href="https://www.mock-server.com/mock_server/creating_expectations.html">documentation</a> for more.</p><h4>Conclusion</h4><p>MockServer is a good tool to use when you want to simulate the behavior of external APIs and test how your application will respond, both for sunny day and rainy day scenarios.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36b20222878b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Beginner’s guide to tmux]]></title>
            <link>https://danielwilliansc.medium.com/beginners-guide-to-tmux-a050828df13e?source=rss-a6217f88c40d------2</link>
            <guid isPermaLink="false">https://medium.com/p/a050828df13e</guid>
            <category><![CDATA[terminal]]></category>
            <category><![CDATA[command-line]]></category>
            <category><![CDATA[tmux]]></category>
            <category><![CDATA[shell]]></category>
            <category><![CDATA[cli]]></category>
            <dc:creator><![CDATA[Daniel Willian]]></dc:creator>
            <pubDate>Tue, 01 Jun 2021 03:06:16 GMT</pubDate>
            <atom:updated>2021-06-02T00:05:01.466Z</atom:updated>
            <content:encoded><![CDATA[<p>tmux is a terminal multiplexer. It allows you to run multiple command line commands on a single terminal. Never again will you need to open several terminals to execute a task or keep going back and forth between directories.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*Ec2W8Wq5FrRDmNXAMk5BaQ.gif" /></figure><h3>Installing tmux</h3><p>tmux comes out of the box in some distributions. You can also install it by using most of the package management tool. There is the option to compile it yourself as well. Check out its <a href="https://github.com/tmux/tmux/wiki/Installing">installing wiki</a> for more details.</p><h3>Use cases</h3><p>The <a href="https://github.com/tmux/tmux/wiki/Getting-Started">tmux getting started</a> states the main uses of tmux are:</p><ul><li>Protect running programs on a remote server from connection drops by running them inside tmux.</li><li>Allow programs running on a remote server to be accessed from multiple different local computers.</li><li>Work with multiple programs and shells together in one terminal, a bit like a window manager.</li></ul><p>The first two uses are handy sometimes, but the third point is why you are always glad to be using tmux. With it you can organize your workspace into multiple sessions which can group related shells/commands.</p><p>Because of its key bindings, you can quickly find, create or close wanted shells without cycling between multiple terminals and with its copy mode you can browse command outputs or copy its content without using the mouse, allowing you to keep your hands on the keyboard at all times.</p><h3>Key concepts</h3><p>The <a href="https://github.com/tmux/tmux/wiki/Getting-Started">tmux getting started</a> goes over the basic concepts and terminology tmux uses. The main ones to understand are sessions, windows and panes. One session has windows and each window has panes. Panes are where shells or commands are run.</p><p>The prefix key is required to control tmux, usually all key presses are forwarded to the active pane on the current window. When the prefix key is pressed, tmux waits a combination of keys to execute before returning to forward keys to the active pane. The default prefix key is &lt;C-b&gt; (ctrl + b).</p><p>Knowing how to read the status line is also important, in it there is information about the session you are attached to, like its name, windows and the current window you are on.</p><h3>Interacting with tmux</h3><p>tmux has a lot of options and commands, the following sections present some of the commands and key bindings and by no means is an extensive display of all of its features.</p><h4>Attaching clients</h4><p>The first steps to interacting with tmux would be setting up a session in the server and attaching a client to it. This can be done by running:</p><pre>$ tmux new-session -s &lt;session-name&gt;</pre><p>&lt;prefix&gt; + d can be used to detach from the session.</p><p>To list the sessions on the server run:</p><pre>$ tmux list-sessions</pre><p>To attach to already running sessions use:</p><pre>$ tmux attach-session -t &lt;session&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*_E_DRX9UIXriOSxuwHKRuA.gif" /></figure><h4>list-keys and commands</h4><p>You can check the current key bindings available by running &lt;C-b&gt; ? , every key binding is listed there. Key bindings mostly are composed of the key combination, the command triggered by the key binding and the key-table (consider it as a state) at which the combination triggers the command. Consider this default one as example:</p><pre>bind-key -T prefix : command-prompt</pre><p>This key binding is part of the prefix key-table, which represents the state after the prefix key is pressed, the combination is ‘:’ and after the combination is achieved it will run the command ‘command-prompt’ which prompts the user for a command.</p><p>Key bindings are simply shortcuts to commands, you could instead of using a key binding, write its command into the command prompt to produce the same result. So you can open the command prompt both by pressing &lt;C-B&gt; : or by typing ‘command-prompt’ into the command prompt, potentially going infinite if so is desired.</p><p>bind-key is itself a command that can be run to add or change key bindings. It can be run in the command prompt to affect the running server or added to the configuration file for future use.</p><h4>Sessions</h4><p>Separating related activities into different sessions can be beneficial as there are by default only 10 direct key bindings for selecting the windows in them.</p><p>By default there is no key binding for creating new session, you need to run the command new-session to create a new one.</p><p>&lt;prefix&gt; + $ is the default key binding to rename a session if necessary.</p><p>&lt;prefix&gt; + s lists the available session and allows you to attach to them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*HlzNLC3Fd1aPDqnw_rGL5Q.gif" /></figure><h4>Windows</h4><p>A window is composed of an index and a name, it is usually identified by its index.</p><p>&lt;prefix&gt; + c creates a new window in the current session.</p><p>By default there are key bindings to select windows of index from 0 to 9 and it is of form &lt;prefix&gt; + &lt;index&gt;, &lt;index&gt; being the index of the window you want to switch.</p><p>To select windows with index higher than 9, there is also &lt;prefix&gt; + ‘ which prompts the user for an index to switch to.</p><p>&lt;prefix&gt; + n and &lt;prefix&gt; + p can be used to switch to next and previous windows, respectively.</p><p>&lt;prefix&gt; + &amp; can be used to delete a window.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*hinBEmyLRz-C-rnMWszKqg.gif" /></figure><h4>Panes</h4><p>Panes can run different commands and are shown together as part of a window to the user. This is useful in many situations, like editing a source file and compiling or tailing logs.</p><p>&lt;prefix&gt; + % divides the current pane horizontally to create a new pane.</p><p>&lt;prefix&gt; + “ divides the current pane vertically to create a new pane.</p><p>&lt;prefix&gt; + o cycles the active pane through all of them.</p><p>&lt;prefix&gt; + q numbers the panes and pressing its number selects that pane as active.</p><p>&lt;prefix&gt; + x kills the current pane.</p><p>Rearranging or resizing the panes are sometimes handy, there are a lot of commands and shortcuts to do so, check out the list-keys for a list of them. One that you might execute by mistake is &lt;prefix&gt; + &lt;C-o&gt; when trying to cycle through the panes. This key binding rotates the panes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*xDpvYBlmNrckkwxruLSR4w.gif" /></figure><h4>Copy mode</h4><p>Copy mode freezes the active pane allowing you to search and copy the output of some command to a tmux buffer. The key bindings to navegate in copy mode depends on the mode-key option, which is set as state by the <a href="http://man.openbsd.org/OpenBSD-current/man1/tmux.1">manual</a>:</p><pre><a href="http://man.openbsd.org/OpenBSD-current/man1/tmux.1#mode-keys"><strong>mode-keys</strong></a> [<strong>vi</strong> | <strong>emacs</strong>]<br>          Use vi or emacs-style key bindings in copy mode. The default is emacs, unless VISUAL or EDITOR contains ‘vi’.</pre><p>If mode-keys is set to emacs, then the copy-mode key-table will be used for key bindings when in copy mode, if it is vi, then the copy-mode-vi key-table will be used instead.</p><p>Regardless of the mode-keys used, copying a selection copies the content to a tmux buffer, it does not by default go to the OS clipboard.</p><p>Entering copy mode is achieved by using &lt;prefix&gt; + [ and pasting the most recent copy is done through &lt;prefix&gt; + ]. It is possible to paste previous copies by using &lt;prefix&gt; + =.</p><p>Copy mode is useful even if you do not intend to copy as it can be used to scroll up and down the terminal without using the mouse.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*B74QSEslmpXk5vbXZk-sLQ.gif" /></figure><h3>Configuration</h3><p>tmux can be configured through configuration file. By default it uses ~/.tmux.conf. There are a lot of options to configure and they are listed in the manual but the defaults work reasonably well.</p><p>There are also a lot of tmux configuration repositories out there. One of the most popular is <a href="https://github.com/gpakosz/.tmux">.tmux</a> from <a href="https://github.com/gpakosz">gpakosz</a>. It contains some useful features, like additional key bindings and ability to copy to OS clipboard. I personally disable the laptop battery status though.</p><h3>Conclusion</h3><p>This guide presented an introduction to some of tmux features to help anyone to start using it. Once you adopt it in your day to day activities you will not want to go back.</p><p>tmux is a very helpful tool for terminal users. It makes the experience of using command line tools much more pleasant with its key bindings and commands as well as being cool.</p><p>Consider playing with the default configuration for a while to get comfortable with the basic usage. Naturally you will start adding some key bindings or tinkering with configuration. Adopting popular configuration is also a good option, specially for features like copy to OS copyboard.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a050828df13e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>