From dd38c594dec0d2a4a295f082c70c13c651d3d1cd Mon Sep 17 00:00:00 2001 From: Vitaliy Fedoriv Date: Sun, 6 Nov 2016 19:44:21 +0200 Subject: [PATCH] update OwnerRestControllerTests --- .../rest/OwnerRestControllerTests.java | 114 +++++++++++++----- 1 file changed, 85 insertions(+), 29 deletions(-) diff --git a/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java index d86e635ab..0a99f1b4e 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java @@ -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() { - 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 @@ -178,5 +181,58 @@ public class OwnerRestControllerTests { .content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) .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()); + } }