Updating using GitHub API 2025-06-19-15:31

This commit is contained in:
nidhi-vm 2025-06-19 15:31:13 -04:00
parent 33952e823a
commit c2d6cda5c3

View file

@ -3,7 +3,7 @@
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * you may obtain a copy of the License at
* *
* https://www.apache.org/licenses/LICENSE-2.0 * https://www.apache.org/licenses/LICENSE-2.0
* *
@ -61,6 +61,29 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
class OwnerControllerTests { class OwnerControllerTests {
private static final int TEST_OWNER_ID = 1; private static final int TEST_OWNER_ID = 1;
private static final String OWNER_ATTRIBUTE = "owner";
private static final String FIRST_NAME = "firstName";
private static final String LAST_NAME = "lastName";
private static final String ADDRESS = "address";
private static final String CITY = "London";
private static final String TELEPHONE = "telephone";
private static final String VIEW_CREATE_OR_UPDATE_OWNER_FORM = "owners/createOrUpdateOwnerForm";
private static final String VIEW_FIND_OWNERS = "owners/findOwners";
private static final String VIEW_OWNERS_LIST = "owners/ownersList";
private static final String VIEW_OWNER_DETAILS = "owners/ownerDetails";
private static final String OWNER_ID_EDIT_URL = "/owners/{ownerId}/edit";
private static final String OWNER_NEW_URL = "/owners/new";
private static final String OWNER_FIND_URL = "/owners/find";
private static final String OWNER_REDIRECT_URL = "redirect:/owners/";
private static final String TEST_LAST_NAME = "Franklin";
private static final String TEST_FIRST_NAME = "George";
private static final String TEST_ADDRESS = "110 W. Liberty St.";
private static final String TEST_CITY = "Madison";
private static final String TEST_TELEPHONE = "6085551023";
private static final String TEST_LAST_NAME_BLOGGS = "Bloggs";
private static final String TEST_CITY_LONDON = "London";
private static final String TEST_ADDRESS_CARAMEL = "123 Caramel Street";
private static final String TEST_TELEPHONE_JOE = "1316761638";
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
@ -71,11 +94,11 @@ class OwnerControllerTests {
private Owner george() { private Owner george() {
Owner george = new Owner(); Owner george = new Owner();
george.setId(TEST_OWNER_ID); george.setId(TEST_OWNER_ID);
george.setFirstName("George"); george.setFirstName(TEST_FIRST_NAME);
george.setLastName("Franklin"); george.setLastName(TEST_LAST_NAME);
george.setAddress("110 W. Liberty St."); george.setAddress(TEST_ADDRESS);
george.setCity("Madison"); george.setCity(TEST_CITY);
george.setTelephone("6085551023"); george.setTelephone(TEST_TELEPHONE);
Pet max = new Pet(); Pet max = new Pet();
PetType dog = new PetType(); PetType dog = new PetType();
dog.setName("dog"); dog.setName("dog");
@ -89,143 +112,140 @@ class OwnerControllerTests {
@BeforeEach @BeforeEach
void setup() { void setup() {
Owner george = george(); Owner george = george();
given(this.owners.findByLastNameStartingWith(eq("Franklin"), any(Pageable.class))) given(this.owners.findByLastNameStartingWith(eq(TEST_LAST_NAME), any(Pageable.class)))
.willReturn(new PageImpl<>(List.of(george))); .willReturn(new PageImpl<>(List.of(george)));
given(this.owners.findById(TEST_OWNER_ID)).willReturn(Optional.of(george)); given(this.owners.findById(TEST_OWNER_ID)).willReturn(Optional.of(george));
Visit visit = new Visit(); Visit visit = new Visit();
visit.setDate(LocalDate.now()); visit.setDate(LocalDate.now());
george.getPet("Max").getVisits().add(visit); george.getPet("Max").getVisits().add(visit);
} }
@Test @Test
void testInitCreationForm() throws Exception { void testInitCreationForm() throws Exception {
mockMvc.perform(get("/owners/new")) mockMvc.perform(get(OWNER_NEW_URL))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(model().attributeExists("owner")) .andExpect(model().attributeExists(OWNER_ATTRIBUTE))
.andExpect(view().name("owners/createOrUpdateOwnerForm")); .andExpect(view().name(VIEW_CREATE_OR_UPDATE_OWNER_FORM));
} }
@Test @Test
void testProcessCreationFormSuccess() throws Exception { void testProcessCreationFormSuccess() throws Exception {
mockMvc mockMvc
.perform(post("/owners/new").param("firstName", "Joe") .perform(post(OWNER_NEW_URL).param(FIRST_NAME, "Joe")
.param("lastName", "Bloggs") .param(LAST_NAME, TEST_LAST_NAME_BLOGGS)
.param("address", "123 Caramel Street") .param(ADDRESS, TEST_ADDRESS_CARAMEL)
.param("city", "London") .param("city", TEST_CITY_LONDON)
.param("telephone", "1316761638")) .param(TELEPHONE, TEST_TELEPHONE_JOE))
.andExpect(status().is3xxRedirection()); .andExpect(status().is3xxRedirection());
} }
@Test @Test
void testProcessCreationFormHasErrors() throws Exception { void testProcessCreationFormHasErrors() throws Exception {
mockMvc mockMvc
.perform(post("/owners/new").param("firstName", "Joe").param("lastName", "Bloggs").param("city", "London")) .perform(post(OWNER_NEW_URL).param(FIRST_NAME, "Joe").param(LAST_NAME, TEST_LAST_NAME_BLOGGS).param("city", TEST_CITY_LONDON))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(model().attributeHasErrors("owner")) .andExpect(model().attributeHasErrors(OWNER_ATTRIBUTE))
.andExpect(model().attributeHasFieldErrors("owner", "address")) .andExpect(model().attributeHasFieldErrors(OWNER_ATTRIBUTE, ADDRESS))
.andExpect(model().attributeHasFieldErrors("owner", "telephone")) .andExpect(model().attributeHasFieldErrors(OWNER_ATTRIBUTE, TELEPHONE))
.andExpect(view().name("owners/createOrUpdateOwnerForm")); .andExpect(view().name(VIEW_CREATE_OR_UPDATE_OWNER_FORM));
} }
@Test @Test
void testInitFindForm() throws Exception { void testInitFindForm() throws Exception {
mockMvc.perform(get("/owners/find")) mockMvc.perform(get(OWNER_FIND_URL))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(model().attributeExists("owner")) .andExpect(model().attributeExists(OWNER_ATTRIBUTE))
.andExpect(view().name("owners/findOwners")); .andExpect(view().name(VIEW_FIND_OWNERS));
} }
@Test @Test
void testProcessFindFormSuccess() throws Exception { void testProcessFindFormSuccess() throws Exception {
Page<Owner> tasks = new PageImpl<>(List.of(george(), new Owner())); Page<Owner> tasks = new PageImpl<>(List.of(george(), new Owner()));
when(this.owners.findByLastNameStartingWith(anyString(), any(Pageable.class))).thenReturn(tasks); when(this.owners.findByLastNameStartingWith(anyString(), any(Pageable.class))).thenReturn(tasks);
mockMvc.perform(get("/owners?page=1")).andExpect(status().isOk()).andExpect(view().name("owners/ownersList")); mockMvc.perform(get("/owners?page=1")).andExpect(status().isOk()).andExpect(view().name(VIEW_OWNERS_LIST));
} }
@Test @Test
void testProcessFindFormByLastName() throws Exception { void testProcessFindFormByLastName() throws Exception {
Page<Owner> tasks = new PageImpl<>(List.of(george())); Page<Owner> tasks = new PageImpl<>(List.of(george()));
when(this.owners.findByLastNameStartingWith(eq("Franklin"), any(Pageable.class))).thenReturn(tasks); when(this.owners.findByLastNameStartingWith(eq(TEST_LAST_NAME), any(Pageable.class))).thenReturn(tasks);
mockMvc.perform(get("/owners?page=1").param("lastName", "Franklin")) mockMvc.perform(get("/owners?page=1").param(LAST_NAME, TEST_LAST_NAME))
.andExpect(status().is3xxRedirection()) .andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID)); .andExpect(view().name(OWNER_REDIRECT_URL + TEST_OWNER_ID));
} }
@Test @Test
void testProcessFindFormNoOwnersFound() throws Exception { void testProcessFindFormNoOwnersFound() throws Exception {
Page<Owner> tasks = new PageImpl<>(List.of()); Page<Owner> tasks = new PageImpl<>(List.of());
when(this.owners.findByLastNameStartingWith(eq("Unknown Surname"), any(Pageable.class))).thenReturn(tasks); when(this.owners.findByLastNameStartingWith(eq("Unknown Surname"), any(Pageable.class))).thenReturn(tasks);
mockMvc.perform(get("/owners?page=1").param("lastName", "Unknown Surname")) mockMvc.perform(get("/owners?page=1").param(LAST_NAME, "Unknown Surname"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(model().attributeHasFieldErrors("owner", "lastName")) .andExpect(model().attributeHasFieldErrors(OWNER_ATTRIBUTE, LAST_NAME))
.andExpect(model().attributeHasFieldErrorCode("owner", "lastName", "notFound")) .andExpect(model().attributeHasFieldErrorCode(OWNER_ATTRIBUTE, LAST_NAME, "notFound"))
.andExpect(view().name("owners/findOwners")); .andExpect(view().name(VIEW_FIND_OWNERS));
} }
@Test @Test
void testInitUpdateOwnerForm() throws Exception { void testInitUpdateOwnerForm() throws Exception {
mockMvc.perform(get("/owners/{ownerId}/edit", TEST_OWNER_ID)) mockMvc.perform(get(OWNER_ID_EDIT_URL, TEST_OWNER_ID))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(model().attributeExists("owner")) .andExpect(model().attributeExists(OWNER_ATTRIBUTE))
.andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin")))) .andExpect(model().attribute(OWNER_ATTRIBUTE, hasProperty(LAST_NAME, is(TEST_LAST_NAME))))
.andExpect(model().attribute("owner", hasProperty("firstName", is("George")))) .andExpect(model().attribute(OWNER_ATTRIBUTE, hasProperty(FIRST_NAME, is(TEST_FIRST_NAME))))
.andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St.")))) .andExpect(model().attribute(OWNER_ATTRIBUTE, hasProperty(ADDRESS, is(TEST_ADDRESS))))
.andExpect(model().attribute("owner", hasProperty("city", is("Madison")))) .andExpect(model().attribute(OWNER_ATTRIBUTE, hasProperty("city", is(TEST_CITY))))
.andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023")))) .andExpect(model().attribute(OWNER_ATTRIBUTE, hasProperty(TELEPHONE, is(TEST_TELEPHONE))))
.andExpect(view().name("owners/createOrUpdateOwnerForm")); .andExpect(view().name(VIEW_CREATE_OR_UPDATE_OWNER_FORM));
} }
@Test @Test
void testProcessUpdateOwnerFormSuccess() throws Exception { void testProcessUpdateOwnerFormSuccess() throws Exception {
mockMvc mockMvc
.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID).param("firstName", "Joe") .perform(post(OWNER_ID_EDIT_URL, TEST_OWNER_ID).param(FIRST_NAME, "Joe")
.param("lastName", "Bloggs") .param(LAST_NAME, TEST_LAST_NAME_BLOGGS)
.param("address", "123 Caramel Street") .param(ADDRESS, TEST_ADDRESS_CARAMEL)
.param("city", "London") .param("city", TEST_CITY_LONDON)
.param("telephone", "1616291589")) .param(TELEPHONE, "1616291589"))
.andExpect(status().is3xxRedirection()) .andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/{ownerId}")); .andExpect(view().name(OWNER_REDIRECT_URL + "{ownerId}"));
} }
@Test @Test
void testProcessUpdateOwnerFormUnchangedSuccess() throws Exception { void testProcessUpdateOwnerFormUnchangedSuccess() throws Exception {
mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID)) mockMvc.perform(post(OWNER_ID_EDIT_URL, TEST_OWNER_ID))
.andExpect(status().is3xxRedirection()) .andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/{ownerId}")); .andExpect(view().name(OWNER_REDIRECT_URL + "{ownerId}"));
} }
@Test @Test
void testProcessUpdateOwnerFormHasErrors() throws Exception { void testProcessUpdateOwnerFormHasErrors() throws Exception {
mockMvc mockMvc
.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID).param("firstName", "Joe") .perform(post(OWNER_ID_EDIT_URL, TEST_OWNER_ID).param(FIRST_NAME, "Joe")
.param("lastName", "Bloggs") .param(LAST_NAME, TEST_LAST_NAME_BLOGGS)
.param("address", "") .param(ADDRESS, "")
.param("telephone", "")) .param(TELEPHONE, ""))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(model().attributeHasErrors("owner")) .andExpect(model().attributeHasErrors(OWNER_ATTRIBUTE))
.andExpect(model().attributeHasFieldErrors("owner", "address")) .andExpect(model().attributeHasFieldErrors(OWNER_ATTRIBUTE, ADDRESS))
.andExpect(model().attributeHasFieldErrors("owner", "telephone")) .andExpect(model().attributeHasFieldErrors(OWNER_ATTRIBUTE, TELEPHONE))
.andExpect(view().name("owners/createOrUpdateOwnerForm")); .andExpect(view().name(VIEW_CREATE_OR_UPDATE_OWNER_FORM));
} }
@Test @Test
void testShowOwner() throws Exception { void testShowOwner() throws Exception {
mockMvc.perform(get("/owners/{ownerId}", TEST_OWNER_ID)) mockMvc.perform(get("/owners/{ownerId}", TEST_OWNER_ID))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin")))) .andExpect(model().attribute(OWNER_ATTRIBUTE, hasProperty(LAST_NAME, is(TEST_LAST_NAME))))
.andExpect(model().attribute("owner", hasProperty("firstName", is("George")))) .andExpect(model().attribute(OWNER_ATTRIBUTE, hasProperty(FIRST_NAME, is(TEST_FIRST_NAME))))
.andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St.")))) .andExpect(model().attribute(OWNER_ATTRIBUTE, hasProperty(ADDRESS, is(TEST_ADDRESS))))
.andExpect(model().attribute("owner", hasProperty("city", is("Madison")))) .andExpect(model().attribute(OWNER_ATTRIBUTE, hasProperty("city", is(TEST_CITY))))
.andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023")))) .andExpect(model().attribute(OWNER_ATTRIBUTE, hasProperty(TELEPHONE, is(TEST_TELEPHONE))))
.andExpect(model().attribute("owner", hasProperty("pets", not(empty())))) .andExpect(model().attribute(OWNER_ATTRIBUTE, hasProperty("pets", not(empty()))))
.andExpect(model().attribute("owner", .andExpect(model().attribute(OWNER_ATTRIBUTE,
hasProperty("pets", hasItem(hasProperty("visits", hasSize(greaterThan(0))))))) hasProperty("pets", hasItem(hasProperty("visits", hasSize(greaterThan(0)))))))
.andExpect(view().name("owners/ownerDetails")); .andExpect(view().name(VIEW_OWNER_DETAILS));
} }
@Test @Test
@ -242,9 +262,9 @@ class OwnerControllerTests {
when(owners.findById(pathOwnerId)).thenReturn(Optional.of(owner)); when(owners.findById(pathOwnerId)).thenReturn(Optional.of(owner));
mockMvc.perform(MockMvcRequestBuilders.post("/owners/{ownerId}/edit", pathOwnerId).flashAttr("owner", owner)) mockMvc.perform(MockMvcRequestBuilders.post(OWNER_ID_EDIT_URL, pathOwnerId).flashAttr(OWNER_ATTRIBUTE, owner))
.andExpect(status().is3xxRedirection()) .andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("/owners/" + pathOwnerId + "/edit")) .andExpect(redirectedUrl(OWNER_REDIRECT_URL + pathOwnerId + "/edit"))
.andExpect(flash().attributeExists("error")); .andExpect(flash().attributeExists("error"));
} }