|
| 1 | +package uk.gov.hmcts.ccd.v2.external.controller; |
| 2 | + |
| 3 | +import com.google.common.collect.Lists; |
| 4 | +import org.junit.jupiter.api.BeforeEach; |
| 5 | +import org.junit.jupiter.api.DisplayName; |
| 6 | +import org.junit.jupiter.api.Nested; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.mockito.Mock; |
| 9 | +import org.mockito.MockitoAnnotations; |
| 10 | +import org.springframework.hateoas.MediaTypes; |
| 11 | +import org.springframework.http.MediaType; |
| 12 | +import org.springframework.test.web.servlet.MockMvc; |
| 13 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 14 | +import uk.gov.hmcts.ccd.ApplicationParams; |
| 15 | +import uk.gov.hmcts.ccd.data.SecurityUtils; |
| 16 | +import uk.gov.hmcts.ccd.domain.model.std.CaseAssignedUserRole; |
| 17 | +import uk.gov.hmcts.ccd.domain.service.cauroles.CaseAssignedUserRolesOperation; |
| 18 | +import uk.gov.hmcts.ccd.domain.service.common.UIDService; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import static org.mockito.ArgumentMatchers.anyList; |
| 23 | +import static org.mockito.ArgumentMatchers.anyString; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 26 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 27 | + |
| 28 | +class CaseAssignedUserRolesControllerContentNegotiationTest { |
| 29 | + |
| 30 | + private static final String CASE_ID_GOOD = "4444333322221111"; |
| 31 | + |
| 32 | + @Mock |
| 33 | + private ApplicationParams applicationParams; |
| 34 | + |
| 35 | + @Mock |
| 36 | + private UIDService caseReferenceService; |
| 37 | + |
| 38 | + @Mock |
| 39 | + private CaseAssignedUserRolesOperation caseAssignedUserRolesOperation; |
| 40 | + |
| 41 | + @Mock |
| 42 | + private SecurityUtils securityUtils; |
| 43 | + |
| 44 | + private MockMvc mockMvc; |
| 45 | + |
| 46 | + @BeforeEach |
| 47 | + void setUp() { |
| 48 | + MockitoAnnotations.openMocks(this); |
| 49 | + |
| 50 | + CaseAssignedUserRolesController controller = new CaseAssignedUserRolesController( |
| 51 | + applicationParams, |
| 52 | + caseReferenceService, |
| 53 | + caseAssignedUserRolesOperation, |
| 54 | + securityUtils |
| 55 | + ); |
| 56 | + |
| 57 | + mockMvc = MockMvcBuilders |
| 58 | + .standaloneSetup(controller) |
| 59 | + .build(); |
| 60 | + |
| 61 | + when(caseReferenceService.validateUID(anyString())).thenReturn(true); |
| 62 | + when(caseAssignedUserRolesOperation.findCaseUserRoles(anyList(), anyList())) |
| 63 | + .thenReturn(List.of(new CaseAssignedUserRole())); |
| 64 | + } |
| 65 | + |
| 66 | + @Nested |
| 67 | + @DisplayName("GET /case-users content negotiation") |
| 68 | + class GetCaseUserRolesContentNegotiation { |
| 69 | + |
| 70 | + @Test |
| 71 | + @DisplayName("should return 200 when Accept header is application/json") |
| 72 | + void getCaseUserRoles_shouldReturn200_whenAcceptHeaderIsApplicationJson() throws Exception { |
| 73 | + mockMvc.perform(get("/case-users") |
| 74 | + .param("case_ids", CASE_ID_GOOD) |
| 75 | + .accept(MediaType.APPLICATION_JSON)) |
| 76 | + .andExpect(status().isOk()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + @DisplayName("should return 200 when Accept header is application/hal+json") |
| 81 | + void getCaseUserRoles_shouldReturn200_whenAcceptHeaderIsHalJson() throws Exception { |
| 82 | + mockMvc.perform(get("/case-users") |
| 83 | + .param("case_ids", CASE_ID_GOOD) |
| 84 | + .accept(MediaTypes.HAL_JSON)) |
| 85 | + .andExpect(status().isOk()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + @DisplayName("should return 200 when Accept header is wildcard") |
| 90 | + void getCaseUserRoles_shouldReturn200_whenAcceptHeaderIsWildcard() throws Exception { |
| 91 | + mockMvc.perform(get("/case-users") |
| 92 | + .param("case_ids", CASE_ID_GOOD) |
| 93 | + .accept(MediaType.ALL)) |
| 94 | + .andExpect(status().isOk()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + @DisplayName("should return 406 when Accept header is unsupported media type") |
| 99 | + void getCaseUserRoles_shouldReturn406_whenAcceptHeaderIsUnsupported() throws Exception { |
| 100 | + mockMvc.perform(get("/case-users") |
| 101 | + .param("case_ids", CASE_ID_GOOD) |
| 102 | + .accept(MediaType.APPLICATION_XML)) |
| 103 | + .andExpect(status().isNotAcceptable()); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments