Skip to content

Commit 34c1925

Browse files
codesungrapenewmaldenite
authored andcommitted
Feat/implement controller layer (#5)
* Implement HearingController with initial TDD and isolate WebMvc tests * Add not found test * Remove redundant findAll declaration from repository # Conflicts: # src/main/java/uk/gov/hmcts/reform/dataViewstoreRestApi/controllers/HearingController.java
1 parent 47e1dd6 commit 34c1925

3 files changed

Lines changed: 69 additions & 2 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package uk.gov.hmcts.reform.dataViewstoreRestApi.controllers;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
6+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
7+
import org.springframework.test.web.servlet.MockMvc;
8+
import uk.gov.hmcts.reform.dataViewstoreRestApi.dto.HearingResponse;
9+
import uk.gov.hmcts.reform.dataViewstoreRestApi.service.HearingService;
10+
11+
import java.time.OffsetDateTime;
12+
import java.util.List;
13+
import java.util.UUID;
14+
15+
import static org.mockito.Mockito.when;
16+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
17+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
18+
19+
@WebMvcTest(HearingController.class)
20+
class HearingControllerTest {
21+
22+
@Autowired
23+
private MockMvc mockMvc;
24+
25+
@MockitoBean
26+
private HearingService service;
27+
28+
29+
@Test
30+
void shouldReturnOkAndAllHearingDtos() throws Exception {
31+
32+
UUID id1 = UUID.randomUUID();
33+
UUID id2 = UUID.randomUUID();
34+
OffsetDateTime lastModified1 = OffsetDateTime.parse("2024-01-01T10:00:00Z");
35+
OffsetDateTime lastModified2 = OffsetDateTime.parse("2024-06-01T10:00:00Z");
36+
37+
List<HearingResponse> mockDtos = List.of(
38+
new HearingResponse(id1, lastModified1),
39+
new HearingResponse(id2, lastModified2)
40+
);
41+
42+
when(service.getAllHearings()).thenReturn(mockDtos);
43+
44+
mockMvc.perform(get("/hearings"))
45+
.andExpect(status().isOk())
46+
.andExpect(jsonPath("$[0].id").value(id1.toString()))
47+
.andExpect(jsonPath("$[0].lastModified").exists())
48+
.andExpect(jsonPath("$[0].hearingTypeId").doesNotExist());
49+
50+
}
51+
52+
@Test
53+
void shouldReturnOkAndEmptyListWhenNoHearingsExist() throws Exception {
54+
when(service.getAllHearings()).thenReturn(List.of());
55+
56+
mockMvc.perform(get("/hearings"))
57+
.andExpect(status().isOk())
58+
.andExpect(content().contentType("application/json"))
59+
.andExpect(jsonPath("$").isArray())
60+
.andExpect(jsonPath("$").isEmpty());
61+
}
62+
}

src/main/java/uk/gov/hmcts/reform/dataViewstoreRestApi/controllers/HearingController.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
import org.springframework.web.bind.annotation.PathVariable;
99
import org.springframework.web.bind.annotation.RequestMapping;
1010
import org.springframework.web.bind.annotation.RestController;
11+
import uk.gov.hmcts.reform.dataViewstoreRestApi.dto.HearingResponse;
1112
import uk.gov.hmcts.reform.dataViewstoreRestApi.dto.HearingDetailedResponse;
1213
import uk.gov.hmcts.reform.dataViewstoreRestApi.service.HearingService;
1314

15+
import java.util.List;
1416
import java.util.UUID;
1517

1618
@RestController
@@ -24,6 +26,11 @@ public HearingController(HearingService hearingService) {
2426
this.hearingService = hearingService;
2527
}
2628

29+
@GetMapping("/hearings")
30+
public ResponseEntity<List<HearingResponse>> getHearings() {
31+
return ResponseEntity.ok(hearingService.getAllHearings());
32+
}
33+
2734
@GetMapping("/{id}")
2835
@Operation(
2936
summary = "Get a hearing by ID",

src/main/java/uk/gov/hmcts/reform/dataViewstoreRestApi/repository/HearingRepository.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
public interface HearingRepository extends JpaRepository<Hearing, UUID> {
1111

1212

13-
List<Hearing> findAll();
14-
1513

1614
}
1715

0 commit comments

Comments
 (0)