Ratpacked: Using Spring Cloud Contract As Client

In a previous post we learned about Spring Cloud Contract. We saw how we can use contracts to implement the server side of the contract. But Spring Cloud Contract also creates a stub based on the contract. The stub server is implemented with Wiremock and Spring Boot. The server can match incoming requests with the contracts and send back the response as defined in the contract. Let’s write an application that is invoking HTTP requests on the server application we wrote before. In the tests that we write for this client application we use the stub that is generated by Spring Cloud Contract. We know the stub is following the contract of the actual server.

Continue reading

Ratpacked: Using Spring Cloud Contract To Implement Server

Spring Cloud Contract is a project that allows to write a contract for a service using a Groovy DSL. In the contract we describe the expected requests and responses for the service. From this contract a stub is generated that can be used by a client application to test the code that invokes the service. Spring Cloud Contract also generates tests based on the contract for the service implementation. Let’s see how we can use the generated tests for the service implementation for a Ratpack application.

Continue reading

Gradle Goodness: Skip Task When Input Empty Using @SkipWhenEmpty Annotation

Gradle has excellent incremental build support. This means that Gradle can determine if a task needs to be executed based on the input and output of that task. If for example nothing changed in one of the input and output files, then the task can be skipped. We can add incremental build support for our custom tasks by defining the input and output of the task. We can also define that a task can be skipped when a collection of files or a directory that is the input of the task are empty or not exists. Gradle offers the @SkipWhenEmpty annotation we can apply on the input of our task.

Continue reading

Grails Goodness: Custom JSON and Markup Views For Default REST Resources

In Grails we can use the @Resource annotation to make a domain class a REST resource. The annotation adds a controller as URL endpoint for the domain class. Values for the domain class properties are rendered with a default renderer. We can use JSON and markup views to customize the rendering of the domain class annotated with a @Resource annotation. First we must make sure we include views plugin in our build configuration. Then we must create a directory in the grails-app/views directory with the same name as our domain class name. Inside the directory we can add JSON and markup views with names that correspond with the controller actions. For example a file index.gson or index.gml for the index action. We can also create a template view that is automatically used for a resource instance by adding a view with the name of the domain class prefixed with an underscore (_).

Continue reading

Grails Goodness: Using Domain Classes Without Persistence

Normally when we create a domain class in Grails we rely on GORM for all the persistence logic. But we can use the static property mapWith with the value none to instruct Grails the domain class is not persisted. This can be useful for example if we want to use a RestfulController for a resource and use the default data binding support in the RestfulController. The resource must be a domain class to make it work, but we might have a custom persistence implementation that is not GORM. By using the mapWith property we can still have benefits from the RestfulController and implement our own persistence mechanism.

Continue reading

Release NPM package with git-flow

Having an NPM package in an enterprise environment and wanting to release that package using the git-flow model? Then using the node-generate-release can be very helpful.
This blog shows how to execute an integrated git flow release from your NPM package, even if your master and develop branches are protected.

Install plugin

Let’s assume we have all changes in the develop branch and we would like to create a release with all the current changes in develop. With the git-flow release the result will be that all changes will be merged into master and a tag for the release version is created with correct version. Before we can finish the release the correct version in NPM package.json needs to be set. This can all be nicely done with node-generate-release plugin.

Continue reading

Groovy Goodness: Using The Call Operator ()

In Groovy we can add a method named call to a class and then invoke the method without using the name call. We would simply just type the parentheses and optional arguments on an object instance. Groovy calls this the call operator: (). This can be especially useful in for example a DSL written with Groovy. We can add multiple call methods to our class each with different arguments. The correct method is invoked at runtime based on the arguments.

Continue reading

PlantUML Pleasantness: Create diagrams from NPM and Typedoc

What if you have created an awesome diagram with PlantUML, and you would like to include that diagram in your documentation?

In this small tutorial we can include a generated PlantUML diagram in typedoc (a way of documenting typescript packages). Note: Graphiz needs to be installed to run this diagram generation.
First install the typedoc plugin:
npm install typedoc –saveDev
Now, create a typedoc.json in the root of your project. This file describes how typedoc should generate the documentation.
  “emitDecoratorMetadata”: true,
  “excludeExternals”: true,
  “experimentalDecorators”: true,
  “hideGenerator”: true,
  “ignoreCompilerErrors”: true,
  “includeDeclarations”: false,
  “mode”: “file”,
  “module”: “commonjs”,
  “moduleResolution”: “node”,
  “out”: “./build/docs”,
  “preserveConstEnums”: true,
  “stripInternal”: true,
  “suppressExcessPropertyErrors”: true,
  “suppressImplicitAnyIndexErrors”: true,
  “target”: “ES5”
Next install node-plantuml http://ift.tt/1EOlnfx globally. We should install this globally so we can also use the CLI as a NPM script later.
npm install -g node-plantuml
Go ahead and put the following snippet in your package.json
“scripts”: {
     “docs”: “typedoc –options typedoc.json ./src/ && puml generate my-diagram.puml -o build/docs/my-diagram.png”
Finally we are able to create typescript documentation with our generated PNG diagram. If we would include the image in our README as follows:
<a href=”./my-diagram.png” target=”_blank”>![Diagram](./my-diagram.png)</a>
and run the following command from the command-line:
npm run docs
Our documentation at ./build/docs our index page shows the README with our generated diagram.

Groovy Goodness: Creating Root JSON Array With JsonBuilder

To create JSON output with Groovy is easy using JsonBuilder and StreamingJsonBuilder. In the samples mentioned in the links we create a JSON object with a key and values. But what if we want to create JSON with a root JSON array using JsonBuilder or StreamingJsonBuilder? It turns out to be very simple by passing a list of values using the constructor or using the implicit method call.

Continue reading