forked from springframeworkguru/sfg-pet-clinic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
464ad04
commit dcab2ed
Showing
1 changed file
with
104 additions
and
0 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
...-web/src/test/java/guru/springframework/sfgpetclinic/controllers/VisitControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package guru.springframework.sfgpetclinic.controllers; | ||
|
||
import guru.springframework.sfgpetclinic.model.Owner; | ||
import guru.springframework.sfgpetclinic.model.Pet; | ||
import guru.springframework.sfgpetclinic.model.PetType; | ||
import guru.springframework.sfgpetclinic.services.PetService; | ||
import guru.springframework.sfgpetclinic.services.VisitService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.util.UriTemplate; | ||
|
||
import java.net.URI; | ||
import java.time.LocalDate; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
|
||
import static org.mockito.ArgumentMatchers.anyLong; | ||
import static org.mockito.Mockito.when; | ||
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.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class VisitControllerTest { | ||
|
||
private static final String PETS_CREATE_OR_UPDATE_VISIT_FORM = "pets/createOrUpdateVisitForm"; | ||
private static final String REDIRECT_OWNERS_1 = "redirect:/owners/{ownerId}"; | ||
private static final String YET_ANOTHER_VISIT_DESCRIPTION = "yet another visit"; | ||
|
||
@Mock | ||
PetService petService; | ||
|
||
@Mock | ||
VisitService visitService; | ||
|
||
@InjectMocks | ||
VisitController visitController; | ||
|
||
private MockMvc mockMvc; | ||
|
||
private final UriTemplate visitsUriTemplate = new UriTemplate("/owners/{ownerId}/pets/{petId}/visits/new"); | ||
private final Map<String, String> uriVariables = new HashMap<>(); | ||
private URI visitsUri; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
Long petId = 1L; | ||
Long ownerId = 1L; | ||
when(petService.findById(anyLong())) | ||
.thenReturn( | ||
Pet.builder() | ||
.id(petId) | ||
.birthDate(LocalDate.of(2018,11,13)) | ||
.name("Cutie") | ||
.visits(new HashSet<>()) | ||
.owner(Owner.builder() | ||
.id(ownerId) | ||
.lastName("Doe") | ||
.firstName("Joe") | ||
.build()) | ||
.petType(PetType.builder() | ||
.name("Dog").build()) | ||
.build() | ||
); | ||
|
||
uriVariables.clear(); | ||
uriVariables.put("ownerId", ownerId.toString()); | ||
uriVariables.put("petId", petId.toString()); | ||
visitsUri = visitsUriTemplate.expand(uriVariables); | ||
|
||
mockMvc = MockMvcBuilders | ||
.standaloneSetup(visitController) | ||
.build(); | ||
} | ||
|
||
@Test | ||
void initNewVisitForm() throws Exception { | ||
mockMvc.perform(get(visitsUri)) | ||
.andExpect(status().isOk()) | ||
.andExpect(view().name(PETS_CREATE_OR_UPDATE_VISIT_FORM)) | ||
; | ||
} | ||
|
||
|
||
@Test | ||
void processNewVisitForm() throws Exception { | ||
mockMvc.perform(post(visitsUri) | ||
.contentType(MediaType.APPLICATION_FORM_URLENCODED) | ||
.param("date","2018-11-11") | ||
.param("description", YET_ANOTHER_VISIT_DESCRIPTION)) | ||
.andExpect(status().is3xxRedirection()) | ||
.andExpect(view().name(REDIRECT_OWNERS_1)) | ||
.andExpect(model().attributeExists("visit")) | ||
; | ||
} | ||
} |