Unittesting: using AssertJ’s assertThat

JUnit4 provides a standard dependency to the Hamcrest Assertion Frameworks which makes the following assertions possible:

assertThat("Robbert",equalTo("Robbert"));

Many matchers beside equalTo are available, but the problem to me is that I always have search for the appropriate one that I want to use, and static import that matcher.

An alternative to Hamcrest, is AssertJ which provides a fluent interface for the assertions, so you can use your IDE’s code completion for searching the matchers.

AssertJ uses the following syntax:

assertThat("Robbert").isEqualTo("Robbert");

With AssertJ, it is also possible to use soft assertions which makes is possible to do multiple checks, and test all of them instead of stopping the test on the first failure.

SoftAssertions softly = new SoftAssertions();
softly.assertThat("Robert").isEqualTo("Robbert");
softly.assertThat("vd").isEqualTo("vd");
softly.assertThat("Zon").isEqualTo("Son");
softly.assertAll();

This results in the following error:

The following 2 assertions failed:
1) expected:<"Rob[b]ert"> but was:<"Rob[]ert">
at MyTest.test(MyTest.java:11) expected:<"Rob[b]ert"> but was:<"Rob[]ert">
2) expected:<"[S]on"> but was:<"[Z]on">
at MyTest.test(MyTest.java:13) expected:<"[S]on"> but was:<"[Z]on">

More about AssertJ can be found here:http://joel-costigliola.github.io/assertj/

To add a dependency to AssertJ, use the following:

testCompile("org.assertj:assertj-core:3.11.1")

or

<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.11.1</version>
    <scope>test</scope>
</dependency>

 

Enable https and a custom domain name for API Gateway on AWS

In a previous blog I demonstrated how to create a java REST application using AWS Lambda and API Gateway.

In this blog we will modify the API Gateway so we can access the REST endpoint using a custom domain name and use https.

  • Open the API Gateway console and select ‘Custom Domain Names’ from the left menu
  • Press the ‘create new custom domain name’ button and use the following values:
    • domain name: example.api.vdzon.com
    • endpoint: edge optimized
    • certificate: choose the certificate for example.api.vdzon.com (see step 1 in this blog on how to create a certificate)
    • add basemapping:
      • path: /
      • destination: choose the lambda function
      • stage: select the stage to use
    • save the ‘Target Domain Name’
  • Wait until the Custom Domain Name that we created in AWs is finished initializing (may take 40 minutes)
  • While waiting: create a DNS entry on your DNS Service Provider
    • domain name: example.api.vdzon.com
    • type: CNAME
    • value: the target domain name as stored in the previous step

The endpoint can now be called on https://example.api.vdzon.com/

Using the aws commandline tools

  • Download aws here and install aws on your laptop
  • In the AWS web console, click on your name and choose “Your security credentials”
  • Select “create new access key” and store your security key somewhere
  • Run on the command line: “aws configure” and use the just stored access keys
  • check the cli with the following command: aws s3 ls
  • “aws help” shows all commands that you can use

source: https://docs.aws.amazon.com/cli/latest/userguide/awscli-install-windows.html

Creating a java REST application on AWS Lambda with a DynamoDB database

I created a sample java REST application which can be used to easily create an application with one or more REST endpoints and can be deployed, updated and removed to AWS Lambda and AWS Gateway with a single script.

The source code and scripts for an example using a ‘Jersey-style’ syntax can be found here (make sure to use the jersey branch):
https://github.com/robbertvdzon/aws-lambda-rest-example/tree/jersey

The source code and scripts for an example using a ‘Sparkjava-style’ syntax can be found here (make sure to use the sparkjava branch):
https://github.com/robbertvdzon/aws-lambda-rest-example/tree/sparkjava

The source code for using a dynamodb database (as an addition to the sparkjava sample) can be found here (make sure to use the dynamodb branch):
https://github.com/robbertvdzon/aws-lambda-rest-example/tree/dynamodb