|
| 1 | +package org.springframework.samples.petclinic.owner.rest; |
| 2 | + |
| 3 | +import com.jayway.jsonpath.JsonPath; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 7 | +import org.springframework.boot.test.context.SpringBootTest; |
| 8 | +import org.springframework.http.MediaType; |
| 9 | +import org.springframework.samples.petclinic.owner.Owner; |
| 10 | +import org.springframework.samples.petclinic.owner.OwnerRepository; |
| 11 | +import org.springframework.test.context.TestPropertySource; |
| 12 | +import org.springframework.test.context.jdbc.Sql; |
| 13 | +import org.springframework.test.web.servlet.MockMvc; |
| 14 | +import org.springframework.test.web.servlet.MvcResult; |
| 15 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 16 | + |
| 17 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 18 | +import static org.hamcrest.Matchers.*; |
| 19 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 20 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 21 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 22 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 23 | + |
| 24 | +/** |
| 25 | + * Test class for the {@link OwnerRestController} |
| 26 | + */ |
| 27 | +@SpringBootTest |
| 28 | +@TestPropertySource(properties = { |
| 29 | + "spring.docker.compose.enabled=true", |
| 30 | + "spring.docker.compose.skip.in-tests=false", |
| 31 | + "spring.docker.compose.stop.command=down", |
| 32 | + "spring.docker.compose.file=docker-compose-tests.yaml" |
| 33 | +}) |
| 34 | +@AutoConfigureMockMvc |
| 35 | +public class OwnerRestControllerTest { |
| 36 | + |
| 37 | + @Autowired |
| 38 | + private MockMvc mockMvc; |
| 39 | + |
| 40 | + @Autowired |
| 41 | + private OwnerRepository ownerRepository; |
| 42 | + |
| 43 | + @Test |
| 44 | + public void create() throws Exception { |
| 45 | + String ownerDto = """ |
| 46 | + { |
| 47 | + "firstName": "John", |
| 48 | + "lastName": "Doe", |
| 49 | + "address": "123 Main St", |
| 50 | + "city": "New York", |
| 51 | + "telephone": "5551234567" |
| 52 | + }"""; |
| 53 | + |
| 54 | + MvcResult mvcResult = mockMvc.perform(post("/rest/owners") |
| 55 | + .content(ownerDto) |
| 56 | + .contentType(MediaType.APPLICATION_JSON)) |
| 57 | + .andExpect(status() |
| 58 | + .isOk()) |
| 59 | + .andExpect(jsonPath("$.id").isNumber()) |
| 60 | + .andReturn(); |
| 61 | + |
| 62 | + String jsonResponse = mvcResult.getResponse() |
| 63 | + .getContentAsString(); |
| 64 | + Integer id = JsonPath.parse(jsonResponse) |
| 65 | + .read("$.id"); |
| 66 | + |
| 67 | + ownerRepository.findById(id).orElseThrow(); |
| 68 | + |
| 69 | + ownerRepository.deleteById(id); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void createIdNotNull() throws Exception { |
| 74 | + String ownerDto = """ |
| 75 | + { |
| 76 | + "id": 1, |
| 77 | + "firstName": "John", |
| 78 | + "lastName": "Doe", |
| 79 | + "address": "123 Main St", |
| 80 | + "city": "New York", |
| 81 | + "telephone": "5551234567" |
| 82 | + }"""; |
| 83 | + |
| 84 | + mockMvc.perform(post("/rest/owners") |
| 85 | + .content(ownerDto) |
| 86 | + .contentType(MediaType.APPLICATION_JSON)) |
| 87 | + .andExpect(status() |
| 88 | + .isUnprocessableEntity()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + @Sql(scripts = "classpath:insert-owners.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) |
| 93 | + @Sql(scripts = "classpath:delete-owners.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) |
| 94 | + public void getOne() throws Exception { |
| 95 | + mockMvc.perform(get("/rest/owners/{0}", "1")) |
| 96 | + .andExpect(status() |
| 97 | + .isOk()) |
| 98 | + .andExpect(jsonPath("$.id").value(1)) |
| 99 | + .andExpect(jsonPath("$.firstName").value("John")) |
| 100 | + .andExpect(jsonPath("$.lastName").value("Doe")); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void getOneNotFound() throws Exception { |
| 105 | + mockMvc.perform(get("/rest/owners/{id}", 0)) |
| 106 | + .andExpect(status().isNotFound()); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + @Sql(scripts = "classpath:insert-owners.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) |
| 111 | + @Sql(scripts = "classpath:delete-owners.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) |
| 112 | + public void delete() throws Exception { |
| 113 | + mockMvc.perform(MockMvcRequestBuilders.delete("/rest/owners/{0}", "1")) |
| 114 | + .andExpect(status() |
| 115 | + .isOk()) |
| 116 | + .andExpect(jsonPath("$.id").value(1)) |
| 117 | + .andExpect(jsonPath("$.firstName").value("John")); |
| 118 | + |
| 119 | + assertThat(ownerRepository.findById(1).isPresent()).isEqualTo(false); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + @Sql(scripts = "classpath:insert-owners.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) |
| 124 | + @Sql(scripts = "classpath:delete-owners.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) |
| 125 | + public void patch() throws Exception { |
| 126 | + String patchNode = """ |
| 127 | + { |
| 128 | + "id": 1, |
| 129 | + "firstName": "Johny", |
| 130 | + "address": null, |
| 131 | + "city": "Spring ville" |
| 132 | + }"""; |
| 133 | + |
| 134 | + mockMvc.perform(MockMvcRequestBuilders.patch("/rest/owners/{0}", "1") |
| 135 | + .content(patchNode) |
| 136 | + .contentType(MediaType.APPLICATION_JSON)) |
| 137 | + .andExpect(status() |
| 138 | + .isOk()) |
| 139 | + .andExpect(jsonPath("$.address").value(nullValue())) |
| 140 | + .andExpect(jsonPath("$.firstName").value("Johny")) |
| 141 | + .andExpect(jsonPath("$.lastName").value("Doe")) |
| 142 | + .andExpect(jsonPath("$.city").value("Spring ville")) |
| 143 | + .andExpect(jsonPath("$.telephone").value("555-123-4567")); |
| 144 | + |
| 145 | + Owner owner = ownerRepository.findById(1).orElseThrow(); |
| 146 | + |
| 147 | + assertThat(owner.getAddress()).isNull(); |
| 148 | + assertThat(owner.getFirstName()).isEqualTo("Johny"); |
| 149 | + assertThat(owner.getCity()).isEqualTo("Spring ville"); |
| 150 | + assertThat(owner.getTelephone()).isEqualTo("555-123-4567"); |
| 151 | + assertThat(owner.getLastName()).isEqualTo("Doe"); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + @Sql(scripts = "classpath:insert-owners.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) |
| 156 | + @Sql(scripts = "classpath:delete-owners.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) |
| 157 | + public void getAll() throws Exception { |
| 158 | + mockMvc.perform(get("/rest/owners") |
| 159 | + .param("firstNameContains", "J")) |
| 160 | + .andExpect(status() |
| 161 | + .isOk()) |
| 162 | + .andExpect(jsonPath("$.content[*].firstName").value(everyItem(startsWith("J")))) |
| 163 | + .andExpect(jsonPath("$.content.length()").value(2)); |
| 164 | + } |
| 165 | +} |
0 commit comments