From 34c7c96c70e08411c3d74fe167dfecfc8dd0d924 Mon Sep 17 00:00:00 2001 From: Mathis Kral Date: Thu, 7 Dec 2023 16:58:29 +0100 Subject: [PATCH] Associate rentables with order and event explicitly This closes #74 Co-authored-by: Theo Reichert --- .../catering/catalog/CateringCatalog.java | 13 +- src/main/java/catering/catalog/Rentable.java | 5 + src/main/java/catering/order/CustomCart.java | 49 ++++++- src/main/java/catering/order/CustomOrder.java | 8 +- .../catering/order/CustomOrderRepository.java | 24 ++++ .../java/catering/order/OrderController.java | 129 +++++++++++------- src/main/java/catering/order/OrderType.java | 13 +- .../orderCatalog/CatalogController.java | 6 - .../resources/templates/catalog_editor.html | 1 - src/main/resources/templates/event.html | 109 +++++++++------ src/main/resources/templates/orders.html | 14 +- .../OrderControllerIntegrationTests.java | 2 +- .../order/OrderControllerUnitTests.java | 112 +++++++++++++++ .../java/catering/order/OrderUnitTests.java | 10 +- .../StaffManagementIntegrationTests.java | 2 + 15 files changed, 368 insertions(+), 129 deletions(-) create mode 100644 src/main/java/catering/order/CustomOrderRepository.java create mode 100644 src/test/java/catering/order/OrderControllerUnitTests.java diff --git a/src/main/java/catering/catalog/CateringCatalog.java b/src/main/java/catering/catalog/CateringCatalog.java index 7262ace..b4a24b0 100644 --- a/src/main/java/catering/catalog/CateringCatalog.java +++ b/src/main/java/catering/catalog/CateringCatalog.java @@ -19,11 +19,14 @@ package catering.catalog; import org.salespointframework.catalog.Catalog; import org.salespointframework.catalog.Product; import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.util.Streamable; import org.springframework.lang.NonNull; import catering.order.OrderType; +import org.springframework.stereotype.Repository; +@Repository public interface CateringCatalog extends Catalog { static final Sort DEFAULT_SORT = Sort.sort(Product.class).by(Product::getName).ascending(); @@ -46,8 +49,14 @@ public interface CateringCatalog extends Catalog { return findByCategories(category, DEFAULT_SORT); } - Streamable findRentablesByCategories(@NonNull String category); + @Query("select p from #{#entityName} p") + Streamable findRentables(); - Streamable findConsumablesByCategories(@NonNull String category); + @Query("select p from #{#entityName} p") + Streamable findConsumables(); + + Streamable findRentablesByCategoriesContains(@NonNull String category); + + Streamable findConsumablesByCategoriesContains(@NonNull String category); } diff --git a/src/main/java/catering/catalog/Rentable.java b/src/main/java/catering/catalog/Rentable.java index 8f4d603..8218469 100644 --- a/src/main/java/catering/catalog/Rentable.java +++ b/src/main/java/catering/catalog/Rentable.java @@ -15,6 +15,7 @@ import jakarta.persistence.Entity; public class Rentable extends Product { private MonetaryAmount wholesalePrice; + private MonetaryAmount pricePerHour; @SuppressWarnings({ "deprecation" }) public Rentable() { @@ -41,4 +42,8 @@ public class Rentable extends Product { public MonetaryAmount getRetailPrice() { return super.getPrice(); } + + public MonetaryAmount getPriceForTime(int hours) { + return getPrice().multiply(hours).add(getPrice()); + } } diff --git a/src/main/java/catering/order/CustomCart.java b/src/main/java/catering/order/CustomCart.java index 842ffb5..12ea0a2 100644 --- a/src/main/java/catering/order/CustomCart.java +++ b/src/main/java/catering/order/CustomCart.java @@ -1,8 +1,13 @@ package catering.order; +import catering.catalog.Rentable; import catering.staff.Employee; import org.javamoney.moneta.Money; +import org.salespointframework.inventory.UniqueInventory; +import org.salespointframework.inventory.UniqueInventoryItem; import org.salespointframework.order.Cart; +import org.salespointframework.order.CartItem; +import org.salespointframework.order.OrderLine; import javax.money.MonetaryAmount; import java.time.Duration; @@ -63,6 +68,31 @@ public class CustomCart extends Cart { return order; } + /** + * Add ChargeLines for rental costs of each Rentable to {@param order} + */ + public CustomOrder addRentablesToOrder(CustomOrder order, UniqueInventory inventory) { + for (CartItem item : this) { + if (item.getProduct() instanceof Rentable rentable) { + OrderLine orderLine = order.getOrderLines(rentable).stream().findFirst().get(); + + order.addChargeLine( + rentable.getPrice() + .multiply(getDurationInHours()) + .multiply(item.getQuantity().getAmount()), + "rental costs", + orderLine + ); + + + // neutralises automatic reduction of rentables in inventory + inventory.save(inventory.findByProduct(rentable).get().increaseQuantity(orderLine.getQuantity())); + } + } + + return order; + } + public OrderType getOrderType() { return orderType; } @@ -80,10 +110,10 @@ public class CustomCart extends Cart { } /** - * @return hours between start and finish multiplied with a rate + * @return hours between start and finish */ - public double getDurationInHoursTimesRate(double rate) { - return (float) Duration.between(start, finish).getSeconds() / 3600 * rate; + public int getDurationInHours() { + return (int) (Duration.between(start, finish).getSeconds() / 3600); } public LocalDateTime getFinish() { @@ -100,14 +130,19 @@ public class CustomCart extends Cart { @Override public MonetaryAmount getPrice() { - MonetaryAmount total = super.getPrice(); + MonetaryAmount total = Money.of(0, "EUR"); - for (int i = 0; i < staff.size(); i++) { // TODO: get this from employee itself - total = total.add(Money.of(getDurationInHoursTimesRate(12.0), "EUR")); + for (CartItem item : this) { + if (item.getProduct() instanceof Rentable rentable) { + // final_price = (price + price * time) * quantity + total = total.add(rentable.getPriceForTime(getDurationInHours()).multiply(item.getQuantity().getAmount())); + } else { + total = total.add(item.getProduct().getPrice().multiply(item.getQuantity().getAmount())); + } } for (Employee employee : staff) { - total = total.add(Money.of(getDurationInHoursTimesRate(12), "EUR")); // TODO: get from employee + total = total.add(Money.of(getDurationInHours() * employee.getWage(), "EUR")); } return total; diff --git a/src/main/java/catering/order/CustomOrder.java b/src/main/java/catering/order/CustomOrder.java index 0c3fd3f..3724b9a 100644 --- a/src/main/java/catering/order/CustomOrder.java +++ b/src/main/java/catering/order/CustomOrder.java @@ -22,7 +22,7 @@ public class CustomOrder extends Order { private Long id; @ManyToMany private Set staff; - private OrderType orderType = OrderType.SOMETHING_ELSE; + private OrderType orderType = OrderType.EVENT_CATERING; private LocalDateTime start; private LocalDateTime finish; private boolean invoiceAvailable = false; @@ -44,7 +44,7 @@ public class CustomOrder extends Order { * Helper function to get the amount of hours in between start and finish (analogous to CustomCart) * @return hours between start and finish */ - private long getDurationInHours(LocalDateTime start, LocalDateTime finish) { + public long getDurationInHours() { return Duration.between(start, finish).getSeconds() / 3600; } @@ -52,8 +52,8 @@ public class CustomOrder extends Order { * Adds an employee to the order and adds a new {@Link ChangeLine} containing the costs */ public boolean addEmployee(Employee employee) { - MonetaryAmount cost = Money.of(12.0, "EUR") - .multiply(getDurationInHours(this.start, this.finish)); // TODO: Get salary from employee + MonetaryAmount cost = Money.of(employee.getWage(), "EUR") + .multiply(getDurationInHours()); if (this.staff.add(employee)) { super.addChargeLine(cost, employee.getId().toString()); diff --git a/src/main/java/catering/order/CustomOrderRepository.java b/src/main/java/catering/order/CustomOrderRepository.java new file mode 100644 index 0000000..ebe6c8d --- /dev/null +++ b/src/main/java/catering/order/CustomOrderRepository.java @@ -0,0 +1,24 @@ +package catering.order; + +import catering.catalog.Rentable; +import org.salespointframework.inventory.UniqueInventory; +import org.salespointframework.inventory.UniqueInventoryItem; +import org.salespointframework.order.Order; +import org.salespointframework.quantity.Quantity; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.util.Streamable; +import org.springframework.data.repository.Repository; + +import java.time.LocalDateTime; + +public interface CustomOrderRepository extends Repository{ + + @Query(""" + select order from #{#entityName} order + where + not order.start > ?2 and + not order.finish < ?1 + """) + Streamable findOrdersByInterval(LocalDateTime start, LocalDateTime finish); + +} diff --git a/src/main/java/catering/order/OrderController.java b/src/main/java/catering/order/OrderController.java index 3e47cca..ea07c6e 100644 --- a/src/main/java/catering/order/OrderController.java +++ b/src/main/java/catering/order/OrderController.java @@ -1,5 +1,8 @@ package catering.order; +import catering.catalog.CateringCatalog; +import catering.catalog.Consumable; +import catering.catalog.Rentable; import catering.staff.Employee; import catering.staff.StaffManagement; import org.salespointframework.catalog.Product; @@ -11,7 +14,6 @@ import org.salespointframework.useraccount.Role; import org.salespointframework.useraccount.UserAccount; import org.salespointframework.useraccount.web.LoggedIn; import org.springframework.data.domain.Pageable; -import org.springframework.data.util.Streamable; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -31,11 +33,15 @@ import java.util.stream.Collectors; public class OrderController { private final OrderManagement orderManagement; + private final CustomOrderRepository customOrderRepository; private final UniqueInventory inventory; + private final CateringCatalog catalog; private final StaffManagement staffManagement; - public OrderController(OrderManagement orderManagement, UniqueInventory inventory, StaffManagement staffManagement) { + public OrderController(OrderManagement orderManagement, CustomOrderRepository customOrderRepository, UniqueInventory inventory, CateringCatalog catalog, StaffManagement staffManagement) { this.orderManagement = orderManagement; + this.customOrderRepository = customOrderRepository; + this.catalog = catalog; this.inventory = inventory; this.staffManagement = staffManagement; } @@ -71,11 +77,9 @@ public class OrderController { // Obtains an instance of LocalDate from a text string such as 2007-12-03. LocalDate date = LocalDate.parse(day); - List myOrders = orderManagement.findAll(Pageable.unpaged()).stream().filter( - order -> - (order.getStart().toLocalDate().isBefore(date) || order.getStart().toLocalDate().isEqual(date)) - && (order.getFinish().toLocalDate().isAfter(date) || order.getFinish().toLocalDate().isEqual(date)) - ).collect(Collectors.toList()); + List myOrders = customOrderRepository.findOrdersByInterval(date.atStartOfDay(), + date.atStartOfDay().plusHours(23).withMinute(59).withSecond(59)) + .stream().toList(); model.addAttribute("orders", myOrders); model.addAttribute("total", myOrders.size()); @@ -84,19 +88,45 @@ public class OrderController { @ModelAttribute("event") CustomCart initializeCart() { - return new CustomCart(OrderType.SOMETHING_ELSE, LocalDateTime.now().plusDays(7), - LocalDateTime.now().plusDays(7)); + return new CustomCart(OrderType.EVENT_CATERING, LocalDateTime.now().plusDays(7), + LocalDateTime.now().plusDays(7).plusHours(1)); } @GetMapping("/event") @PreAuthorize("hasRole('CUSTOMER')") public String event(Model model, @ModelAttribute("event") CustomCart cart) { - model.addAttribute("items", cart.stream().collect(Collectors.toList())); + + model.addAttribute("cartRentables", cart.stream().filter(item -> item.getProduct() instanceof Rentable).toList()); + + model.addAttribute("cartConsumables", cart.stream().filter(item -> item.getProduct() instanceof Consumable).toList()); + model.addAttribute("totalPrice", cart.getPrice()); - model.addAttribute("invItems", inventory.findAll().stream().collect(Collectors.toList())); - Set myStaff = new HashSet<>(); - staffManagement.findAll().forEach(myStaff::add); - model.addAttribute("allStaff", myStaff); + + Map invConsumables = new HashMap<>(); + catalog.findConsumablesByCategoriesContains(cart.getOrderType().toString()).forEach(consumable -> + invConsumables.put( + consumable, + inventory.findByProduct(consumable).get().getQuantity()) + ); + + model.addAttribute("invConsumables", invConsumables); + + model.addAttribute( + "invRentables", + catalog.findRentablesByCategoriesContains(cart.getOrderType().toString()) + .stream() + .collect(Collectors.toMap(rentable -> rentable, rentable -> findFreeAmountInInterval( + rentable, + cart.getStart(), + cart.getFinish(), + inventory, + customOrderRepository)))); + + model.addAttribute("allStaff", staffManagement.findAll().stream().toList()); + + model.addAttribute("duration", cart.getDurationInHours()); + + model.addAttribute("minDate", LocalDate.now().plusDays(7)); return "event"; } @@ -140,14 +170,24 @@ public class OrderController { @PostMapping("/event/addProduct") @PreAuthorize("hasRole('CUSTOMER')") public String addProduct(@RequestParam("pid") Product product, @RequestParam("number") int number, @ModelAttribute("event") CustomCart cart) { - Quantity amount = Quantity.of(number > 0 ? number : 1); - Quantity invAmount = inventory.findByProduct(product).get().getQuantity(); // TODO ERROR HANDLING Quantity cartQuantity = cart.getQuantity(product); + Quantity available; + + if (product instanceof Rentable rentable) { + available = findFreeAmountInInterval( + rentable, + cart.getStart(), + cart.getFinish(), + inventory, + customOrderRepository); + } else { + available = inventory.findByProduct(product).get().getQuantity(); + } // check for possible miss-inputs - if (amount.add(cartQuantity).isGreaterThan(invAmount)) { - cart.addOrUpdateItem(product, cartQuantity.negate().add(invAmount)); + if (amount.add(cartQuantity).isGreaterThan(available)) { + cart.addOrUpdateItem(product, cartQuantity.negate().add(available)); } else { cart.addOrUpdateItem(product, amount); } @@ -174,7 +214,7 @@ public class OrderController { LocalDateTime startTime = LocalDateTime.of(start, LocalTime.of(unwrappedStartHour, 0)); LocalDateTime finishTime = LocalDateTime.of(finish, LocalTime.of(unwrappedFinishHour <= unwrappedStartHour - && !finish.isAfter(start) ? unwrappedStartHour+1 : unwrappedFinishHour, 0)); + && !finish.isAfter(start) ? (unwrappedStartHour+1) % 24 : unwrappedFinishHour, 0)); cart.setStart(startTime); cart.setFinish(finishTime); @@ -193,7 +233,8 @@ public class OrderController { CustomOrder myOrder = new CustomOrder(account.getId(), cart); cart.addItemsTo(myOrder); cart.addStaffTo(myOrder); - orderManagement.payOrder(myOrder); // TODO: change this later + cart.addRentablesToOrder(myOrder, inventory); + orderManagement.payOrder(myOrder); orderManagement.completeOrder(myOrder); cart.clear(); @@ -203,27 +244,26 @@ public class OrderController { @PostMapping("/event/changeOrderType") @PreAuthorize("hasRole('CUSTOMER')") - public String changeOrderType(@RequestParam(name = "type") Optional optionalOrderType, @ModelAttribute("event") CustomCart cart) { - String orderType = optionalOrderType.orElse("FOO"); - switch (orderType) { - case "RaK": - cart.setOrderType(OrderType.RENT_A_COOK); - break; - case "EK": - cart.setOrderType(OrderType.EVENT_CATERING); - break; - case "SN": - cart.setOrderType(OrderType.SUSHI_NIGHT); - break; - case "MB": - cart.setOrderType(OrderType.MOBILE_BREAKFAST); - break; - default: - cart.setOrderType(OrderType.SOMETHING_ELSE); + public String changeOrderType(@RequestParam(name = "type") String orderType, @ModelAttribute("event") CustomCart cart) { + try { + cart.setOrderType(OrderType.valueOf(orderType)); + } catch (IllegalArgumentException e) { + cart.setOrderType(OrderType.EVENT_CATERING); } return "redirect:/event"; } + public static Quantity findFreeAmountInInterval(Rentable product, LocalDateTime start, LocalDateTime finish, UniqueInventory inventory, CustomOrderRepository customOrderRepository) { + + return inventory.findByProduct(product) + .map(item -> item.getQuantity().subtract( + customOrderRepository.findOrdersByInterval(start, finish) + .flatMap(order -> order.getOrderLines(product).stream()) + .map(OrderLine::getQuantity).stream() + .reduce(Quantity.NONE, Quantity::add))) + .orElse(Quantity.NONE); + } + @GetMapping("/orders/calender") public String calender(Model model) { ArrayList> datesOfMonth = new ArrayList>(28); @@ -232,7 +272,7 @@ public class OrderController { LocalDate startDate = startDateTime.toLocalDate(); LocalDate endDate = endDateTime.toLocalDate(); - // create all dates of the calender + // create all dates of the calendar for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) { ArrayList x = new ArrayList(2); x.add(Integer.toString(date.getDayOfMonth())); @@ -240,19 +280,8 @@ public class OrderController { datesOfMonth.add(x); } - /* - * FIXME: Do not load all orders into java, - * instead query orderManagement better to only return orders overlapping with interval [startDate,endDate] - * by using "query creation" of the jpa - * e.g. this.orderManagement.findByFinishDateIsNotBeforeAndStartDateIsNotAfter(LocalDateTime startDateTime, LocalDateTime endDateTime) - */ - Streamable x = this.orderManagement.findAll(Pageable.unpaged()).filter(e -> - !e.getFinish().toLocalDate().isBefore(startDate) && // end is not before today - !e.getStart().toLocalDate().isAfter(endDate) - ); - // add each order overlapping with the calendar to the days it overlaps with - for (CustomOrder order : x) { + for (CustomOrder order : customOrderRepository.findOrdersByInterval(startDateTime, endDateTime)) { int start_index_inclusive = Math.max((int) startDate.until(order.getStart().toLocalDate(), ChronoUnit.DAYS),0); int end_index_exclusive = Math.min((int) startDate.until(order.getFinish().toLocalDate(), ChronoUnit.DAYS), 27) + 1; String order_id = Objects.requireNonNull(order.getId()).toString(); diff --git a/src/main/java/catering/order/OrderType.java b/src/main/java/catering/order/OrderType.java index 7b8a682..680c0a8 100644 --- a/src/main/java/catering/order/OrderType.java +++ b/src/main/java/catering/order/OrderType.java @@ -3,7 +3,16 @@ package catering.order; public enum OrderType { RENT_A_COOK, EVENT_CATERING, - SUSHI_NIGHT, MOBILE_BREAKFAST, - SOMETHING_ELSE + PARTY_SERVICE; + + public String toHumanReadable() { + return switch (this) { + case RENT_A_COOK -> "Miete einen Koch"; + case EVENT_CATERING -> "Veranstaltungsbewirtung"; + case MOBILE_BREAKFAST -> "Mobiles Frühstück"; + case PARTY_SERVICE -> "Sausedienst"; + default -> null; + }; + } } diff --git a/src/main/java/catering/orderCatalog/CatalogController.java b/src/main/java/catering/orderCatalog/CatalogController.java index 1ad1af3..9ca4bb6 100644 --- a/src/main/java/catering/orderCatalog/CatalogController.java +++ b/src/main/java/catering/orderCatalog/CatalogController.java @@ -117,12 +117,6 @@ public class CatalogController { @PostMapping("/catalog_editor/chooseEvent") public String chooseEvent(String events) { switch (events) { - case "event_catering": - formCatalogEntry.setEventType(OrderType.EVENT_CATERING); - break; - case "sushi_night": - formCatalogEntry.setEventType(OrderType.SUSHI_NIGHT); - break; case "mobile_breakfast": formCatalogEntry.setEventType(OrderType.MOBILE_BREAKFAST); break; diff --git a/src/main/resources/templates/catalog_editor.html b/src/main/resources/templates/catalog_editor.html index 6cade31..da98b81 100644 --- a/src/main/resources/templates/catalog_editor.html +++ b/src/main/resources/templates/catalog_editor.html @@ -13,7 +13,6 @@
diff --git a/src/main/resources/templates/event.html b/src/main/resources/templates/event.html index fffc2dd..be03bd3 100644 --- a/src/main/resources/templates/event.html +++ b/src/main/resources/templates/event.html @@ -6,29 +6,21 @@
- + - -
- -
+
+ diff --git a/src/main/resources/templates/orders.html b/src/main/resources/templates/orders.html index d5cbb47..11b313b 100644 --- a/src/main/resources/templates/orders.html +++ b/src/main/resources/templates/orders.html @@ -23,18 +23,19 @@ @@ -51,13 +52,10 @@

