Application battery covers technologies Java background thread, Spring Boot, REST api, Redis, Spring and Maven Profile, Postgresql, Docker, Dockerized Spring Boot battery application for Production, Jacoco code coverage, Unit Test, Testcontainers, swagger and logback for logging
- Docker engine must be installed in developer machine.
- Clone the repository using command
git clone git@github.com:javagrails/battery.git
OR
git clone https://github.com/javagrails/battery.git
then enter the folder using bash command [ cd battery ]
- Redis and Postgresql run using docker
docker-compose -f src/main/docker/pgredis.yml up
- Run battery application
Development -> ./mvnw spring-boot:run -Dspring-boot.run.profiles=dev
Production -> ./mvnw spring-boot:run -Dspring-boot.run.profiles=prod
- Run Tests of battery application
./mvnw verify
OR
./mvnw test
- Dockerized battery application
1. docker build -t battery:latest .
2. docker-compose -f battery.yml up
Both Dev run or Prod run or Dockerized app run (above point 6) you will get below urls for test
- http://localhost:8080/api/index it will show message like - Application { battery-prod } is running !!!! based on environment, so first proof your application is running, great.
- BatteryRedisAdmin UI http://localhost:7777/?overview you can look Cached data here based on keys or you can use redis-cli in the docker container see screen shoot
redis-cli
- Swagger-UI http://localhost:8080/swagger-ui/index.html very important you can test api end points from here
- To create batteries you can use these curl command files look at folder httpreqs in the project root
httpreqs/create-batteries.http
OR
httpreqs/create-batteries-v1.http
- To get range information use below files and just after running these curl command files look at the redis cache here http://localhost:7777/?overview you will get data cached and if you again hit the curl commands it will provide data from cache not from sql server, fantastic.
httpreqs/batteries-range.http
OR
batteries-range.http
- Sample dat and curl Requests A sample json data file is here src/main/resources/sample.json
CREATE REQUEST
curl -X POST \
'http://localhost:8080/api/batteries' \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d '[
{
"name": "Cannington",
"postcode": "6107",
"wattCapacity": 13500
},
{
"name": "Midland",
"postcode": "6057",
"wattCapacity": 50500
},
{
"name": "Gold Coast Mc",
"postcode": "9729",
"wattCapacity": 50000
}
]'
GET REQUEST
curl -X GET \
'http://localhost:8080/api/batteries/range?startPostcode=0820&endPostcode=9729&minCapacity=1000&maxCapacity=10000&page=0&size=10&sort=name,asc' \
-H 'accept: */*'
Java : 21
Spring Boot : 3.4.5
Packaging : jar
Spring Profiles
src/main/resources/application.yml
src/main/resources/application-dev.yml
src/main/resources/application-prod.yml
src/test/resources/application-test.yml
Maven Profiles
pom.xml
<profiles>
<profile>
<id>dev</id>
</profile>
<profile>
<id>prod</id>
</profile>
<profile>
<id>swagger</id>
</profile>
<profiles>
More : REST, Lombok, Mapstruct
Development and Test inmemory H2DB
Production PostgreSQL 16.9 (Debian 16.9-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
Redis:7 or Redis:latest used as Cache server for development, testcontainer and production environment
You will get Redis UI here http://localhost:7777/?overview
Details here src/main/resources/logback.xml
File write here logs/battery.log
Necessary dependencies are added in pom.xml
Jacoco generates report at target/site/jacoco/index.html after any of the below command
./mvnw verify or ./mvnw test
Unit tests are written
src/test/java/bk/battery/app/service/BatteryServiceUnitTest.java
src/test/java/bk/battery/app/service/BatteryServiceV1UnitTest.java
Testcontainers apply on below integration test
src/test/java/bk/battery/app/web/rest/BatteryResourceIT.java
Some code java snip like below from the above class and image snap
@Container
private static final GenericContainer<?> redisContainer = new GenericContainer<>(
DockerImageName.parse("redis:7.0"))
.withExposedPorts(6379);
@Container
private static final PostgreSQLContainer<?> postgresContainer = new PostgreSQLContainer<>(
DockerImageName.parse("postgres:16"))
.withDatabaseName("testdb")
.withUsername("test")
.withPassword("test");target/site/jacoco/index.html
Docker : 24.0.7
Battery docker image building using Dockerfile att application root folder
Docker Compose Files
Development here src/main/docker/pgredis.yml
Battery compose battery.yml file in application root folder
Swagger dependencies are added in pom.xml
You will get swagger-ui here http://localhost:8080/swagger-ui/index.html