diff --git a/src/main/java/com/heftyb/dms/controllers/AdminController.java b/src/main/java/com/heftyb/dms/controllers/AdminController.java index a949757..2260b6a 100644 --- a/src/main/java/com/heftyb/dms/controllers/AdminController.java +++ b/src/main/java/com/heftyb/dms/controllers/AdminController.java @@ -12,7 +12,6 @@ import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -51,13 +50,6 @@ public String adminHome(HttpServletRequest request, Model model, Principal princ return "hr_administrator"; } - @GetMapping({"/create_employee", "/create_employee/"}) - public String create_employee_form(Principal principal, ModelMap map) { - map.addAttribute("username", principal.getName()); - - return "employee_form"; - } - @PostMapping({"/create_employee", "/create_employee/"}) public String create_employee(@Valid Employee employee) { diff --git a/src/main/java/com/heftyb/dms/controllers/SearchController.java b/src/main/java/com/heftyb/dms/controllers/SearchController.java index 055a029..661c448 100644 --- a/src/main/java/com/heftyb/dms/controllers/SearchController.java +++ b/src/main/java/com/heftyb/dms/controllers/SearchController.java @@ -31,13 +31,12 @@ public SearchController(final VehicleService vehicleService, @GetMapping({"/", ""}) public String results(Principal principal, ModelMap map, - @RequestParam(required = false) long custId, - @RequestParam(required = false) boolean isCool, - @RequestParam(required = false) String fname, - @RequestParam(required = false) String lname, - @RequestParam(required = false) String phone, - @RequestParam(required = false) String email, - @RequestParam(required = false) String vin, + @RequestParam(required = false) Long custId, + @RequestParam(required = false, defaultValue = "") String fname, + @RequestParam(required = false, defaultValue = "") String lname, + @RequestParam(required = false, defaultValue = "") String phone, + @RequestParam(required = false, defaultValue = "") String email, + @RequestParam(required = false, defaultValue = "") String vin, @RequestParam(required = false) boolean custSearch, @RequestParam(required = false) boolean vehicleSearch) { map.addAttribute("username", principal.getName()); @@ -45,7 +44,7 @@ public String results(Principal principal, List customers = new ArrayList<>(); List vehicles = new ArrayList<>(); - if (custId != 0) { + if (custId != null && custId != 0) { Customer c = customerService.findById(custId); if (custSearch) customers.add(c); if (vehicleSearch) { @@ -53,16 +52,11 @@ public String results(Principal principal, } } - if (isCool) { - System.out.println("You're Cool!"); - } - if (!fname.isEmpty() && !lname.isEmpty()) { List c = customerService.findByName(fname, lname); if (custSearch) c.iterator().forEachRemaining(customers::add); if (vehicleSearch) { -// c.stream().map(cust -> cust.getVehicles().iterator().forEachRemaining(vehicles::add)); final List finalVehicles = vehicles; c.stream().forEach(cust -> { cust.getVehicles().iterator().forEachRemaining(finalVehicles::add); @@ -70,9 +64,6 @@ public String results(Principal principal, finalVehicles.iterator().forEachRemaining(vehicles::add); } } else if (!lname.isEmpty()) { -// customerService.findByLastName(lname) -// .iterator().forEachRemaining(customers::add); -// List c = customerService.findByLastName(lname); if (custSearch) c.iterator().forEachRemaining(customers::add); @@ -82,10 +73,8 @@ public String results(Principal principal, cust.getVehicles().iterator().forEachRemaining(finalVehicles::add); }); vehicles = finalVehicles; -// finalVehicles.iterator().forEachRemaining(vehicles::add); } } else if (!fname.isEmpty()) { -// customerService.findByFirstName(fname).iterator().forEachRemaining(customers::add); List c = customerService.findByFirstName(fname); if (custSearch) c.iterator().forEachRemaining(customers::add); @@ -93,7 +82,6 @@ public String results(Principal principal, c.stream().forEach(cust -> { cust.getVehicles().iterator().forEachRemaining(finalVehicles::add); }); -// finalVehicles.iterator().forEachRemaining(vehicles::add); vehicles = finalVehicles; } diff --git a/src/main/java/com/heftyb/dms/vehicles/controllers/VehicleController.java b/src/main/java/com/heftyb/dms/vehicles/controllers/VehicleController.java index 0bf40e5..ecc99ea 100644 --- a/src/main/java/com/heftyb/dms/vehicles/controllers/VehicleController.java +++ b/src/main/java/com/heftyb/dms/vehicles/controllers/VehicleController.java @@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import java.security.Principal; @@ -38,11 +39,12 @@ public String vehicle_details(Principal principal, ModelMap map, @RequestParam l return "vehicle_details"; } - @GetMapping({"/search", "/search/"}) + // The shared vin-search fragment submits with POST, so this accepts both methods. + @RequestMapping(value = {"/search", "/search/"}, method = {RequestMethod.GET, RequestMethod.POST}) public String vin_search(Principal principal, ModelMap map, @RequestParam String vin) { map.addAttribute("username", principal.getName()); if (vin.isBlank()) { - return "add_new_vehicle"; + return "redirect:/vehicles"; } List vehicles = vehicleService.findByVin(vin); diff --git a/src/main/resources/templates/search.html b/src/main/resources/templates/search.html index 80e3d95..c096bb7 100644 --- a/src/main/resources/templates/search.html +++ b/src/main/resources/templates/search.html @@ -12,10 +12,6 @@

Search

-
- - -
diff --git a/src/test/java/com/heftyb/dms/controllers/SearchControllerTest.java b/src/test/java/com/heftyb/dms/controllers/SearchControllerTest.java new file mode 100644 index 0000000..9c96ec8 --- /dev/null +++ b/src/test/java/com/heftyb/dms/controllers/SearchControllerTest.java @@ -0,0 +1,30 @@ +package com.heftyb.dms.controllers; + +import org.junit.jupiter.api.Test; +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.context.ActiveProfiles; +import org.springframework.test.web.servlet.MockMvc; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +@SpringBootTest +@AutoConfigureMockMvc +@ActiveProfiles("test") +@WithMockUser +class SearchControllerTest { + + @Autowired + private MockMvc mvc; + + @Test + void searchWithNoParametersRendersEmptyResults() throws Exception { + mvc.perform(get("/search")) + .andExpect(status().isOk()) + .andExpect(view().name("results")); + } +} diff --git a/src/test/java/com/heftyb/dms/vehicles/controllers/VehicleControllerTest.java b/src/test/java/com/heftyb/dms/vehicles/controllers/VehicleControllerTest.java new file mode 100644 index 0000000..29b3a7c --- /dev/null +++ b/src/test/java/com/heftyb/dms/vehicles/controllers/VehicleControllerTest.java @@ -0,0 +1,39 @@ +package com.heftyb.dms.vehicles.controllers; + +import org.junit.jupiter.api.Test; +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.context.ActiveProfiles; +import org.springframework.test.web.servlet.MockMvc; + +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +@SpringBootTest +@AutoConfigureMockMvc +@ActiveProfiles("test") +@WithMockUser +class VehicleControllerTest { + + @Autowired + private MockMvc mvc; + + @Test + void vinSearchAcceptsThePostTheFormSends() throws Exception { + mvc.perform(post("/vehicles/search").with(csrf()).param("vin", "JH4TB2H26CC000000")) + .andExpect(status().isOk()) + .andExpect(view().name("vehicles")); + } + + @Test + void blankVinSearchRedirectsToTheVehicleList() throws Exception { + mvc.perform(get("/vehicles/search").param("vin", "")) + .andExpect(redirectedUrl("/vehicles")); + } +}