Skip to content

Commit

Permalink
test: add model layer tests for loan-service
Browse files Browse the repository at this point in the history
  • Loading branch information
thisdudkin committed Oct 10, 2024
1 parent 7a89a2b commit 8a145df
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.earlspilner.loans.model;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.builder.ToStringBuilder;
Expand All @@ -20,18 +22,22 @@ public class Loan {
private Integer id;

@Setter
@NotNull
@Column(name = "user_id", nullable = false, updatable = false)
private Integer userId;

@Setter
@NotNull
@Column(name = "book_id", nullable = false, updatable = false)
private Integer bookId;

@Setter
@NotNull
@Column(name = "issued_at", nullable = false)
private Instant issuedAt;

@Setter
@NotNull
@Column(name = "due_to", nullable = false)
private Instant dueTo;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
INSERT INTO loans (user_id, book_id, issued_at, due_to, returned_at)
VALUES (1, 101, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP + INTERVAL '14' DAY, NULL),
(2, 102, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP + INTERVAL '7' DAY, NULL),
(3, 103, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP + INTERVAL '30' DAY, NULL);
14 changes: 14 additions & 0 deletions library-api-loan-service/src/main/resources/db/hsqldb/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
DROP TABLE IF EXISTS loans;

CREATE TABLE IF NOT EXISTS loans
(
id INTEGER IDENTITY PRIMARY KEY,
user_id BIGINT NOT NULL,
book_id BIGINT NOT NULL,
issued_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
due_to TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL,
returned_at TIMESTAMP WITH TIME ZONE
);

CREATE INDEX IF NOT EXISTS idx_loans_user_id ON loans (user_id);
CREATE INDEX IF NOT EXISTS idx_loans_book_id ON loans (book_id);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.earlspilner.loans;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

/**
* @author Alexander Dudkin
*/
@SpringBootTest
class LoanServiceApplicationTests {

@Test
void contextLoads() {
// Test the Spring configuration
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package dev.earlspilner.loans.model;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validator;
import org.junit.jupiter.api.Test;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

import java.util.Set;

import static java.time.Instant.now;
import static java.time.temporal.ChronoUnit.DAYS;
import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Alexander Dudkin
*/
class ValidatorTests {

private Validator createValidator() {
LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
localValidatorFactoryBean.afterPropertiesSet();
return localValidatorFactoryBean;
}

@Test
void shouldValidate() {
Loan loan = new Loan();
loan.setUserId(5);
loan.setBookId(5);
loan.setIssuedAt(now());
loan.setDueTo(now().plus(30, DAYS));

Validator validator = createValidator();
Set<ConstraintViolation<Loan>> constraintViolations = validator.validate(loan);

assertThat(constraintViolations.size()).isZero();
}

@Test
void shouldNotValidateWhenUserIdEmpty() {
Loan loan = new Loan();
loan.setBookId(5);
loan.setIssuedAt(now());
loan.setDueTo(now().plus(30, DAYS));

Validator validator = createValidator();
Set<ConstraintViolation<Loan>> constraintViolations = validator.validate(loan);

assertThat(constraintViolations.size()).isEqualTo(1);
ConstraintViolation<Loan> violation = constraintViolations.iterator().next();
assertThat(violation.getPropertyPath().toString()).isEqualTo("userId");
assertThat(violation.getMessage()).endsWith("null");
}

@Test
void shouldNotValidateWhenBookIdEmpty() {
Loan loan = new Loan();
loan.setUserId(5);
loan.setIssuedAt(now());
loan.setDueTo(now().plus(30, DAYS));

Validator validator = createValidator();
Set<ConstraintViolation<Loan>> constraintViolations = validator.validate(loan);

assertThat(constraintViolations.size()).isEqualTo(1);
ConstraintViolation<Loan> violation = constraintViolations.iterator().next();
assertThat(violation.getPropertyPath().toString()).isEqualTo("bookId");
assertThat(violation.getMessage()).endsWith("null");
}

@Test
void shouldNotValidateWhenIssuedAtEmpty() {
Loan loan = new Loan();
loan.setUserId(5);
loan.setBookId(5);
loan.setDueTo(now().plus(30, DAYS));

Validator validator = createValidator();
Set<ConstraintViolation<Loan>> constraintViolations = validator.validate(loan);

assertThat(constraintViolations.size()).isEqualTo(1);
ConstraintViolation<Loan> violation = constraintViolations.iterator().next();
assertThat(violation.getPropertyPath().toString()).isEqualTo("issuedAt");
assertThat(violation.getMessage()).endsWith("null");
}

@Test
void shouldNotValidateWhenDueToEmpty() {
Loan loan = new Loan();
loan.setUserId(5);
loan.setBookId(5);
loan.setIssuedAt(now());

Validator validator = createValidator();
Set<ConstraintViolation<Loan>> constraintViolations = validator.validate(loan);

assertThat(constraintViolations.size()).isEqualTo(1);
ConstraintViolation<Loan> violation = constraintViolations.iterator().next();
assertThat(violation.getPropertyPath().toString()).isEqualTo("dueTo");
assertThat(violation.getMessage()).endsWith("null");
}

}
37 changes: 37 additions & 0 deletions library-api-loan-service/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# active profiles config
#
# application use two active profiles
# ------------------------------------------------
# When using HSQL, use: hsqldb
# When using PostgreSQL, use: postgres
# ------------------------------------------------

spring.profiles.active=hsqldb

# ------------------------------------------------

server.port=9091
server.servlet.context-path=/api/
spring.jpa.open-in-view=false

# database init
spring.sql.init.schema-locations=classpath*:db/hsqldb/schema.sql
spring.sql.init.data-locations=classpath*:db/hsqldb/data.sql

logging.level.org.springframework=INFO
#logging.level.org.springframework=DEBUG

#logging.level.org.hibernate.SQL=DEBUG
#logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

# ------------------------------------------------

# those are used only for testing purpose
jwt.secret.key=53A73E5F1C4E0A2D3B5F2D784E6A1B423D6F247D1F6E5C3A596D635A75327855

# ------------------------------------------------

eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false

# ------------------------------------------------

0 comments on commit 8a145df

Please sign in to comment.