Posts

Showing posts from July, 2022

Validate model with Strategy Pattern to keep Open–Closed Principle

Image
  Defensive Programming with Strategy Pattern Defensive programming is an approach to programming that attempts to ensure that software still functions under adverse or unforeseen circumstances.  Open–Closed Principle Classes, functions, and modules should be closed for modification but open for extension. That means closed for modification and open for extension. Adding new functionality should not modify existing source code. A component should be extendable to make it behave in new ways. Why Strategy pattern? The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. 

Using Spring REST Docs with automated tests

Image
  Spring REST docs provide a lot of features and flexibility to generate documentation for your REST APIs. One of the powers of REST docs is its ability to generate the documentation based on the unit tests which ensure that documentation is accurate, concise, and well-structured. To enable the Spring REST Docs support, add spring-restdocs-mockmvc as an additional dependency in your application.  Spring REST Docs use the test cases to generate accurate and updated documentation for the REST API. If all tests pass with no issue, it will generate a sub-folder under the target/generated-snippets with a name matching the argument passed to the document method (in our case it is itShouldReturnThreeCustomers). There will be several .adoc files. One of the limitations with the AsciiDoc doc is that it is unable to render in the browser. So AsciiDoc need to be converted into a browser-friendly format like HTML. To convert our documentation into HTML format, must be use th...

Generate the documentation at runtime when app is deployed

Image
To generate the documentation at runtime, when app is deployed you need springdoc-openapi-maven-plugin .  The aim of springdoc-openapi-maven-plugin is to generate json and yaml OpenAPI description during runtime.  If you want to get swagger definitions properly, the application should completely running as locally.  The plugin works during integration-tests phase, and generate the OpenAPI description.  The plugin works in conjunction with spring-boot-maven plugin.  You can test it during the integration tests phase using the maven command: mvn verify   In order to use this functionality, you need to add the plugin declaration on the plugins section of your pom.xml. References:  springdoc-openapi-maven-plugin