mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 05:15:50 +00:00
Updating using GitHub API 2025-06-19-15:31
This commit is contained in:
parent
516f13e3fc
commit
55ae3ad427
1 changed files with 44 additions and 37 deletions
|
@ -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
|
||||||
*
|
*
|
||||||
|
@ -52,8 +52,15 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||||
class PetControllerTests {
|
class PetControllerTests {
|
||||||
|
|
||||||
private static final int TEST_OWNER_ID = 1;
|
private static final int TEST_OWNER_ID = 1;
|
||||||
|
|
||||||
private static final int TEST_PET_ID = 1;
|
private static final int TEST_PET_ID = 1;
|
||||||
|
private static final String PET_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm";
|
||||||
|
private static final String PARAM_BIRTH_DATE = "birthDate";
|
||||||
|
private static final String PARAM_NAME = "name";
|
||||||
|
private static final String PARAM_TYPE = "type";
|
||||||
|
private static final String PET_TYPE_HAMSTER = "hamster";
|
||||||
|
private static final String DEFAULT_BIRTH_DATE = "2015-02-12";
|
||||||
|
private static final String ERROR_CODE_REQUIRED = "required";
|
||||||
|
private static final String ERROR_CODE_DUPLICATE = "duplicate";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
|
@ -65,7 +72,7 @@ class PetControllerTests {
|
||||||
void setup() {
|
void setup() {
|
||||||
PetType cat = new PetType();
|
PetType cat = new PetType();
|
||||||
cat.setId(3);
|
cat.setId(3);
|
||||||
cat.setName("hamster");
|
cat.setName(PET_TYPE_HAMSTER);
|
||||||
given(this.owners.findPetTypes()).willReturn(List.of(cat));
|
given(this.owners.findPetTypes()).willReturn(List.of(cat));
|
||||||
|
|
||||||
Owner owner = new Owner();
|
Owner owner = new Owner();
|
||||||
|
@ -84,16 +91,16 @@ class PetControllerTests {
|
||||||
void testInitCreationForm() throws Exception {
|
void testInitCreationForm() throws Exception {
|
||||||
mockMvc.perform(get("/owners/{ownerId}/pets/new", TEST_OWNER_ID))
|
mockMvc.perform(get("/owners/{ownerId}/pets/new", TEST_OWNER_ID))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(view().name("pets/createOrUpdatePetForm"))
|
.andExpect(view().name(PET_CREATE_OR_UPDATE_FORM))
|
||||||
.andExpect(model().attributeExists("pet"));
|
.andExpect(model().attributeExists("pet"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testProcessCreationFormSuccess() throws Exception {
|
void testProcessCreationFormSuccess() throws Exception {
|
||||||
mockMvc
|
mockMvc
|
||||||
.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID).param("name", "Betty")
|
.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID).param(PARAM_NAME, "Betty")
|
||||||
.param("type", "hamster")
|
.param(PARAM_TYPE, PET_TYPE_HAMSTER)
|
||||||
.param("birthDate", "2015-02-12"))
|
.param(PARAM_BIRTH_DATE, DEFAULT_BIRTH_DATE))
|
||||||
.andExpect(status().is3xxRedirection())
|
.andExpect(status().is3xxRedirection())
|
||||||
.andExpect(view().name("redirect:/owners/{ownerId}"));
|
.andExpect(view().name("redirect:/owners/{ownerId}"));
|
||||||
}
|
}
|
||||||
|
@ -104,40 +111,40 @@ class PetControllerTests {
|
||||||
@Test
|
@Test
|
||||||
void testProcessCreationFormWithBlankName() throws Exception {
|
void testProcessCreationFormWithBlankName() throws Exception {
|
||||||
mockMvc
|
mockMvc
|
||||||
.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID).param("name", "\t \n")
|
.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID).param(PARAM_NAME, "\t \n")
|
||||||
.param("birthDate", "2015-02-12"))
|
.param(PARAM_BIRTH_DATE, DEFAULT_BIRTH_DATE))
|
||||||
.andExpect(model().attributeHasNoErrors("owner"))
|
.andExpect(model().attributeHasNoErrors("owner"))
|
||||||
.andExpect(model().attributeHasErrors("pet"))
|
.andExpect(model().attributeHasErrors("pet"))
|
||||||
.andExpect(model().attributeHasFieldErrors("pet", "name"))
|
.andExpect(model().attributeHasFieldErrors("pet", PARAM_NAME))
|
||||||
.andExpect(model().attributeHasFieldErrorCode("pet", "name", "required"))
|
.andExpect(model().attributeHasFieldErrorCode("pet", PARAM_NAME, ERROR_CODE_REQUIRED))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(view().name("pets/createOrUpdatePetForm"));
|
.andExpect(view().name(PET_CREATE_OR_UPDATE_FORM));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testProcessCreationFormWithDuplicateName() throws Exception {
|
void testProcessCreationFormWithDuplicateName() throws Exception {
|
||||||
mockMvc
|
mockMvc
|
||||||
.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID).param("name", "petty")
|
.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID).param(PARAM_NAME, "petty")
|
||||||
.param("birthDate", "2015-02-12"))
|
.param(PARAM_BIRTH_DATE, DEFAULT_BIRTH_DATE))
|
||||||
.andExpect(model().attributeHasNoErrors("owner"))
|
.andExpect(model().attributeHasNoErrors("owner"))
|
||||||
.andExpect(model().attributeHasErrors("pet"))
|
.andExpect(model().attributeHasErrors("pet"))
|
||||||
.andExpect(model().attributeHasFieldErrors("pet", "name"))
|
.andExpect(model().attributeHasFieldErrors("pet", PARAM_NAME))
|
||||||
.andExpect(model().attributeHasFieldErrorCode("pet", "name", "duplicate"))
|
.andExpect(model().attributeHasFieldErrorCode("pet", PARAM_NAME, ERROR_CODE_DUPLICATE))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(view().name("pets/createOrUpdatePetForm"));
|
.andExpect(view().name(PET_CREATE_OR_UPDATE_FORM));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testProcessCreationFormWithMissingPetType() throws Exception {
|
void testProcessCreationFormWithMissingPetType() throws Exception {
|
||||||
mockMvc
|
mockMvc
|
||||||
.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID).param("name", "Betty")
|
.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID).param(PARAM_NAME, "Betty")
|
||||||
.param("birthDate", "2015-02-12"))
|
.param(PARAM_BIRTH_DATE, DEFAULT_BIRTH_DATE))
|
||||||
.andExpect(model().attributeHasNoErrors("owner"))
|
.andExpect(model().attributeHasNoErrors("owner"))
|
||||||
.andExpect(model().attributeHasErrors("pet"))
|
.andExpect(model().attributeHasErrors("pet"))
|
||||||
.andExpect(model().attributeHasFieldErrors("pet", "type"))
|
.andExpect(model().attributeHasFieldErrors("pet", "type"))
|
||||||
.andExpect(model().attributeHasFieldErrorCode("pet", "type", "required"))
|
.andExpect(model().attributeHasFieldErrorCode("pet", "type", ERROR_CODE_REQUIRED))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(view().name("pets/createOrUpdatePetForm"));
|
.andExpect(view().name(PET_CREATE_OR_UPDATE_FORM));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -146,14 +153,14 @@ class PetControllerTests {
|
||||||
String futureBirthDate = currentDate.plusMonths(1).toString();
|
String futureBirthDate = currentDate.plusMonths(1).toString();
|
||||||
|
|
||||||
mockMvc
|
mockMvc
|
||||||
.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID).param("name", "Betty")
|
.perform(post("/owners/{ownerId}/pets/new", TEST_OWNER_ID).param(PARAM_NAME, "Betty")
|
||||||
.param("birthDate", futureBirthDate))
|
.param(PARAM_BIRTH_DATE, futureBirthDate))
|
||||||
.andExpect(model().attributeHasNoErrors("owner"))
|
.andExpect(model().attributeHasNoErrors("owner"))
|
||||||
.andExpect(model().attributeHasErrors("pet"))
|
.andExpect(model().attributeHasErrors("pet"))
|
||||||
.andExpect(model().attributeHasFieldErrors("pet", "birthDate"))
|
.andExpect(model().attributeHasFieldErrors("pet", "birthDate"))
|
||||||
.andExpect(model().attributeHasFieldErrorCode("pet", "birthDate", "typeMismatch.birthDate"))
|
.andExpect(model().attributeHasFieldErrorCode("pet", "birthDate", "typeMismatch.birthDate"))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(view().name("pets/createOrUpdatePetForm"));
|
.andExpect(view().name(PET_CREATE_OR_UPDATE_FORM));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -161,7 +168,7 @@ class PetControllerTests {
|
||||||
mockMvc.perform(get("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID))
|
mockMvc.perform(get("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(model().attributeExists("pet"))
|
.andExpect(model().attributeExists("pet"))
|
||||||
.andExpect(view().name("pets/createOrUpdatePetForm"));
|
.andExpect(view().name(PET_CREATE_OR_UPDATE_FORM));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -169,9 +176,9 @@ class PetControllerTests {
|
||||||
@Test
|
@Test
|
||||||
void testProcessUpdateFormSuccess() throws Exception {
|
void testProcessUpdateFormSuccess() throws Exception {
|
||||||
mockMvc
|
mockMvc
|
||||||
.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID).param("name", "Betty")
|
.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID).param(PARAM_NAME, "Betty")
|
||||||
.param("type", "hamster")
|
.param(PARAM_TYPE, PET_TYPE_HAMSTER)
|
||||||
.param("birthDate", "2015-02-12"))
|
.param(PARAM_BIRTH_DATE, DEFAULT_BIRTH_DATE))
|
||||||
.andExpect(status().is3xxRedirection())
|
.andExpect(status().is3xxRedirection())
|
||||||
.andExpect(view().name("redirect:/owners/{ownerId}"));
|
.andExpect(view().name("redirect:/owners/{ownerId}"));
|
||||||
}
|
}
|
||||||
|
@ -182,27 +189,27 @@ class PetControllerTests {
|
||||||
@Test
|
@Test
|
||||||
void testProcessUpdateFormWithInvalidBirthDate() throws Exception {
|
void testProcessUpdateFormWithInvalidBirthDate() throws Exception {
|
||||||
mockMvc
|
mockMvc
|
||||||
.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID).param("name", " ")
|
.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID).param(PARAM_NAME, " ")
|
||||||
.param("birthDate", "2015/02/12"))
|
.param(PARAM_BIRTH_DATE, DEFAULT_BIRTH_DATE))
|
||||||
.andExpect(model().attributeHasNoErrors("owner"))
|
.andExpect(model().attributeHasNoErrors("owner"))
|
||||||
.andExpect(model().attributeHasErrors("pet"))
|
.andExpect(model().attributeHasErrors("pet"))
|
||||||
.andExpect(model().attributeHasFieldErrors("pet", "birthDate"))
|
.andExpect(model().attributeHasFieldErrors("pet", "birthDate"))
|
||||||
.andExpect(model().attributeHasFieldErrorCode("pet", "birthDate", "typeMismatch"))
|
.andExpect(model().attributeHasFieldErrorCode("pet", "birthDate", "typeMismatch"))
|
||||||
.andExpect(view().name("pets/createOrUpdatePetForm"));
|
.andExpect(view().name(PET_CREATE_OR_UPDATE_FORM));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testProcessUpdateFormWithBlankName() throws Exception {
|
void testProcessUpdateFormWithBlankName() throws Exception {
|
||||||
mockMvc
|
mockMvc
|
||||||
.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID).param("name", " ")
|
.perform(post("/owners/{ownerId}/pets/{petId}/edit", TEST_OWNER_ID, TEST_PET_ID).param(PARAM_NAME, " ")
|
||||||
.param("birthDate", "2015-02-12"))
|
.param(PARAM_BIRTH_DATE, DEFAULT_BIRTH_DATE))
|
||||||
.andExpect(model().attributeHasNoErrors("owner"))
|
.andExpect(model().attributeHasNoErrors("owner"))
|
||||||
.andExpect(model().attributeHasErrors("pet"))
|
.andExpect(model().attributeHasErrors("pet"))
|
||||||
.andExpect(model().attributeHasFieldErrors("pet", "name"))
|
.andExpect(model().attributeHasFieldErrors("pet", PARAM_NAME))
|
||||||
.andExpect(model().attributeHasFieldErrorCode("pet", "name", "required"))
|
.andExpect(model().attributeHasFieldErrorCode("pet", PARAM_NAME, ERROR_CODE_REQUIRED))
|
||||||
.andExpect(view().name("pets/createOrUpdatePetForm"));
|
.andExpect(view().name(PET_CREATE_OR_UPDATE_FORM));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue