diff --git a/src/main/java/catering/inventory/InventoryController.java b/src/main/java/catering/inventory/InventoryController.java index 679595d..6bfc62f 100644 --- a/src/main/java/catering/inventory/InventoryController.java +++ b/src/main/java/catering/inventory/InventoryController.java @@ -31,6 +31,8 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import catering.catalog.CateringCatalog; +import catering.catalog.Consumable; +import catering.catalog.Rentable; import jakarta.validation.Valid; /* @@ -94,13 +96,13 @@ class InventoryController { @PostMapping(path = "/inventory/edit/{pid}", params = "type=Consumable") String editConsumable(@Valid @ModelAttribute("form") ConsumableMutateForm form, Errors result, - @PathVariable Product pid, Model model) { + @PathVariable Consumable pid, Model model) { return edit(form, result, pid, model); } @PostMapping(path = "/inventory/edit/{pid}", params = "type=Rentable") String editRentable(@Valid @ModelAttribute("form") RentableMutateForm form, Errors result, - @PathVariable Product pid, Model model) { + @PathVariable Rentable pid, Model model) { return edit(form, result, pid, model); } diff --git a/src/test/java/catering/inventory/InventoryControllerIntegrationTests.java b/src/test/java/catering/inventory/InventoryControllerIntegrationTests.java index 202793c..cf95dc1 100644 --- a/src/test/java/catering/inventory/InventoryControllerIntegrationTests.java +++ b/src/test/java/catering/inventory/InventoryControllerIntegrationTests.java @@ -55,6 +55,7 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import catering.catalog.CateringCatalog; import catering.catalog.Consumable; import catering.catalog.Rentable; import catering.order.OrderType; @@ -68,15 +69,39 @@ class InventoryControllerIntegrationTests { @Autowired UniqueInventory inventory; + @Autowired + CateringCatalog catalog; + UniqueInventoryItem anyInventoryItem; Product anyProduct; ProductIdentifier anyPid; + UniqueInventoryItem anyConsumableItem; + UniqueInventoryItem anyRentableItem; + + UniqueInventoryItem anyItemOf(Class type) { + switch (type.getSimpleName()) { + case "Consumable": + return anyConsumableItem; + case "Rentable": + return anyRentableItem; + default: + throw new IllegalArgumentException("Invalid product type!"); + } + } @BeforeEach void populateAnyInventoryItem() { anyInventoryItem = inventory.findAll().stream().findAny().get(); anyProduct = anyInventoryItem.getProduct(); anyPid = anyProduct.getId(); + anyConsumableItem = inventory.save(new UniqueInventoryItem( + catalog.save(new Consumable("Any Consumable", Money.of(1, EURO), Money.of(0.5, EURO), + Optional.of(Money.of(0.75, EURO)), Set.of(OrderType.EVENT_CATERING, OrderType.PARTY_SERVICE))), + Quantity.of(1))); + anyRentableItem = inventory.save(new UniqueInventoryItem( + catalog.save(new Rentable("Any Rentable", Money.of(1, EURO), Money.of(0.5, EURO), + Set.of(OrderType.EVENT_CATERING, OrderType.PARTY_SERVICE))), + Quantity.of(1))); } @Test @@ -295,7 +320,9 @@ class InventoryControllerIntegrationTests { @Test void checkPermissionsForEditPage() throws Exception { for (Class type : Set.of(Consumable.class, Rentable.class)) { - checkPermissions(get("/inventory/edit/" + anyPid).queryParam("type", type.getSimpleName()), + checkPermissions( + get("/inventory/edit/" + anyItemOf(type).getProduct().getId()) + .queryParam("type", type.getSimpleName()), LOGIN, FORBIDDEN, OK); } } @@ -303,7 +330,9 @@ class InventoryControllerIntegrationTests { @Test void checkPermissionsForEdit() throws Exception { for (Class type : Set.of(Consumable.class, Rentable.class)) { - checkPermissions(post("/inventory/edit/" + anyPid).queryParam("type", type.getSimpleName()), + checkPermissions( + post("/inventory/edit/" + anyItemOf(type).getProduct().getId()) + .queryParam("type", type.getSimpleName()), LOGIN, FORBIDDEN, OK); } }