update OwnerRestControllerTests

This commit is contained in:
Vitaliy Fedoriv 2016-11-06 19:44:21 +02:00
parent 2f96792cb3
commit dd38c594de

View file

@ -1,10 +1,10 @@
package org.springframework.samples.petclinic.rest;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.doAnswer;
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;
@ -13,15 +13,12 @@ import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
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.http.MediaType;
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.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@ -45,7 +42,7 @@ public class OwnerRestControllerTests {
private OwnerRestController ownerRestController;
@Autowired
private ClinicService clinicService;
private ClinicServiceExt clinicService;
private MockMvc mockMvc;
@ -99,7 +96,7 @@ public class OwnerRestControllerTests {
public void testGetOwnerSuccess() throws Exception {
given(this.clinicService.findOwnerById(1)).willReturn(owners.get(0));
this.mockMvc.perform(get("/api/owners/1")
.accept(MediaType.APPLICATION_JSON))
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.id").value(1))
@ -119,7 +116,7 @@ public class OwnerRestControllerTests {
owners.remove(0);
owners.remove(1);
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))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
@ -138,33 +135,39 @@ public class OwnerRestControllerTests {
.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
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);
newOwner.setId(null);
newOwner.setId(999);
ObjectMapper mapper = new ObjectMapper();
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/")
.content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isCreated())
.andExpect(content().json(resultOwnerAsJSON));
.andExpect(status().isCreated());
}
@Test
@ -179,4 +182,57 @@ public class OwnerRestControllerTests {
.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());
}
}