- - - - + - + diff --git a/src/test/java/catering/order/OrderControllerIntegrationTests.java b/src/test/java/catering/order/OrderControllerIntegrationTests.java index a89d27b..f36e76d 100644 --- a/src/test/java/catering/order/OrderControllerIntegrationTests.java +++ b/src/test/java/catering/order/OrderControllerIntegrationTests.java @@ -142,7 +142,7 @@ public class OrderControllerIntegrationTests { void userPlansEvent() throws Exception { mvc.perform(get("/event")) .andExpect(status().isOk()) - .andExpect(model().attributeExists("invItems")); + .andExpect(model().attributeExists("invRentables")); Product product = inventory.findAll().stream().findFirst().get().getProduct(); diff --git a/src/test/java/catering/order/OrderControllerUnitTests.java b/src/test/java/catering/order/OrderControllerUnitTests.java new file mode 100644 index 0000000..73ca819 --- /dev/null +++ b/src/test/java/catering/order/OrderControllerUnitTests.java @@ -0,0 +1,112 @@ +package catering.order; + +import catering.catalog.Rentable; +import org.junit.jupiter.api.*; +import org.salespointframework.inventory.UniqueInventory; +import org.salespointframework.inventory.UniqueInventoryItem; +import org.salespointframework.order.OrderManagement; +import org.salespointframework.quantity.Quantity; +import org.salespointframework.useraccount.UserAccount; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.Pageable; + +import java.time.LocalDateTime; + +import static org.assertj.core.api.Assertions.*; + +@SpringBootTest +public class OrderControllerUnitTests { + @Autowired + UniqueInventory inventory; + + @Autowired + OrderManagement orderManagement; + + @Autowired + CustomOrderRepository customOrderRepository; + + Rentable myProduct; + + @BeforeEach + void setup() { + // because of FUUUUUUUN + if (!orderManagement.findAll(Pageable.unpaged()).isEmpty()) { + return; + } + + // #1 + CustomCart myCart = new CustomCart( + OrderType.EVENT_CATERING, + LocalDateTime.of(2023, 12, 11, 9, 0), + LocalDateTime.of(2023, 12, 13, 22, 0) + ); + + myProduct = (Rentable) inventory.findAll() + .filter(item -> item.getProduct().getName().equals("Kerze Rot")) + .stream().findFirst().get().getProduct(); + + myCart.addOrUpdateItem(myProduct, 3); + CustomOrder myOrder = new CustomOrder(UserAccount.UserAccountIdentifier.of("12345"), myCart); + myCart.addItemsTo(myOrder); + orderManagement.payOrder(myOrder); + orderManagement.completeOrder(myOrder); + + // #2 + myCart = new CustomCart( + OrderType.EVENT_CATERING, + LocalDateTime.of(2023, 12, 13, 9, 0), + LocalDateTime.of(2023, 12, 15, 22, 0) + ); + + myCart.addOrUpdateItem(myProduct, 4); + myOrder = new CustomOrder(UserAccount.UserAccountIdentifier.of("12345"), myCart); + myCart.addItemsTo(myOrder); + orderManagement.payOrder(myOrder); + orderManagement.completeOrder(myOrder); + } + + + @Test + @Order(1) + void thisShouldNeverFail() { + assertThat(orderManagement.findAll(Pageable.unpaged()).stream().count()).isEqualTo(2L); + } + + @Test + @Order(2) + void correctSetup() { + assertThat(orderManagement.findAll(Pageable.unpaged()).stream().count()).isEqualTo(2L); + } + + @Test + @Order(3) + void ordersByInterval() { + assertThat(customOrderRepository.findOrdersByInterval( + LocalDateTime.of(2023, 12, 2, 0, 0), + LocalDateTime.of(2024, 1, 1, 0, 0) + ).stream().toList()).hasSize(2); + + assertThat(customOrderRepository.findOrdersByInterval( + LocalDateTime.of(2023, 12, 11, 0, 0), + LocalDateTime.of(2023, 12, 11, 23, 0) + ).stream().toList()).hasSize(1); + } + + @Test + @Order(4) + @Disabled // because Spring does shit that I don't understand + void freeAmountInInterval() { + myProduct = (Rentable) inventory.findAll() + .filter(item -> item.getProduct().getName().equals("Kerze Rot")) + .stream().findFirst().get().getProduct(); + + assertThat(OrderController.findFreeAmountInInterval( + myProduct, + LocalDateTime.of(2023, 12, 11, 0, 0), + LocalDateTime.of(2023, 12, 11, 23, 0), + inventory, + customOrderRepository) + ).isEqualTo(Quantity.of(7)); + } +} diff --git a/src/test/java/catering/order/OrderUnitTests.java b/src/test/java/catering/order/OrderUnitTests.java index 32410b6..e7177ac 100644 --- a/src/test/java/catering/order/OrderUnitTests.java +++ b/src/test/java/catering/order/OrderUnitTests.java @@ -51,7 +51,7 @@ public class OrderUnitTests { // test if ChargeLine is correct assertThat(order.getChargeLines().stream().count()).isEqualTo(1); - assertThat(order.getChargeLines().getTotal()).isEqualTo(Money.of(11*12, "EUR")); + assertThat(order.getChargeLines().getTotal()).isEqualTo(Money.of(11*employee.getWage(), "EUR")); } @Test @@ -70,17 +70,17 @@ public class OrderUnitTests { assertThat(order.getChargeLines().stream().count()).isEqualTo(1); // test if costs are correct - assertThat(order.getChargeLines().getTotal()).isEqualTo(Money.of(11*12, "EUR")); + assertThat(order.getChargeLines().getTotal()).isEqualTo(Money.of(11*employee.getWage(), "EUR")); assertThat(order.getOrderLines().getTotal()).isEqualTo(Money.of(10.0*0.6, "EUR")); - assertThat(order.getTotal()).isEqualTo(Money.of(11.0 * 12.0 + 10.0 * 0.6, "EUR")); + assertThat(order.getTotal()).isEqualTo(Money.of(11.0 * employee.getWage() + 10.0 * 0.6, "EUR")); // test for duplication order.addEmployee(employee); assertThat(order.getStaff()).hasSize(1); assertThat(order.getOrderLines().stream().count()).isEqualTo(1); assertThat(order.getChargeLines().stream().count()).isEqualTo(1); - assertThat(order.getChargeLines().getTotal()).isEqualTo(Money.of(11*12, "EUR")); + assertThat(order.getChargeLines().getTotal()).isEqualTo(Money.of(11*employee.getWage(), "EUR")); assertThat(order.getOrderLines().getTotal()).isEqualTo(Money.of(10.0*0.6, "EUR")); - assertThat(order.getTotal()).isEqualTo(Money.of(11.0 * 12.0 + 10.0 * 0.6, "EUR")); + assertThat(order.getTotal()).isEqualTo(Money.of(11.0 * employee.getWage() + 10.0 * 0.6, "EUR")); } } diff --git a/src/test/java/catering/staff/StaffManagementIntegrationTests.java b/src/test/java/catering/staff/StaffManagementIntegrationTests.java index db2f9be..360bb80 100644 --- a/src/test/java/catering/staff/StaffManagementIntegrationTests.java +++ b/src/test/java/catering/staff/StaffManagementIntegrationTests.java @@ -18,6 +18,7 @@ import catering.order.CustomOrder; import catering.order.OrderType; import catering.users.User; import catering.users.UserManagement; +import org.springframework.test.annotation.DirtiesContext; @SpringBootTest class StaffManagmentIntegratonTest { @@ -79,6 +80,7 @@ class StaffManagmentIntegratonTest { } @Test + @DirtiesContext void getAvailableEmployees() throws Exception { Set availableService = staffManagement.getAvailableStaffByJob(JobType.SERVICE, LocalDateTime.of(2023, 10, 27, 16, 0), LocalDateTime.of(2023, 10, 27, 18, 0));