From 8df82fec855741c640cf5b0a6763b897bd24de45 Mon Sep 17 00:00:00 2001 From: Denis Natusch Date: Thu, 7 Dec 2023 20:14:47 +0100 Subject: [PATCH] Fix Me The order and staff integration tests are strongly influencing each other. For example just adding one order breaks basiclly every OrderControllerIntegrationTest. --- .../OrderControllerIntegrationTests.java | 1 + .../StaffControllerIntegrationTests.java | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/test/java/catering/order/OrderControllerIntegrationTests.java b/src/test/java/catering/order/OrderControllerIntegrationTests.java index f36e76d..756aea1 100644 --- a/src/test/java/catering/order/OrderControllerIntegrationTests.java +++ b/src/test/java/catering/order/OrderControllerIntegrationTests.java @@ -72,6 +72,7 @@ public class OrderControllerIntegrationTests { myUser = userAccountManagement.findByUsername("andi").get(); admin = userAccountManagement.findByUsername("admin").get(); + orderManagement.findAll(Pageable.unpaged()).forEach(orderManagement::delete); if (orderManagement.findAll(Pageable.unpaged()).stream().findAny().isEmpty()) { myCart = new CustomCart(OrderType.EVENT_CATERING, diff --git a/src/test/java/catering/staff/StaffControllerIntegrationTests.java b/src/test/java/catering/staff/StaffControllerIntegrationTests.java index 45edb4c..b9bea76 100644 --- a/src/test/java/catering/staff/StaffControllerIntegrationTests.java +++ b/src/test/java/catering/staff/StaffControllerIntegrationTests.java @@ -3,6 +3,7 @@ package catering.staff; import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.endsWith; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.salespointframework.core.Currencies.EURO; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @@ -11,19 +12,32 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import java.time.LocalDateTime; +import java.time.YearMonth; +import java.util.Set; + import org.javamoney.moneta.Money; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.salespointframework.order.OrderManagement; +import org.salespointframework.useraccount.UserAccountManagement; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.Pageable; import org.springframework.http.HttpHeaders; import org.springframework.security.test.context.support.WithAnonymousUser; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import catering.order.CustomCart; +import catering.order.CustomOrder; +import catering.order.OrderType; +import catering.users.User; +import catering.users.UserManagement; + @AutoConfigureMockMvc @SpringBootTest class StaffControllerIntegrationTests { @@ -38,13 +52,40 @@ class StaffControllerIntegrationTests { @Autowired private StaffManagement staffManagement; + @Autowired + private OrderManagement orderManagement; + + @Autowired + private UserManagement userManagement; + + @Autowired + private UserAccountManagement userAccountManagement; + Employee defaultEmployee; Long defaultEmployeeId; + User defaultCustomer; + Employee orderEmployee; + CustomOrder defaultStaffOrder; + + CustomOrder createCustomOrder(LocalDateTime start, LocalDateTime end, Set staff) { + CustomOrder order = orderManagement.save(new CustomOrder(defaultCustomer.getUserAccount().getId(), + new CustomCart(OrderType.EVENT_CATERING, start, end))); + staff.forEach(order::addEmployee); + return orderManagement.save(order); + } @BeforeEach void setup() throws Exception { + orderManagement.findAll(Pageable.unpaged()).forEach(orderManagement::delete); + staffManagement.findAll().forEach(e -> staffManagement.delete(e.getId())); + defaultEmployee = staffManagement.save(new Employee("Dieter Baum", JobType.COOK, Money.of(0, EURO))); defaultEmployeeId = defaultEmployee.getId(); + + if (userAccountManagement.findByUsername("hemming").isEmpty()) { + userManagement.createCustomer("hemming", "Nürnberger Platz", "123", "Hemming Quark"); + } + defaultCustomer = userManagement.getUserByName("hemming").get(); } @AfterEach @@ -158,6 +199,20 @@ class StaffControllerIntegrationTests { .containsExactly("Dieter Baum", JobType.COOK); } + @Test + @WithMockUser(username = "admin", password = "admin", roles = "ADMIN") + void viewHoursesOfWork() throws Exception { + orderEmployee = staffManagement.save(new Employee("Tyler Baum", JobType.COOK, Money.of(10, EURO))); + defaultStaffOrder = createCustomOrder(LocalDateTime.of(2023, 12, 20, 10, 0), + LocalDateTime.of(2023, 12, 20, 21, 0), Set.of(orderEmployee)); + + mvc.perform(get("/staff")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("11.0"))); + assertThat(staffManagement.getWorkingHoursByEmployee(orderEmployee, YearMonth.of(2023, 12))).isEqualTo(11); + assertThat(staffManagement.getWorkingHoursByEmployee(orderEmployee, YearMonth.of(2023, 11))).isEqualTo(0); + } + @Test @WithMockUser(username = "dieter", password = "123", roles = "CUSTOMER") void refuseCustomer() throws Exception {