|
1 | 1 | package io.pillopl.library.lending.patron.application.hold |
2 | 2 |
|
3 | | -import io.pillopl.library.commons.commands.Result |
| 3 | +import io.pillopl.library.catalogue.BookId |
4 | 4 | import io.pillopl.library.lending.book.model.BookDuplicateHoldFound |
5 | | -import io.pillopl.library.lending.book.model.BookOnHold |
6 | | -import io.pillopl.library.lending.patron.model.Patron |
7 | | -import io.pillopl.library.lending.patron.model.PatronEvent |
8 | | -import io.pillopl.library.lending.patron.model.PatronFixture |
9 | 5 | import io.pillopl.library.lending.patron.model.PatronId |
10 | | -import io.pillopl.library.lending.patron.model.Patrons |
11 | | -import io.vavr.control.Option |
12 | | -import io.vavr.control.Try |
13 | 6 | import spock.lang.Specification |
14 | 7 |
|
| 8 | +import java.time.Clock |
| 9 | +import java.time.Instant |
| 10 | +import java.time.ZoneOffset |
| 11 | + |
15 | 12 | import static io.pillopl.library.lending.book.model.BookFixture.anyBookId |
16 | | -import static io.pillopl.library.lending.book.model.BookFixture.bookOnHold |
17 | 13 | import static io.pillopl.library.lending.librarybranch.model.LibraryBranchFixture.anyBranch |
18 | 14 | import static io.pillopl.library.lending.patron.model.PatronFixture.anyPatronId |
19 | | -import static io.pillopl.library.lending.patron.model.PatronFixture.regularPatron |
20 | 15 | import static java.time.Instant.now |
21 | 16 |
|
22 | 17 | class HandleDuplicateHoldTest extends Specification { |
23 | 18 |
|
24 | | - BookOnHold bookOnHold = bookOnHold() |
25 | | - PatronId patronId = anyPatronId() |
26 | | - |
27 | | - FindBookOnHold willFindBook = { bookId, patronId -> Option.of(bookOnHold) } |
28 | | - Patrons repository = Stub() |
| 19 | + CancelingHold cancelingHold = Mock() |
29 | 20 |
|
30 | | - def "should successfully cancel hold if book was hold by the patron"() { |
| 21 | + def "should start cancelling hold if book was already hold by other patron"() { |
31 | 22 | given: |
32 | | - CancelingHold cancelingHold = new CancelingHold(willFindBook, repository) |
33 | | - HandleDuplicateHold duplicateHold = new HandleDuplicateHold(cancelingHold) |
| 23 | + Clock clock = Clock.fixed(Instant.parse('2020-02-27T12:21:00Z'), ZoneOffset.UTC) |
34 | 24 | and: |
35 | | - persistedRegularPatronWithBookOnHold() |
36 | | - when: |
37 | | - Try<Result> result = duplicateHold.handle(duplicateHoldFoundBy(patronId)) |
38 | | - then: |
39 | | - result.isSuccess() |
40 | | - result.get() == Result.Success |
41 | | - } |
42 | | - |
43 | | - def "should reject cancelling hold if book was not hold by the patron"() { |
44 | | - given: |
45 | | - CancelingHold cancelingHold = new CancelingHold(willFindBook, repository) |
46 | | - HandleDuplicateHold duplicateHold = new HandleDuplicateHold(cancelingHold) |
| 25 | + HandleDuplicateHold duplicateHold = new HandleDuplicateHold(cancelingHold, clock) |
| 26 | + and: |
| 27 | + BookDuplicateHoldFound bookDuplicateHoldFound = duplicateHoldFoundBy() |
47 | 28 | and: |
48 | | - persistedRegularPatron() |
| 29 | + CancelHoldCommand cancelHoldCommand = cancelHoldCommandFrom(bookDuplicateHoldFound, clock) |
49 | 30 | when: |
50 | | - Try<Result> result = duplicateHold.handle(duplicateHoldFoundBy(patronId)) |
| 31 | + duplicateHold.handle(bookDuplicateHoldFound) |
51 | 32 | then: |
52 | | - result.isSuccess() |
53 | | - result.get() == Result.Rejection |
| 33 | + 1 * cancelingHold.cancelHold(cancelHoldCommand) |
54 | 34 | } |
55 | 35 |
|
56 | | - PatronId persistedRegularPatronWithBookOnHold() { |
57 | | - Patron patron = PatronFixture.regularPatronWithHold(bookOnHold) |
58 | | - repository.findBy(patronId) >> Option.of(patron) |
59 | | - repository.publish(_ as PatronEvent) >> patron |
60 | | - return patronId |
61 | | - } |
62 | | - |
63 | | - PatronId persistedRegularPatron() { |
64 | | - Patron patron = regularPatron(patronId) |
65 | | - repository.findBy(patronId) >> Option.of(patron) |
66 | | - repository.publish(_ as PatronEvent) >> patron |
67 | | - return patronId |
68 | | - } |
69 | | - |
70 | | - BookDuplicateHoldFound duplicateHoldFoundBy(PatronId patron) { |
| 36 | + BookDuplicateHoldFound duplicateHoldFoundBy() { |
71 | 37 | return new BookDuplicateHoldFound( |
72 | 38 | now(), |
73 | 39 | anyPatronId().patronId, |
74 | | - patron.patronId, |
| 40 | + anyPatronId().patronId, |
75 | 41 | anyBranch().libraryBranchId, |
76 | 42 | anyBookId().bookId |
77 | 43 | ) |
78 | 44 | } |
79 | 45 |
|
| 46 | + CancelHoldCommand cancelHoldCommandFrom(BookDuplicateHoldFound event, Clock clock) { |
| 47 | + return new CancelHoldCommand( |
| 48 | + clock.instant(), |
| 49 | + new PatronId(event.getSecondPatronId()), |
| 50 | + new BookId(event.getBookId()) |
| 51 | + ) |
| 52 | + } |
| 53 | + |
80 | 54 | } |
0 commit comments