<?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 Sonal Chandani on Medium]]></title>
        <description><![CDATA[Stories by Sonal Chandani on Medium]]></description>
        <link>https://medium.com/@sonalchandani29?source=rss-1612144d50c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*TkmCT1TgGZDOYcVH8cteuA.jpeg</url>
            <title>Stories by Sonal Chandani on Medium</title>
            <link>https://medium.com/@sonalchandani29?source=rss-1612144d50c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 04:36:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sonalchandani29/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[Contract Tests Using Swagger-Diff]]></title>
            <link>https://medium.com/@sonalchandani29/contract-tests-using-swagger-diff-ca41c2187f8f?source=rss-1612144d50c------2</link>
            <guid isPermaLink="false">https://medium.com/p/ca41c2187f8f</guid>
            <category><![CDATA[contract-test]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[swagger]]></category>
            <category><![CDATA[thoughtworks]]></category>
            <category><![CDATA[twbloggersclub]]></category>
            <dc:creator><![CDATA[Sonal Chandani]]></dc:creator>
            <pubDate>Mon, 11 Jun 2018 11:56:28 GMT</pubDate>
            <atom:updated>2018-06-11T14:31:06.971Z</atom:updated>
            <content:encoded><![CDATA[<p>With micro services coming into the mainstream and increased complexity of applications, it is generally the case that services depend on other external services, which are maintained and developed by the third party. This leads to increased dependency on providers of external services for the proper functioning of consumer service at hand.</p><p>It’s a norm in ThoughtWorks to have a <a href="https://www.gocd.org/2018/05/08/continuous-delivery-microservices-test-strategy/?utm_campaign=cd_microservices&amp;utm_medium=twitter&amp;utm_source=tw_twitter&amp;utm_content=test_strategy_microservices_blog&amp;utm_term=">test pyramid</a> implemented comprising of, unit tests, which test individual pieces of code, integration tests, which test if independently developed portions of a service work as intended when put together, and then end-to-end test, which gives us the ability to run automated tests against deployed services by simulating clicks and entering data through applications user interface. Though integration and end-to-end tests can be used to test if the external services are behaving as expected, they can be time-consuming or as a general practice use stubbed services to deal with reliability issues in external services like network latency, availability, etc., leading to delay/ failure in identifying a change in contract. For the very same reason, we introduce contract tests into the pyramid, which run at regular intervals to enable us to quickly detect changes in the contract of external services and address the issues at the earliest.</p><p>Let’s take a scenario of an online retail application with three micro services namely Ordering, Payment and Delivery services respectively. In which Payment service is consumed by Ordering service which is dependent on Delivery service. In this case, minor changes to the response of producer Payment service could lead to the breaking of Ordering service which in turn could lead to the breaking of Delivery service. Is there a way where the consumer Ordering service could be informed about the changes in payment service in a fully automated fashion with minimal delay, so that, its downtime can be reduced?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*EpPJ8tNozdm7w1FU9sKEFA.png" /></figure><p>To tackle this situation, we run a separate set of tests called <strong>Contract Tests</strong>, which make calls to all the endpoints of Payment service (producer), which are used by Ordering service (consumer), and verify if their responses adhere to the expected schema, else an alert is triggered indicating a contract change.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cB6H3sVBqrd2NmLe4wxPuA.png" /></figure><p><strong>In contract tests what matters the most is the format of the response</strong>, i.e., the parameters and structure, rather than the actual data, which is dynamic and keeps changing. There are a number of tools available for API documentation, such as swagger, helps in defining the API structure and gives the ability to write the contract tests using “swagger-diff.”</p><h4>So what is swagger-diff and how does it come handy?</h4><p>swagger-diff compares the parameters of API responses for different HTTP methods and renders the difference in response if any.</p><p>For writing a contract test, we define the schema of the endpoint to be tested and use swagger-diff to compare the actual API response with the defined schema. If there is a difference observed, tests fail while stating the deviation in the contract.</p><p>Let’s take an example to get a clear picture. We have defined a schema map which is used for running the test and schema structure(in find-payment-by-id.json) that contains the expected schema response from the producer for the endpoint /payment.These schemas are used by swagger-diff of the consumer to run contract tests.</p><pre><strong>schema_map.json</strong><br>[<br>    {<br>     &quot;method&quot;: &quot;get&quot;,<br>     &quot;endpoints&quot;: [<br>      {<br>       &quot;url&quot;: &quot;/v2/payment/&quot;,<br>       &quot;schema&quot;: &quot;find-payment-list&quot;<br>      }<br>     ]<br>    }<br>]</pre><pre><strong>find-payment-list.json</strong><br> {<br>    &quot;paths&quot;: {<br>     &quot;/payment&quot;: {<br>      &quot;get&quot;: {<br>       &quot;description&quot;: &quot;Returns all the payment from the system that the user has made&quot;,<br>       &quot;produces&quot;: [<br>        &quot;application/json&quot;<br>       ],<br>       &quot;responses&quot;: {<br>        &quot;200&quot;: {<br>         &quot;description&quot;: &quot;list of payments for the user&quot;,<br>         &quot;schema&quot;: {<br>          &quot;type&quot;: &quot;array&quot;,<br>          &quot;items&quot;: {<br>           &quot;$ref&quot;: &quot;#/definitions/Payment&quot;<br>          }<br>         }<br>        }<br>       }<br>      }<br>     }<br>    },<br>    &quot;definitions&quot;: {<br>     &quot;Payment&quot;: {<br>      &quot;type&quot;: &quot;object&quot;,<br>      &quot;required&quot;: [<br>       &quot;id&quot;,<br>       &quot;code&quot;<br>      ],<br>      &quot;properties&quot;: {<br>       &quot;id&quot;: {<br>        &quot;type&quot;: &quot;integer&quot;,<br>        &quot;format&quot;: &quot;int64&quot;<br>       },<br>       &quot;code&quot;: {<br>        &quot;type&quot;: &quot;string&quot;<br>       },<br>       &quot;tag&quot;: {<br>        &quot;type&quot;: &quot;string&quot;<br>       }<br>      }<br>     }<br>    }<br>}</pre><h4>A scenario in which tests fail when contract changes</h4><p>So if the producer service changes the contract by sending ‘amount’ instead of ‘code’ and the consumer is not priorly informed, the contract tests fail specifying the difference in the schema structure. Below is the response received from producer, which is compared against the schema defined in swagger-diff on the consumer side.</p><pre><strong>find-payment-list.json</strong><br>{<br>    &quot;paths&quot;: {<br>     &quot;/payment&quot;: {<br>      &quot;get&quot;: {<br>       &quot;description&quot;: &quot;Returns all the payment from the system that the user has made&quot;,<br>       &quot;produces&quot;: [<br>        &quot;application/json&quot;<br>       ],<br>       &quot;responses&quot;: {<br>        &quot;200&quot;: {<br>         &quot;description&quot;: &quot;list of payments for the user&quot;,<br>         &quot;schema&quot;: {<br>          &quot;type&quot;: &quot;array&quot;,<br>          &quot;items&quot;: {<br>           &quot;$ref&quot;: &quot;#/definitions/Payment&quot;<br>          }<br>         }<br>        }<br>       }<br>      }<br>     }<br>    },<br>    &quot;definitions&quot;: {<br>     &quot;Payment&quot;: {<br>      &quot;type&quot;: &quot;object&quot;,<br>      &quot;required&quot;: [<br>       &quot;id&quot;,<br>       &quot;code&quot;<br>      ],<br>      &quot;properties&quot;: {<br>       &quot;id&quot;: {<br>        &quot;type&quot;: &quot;integer&quot;,<br>        &quot;format&quot;: &quot;int64&quot;<br>       },<br>       <strong>&quot;amount&quot;</strong>: { <strong>&lt;---</strong> <strong>change in contract - <em>amount</em> in place of code</strong><br>        &quot;type&quot;: &quot;string&quot;<br>       },<br>       &quot;tag&quot;: {<br>        &quot;type&quot;: &quot;string&quot;<br>       }<br>      }<br>     }<br>    }<br>}</pre><h4>A scenario in which tests pass even though the contract has changed</h4><p>When the provider service adds new parameters, for instance, it sends ‘amount’ which is not used by the consumer service this should not break our test as it’s not affecting the existing contract.</p><pre><strong>find-payment-list.json</strong><br>{<br>    &quot;paths&quot;: {<br>     &quot;/payment&quot;: {<br>      &quot;get&quot;: {<br>       &quot;description&quot;: &quot;Returns all the payment from the system that the user has made&quot;,<br>       &quot;produces&quot;: [<br>        &quot;application/json&quot;<br>       ],<br>       &quot;responses&quot;: {<br>        &quot;200&quot;: {<br>         &quot;description&quot;: &quot;list of payments for the user&quot;,<br>         &quot;schema&quot;: {<br>          &quot;type&quot;: &quot;array&quot;,<br>          &quot;items&quot;: {<br>           &quot;$ref&quot;: &quot;#/definitions/Payment&quot;<br>          }<br>         }<br>        }<br>       }<br>      }<br>     }<br>    },<br>    &quot;definitions&quot;: {<br>     &quot;Payment&quot;: {<br>      &quot;type&quot;: &quot;object&quot;,<br>      &quot;required&quot;: [<br>       &quot;id&quot;,<br>       &quot;code&quot;<br>      ],<br>      &quot;properties&quot;: {<br>       &quot;id&quot;: {<br>        &quot;type&quot;: &quot;integer&quot;,<br>        &quot;format&quot;: &quot;int64&quot;<br>       },<br>       &quot;code&quot;: {<br>        &quot;type&quot;: &quot;string&quot;<br>       },<br>       &quot;tag&quot;: {<br>        &quot;type&quot;: &quot;string&quot;<br>       },<br>       <strong>&quot;amount&quot;</strong>: {  <strong>&lt;--- change in contract - added new key</strong><br>        &quot;type&quot;: &quot;string&quot;<br>       }<br>      }<br>     }<br>    }<br>}</pre><p>So ideally failure of contract tests need not necessarily break the build like in the case of other testing scenarios. Instead, it should have a trigger that informs the team about the contract change affecting our application through this failure so that it could be fixed immediately.</p><p>Using contract tests the chances of unexpected failures reduces especially in a production application, where breakage is a huge risk, as the consumer is informed immediately about the contract changes by the producer service, they can make appropriate fixes to resume its normal functioning.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ca41c2187f8f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>