Skip to content

javagrails/battery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

battery

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

Repository Clone and Application Run

  1. Docker engine must be installed in developer machine.
  2. 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 ]
  1. Redis and Postgresql run using docker
docker-compose -f src/main/docker/pgredis.yml up
  1. Run battery application
Development -> ./mvnw spring-boot:run -Dspring-boot.run.profiles=dev
Production  -> ./mvnw spring-boot:run -Dspring-boot.run.profiles=prod
  1. Run Tests of battery application
    ./mvnw verify 
    OR 
    ./mvnw test
  1. Dockerized battery application
    1. docker build -t battery:latest .
    2. docker-compose -f battery.yml up

Important URLs and Instructions to Use the Application

Both Dev run or Prod run or Dockerized app run (above point 6) you will get below urls for test

  1. 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. Resources
  2. 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 Resources redis-cli Resources
  3. Swagger-UI http://localhost:8080/swagger-ui/index.html very important you can test api end points from here Resources

REST api run and cache check

  1. 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
  1. 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
  1. 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: */*'

Technologies used

Java, Spring Boot and Maven Profile

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  

Database and Cache

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

Resources

Logback fog logging

Details here src/main/resources/logback.xml
File write here logs/battery.log

Jacoco code coverage, Unit Test, Testcontainers

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");

Resources

target/site/jacoco/index.html

Resources

Docker and Dockerized Application

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 and swagger-ui

Swagger dependencies are added in pom.xml
    You will get swagger-ui here http://localhost:8080/swagger-ui/index.html

About

Java Background Thread, Spring Boot, REST api, Redis, Profile, Postgresql, Docker

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published