mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 21:15:50 +00:00
update OwnerRestControllerTests
This commit is contained in:
parent
2f96792cb3
commit
dd38c594de
1 changed files with 85 additions and 29 deletions
|
@ -1,10 +1,10 @@
|
||||||
package org.springframework.samples.petclinic.rest;
|
package org.springframework.samples.petclinic.rest;
|
||||||
|
|
||||||
import static org.mockito.BDDMockito.given;
|
import static org.mockito.BDDMockito.given;
|
||||||
import static org.mockito.Matchers.anyObject;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
||||||
import static org.mockito.Mockito.doAnswer;
|
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
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.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.content;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
@ -13,15 +13,12 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.invocation.InvocationOnMock;
|
|
||||||
import org.mockito.stubbing.Answer;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.samples.petclinic.model.Owner;
|
import org.springframework.samples.petclinic.model.Owner;
|
||||||
import org.springframework.samples.petclinic.service.ClinicService;
|
import org.springframework.samples.petclinic.service.ClinicServiceExt;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
|
@ -45,7 +42,7 @@ public class OwnerRestControllerTests {
|
||||||
private OwnerRestController ownerRestController;
|
private OwnerRestController ownerRestController;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ClinicService clinicService;
|
private ClinicServiceExt clinicService;
|
||||||
|
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@ -99,7 +96,7 @@ public class OwnerRestControllerTests {
|
||||||
public void testGetOwnerSuccess() throws Exception {
|
public void testGetOwnerSuccess() throws Exception {
|
||||||
given(this.clinicService.findOwnerById(1)).willReturn(owners.get(0));
|
given(this.clinicService.findOwnerById(1)).willReturn(owners.get(0));
|
||||||
this.mockMvc.perform(get("/api/owners/1")
|
this.mockMvc.perform(get("/api/owners/1")
|
||||||
.accept(MediaType.APPLICATION_JSON))
|
.accept(MediaType.APPLICATION_JSON_VALUE))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(content().contentType("application/json;charset=UTF-8"))
|
.andExpect(content().contentType("application/json;charset=UTF-8"))
|
||||||
.andExpect(jsonPath("$.id").value(1))
|
.andExpect(jsonPath("$.id").value(1))
|
||||||
|
@ -119,7 +116,7 @@ public class OwnerRestControllerTests {
|
||||||
owners.remove(0);
|
owners.remove(0);
|
||||||
owners.remove(1);
|
owners.remove(1);
|
||||||
given(this.clinicService.findOwnerByLastName("Davis")).willReturn(owners);
|
given(this.clinicService.findOwnerByLastName("Davis")).willReturn(owners);
|
||||||
this.mockMvc.perform(get("/api/owners/?lastName=Davis")
|
this.mockMvc.perform(get("/api/owners/*/lastname/Davis")
|
||||||
.accept(MediaType.APPLICATION_JSON))
|
.accept(MediaType.APPLICATION_JSON))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(content().contentType("application/json;charset=UTF-8"))
|
.andExpect(content().contentType("application/json;charset=UTF-8"))
|
||||||
|
@ -138,33 +135,39 @@ public class OwnerRestControllerTests {
|
||||||
.andExpect(status().isNotFound());
|
.andExpect(status().isNotFound());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Ignore
|
@Test
|
||||||
|
public void testGetAllOwnersSuccess() throws Exception {
|
||||||
|
owners.remove(0);
|
||||||
|
owners.remove(1);
|
||||||
|
given(this.clinicService.findAllOwners()).willReturn(owners);
|
||||||
|
this.mockMvc.perform(get("/api/owners/")
|
||||||
|
.accept(MediaType.APPLICATION_JSON))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(content().contentType("application/json;charset=UTF-8"))
|
||||||
|
.andExpect(jsonPath("$.[0].id").value(2))
|
||||||
|
.andExpect(jsonPath("$.[0].firstName").value("Betty"))
|
||||||
|
.andExpect(jsonPath("$.[1].id").value(4))
|
||||||
|
.andExpect(jsonPath("$.[1].firstName").value("Harold"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAllOwnersNotFound() throws Exception {
|
||||||
|
owners.clear();
|
||||||
|
given(this.clinicService.findAllOwners()).willReturn(owners);
|
||||||
|
this.mockMvc.perform(get("/api/owners/")
|
||||||
|
.accept(MediaType.APPLICATION_JSON))
|
||||||
|
.andExpect(status().isNotFound());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCreateOwnerSuccess() throws Exception {
|
public void testCreateOwnerSuccess() throws Exception {
|
||||||
doAnswer(new Answer<Void>() {
|
|
||||||
public Void answer(InvocationOnMock invocation) {
|
|
||||||
Owner resultOwner = (Owner) invocation.getArguments()[0];
|
|
||||||
resultOwner.setId(999);
|
|
||||||
System.out.println("resultOwner after call " + resultOwner.toString());
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}).when(clinicService).saveOwner((Owner) anyObject());
|
|
||||||
|
|
||||||
Owner newOwner = owners.get(0);
|
Owner newOwner = owners.get(0);
|
||||||
newOwner.setId(null);
|
newOwner.setId(999);
|
||||||
|
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
String newOwnerAsJSON = mapper.writeValueAsString(newOwner);
|
String newOwnerAsJSON = mapper.writeValueAsString(newOwner);
|
||||||
System.out.println("newOwnerAsJSON" + newOwnerAsJSON);
|
|
||||||
newOwner.setId(999);
|
|
||||||
//newOwner.as;
|
|
||||||
String resultOwnerAsJSON = mapper.writeValueAsString(newOwner);
|
|
||||||
System.out.println("resultOwnerAsJSON" + resultOwnerAsJSON);
|
|
||||||
// TODO Uncompleted test
|
|
||||||
this.mockMvc.perform(post("/api/owners/")
|
this.mockMvc.perform(post("/api/owners/")
|
||||||
.content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
|
.content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
|
||||||
.andExpect(status().isCreated())
|
.andExpect(status().isCreated());
|
||||||
.andExpect(content().json(resultOwnerAsJSON));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -178,5 +181,58 @@ public class OwnerRestControllerTests {
|
||||||
.content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
|
.content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
|
||||||
.andExpect(status().isBadRequest());
|
.andExpect(status().isBadRequest());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateOwnerSuccess() throws Exception {
|
||||||
|
Owner newOwner = owners.get(0);
|
||||||
|
newOwner.setFirstName("George I");
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
String newOwnerAsJSON = mapper.writeValueAsString(newOwner);
|
||||||
|
this.mockMvc.perform(put("/api/owners/1")
|
||||||
|
.content(newOwnerAsJSON).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/owners/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("George I"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateOwnerError() throws Exception {
|
||||||
|
Owner newOwner = owners.get(0);
|
||||||
|
newOwner.setFirstName("");
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
String newOwnerAsJSON = mapper.writeValueAsString(newOwner);
|
||||||
|
this.mockMvc.perform(put("/api/owners/1")
|
||||||
|
.content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
|
||||||
|
.andExpect(status().isBadRequest());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteOwnerSuccess() throws Exception {
|
||||||
|
Owner newOwner = owners.get(0);
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
String newOwnerAsJSON = mapper.writeValueAsString(newOwner);
|
||||||
|
given(this.clinicService.findOwnerById(1)).willReturn(owners.get(0));
|
||||||
|
this.mockMvc.perform(delete("/api/owners/1")
|
||||||
|
.content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
|
||||||
|
.andExpect(status().isNoContent());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteOwnerError() throws Exception {
|
||||||
|
Owner newOwner = owners.get(0);
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
String newOwnerAsJSON = mapper.writeValueAsString(newOwner);
|
||||||
|
given(this.clinicService.findOwnerById(-1)).willReturn(null);
|
||||||
|
this.mockMvc.perform(delete("/api/owners/-1")
|
||||||
|
.content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
|
||||||
|
.andExpect(status().isNotFound());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue