complete Rest controllers tests

This commit is contained in:
Vitaliy Fedoriv 2016-11-08 00:09:54 +02:00
parent 19f8c0cb38
commit b6dc54f743
4 changed files with 594 additions and 4 deletions

View file

@ -106,8 +106,6 @@ public class PetRestControllerTests {
@Test
public void testGetAllPetsSuccess() throws Exception {
//pets.remove(0);
//pets.remove(1);
given(this.clinicService.findAllPets()).willReturn(pets);
this.mockMvc.perform(get("/api/pets/")
.accept(MediaType.APPLICATION_JSON))
@ -153,6 +151,7 @@ public class PetRestControllerTests {
@Test
public void testUpdatePetSuccess() throws Exception {
given(this.clinicService.findPetById(3)).willReturn(pets.get(0));
Pet newPet = pets.get(0);
newPet.setName("Rosy I");
ObjectMapper mapper = new ObjectMapper();

View file

@ -1,5 +1,196 @@
package org.springframework.samples.petclinic.rest;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
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.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.service.ClinicServiceExt;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Test class for {@link PetTypeRestController}
*
* @author Vitaliy Fedoriv
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/mvc-test-config.xml", "classpath:spring/mvc-core-config.xml"})
@WebAppConfiguration
public class PetTypeRestControllerTests {
@Autowired
private PetTypeRestController petTypeRestController;
@Autowired
private ClinicServiceExt clinicService;
private MockMvc mockMvc;
private List<PetType> petTypes;
@Before
public void initPetTypes(){
this.mockMvc = MockMvcBuilders.standaloneSetup(petTypeRestController).build();
petTypes = new ArrayList<PetType>();
PetType petType = new PetType();
petType.setId(1);
petType.setName("cat");
petTypes.add(petType);
petType = new PetType();
petType.setId(2);
petType.setName("dog");
petTypes.add(petType);
petType = new PetType();
petType.setId(3);
petType.setName("lizard");
petTypes.add(petType);
petType = new PetType();
petType.setId(4);
petType.setName("snake");
petTypes.add(petType);
}
@Test
public void testGetPetTypeSuccess() throws Exception {
given(this.clinicService.findPetTypeById(1)).willReturn(petTypes.get(0));
this.mockMvc.perform(get("/api/pettypes/1")
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.name").value("cat"));
}
@Test
public void testGetPetTypeNotFound() throws Exception {
given(this.clinicService.findPetTypeById(-1)).willReturn(null);
this.mockMvc.perform(get("/api/pettypes/-1")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}
@Test
public void testGetAllPetTypesSuccess() throws Exception {
petTypes.remove(0);
petTypes.remove(1);
given(this.clinicService.findAllPetTypes()).willReturn(petTypes);
this.mockMvc.perform(get("/api/pettypes/")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.[0].id").value(2))
.andExpect(jsonPath("$.[0].name").value("dog"))
.andExpect(jsonPath("$.[1].id").value(4))
.andExpect(jsonPath("$.[1].name").value("snake"));
}
@Test
public void testGetAllPetTypesNotFound() throws Exception {
petTypes.clear();
given(this.clinicService.findAllPetTypes()).willReturn(petTypes);
this.mockMvc.perform(get("/api/pettypes/")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}
@Test
public void testCreatePetTypeSuccess() throws Exception {
PetType newPetType = petTypes.get(0);
newPetType.setId(999);
ObjectMapper mapper = new ObjectMapper();
String newPetTypeAsJSON = mapper.writeValueAsString(newPetType);
this.mockMvc.perform(post("/api/pettypes/")
.content(newPetTypeAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isCreated());
}
@Test
public void testCreatePetTypeError() throws Exception {
PetType newPetType = petTypes.get(0);
newPetType.setId(null);
newPetType.setName(null);
ObjectMapper mapper = new ObjectMapper();
String newPetTypeAsJSON = mapper.writeValueAsString(newPetType);
this.mockMvc.perform(post("/api/pettypes/")
.content(newPetTypeAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isBadRequest());
}
@Test
public void testUpdatePetTypeSuccess() throws Exception {
given(this.clinicService.findPetTypeById(2)).willReturn(petTypes.get(1));
PetType newPetType = petTypes.get(1);
newPetType.setName("dog I");
ObjectMapper mapper = new ObjectMapper();
String newPetTypeAsJSON = mapper.writeValueAsString(newPetType);
this.mockMvc.perform(put("/api/pettypes/2")
.content(newPetTypeAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(status().isNoContent());
this.mockMvc.perform(get("/api/pettypes/2")
.accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.id").value(2))
.andExpect(jsonPath("$.name").value("dog I"));
}
@Test
public void testUpdatePetTypeError() throws Exception {
PetType newPetType = petTypes.get(0);
newPetType.setName("");
ObjectMapper mapper = new ObjectMapper();
String newPetTypeAsJSON = mapper.writeValueAsString(newPetType);
this.mockMvc.perform(put("/api/pettypes/1")
.content(newPetTypeAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isBadRequest());
}
@Test
public void testDeletePetTypeSuccess() throws Exception {
PetType newPetType = petTypes.get(0);
ObjectMapper mapper = new ObjectMapper();
String newPetTypeAsJSON = mapper.writeValueAsString(newPetType);
given(this.clinicService.findPetTypeById(1)).willReturn(petTypes.get(0));
this.mockMvc.perform(delete("/api/pettypes/1")
.content(newPetTypeAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isNoContent());
}
@Test
public void testDeletePetTypeError() throws Exception {
PetType newPetType = petTypes.get(0);
ObjectMapper mapper = new ObjectMapper();
String newPetTypeAsJSON = mapper.writeValueAsString(newPetType);
given(this.clinicService.findPetTypeById(-1)).willReturn(null);
this.mockMvc.perform(delete("/api/pettypes/-1")
.content(newPetTypeAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isNotFound());
}
}

View file

@ -1,5 +1,196 @@
package org.springframework.samples.petclinic.rest;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
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.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.samples.petclinic.model.Vet;
import org.springframework.samples.petclinic.service.ClinicServiceExt;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Test class for {@link VetRestController}
*
* @author Vitaliy Fedoriv
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/mvc-test-config.xml", "classpath:spring/mvc-core-config.xml"})
@WebAppConfiguration
public class VetRestControllerTests {
@Autowired
private VetRestController vetRestController;
@Autowired
private ClinicServiceExt clinicService;
private MockMvc mockMvc;
private List<Vet> vets;
@Before
public void initVets(){
this.mockMvc = MockMvcBuilders.standaloneSetup(vetRestController).build();
vets = new ArrayList<Vet>();
Vet vet = new Vet();
vet.setId(1);
vet.setFirstName("James");
vet.setLastName("Carter");
vets.add(vet);
vet = new Vet();
vet.setId(2);
vet.setFirstName("Helen");
vet.setLastName("Leary");
vets.add(vet);
vet = new Vet();
vet.setId(3);
vet.setFirstName("Linda");
vet.setLastName("Douglas");
vets.add(vet);
}
@Test
public void testGetVetSuccess() throws Exception {
given(this.clinicService.findVetById(1)).willReturn(vets.get(0));
this.mockMvc.perform(get("/api/vets/1")
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.firstName").value("James"));
}
@Test
public void testGetVetNotFound() throws Exception {
given(this.clinicService.findVetById(-1)).willReturn(null);
this.mockMvc.perform(get("/api/vets/-1")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}
@Test
public void testGetAllVetsSuccess() throws Exception {
//vets.remove(2);
//vets.remove(1);
given(this.clinicService.findAllVets()).willReturn(vets);
this.mockMvc.perform(get("/api/vets/")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.[0].id").value(1))
.andExpect(jsonPath("$.[0].firstName").value("James"))
.andExpect(jsonPath("$.[1].id").value(2))
.andExpect(jsonPath("$.[1].firstName").value("Helen"));
}
@Test
public void testGetAllVetsNotFound() throws Exception {
vets.clear();
given(this.clinicService.findAllVets()).willReturn(vets);
this.mockMvc.perform(get("/api/vets/")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}
@Test
public void testCreateVetSuccess() throws Exception {
Vet newVet = vets.get(0);
newVet.setId(999);
ObjectMapper mapper = new ObjectMapper();
String newVetAsJSON = mapper.writeValueAsString(newVet);
this.mockMvc.perform(post("/api/vets/")
.content(newVetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isCreated());
}
@Test
public void testCreateVetError() throws Exception {
Vet newVet = vets.get(0);
newVet.setId(null);
newVet.setFirstName(null);
ObjectMapper mapper = new ObjectMapper();
String newVetAsJSON = mapper.writeValueAsString(newVet);
this.mockMvc.perform(post("/api/vets/")
.content(newVetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isBadRequest());
}
@Test
public void testUpdateVetSuccess() throws Exception {
Vet newVet = vets.get(0);
newVet.setFirstName("James");
ObjectMapper mapper = new ObjectMapper();
String newVetAsJSON = mapper.writeValueAsString(newVet);
this.mockMvc.perform(put("/api/vets/1")
.content(newVetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(status().isNoContent());
this.mockMvc.perform(get("/api/vets/1")
.accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.firstName").value("James"));
}
@Test
public void testUpdateVetError() throws Exception {
Vet newVet = vets.get(0);
newVet.setFirstName("");
ObjectMapper mapper = new ObjectMapper();
String newVetAsJSON = mapper.writeValueAsString(newVet);
this.mockMvc.perform(put("/api/vets/1")
.content(newVetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isBadRequest());
}
@Test
public void testDeleteVetSuccess() throws Exception {
Vet newVet = vets.get(0);
ObjectMapper mapper = new ObjectMapper();
String newVetAsJSON = mapper.writeValueAsString(newVet);
given(this.clinicService.findVetById(1)).willReturn(vets.get(0));
this.mockMvc.perform(delete("/api/vets/1")
.content(newVetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isNoContent());
}
@Test
public void testDeleteVetError() throws Exception {
Vet newVet = vets.get(0);
ObjectMapper mapper = new ObjectMapper();
String newVetAsJSON = mapper.writeValueAsString(newVet);
given(this.clinicService.findVetById(-1)).willReturn(null);
this.mockMvc.perform(delete("/api/vets/-1")
.content(newVetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isNotFound());
}
}

View file

@ -1,5 +1,214 @@
package org.springframework.samples.petclinic.rest;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
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.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.samples.petclinic.model.Owner;
import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.model.Visit;
import org.springframework.samples.petclinic.service.ClinicServiceExt;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Test class for {@link VisitRestController}
*
* @author Vitaliy Fedoriv
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/mvc-test-config.xml", "classpath:spring/mvc-core-config.xml"})
@WebAppConfiguration
public class VisitRestControllerTests {
@Autowired
private VisitRestController visitRestController;
@Autowired
private ClinicServiceExt clinicService;
private MockMvc mockMvc;
private List<Visit> visits;
@Before
public void initVisits(){
this.mockMvc = MockMvcBuilders.standaloneSetup(visitRestController).build();
visits = new ArrayList<Visit>();
Owner owner = new Owner();
owner.setId(1);
owner.setFirstName("Eduardo");
owner.setLastName("Rodriquez");
owner.setAddress("2693 Commerce St.");
owner.setCity("McFarland");
owner.setTelephone("6085558763");
PetType petType = new PetType();
petType.setId(2);
petType.setName("dog");
Pet pet = new Pet();
pet.setId(8);
pet.setName("Rosy");
pet.setBirthDate(new Date());
pet.setOwner(owner);
pet.setType(petType);
Visit visit = new Visit();
visit.setId(2);
visit.setPet(pet);
visit.setDate(new Date());
visit.setDescription("rabies shot");
visits.add(visit);
visit = new Visit();
visit.setId(3);
visit.setPet(pet);
visit.setDate(new Date());
visit.setDescription("neutered");
visits.add(visit);
}
@Test
public void testGetVisitSuccess() throws Exception {
given(this.clinicService.findVisitById(2)).willReturn(visits.get(0));
this.mockMvc.perform(get("/api/visits/2")
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.id").value(2))
.andExpect(jsonPath("$.description").value("rabies shot"));
}
@Test
public void testGetVisitNotFound() throws Exception {
given(this.clinicService.findVisitById(-1)).willReturn(null);
this.mockMvc.perform(get("/api/visits/-1")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}
@Test
public void testGetAllVisitsSuccess() throws Exception {
given(this.clinicService.findAllVisits()).willReturn(visits);
this.mockMvc.perform(get("/api/visits/")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.[0].id").value(2))
.andExpect(jsonPath("$.[0].description").value("rabies shot"))
.andExpect(jsonPath("$.[1].id").value(3))
.andExpect(jsonPath("$.[1].description").value("neutered"));
}
@Test
public void testGetAllVisitsNotFound() throws Exception {
visits.clear();
given(this.clinicService.findAllVisits()).willReturn(visits);
this.mockMvc.perform(get("/api/visits/")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}
@Test
public void testCreateVisitSuccess() throws Exception {
Visit newVisit = visits.get(0);
newVisit.setId(999);
ObjectMapper mapper = new ObjectMapper();
String newVisitAsJSON = mapper.writeValueAsString(newVisit);
this.mockMvc.perform(post("/api/visits/")
.content(newVisitAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isCreated());
}
@Test
public void testCreateVisitError() throws Exception {
Visit newVisit = visits.get(0);
newVisit.setId(null);
newVisit.setPet(null);
ObjectMapper mapper = new ObjectMapper();
String newVisitAsJSON = mapper.writeValueAsString(newVisit);
this.mockMvc.perform(post("/api/visits/")
.content(newVisitAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isBadRequest());
}
@Test
public void testUpdateVisitSuccess() throws Exception {
given(this.clinicService.findVisitById(2)).willReturn(visits.get(0));
Visit newVisit = visits.get(0);
newVisit.setDescription("rabies shot test");
ObjectMapper mapper = new ObjectMapper();
String newVisitAsJSON = mapper.writeValueAsString(newVisit);
this.mockMvc.perform(put("/api/visits/2")
.content(newVisitAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(status().isNoContent());
this.mockMvc.perform(get("/api/visits/2")
.accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.id").value(2))
.andExpect(jsonPath("$.description").value("rabies shot test"));
}
@Test
public void testUpdateVisitError() throws Exception {
Visit newVisit = visits.get(0);
newVisit.setPet(null);
ObjectMapper mapper = new ObjectMapper();
String newVisitAsJSON = mapper.writeValueAsString(newVisit);
this.mockMvc.perform(put("/api/visits/2")
.content(newVisitAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isBadRequest());
}
@Test
public void testDeleteVisitSuccess() throws Exception {
Visit newVisit = visits.get(0);
ObjectMapper mapper = new ObjectMapper();
String newVisitAsJSON = mapper.writeValueAsString(newVisit);
given(this.clinicService.findVisitById(2)).willReturn(visits.get(0));
this.mockMvc.perform(delete("/api/visits/2")
.content(newVisitAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isNoContent());
}
@Test
public void testDeleteVisitError() throws Exception {
Visit newVisit = visits.get(0);
ObjectMapper mapper = new ObjectMapper();
String newVisitAsJSON = mapper.writeValueAsString(newVisit);
given(this.clinicService.findVisitById(-1)).willReturn(null);
this.mockMvc.perform(delete("/api/visits/-1")
.content(newVisitAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isNotFound());
}
}