From cda383ca8870ff46bb57359d6bf95cbd2151484e Mon Sep 17 00:00:00 2001 From: Erik Hohlfeld Date: Sun, 10 Dec 2023 19:49:33 +0100 Subject: [PATCH] Add orderCatalog integration tests --- .../CatalogControllerIntegrationTests.java | 81 +++++++++++++++++-- 1 file changed, 75 insertions(+), 6 deletions(-) diff --git a/src/test/java/catering/orderCatalog/CatalogControllerIntegrationTests.java b/src/test/java/catering/orderCatalog/CatalogControllerIntegrationTests.java index 2e6b966..21be146 100644 --- a/src/test/java/catering/orderCatalog/CatalogControllerIntegrationTests.java +++ b/src/test/java/catering/orderCatalog/CatalogControllerIntegrationTests.java @@ -1,18 +1,25 @@ package catering.orderCatalog; +import catering.order.OrderType; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.salespointframework.quantity.Quantity; import org.salespointframework.useraccount.Password; import org.salespointframework.useraccount.Role; 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.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; +import java.util.HashMap; + import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; +import static org.springframework.test.util.AssertionErrors.assertEquals; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @SpringBootTest @@ -28,20 +35,82 @@ public class CatalogControllerIntegrationTests { @Autowired UserAccountManagement userAccountManagement; + @Autowired + private CustomCatalogEntryRepository catalogEntryRepository; + @BeforeEach void setUp() { - if (userAccountManagement.findByUsername("anna").isEmpty()) { - userAccountManagement.create("anna", + if (userAccountManagement.findByUsername("andi").isEmpty()) { + userAccountManagement.create("andi", Password.UnencryptedPassword.of("12345"), Role.of("CUSTOMER")); } + + CustomCatalogEntry testCatalogEntry = new CustomCatalogEntry( + OrderType.RENT_A_COOK, + // TODO: Following HashMap has to be changed to after event_planner-use-orderCatalog + // is merged + new HashMap(), + 3, + 3000); + catalogEntryRepository.save(testCatalogEntry); } @Test - void customerViewsCatalog() throws Exception { - this.mockMvc.perform(get("/catalog").with(user("anna").roles("CUSTOMER"))) + void testCustomerViewsCatalog() throws Exception { + this.mockMvc.perform(get("/catalog").with(user("andi").roles("CUSTOMER"))) .andExpect(status().isOk()) .andExpect(view().name("catalog")) - .andDo(print()); + .andExpect(model().attributeExists("catalogEntries")); } + @Test + void testCustomerViewsCatalogEditor() throws Exception { + this.mockMvc.perform(get("/catalog_editor").with(user("andi").roles("CUSTOMER"))) + .andExpect(status().isOk()) + .andExpect(view().name("catalog_editor")) + .andExpect(model().attributeExists("formCatalogEntry", "productMap")); + } + + @Test + @WithMockUser(username = "admin", roles = "ADMIN") + void testAddCatalogEntry() throws Exception { + this.mockMvc.perform(post("/catalog_editor/addCatalogEntry") + .param("eventType", "MOBILE_BREAKFAST") + .param("minimumTimePeriod", "6") + .param("totalCost", "5000")) + .andExpect(redirectedUrl("/catalog")); + } + + @Test + @WithMockUser(username = "admin", roles = "ADMIN") + void testRemoveCatalogEntry() throws Exception { + this.mockMvc.perform(post("/catalog/remove") + .param("catalogEntryID", "1")) + .andExpect(redirectedUrl("/catalog")); + } + + // TODO: Fix this test + @Test + @Disabled("The repository's 0th entry is null.") + @WithMockUser(username = "admin", roles = "ADMIN") + void testAddTime() throws Exception { + this.mockMvc.perform(post("/catalog_editor/addTime") + .param("minimumTimePeriod", "8") + .param("eventType", "EVENT_CATERING")) + .andExpect(redirectedUrl("/catalog_editor")); + + assertEquals("Asserting update of minimumTimePeriod.", + 8, + catalogEntryRepository.findById(0L).orElse(null).getMinimumTimePeriod()); + } + + @Test + @Disabled("For some reason the redirect result is null.") + @WithMockUser(username = "admin", roles = "ADMIN") + void testAddProduct() throws Exception { + this.mockMvc.perform(post("/catalog_editor/addProduct") + .param("pid", "1") + .param("number", "10")) + .andExpect(redirectedUrl("/catalog_editor")); + } }