Skip to content

Commit 0bbc899

Browse files
test: improve test coverage across controller, repository, and service layers
1 parent e3db287 commit 0bbc899

6 files changed

Lines changed: 1293 additions & 0 deletions

File tree

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
package com.countyhospital.healthapi.controller;
2+
3+
import java.time.LocalDate;
4+
import java.time.LocalDateTime;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import static org.mockito.ArgumentMatchers.any;
11+
import static org.mockito.ArgumentMatchers.eq;
12+
import static org.mockito.Mockito.when;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
15+
import org.springframework.boot.test.mock.mockito.MockBean;
16+
import org.springframework.data.domain.Page;
17+
import org.springframework.data.domain.PageImpl;
18+
import org.springframework.data.domain.PageRequest;
19+
import org.springframework.http.MediaType;
20+
import org.springframework.test.context.ActiveProfiles;
21+
import org.springframework.test.web.servlet.MockMvc;
22+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
23+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
24+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
25+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
26+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
27+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
28+
29+
import com.countyhospital.healthapi.encounter.controller.EncounterController;
30+
import com.countyhospital.healthapi.encounter.domain.Encounter;
31+
import com.countyhospital.healthapi.encounter.dto.request.EncounterRequest;
32+
import com.countyhospital.healthapi.encounter.dto.response.EncounterResponse;
33+
import com.countyhospital.healthapi.encounter.mapper.EncounterMapper;
34+
import com.countyhospital.healthapi.encounter.service.EncounterService;
35+
import com.countyhospital.healthapi.patient.domain.Patient;
36+
import com.countyhospital.healthapi.patient.service.PatientService;
37+
import com.fasterxml.jackson.databind.ObjectMapper;
38+
39+
@WebMvcTest(EncounterController.class)
40+
@ActiveProfiles("test")
41+
class EncounterControllerTest {
42+
43+
@Autowired
44+
private MockMvc mockMvc;
45+
46+
@MockBean
47+
private EncounterService encounterService;
48+
49+
@MockBean
50+
private PatientService patientService;
51+
52+
@MockBean
53+
private EncounterMapper encounterMapper;
54+
55+
@Autowired
56+
private ObjectMapper objectMapper;
57+
58+
private Patient patient;
59+
private Encounter encounter;
60+
private EncounterResponse encounterResponse;
61+
private EncounterRequest encounterRequest;
62+
63+
@BeforeEach
64+
public void setUp() {
65+
patient = new Patient("PAT-001", "John", "Doe",
66+
LocalDate.of(1985, 5, 15), "MALE");
67+
patient.setId(1L);
68+
patient.setCreatedAt(LocalDateTime.now());
69+
patient.setUpdatedAt(LocalDateTime.now());
70+
71+
encounter = new Encounter(patient,
72+
LocalDateTime.of(2024, 1, 10, 9, 0),
73+
LocalDateTime.of(2024, 1, 10, 10, 30),
74+
"OUTPATIENT", "Annual physical examination");
75+
encounter.setId(1L);
76+
encounter.setCreatedAt(LocalDateTime.now());
77+
encounter.setUpdatedAt(LocalDateTime.now());
78+
79+
encounterResponse = new EncounterResponse(1L, 1L, "John", "Doe",
80+
LocalDateTime.of(2024, 1, 10, 9, 0),
81+
LocalDateTime.of(2024, 1, 10, 10, 30),
82+
"OUTPATIENT", "Annual physical examination",
83+
encounter.getCreatedAt(), encounter.getUpdatedAt());
84+
85+
encounterRequest = new EncounterRequest(1L,
86+
LocalDateTime.of(2024, 1, 10, 9, 0),
87+
LocalDateTime.of(2024, 1, 10, 10, 30),
88+
"OUTPATIENT", "Annual physical examination");
89+
}
90+
91+
@Test
92+
void createEncounter_WithValidData_ReturnsCreated() throws Exception {
93+
when(patientService.getPatientById(1L)).thenReturn(patient);
94+
when(encounterMapper.toEntity(any(EncounterRequest.class), eq(patient))).thenReturn(encounter);
95+
when(encounterService.createEncounter(any(Encounter.class))).thenReturn(encounter);
96+
when(encounterMapper.toResponse(any(Encounter.class))).thenReturn(encounterResponse);
97+
98+
mockMvc.perform(post("/api/encounters")
99+
.contentType(MediaType.APPLICATION_JSON)
100+
.content(objectMapper.writeValueAsString(encounterRequest)))
101+
.andExpect(status().isCreated());
102+
}
103+
104+
@Test
105+
void createEncounter_WithInvalidData_ReturnsBadRequest() throws Exception {
106+
EncounterRequest invalidRequest = new EncounterRequest(null, null, null, "", null);
107+
108+
mockMvc.perform(post("/api/encounters")
109+
.contentType(MediaType.APPLICATION_JSON)
110+
.content(objectMapper.writeValueAsString(invalidRequest)))
111+
.andExpect(status().isBadRequest());
112+
}
113+
114+
@Test
115+
void getEncounter_WithValidId_ReturnsEncounter() throws Exception {
116+
when(encounterService.getEncounterById(1L)).thenReturn(encounter);
117+
when(encounterMapper.toResponse(encounter)).thenReturn(encounterResponse);
118+
119+
mockMvc.perform(get("/api/encounters/1"))
120+
.andExpect(status().isOk());
121+
}
122+
123+
@Test
124+
void getEncounter_WithInvalidId_ReturnsNotFound() throws Exception {
125+
when(encounterService.getEncounterById(99L))
126+
.thenThrow(new RuntimeException("Encounter not found"));
127+
128+
mockMvc.perform(get("/api/encounters/99"))
129+
.andExpect(status().isNotFound());
130+
}
131+
132+
@Test
133+
void getEncountersByPatientId_ReturnsEncountersList() throws Exception {
134+
List<Encounter> encounters = Arrays.asList(encounter);
135+
List<EncounterResponse> responses = Arrays.asList(encounterResponse);
136+
137+
when(encounterService.getEncountersByPatientId(1L)).thenReturn(encounters);
138+
when(encounterMapper.toResponseList(encounters)).thenReturn(responses);
139+
140+
mockMvc.perform(get("/api/encounters/patient/1"))
141+
.andExpect(status().isOk());
142+
}
143+
144+
@Test
145+
void getEncountersByPatientIdWithPagination_ReturnsPaginatedResults() throws Exception {
146+
Page<Encounter> encounterPage = new PageImpl<>(Arrays.asList(encounter));
147+
when(encounterService.getEncountersByPatientId(eq(1L), any(PageRequest.class))).thenReturn(encounterPage);
148+
when(encounterMapper.toResponse(any(Encounter.class))).thenReturn(encounterResponse);
149+
150+
mockMvc.perform(get("/api/encounters/patient/1/page")
151+
.param("page", "0")
152+
.param("size", "10")
153+
.param("sortBy", "startDateTime")
154+
.param("direction", "desc"))
155+
.andExpect(status().isOk());
156+
}
157+
158+
@Test
159+
void getAllEncounters_ReturnsPaginatedResults() throws Exception {
160+
Page<Encounter> encounterPage = new PageImpl<>(Arrays.asList(encounter));
161+
when(encounterService.getAllEncounters(any(PageRequest.class))).thenReturn(encounterPage);
162+
when(encounterMapper.toResponse(any(Encounter.class))).thenReturn(encounterResponse);
163+
164+
mockMvc.perform(get("/api/encounters")
165+
.param("page", "0")
166+
.param("size", "10")
167+
.param("sortBy", "startDateTime")
168+
.param("direction", "desc"))
169+
.andExpect(status().isOk());
170+
}
171+
172+
@Test
173+
void getEncountersByDateRange_ReturnsResults() throws Exception {
174+
List<Encounter> encounters = Arrays.asList(encounter);
175+
List<EncounterResponse> responses = Arrays.asList(encounterResponse);
176+
177+
when(encounterService.getEncountersByDateRange(any(LocalDateTime.class), any(LocalDateTime.class)))
178+
.thenReturn(encounters);
179+
when(encounterMapper.toResponseList(encounters)).thenReturn(responses);
180+
181+
mockMvc.perform(get("/api/encounters/search/date-range")
182+
.param("start", "2024-01-01 00:00:00")
183+
.param("end", "2024-01-31 23:59:59"))
184+
.andExpect(status().isOk());
185+
}
186+
187+
@Test
188+
void getEncountersByClass_ReturnsResults() throws Exception {
189+
List<Encounter> encounters = Arrays.asList(encounter);
190+
List<EncounterResponse> responses = Arrays.asList(encounterResponse);
191+
192+
when(encounterService.getEncountersByClass("OUTPATIENT")).thenReturn(encounters);
193+
when(encounterMapper.toResponseList(encounters)).thenReturn(responses);
194+
195+
mockMvc.perform(get("/api/encounters/search/class/OUTPATIENT"))
196+
.andExpect(status().isOk());
197+
}
198+
199+
@Test
200+
void updateEncounter_WithValidData_ReturnsUpdatedEncounter() throws Exception {
201+
when(patientService.getPatientById(1L)).thenReturn(patient);
202+
when(encounterMapper.toEntity(any(EncounterRequest.class), eq(patient))).thenReturn(encounter);
203+
when(encounterService.updateEncounter(eq(1L), any(Encounter.class))).thenReturn(encounter);
204+
when(encounterMapper.toResponse(any(Encounter.class))).thenReturn(encounterResponse);
205+
206+
mockMvc.perform(put("/api/encounters/1")
207+
.contentType(MediaType.APPLICATION_JSON)
208+
.content(objectMapper.writeValueAsString(encounterRequest)))
209+
.andExpect(status().isOk());
210+
}
211+
212+
@Test
213+
void deleteEncounter_WithValidId_ReturnsNoContent() throws Exception {
214+
mockMvc.perform(delete("/api/encounters/1"))
215+
.andExpect(status().isNoContent());
216+
}
217+
218+
@Test
219+
void getEncounterCountByPatient_ReturnsCount() throws Exception {
220+
when(encounterService.getEncounterCountByPatientId(1L)).thenReturn(3L);
221+
222+
mockMvc.perform(get("/api/encounters/patient/1/count"))
223+
.andExpect(status().isOk())
224+
.andExpect(content().string("3"));
225+
}
226+
227+
@Test
228+
void createEncounter_WithNonExistentPatient_ReturnsNotFound() throws Exception {
229+
when(patientService.getPatientById(99L))
230+
.thenThrow(new RuntimeException("Patient not found"));
231+
232+
mockMvc.perform(post("/api/encounters")
233+
.contentType(MediaType.APPLICATION_JSON)
234+
.content(objectMapper.writeValueAsString(encounterRequest)))
235+
.andExpect(status().isNotFound());
236+
}
237+
238+
@Test
239+
void updateEncounter_WithInvalidEncounterClass_ReturnsBadRequest() throws Exception {
240+
EncounterRequest invalidRequest = new EncounterRequest(1L,
241+
LocalDateTime.of(2024, 1, 10, 9, 0),
242+
LocalDateTime.of(2024, 1, 10, 10, 30),
243+
"INVALID_CLASS", "Test description");
244+
245+
mockMvc.perform(put("/api/encounters/1")
246+
.contentType(MediaType.APPLICATION_JSON)
247+
.content(objectMapper.writeValueAsString(invalidRequest)))
248+
.andExpect(status().isBadRequest());
249+
}
250+
251+
@Test
252+
void getEncountersByDateRange_WithInvalidDateRange_ReturnsBadRequest() throws Exception {
253+
mockMvc.perform(get("/api/encounters/search/date-range")
254+
.param("start", "invalid-date")
255+
.param("end", "2024-01-31 23:59:59"))
256+
.andExpect(status().isBadRequest());
257+
}
258+
}

0 commit comments

Comments
 (0)