Stack

Simple REST backend service.

  • Sprong Boot
  • Unit Test 4
  • Mockito
  • HSQL

Task

  • Client can apply for loan asking for amount and term
  • Loan application evaluation: Application is risky if:
    • Loan application is done for max amount and loan application is created between 0:00–6:00
    • Client Applied 3 times using the same IP address
  • Loan is granted if loan application goes thru risk evaluation without errors.
  • Once within loan term client can apply for term to be prolonged up to 14 days.

Analyse

I assume that the risky loan request will be rejected.

Loan entity

static MAX_LOAN = 1000 – overrun

  • loanId Int artifical id.
  • Amount Long
  • LoanName String
  • Ip String
  • TermAt Date
  • historyLog String – info about allowed operations.
  • CreatedAt Date
  • UpdatedAT Date

example:

{
    "id": 4,
    "amount": 999,
    "loanName": "IBM",
    "ip": "192.168.1.6",
    "termAt": "2020-02-22T16:01:08.756+0000",
    "historyLog": null,
    "createdAt": "2020-02-08T16:01:08.756+0000",
    "loanId": 4,
    "curentTime": "2020-02-08T16:01:08.900+0000",
    "prolongedTerm": false,
    "curentIp": "192.168.1.6"
}

riskEvaluation

  • amount > MAX_LOAN.
  • createdAt <0:00 – 6:00>.
  • ip same for 3 last apply in the same loanName.

UnitTest

  • Take Loan < MAX_LOAN out of <00:00 – 06:00> – accepted.
  • Take loan > MAX_LOAN – declined.
  • Take loan from different IP.
  • Take loan in different time <00:00 – 06:00>.
  • TrolongTerm – check changes.
  • Prolong two times loan – declined.

Impementation

  • spring-boot-rest
  • sample init data HSQL in import.sql
  • junit test

Setting environment (InteliJ)

  • http://start.spring.io/pom.xml
  • -add-opens and dependency for jaxb-api for suppres warning Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 StackOverflow
  • GROOVY_TURN_OFF_JAVA_WARNINGS=true temp fix WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1
  • sample init data HSQL in import.sql.

REST APIs & logic

The app defines following simple CRU APIs. Without delete.

MAX_LOAN = 1000 define loan overrun defined in entity model as column.

@Column
@NotNull(message = "Please enter correct amount of loan")
@Min(0)
@Max(MAX_LOAN)
private long Amount;

Important case for variable entity ie nameLoan, cratedAt.

{
    "prolongedTerm": false,
    "loanName": "IBM",
    "ip": "127.0.0.1",
    "loanId": 1,
    "createdAt": "2020-02-06T22:19:44.000+0000",
    "termAt": "2020-03-06T22:19:44.000+0000",
    "amount": 999
}

Resposne:

{
    "id": 4,
    "amount": 999,
    "loanName": "IBM",
    "ip": "192.168.1.6",
    "termAt": "2020-02-22T16:01:08.756+0000",
    "historyLog": null,
    "createdAt": "2020-02-08T16:01:08.756+0000",
    "loanId": 4,
    "curentTime": "2020-02-08T16:01:08.900+0000",
    "prolongedTerm": false,
    "curentIp": "192.168.1.6"
}
{
    "prolongedTerm": false,
    "loanName": "Cabacki",
    "ip": "127.0.0.1",
    "loanId": 1,
    "createdAt": "2020-02-06T22:19:44.000+0000",
    "termAt": "2020-03-06T22:19:44.000+0000"
}

You can test them using postman (or any other rest client). Postman test collection: https://www.getpostman.com/collections/f4f319431e96e245d5a8

HSQL at mem

spring.datasource.url: jdbc:hsqldb:mem:restdb

Database Initialization

In a JPA-based applications, we can either choose to let Hibernate create the schema using entity classes or use schema.sql, but we cannot do both. Make sure to disable spring.jpa.hibernate.ddl-auto if using schema.sql in application.properties

application.properties

spring.jpa.hibernate.ddl-auto=on

schema.sql

drop table if exists loans CASCADE
  
create table loans (
       Id bigint generated by default as identity (start with 1),
        Amount varbinary(255) not null,
        CreatedAt timestamp not null,
        Ip varchar(255),
        LoanName varchar(20),
        ProlongedTerm boolean,
        TermAt timestamp,
        UpdatedAt timestamp,
        primary key (Id)
    )

Sample init data HSQL in import.sql.

Code

https://github.com/karol-preiskorn/LoanREST

Help for Spring Boot REST

This module contains articles about Spring Boot RESTful APIs.

Relevant Articles

E-book

These articles are part of the Spring REST E-book:

  1. Bootstrap a Web Application with Spring 5
  2. Build a REST API with Spring and Java Config
  3. Http Message Converters with the Spring Framework
  4. Spring’s RequestBody and ResponseBody Annotations
  5. Entity To DTO Conversion for a Spring REST API
  6. Error Handling for REST with Spring
  7. REST API Discoverability and HATEOAS
  8. An Intro to Spring HATEOAS
  9. REST Pagination in Spring
  10. Test a REST API with Java