Helidon: 2x productivity with Microprofile REST Client

0
160

We have already many articles before on how to write Microservice using the Helidon framework. We are going to further enhance our code with Microprofile REST Client to increase 2x productivity.

Microprofile REST Client overview

There are many frameworks available in the market to test REST APIs + automation. Microprofile introduced a new concept of REST Client by default. So that we can write type-safe REST Client while writing our code with few lines of code. This will speed up our API development and automation productivity in a great style.

Code example

The example code covers the following steps. For the sake of clarity, I have cut down the API implementation code. (Refer the previous article for implementation details)

  • Write REST API definition
  • A type-safe REST client implementation

REST API definition

@Path("/countries")
@Produces(MediaType.APPLICATION_JSON)
public interface ICountryApi {
    @GET
    Response all() throws DailyApiException; //----------> (1)

    @Path("{code}")
    @GET
    Response get(@PathParam("code") String code) throws DailyApiException; //---> (2)
}

(1) -> To fetch all the available countries from the data source

(2) -> To get a particular country by the given id

A type-safe REST client implementation

@DisplayName("Country Api test cases")
public class CountryApiTest {
    static ICountryApi countryApi;
    static final String HOST = "http://localhost:8080"; //Microservice base Uri

    @BeforeAll
    public static void initAll() throws URISyntaxException { 
        countryApi = RestClientBuilder.newBuilder()          //---> (1)
                .baseUri(new URI(HOST))
                .build(ICountryApi.class);                   //---> (2)
    }

    @Test
    @DisplayName("Method to test all() countries")
    public void testAll() throws DailyApiException {
        final Response response = countryApi.all();
        List<Country> countries = response.readEntity(
                new GenericType<List<Country>>() {});       //---> (3)
        assertEquals(2, countries.size(), "Result should be 2");
    }

    @Test
    @DisplayName("Method to test get() with proper country code")
    public void testGetWithProperId() throws DailyApiException {
        final Response response = countryApi.get("IND");
        assertEquals(200, response.getStatus(),
                "Status code should be 200");

        final Country country = response.readEntity(Country.class); //---> (4)
        assertAll("country('IND')",
                () -> assertEquals("IND", country.getCode(),
                        "Country code should be IND"),
                () -> assertEquals("New Delhi", country.getCapital(),
                        "Capital should be New Delhi")
        );
    }
}

(1) -> Define a REST Client using the RestClientBuilder API introduced in Microprofile

(2) -> Use our ICountryApi to build REST API Client

(3) -> Deconstruct the Java bean directly from the HTTP response. Marshaling/ Un-marshalling will be taken care by the Helidon engine (Standard process for Microprofile implementation) [NOTE: I have included only 2 countries in the data store]

(4) Get the Country bean from the get() method and validate the response

I believe this would have given you clarity on writing a REST API client using the modern Microprofile REST Client specification. Deep dive into REST client specification to increase your productivity and speed up your automation coverage.

NOTE: Tests are written using the JUnit 5 framework.

{ Happy coding }