Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.pillopl.library.lending.LendingTestContext;
import io.pillopl.library.lending.book.model.BookFixture;
import io.pillopl.library.lending.patron.application.hold.CancelingHold;
import io.pillopl.library.lending.patron.application.hold.PlacingOnHold;
import io.pillopl.library.lending.patron.model.PatronFixture;
import io.pillopl.library.lending.patron.model.PatronId;
import io.pillopl.library.lending.patronprofile.model.Checkout;
Expand All @@ -20,6 +21,7 @@
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.hateoas.MediaTypes;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
Expand All @@ -37,6 +39,7 @@
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand All @@ -60,6 +63,9 @@ public class PatronProfileControllerIT {
@MockBean
private PatronProfiles patronProfiles;

@MockBean
private PlacingOnHold placingOnHold;

@MockBean
private CancelingHold cancelingHold;

Expand Down Expand Up @@ -160,6 +166,32 @@ public void shouldReturnResourceForCheckout() throws Exception {
.andExpect(jsonPath("$._links.self.href", containsString("profiles/" + patronId.getPatronId() + "/checkouts/" + anotherBook.getBookId())));
}

@Test
public void shouldPlaceBookOnHold() throws Exception {
given(placingOnHold.placeOnHold(any())).willReturn(Try.success(Success));
var request = "{\"bookId\":\"6e1dfec5-5cfe-487e-814e-d70114f5396e\", \"libraryBranchId\":\"a518e2ef-5f6c-43e3-a7fc-5d895e15be3a\",\"numberOfDays\":1}";

// expect
mvc.perform(post("/profiles/" + patronId.getPatronId() + "/holds")
.accept(MediaTypes.HAL_FORMS_JSON_VALUE)
.contentType(MediaType.APPLICATION_JSON)
.content(request))
.andExpect(status().isOk());
}

@Test
public void shouldReturn500IfSomethingFailedWhileDuringPlacingOnHold() throws Exception {
given(placingOnHold.placeOnHold(any())).willReturn(Try.failure(new IllegalArgumentException()));
var request = "{\"bookId\":\"6e1dfec5-5cfe-487e-814e-d70114f5396e\", \"libraryBranchId\":\"a518e2ef-5f6c-43e3-a7fc-5d895e15be3a\",\"numberOfDays\":1}";

// expect
mvc.perform(post("/profiles/" + patronId.getPatronId() + "/holds")
.accept(MediaTypes.HAL_FORMS_JSON_VALUE)
.contentType(MediaType.APPLICATION_JSON)
.content(request))
.andExpect(status().isInternalServerError());
}

@Test
public void shouldCancelExistingHold() throws Exception {
given(patronProfiles.fetchFor(patronId)).willReturn(profiles());
Expand Down Expand Up @@ -199,4 +231,4 @@ PatronProfile profiles() {
new HoldsView(of(new Hold(bookId, anyDate))),
new CheckoutsView(of(new Checkout(anotherBook, anotherDate))));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ ResponseEntity<EntityModel<Checkout>> findCheckout(@PathVariable UUID patronId,
.getOrElse(notFound().build());
}

@PostMapping("/holds")
ResponseEntity placeHold(@RequestBody PlaceHoldRequest request) {
@PostMapping("/profiles/{patronId}/holds")
ResponseEntity placeHold(@PathVariable UUID patronId, @RequestBody PlaceHoldRequest request) {
Try<Result> result = placingOnHold.placeOnHold(
new PlaceOnHoldCommand(
Instant.now(),
new PatronId(request.getPatronId()),
new PatronId(patronId),
new LibraryBranchId(request.getLibraryBranchId()),
new BookId(request.getBookId()),
Option.of(request.getNumberOfDays())
Expand Down Expand Up @@ -183,7 +183,6 @@ class Checkout {
@AllArgsConstructor(onConstructor = @__(@JsonCreator))
class PlaceHoldRequest {
UUID bookId;
UUID patronId;
UUID libraryBranchId;
Integer numberOfDays;
}