Skip to content

Latest commit

 

History

History
165 lines (122 loc) · 4.57 KB

README.md

File metadata and controls

165 lines (122 loc) · 4.57 KB

Spring Boot Template

Docker Unit and Integration Tests

In this template:

  • Spring Boot,
  • PostgreSQL,
  • Docker,
  • GitHub workflows:
    • Docker setup, and
    • unit and integration tests of the Spring Boot application.

This template has been bootstrapped using this Spring Initializr configuration.

Documentation

Setup

GitHub Workflows

Add CR_PAT secret to the repository to be able to sign into container registry. Read more on this in GitHub's documentation.

Docker

export SPRING_DATASOURCE_URL='jdbc:postgresql://database-container:5432/dbname'
export SPRING_DATASOURCE_USERNAME='spring_user'
export SPRING_DATASOURCE_PASSWORD='SpringUserPassword'

export POSTGRES_URL='jdbc:postgresql://localhost:5432'
export POSTGRES_DB='dbname'
export POSTGRES_USER='postgres'
export POSTGRES_PASSWORD='SuperuserPassword'

./bin/run.py --apply-migrations [--debug]

The application is now listening at http://localhost:8080. If the --debug option has been used, the debugger is listening on port 8000.

Defaults such as database and Spring ports, and volume, network, image, container names can be adjusted inside ./bin/config.ini.

Clean

./bin/teardown.py [--cache --db --network --tmp]

Change spring_user database password

ALTER USER spring_user WITH PASSWORD '<new_password>';

Test

Unit tests

./gradlew test

Integration tests

./bin/integration_tests.py

API

/api/welcome

GET

Returns all welcome messages.

Example
# {"id":"c3f7bfa4-fac9-46bd-b139-866e34b1f83f","content":"Foo"}
curl http://localhost:8080/api/welcome

POST

Stores a new welcome message in the database.

Request body:

  • content – welcome message content.
Example
curl \
    --request POST \
    --header 'Content-Type: application/json' \
    --data '{"content": "Bar"}' \
    http://localhost:8080/api/welcome

White-label clean-up

Places around the project that need renaming.

Click here to expand
  1. .github/workflows/docker.yml:
    • MAIN_IMAGE: 'renameme'
    • export POSTGRES_DB='renameme'
    • export SPRING_DATASOURCE_URL='jdbc:postgresql://renameme-database:5432/renameme'
  2. bin/:
    1. config.ini:
      • database_container = renameme-database
      • database_test_container = renameme-test-database
      • network = renameme-network
      • test_network = renameme-test-network
      • main_image = renameme
      • build_image = renameme-gradle-build
  3. src/:
    1. Package name:
      • src/main/java/me/rename/renameme
      • src/test/java/me/rename/renameme
    2. main/resources/:
      1. db/changelog/20210310-03--create-readwrite-role.sql:
        • REVOKE ALL ON DATABASE renameme FROM PUBLIC;
        • GRANT CONNECT ON DATABASE renameme TO readwrite;
        • --rollback REVOKE CONNECT ON DATABASE renameme FROM readwrite;
        • --rollback GRANT ALL ON DATABASE renameme TO PUBLIC;
      2. log4j2.xml:
        • fileName="log/renameme.log"
        • filePattern="log/renameme-%d{yyyy-MM-dd}-%i.log.gz"
        • <IfFileName glob="log/renameme-*.log.gz"/>
    3. test/resources/:
      1. log4j2.xml:
        • fileName="log/test/renameme.log"
        • filePattern="log/test/renameme-%d{yyyy-MM-dd}-%i.log.gz"
        • <IfFileName glob="log/test/renameme-*.log.gz"/>
  4. build.gradle:
    • group: 'me.rename'
  5. settings.gradle:
    • rootProject.name = 'renameme'