Skip to content

Commit bb00638

Browse files
authored
Merge pull request #40 from starspire/fix/restful-placehold-endpoint
[IMPROVEMENT] Changed placeHold endpoint of PatronProfileController to be more restful
2 parents 421ef2d + e838aa9 commit bb00638

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/integration-test/groovy/io/pillopl/library/lending/patronprofile/web/PatronProfileControllerIT.java

+33-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.pillopl.library.lending.LendingTestContext;
66
import io.pillopl.library.lending.book.model.BookFixture;
77
import io.pillopl.library.lending.patron.application.hold.CancelingHold;
8+
import io.pillopl.library.lending.patron.application.hold.PlacingOnHold;
89
import io.pillopl.library.lending.patron.model.PatronFixture;
910
import io.pillopl.library.lending.patron.model.PatronId;
1011
import io.pillopl.library.lending.patronprofile.model.Checkout;
@@ -20,6 +21,7 @@
2021
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
2122
import org.springframework.boot.test.mock.mockito.MockBean;
2223
import org.springframework.hateoas.MediaTypes;
24+
import org.springframework.http.MediaType;
2325
import org.springframework.test.context.ContextConfiguration;
2426
import org.springframework.test.context.junit4.SpringRunner;
2527
import org.springframework.test.web.servlet.MockMvc;
@@ -37,6 +39,7 @@
3739
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
3840
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
3941
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
42+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
4043
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
4144
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
4245
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -60,6 +63,9 @@ public class PatronProfileControllerIT {
6063
@MockBean
6164
private PatronProfiles patronProfiles;
6265

66+
@MockBean
67+
private PlacingOnHold placingOnHold;
68+
6369
@MockBean
6470
private CancelingHold cancelingHold;
6571

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

169+
@Test
170+
public void shouldPlaceBookOnHold() throws Exception {
171+
given(placingOnHold.placeOnHold(any())).willReturn(Try.success(Success));
172+
var request = "{\"bookId\":\"6e1dfec5-5cfe-487e-814e-d70114f5396e\", \"libraryBranchId\":\"a518e2ef-5f6c-43e3-a7fc-5d895e15be3a\",\"numberOfDays\":1}";
173+
174+
// expect
175+
mvc.perform(post("/profiles/" + patronId.getPatronId() + "/holds")
176+
.accept(MediaTypes.HAL_FORMS_JSON_VALUE)
177+
.contentType(MediaType.APPLICATION_JSON)
178+
.content(request))
179+
.andExpect(status().isOk());
180+
}
181+
182+
@Test
183+
public void shouldReturn500IfSomethingFailedWhileDuringPlacingOnHold() throws Exception {
184+
given(placingOnHold.placeOnHold(any())).willReturn(Try.failure(new IllegalArgumentException()));
185+
var request = "{\"bookId\":\"6e1dfec5-5cfe-487e-814e-d70114f5396e\", \"libraryBranchId\":\"a518e2ef-5f6c-43e3-a7fc-5d895e15be3a\",\"numberOfDays\":1}";
186+
187+
// expect
188+
mvc.perform(post("/profiles/" + patronId.getPatronId() + "/holds")
189+
.accept(MediaTypes.HAL_FORMS_JSON_VALUE)
190+
.contentType(MediaType.APPLICATION_JSON)
191+
.content(request))
192+
.andExpect(status().isInternalServerError());
193+
}
194+
163195
@Test
164196
public void shouldCancelExistingHold() throws Exception {
165197
given(patronProfiles.fetchFor(patronId)).willReturn(profiles());
@@ -199,4 +231,4 @@ PatronProfile profiles() {
199231
new HoldsView(of(new Hold(bookId, anyDate))),
200232
new CheckoutsView(of(new Checkout(anotherBook, anotherDate))));
201233
}
202-
}
234+
}

src/main/java/io/pillopl/library/lending/patronprofile/web/PatronProfileController.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ ResponseEntity<EntityModel<Checkout>> findCheckout(@PathVariable UUID patronId,
9797
.getOrElse(notFound().build());
9898
}
9999

100-
@PostMapping("/holds")
101-
ResponseEntity placeHold(@RequestBody PlaceHoldRequest request) {
100+
@PostMapping("/profiles/{patronId}/holds")
101+
ResponseEntity placeHold(@PathVariable UUID patronId, @RequestBody PlaceHoldRequest request) {
102102
Try<Result> result = placingOnHold.placeOnHold(
103103
new PlaceOnHoldCommand(
104104
Instant.now(),
105-
new PatronId(request.getPatronId()),
105+
new PatronId(patronId),
106106
new LibraryBranchId(request.getLibraryBranchId()),
107107
new BookId(request.getBookId()),
108108
Option.of(request.getNumberOfDays())
@@ -183,7 +183,6 @@ class Checkout {
183183
@AllArgsConstructor(onConstructor = @__(@JsonCreator))
184184
class PlaceHoldRequest {
185185
UUID bookId;
186-
UUID patronId;
187186
UUID libraryBranchId;
188187
Integer numberOfDays;
189188
}

0 commit comments

Comments
 (0)