-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add model layer tests for loan-service
- Loading branch information
1 parent
7a89a2b
commit 8a145df
Showing
6 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
library-api-loan-service/src/main/resources/db/hsqldb/data.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
library-api-loan-service/src/main/resources/db/hsqldb/schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
17 changes: 17 additions & 0 deletions
17
...ary-api-loan-service/src/test/java/dev/earlspilner/loans/LoanServiceApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
103 changes: 103 additions & 0 deletions
103
library-api-loan-service/src/test/java/dev/earlspilner/loans/model/ValidatorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
library-api-loan-service/src/test/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
# ------------------------------------------------ |