Skip to content

Commit

Permalink
DTSCCI-313 check defendant is linked for civil claims (#5098)
Browse files Browse the repository at this point in the history
* check if civil claim is linked.

* clean up functionality to use 1 endpoint for both ocmc and civil cases.

* remove unnecessary try catch

* fix build

* add test for sonar

* add test for sonar (2)

* clean + test

* improve test

---------

Co-authored-by: Nigel Dunne <[email protected]>
Co-authored-by: mfallonhmcts <[email protected]>
  • Loading branch information
3 people committed Aug 15, 2024
1 parent 0b6d0a1 commit b5ef5bb
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import uk.gov.hmcts.reform.ccd.client.model.CaseDetails;
import uk.gov.hmcts.reform.civil.CaseDefinitionConstants;
import uk.gov.hmcts.reform.civil.controllers.BaseIntegrationTest;
import uk.gov.hmcts.reform.civil.model.DefendantLinkStatus;
import uk.gov.hmcts.reform.civil.service.AssignCaseService;
import uk.gov.hmcts.reform.civil.service.CoreCaseDataService;
import uk.gov.hmcts.reform.civil.service.citizen.defendant.LipDefendantCaseAssignmentService;
Expand All @@ -27,7 +29,7 @@ public class CaseAssignmentControllerTest extends BaseIntegrationTest {
private static final String VALIDATE_OCMC_PIN_URL = CASES_URL + "/reference/{caseReference}/ocmc";
private static final String ASSIGN_CASE = CASES_URL + "/case/{caseId}/{caseRole}";

private static final String DEFENDENT_LINK_CHECK_URL = CASES_URL + "/reference/{caseReference}/ocmc";
private static final String DEFENDENT_LINK_CHECK_URL = CASES_URL + "/reference/{caseReference}/defendant-link-status";

@MockBean
private CaseLegacyReferenceSearchService caseByLegacyReferenceSearchService;
Expand Down Expand Up @@ -101,20 +103,56 @@ void givenCorrectParams_whenAssignClaim_forDefendant_shouldReturnStatusOk() {
doPost("authorization", "12345", ASSIGN_CASE, "123", "DEFENDANT")
.andExpect(status().isOk());
}

@Test
@SneakyThrows
void givenCorrectClaim_whenDefendantLinkedStatusFalse_shouldReturnStatusOk() {
void givenCorrectOcmcClaim_whenDefendantLinkedStatusFalse_shouldReturnStatusOk() {
CaseDetails caseDetails = givenCaseIsFound();
caseDetails.setCaseTypeId(CaseDefinitionConstants.CMC_CASE_TYPE);
when(defendantPinToPostLRspecService.isOcmcDefendantLinked(anyString())).thenReturn(false);
DefendantLinkStatus defendantLinkStatus = new DefendantLinkStatus(true, false);

doGet("", DEFENDENT_LINK_CHECK_URL, "620MC123")
.andExpect(content().json(toJson(defendantLinkStatus)))
.andExpect(status().isOk());
}

@Test
@SneakyThrows
void givenCorrectClaim_whenDefendantLinkedStatusTrue_shouldReturnStatusOk() {
void givenCorrectOcmcClaim_whenDefendantLinkedStatusTrue_shouldReturnStatusOk() {
CaseDetails caseDetails = givenCaseIsFound();
caseDetails.setCaseTypeId(CaseDefinitionConstants.CMC_CASE_TYPE);
when(defendantPinToPostLRspecService.isOcmcDefendantLinked(anyString())).thenReturn(true);
DefendantLinkStatus defendantLinkStatus = new DefendantLinkStatus(true, true);

doGet("", DEFENDENT_LINK_CHECK_URL, "620MC123")
.andExpect(content().json(toJson(defendantLinkStatus)))
.andExpect(status().isOk());
}

@Test
@SneakyThrows
void givenCorrectClaim_whenDefendantLinkedStatusFalse_shouldReturnStatusOk() {
CaseDetails caseDetails = givenCaseIsFound();
caseDetails.setCaseTypeId(CaseDefinitionConstants.CASE_TYPE);
when(defendantPinToPostLRspecService.isDefendantLinked(any())).thenReturn(false);
DefendantLinkStatus defendantLinkStatus = new DefendantLinkStatus(false, false);

doGet("", DEFENDENT_LINK_CHECK_URL, "620MC123")
.andExpect(content().json(toJson(defendantLinkStatus)))
.andExpect(status().isOk());
}

@Test
@SneakyThrows
void givenCorrectClaim_whenDefendantLinkedStatusTrue_shouldReturnStatusOk() {
CaseDetails caseDetails = givenCaseIsFound();
caseDetails.setCaseTypeId(CaseDefinitionConstants.CASE_TYPE);
when(defendantPinToPostLRspecService.isDefendantLinked(any())).thenReturn(true);
DefendantLinkStatus defendantLinkStatus = new DefendantLinkStatus(false, true);

doGet("", DEFENDENT_LINK_CHECK_URL, "620MC123")
.andExpect(content().json(toJson(defendantLinkStatus)))
.andExpect(status().isOk());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import uk.gov.hmcts.reform.ccd.client.model.CaseDetails;
import uk.gov.hmcts.reform.civil.CaseDefinitionConstants;
import uk.gov.hmcts.reform.civil.enums.CaseRole;
import uk.gov.hmcts.reform.civil.model.DefendantLinkStatus;
import uk.gov.hmcts.reform.civil.model.citizenui.dto.PinDto;
import uk.gov.hmcts.reform.civil.service.AssignCaseService;
import uk.gov.hmcts.reform.civil.service.CoreCaseDataService;
Expand Down Expand Up @@ -77,18 +79,26 @@ public ResponseEntity<String> validateOcmcPin(
}

@GetMapping(path = {
"/reference/{caseReference}/ocmc"
"/reference/{caseReference}/defendant-link-status"
})
@Operation(summary = "Check whether a claim is linked to a defendant")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "401", description = "Not Authorized"),
@ApiResponse(responseCode = "400", description = "Bad Request")})
public ResponseEntity<Boolean> isOcmcDefendantLinked(
public ResponseEntity<DefendantLinkStatus> isDefendantLinked(
@PathVariable("caseReference") String caseReference) {
log.info("Check claim reference {} is linked to defendant", caseReference);
boolean status = defendantPinToPostLRspecService.isOcmcDefendantLinked(caseReference);
return new ResponseEntity<>(status, HttpStatus.OK);
CaseDetails caseDetails = caseByLegacyReferenceSearchService.getCaseDataByLegacyReference(caseReference);
boolean isOcmcCase = caseDetails.getCaseTypeId().equals(CaseDefinitionConstants.CMC_CASE_TYPE);
boolean status;
if (isOcmcCase) {
status = defendantPinToPostLRspecService.isOcmcDefendantLinked(caseReference);
} else {
status = defendantPinToPostLRspecService.isDefendantLinked(caseDetails);
}
DefendantLinkStatus defendantLinkStatus = new DefendantLinkStatus(isOcmcCase, status);
return new ResponseEntity<>(defendantLinkStatus, HttpStatus.OK);
}

@PostMapping(path = "/case/{caseId}/{caseRole}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package uk.gov.hmcts.reform.civil.model;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class DefendantLinkStatus {

private boolean isOcmcCase;
private boolean linked;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package uk.gov.hmcts.reform.civil.service.pininpost;

import feign.FeignException;
import feign.Response;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -11,6 +10,7 @@
import uk.gov.hmcts.reform.civil.helpers.CaseDetailsConverter;
import uk.gov.hmcts.reform.civil.model.CaseData;
import uk.gov.hmcts.reform.civil.model.DefendantPinToPostLRspec;
import uk.gov.hmcts.reform.civil.model.IdamUserDetails;
import uk.gov.hmcts.reform.civil.service.CoreCaseDataService;
import uk.gov.hmcts.reform.civil.service.claimstore.ClaimStoreService;
import uk.gov.hmcts.reform.civil.service.pininpost.exception.PinNotMatchException;
Expand Down Expand Up @@ -45,21 +45,16 @@ public void validatePin(CaseDetails caseDetails, String pin) {
}

public Map<String, Object> removePinInPostData(CaseDetails caseDetails) {
try {
CaseData caseData = caseDetailsConverter.toCaseData(caseDetails);
DefendantPinToPostLRspec pinInPostData = caseData.getRespondent1PinToPostLRspec();
DefendantPinToPostLRspec updatePinInPostData = DefendantPinToPostLRspec.builder()
.citizenCaseRole(pinInPostData.getCitizenCaseRole())
.respondentCaseRole(pinInPostData.getRespondentCaseRole())
.expiryDate(pinInPostData.getExpiryDate()).build();
CaseData caseData = caseDetailsConverter.toCaseData(caseDetails);
DefendantPinToPostLRspec pinInPostData = caseData.getRespondent1PinToPostLRspec();
DefendantPinToPostLRspec updatePinInPostData = DefendantPinToPostLRspec.builder()
.citizenCaseRole(pinInPostData.getCitizenCaseRole())
.respondentCaseRole(pinInPostData.getRespondentCaseRole())
.expiryDate(pinInPostData.getExpiryDate()).build();

Map<String, Object> data = new HashMap<>();
data.put("respondent1PinToPostLRspec", updatePinInPostData);
return data;
} catch (FeignException e) {
log.error(String.format("Updating case data failed: %s", e.contentUTF8()));
throw e;
}
Map<String, Object> data = new HashMap<>();
data.put("respondent1PinToPostLRspec", updatePinInPostData);
return data;
}

public DefendantPinToPostLRspec buildDefendantPinToPost() {
Expand Down Expand Up @@ -100,4 +95,10 @@ public boolean isOcmcDefendantLinked(String caseReference) {
log.info("ocmc case reference {} defendent status is {}", caseReference, status.isLinked());
return status.isLinked();
}

public boolean isDefendantLinked(CaseDetails caseDetails) {
CaseData caseData = caseDetailsConverter.toCaseData(caseDetails);
IdamUserDetails defendantUserDetails = caseData.getDefendantUserDetails();
return defendantUserDetails != null && defendantUserDetails.getId() != null && defendantUserDetails.getEmail() != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import uk.gov.hmcts.reform.civil.model.BusinessProcess;
import uk.gov.hmcts.reform.civil.model.CaseData;
import uk.gov.hmcts.reform.civil.model.DefendantPinToPostLRspec;
import uk.gov.hmcts.reform.civil.model.IdamUserDetails;
import uk.gov.hmcts.reform.civil.sampledata.CaseDataBuilder;
import uk.gov.hmcts.reform.civil.sampledata.CaseDetailsBuilder;
import uk.gov.hmcts.reform.civil.service.claimstore.ClaimStoreService;
Expand Down Expand Up @@ -149,7 +150,7 @@ void shouldResetPinExpiryDateSuccessfully() {
}

@Test
void shouldReturnTrueIfDefendantIsLinked() {
void shouldReturnTrueIfOcmcDefendantIsLinked() {
when(claimStoreService.isOcmcDefendantLinked("620MC123")).thenReturn(createDefendantLinkStatus(true));

boolean status = defendantPinToPostLRspecService.isOcmcDefendantLinked("620MC123");
Expand All @@ -158,14 +159,62 @@ void shouldReturnTrueIfDefendantIsLinked() {
}

@Test
void shouldReturnFalseIfDefendantIsNotLinked() {
void shouldReturnFalseIfOcmcDefendantIsNotLinked() {
when(claimStoreService.isOcmcDefendantLinked("620MC123")).thenReturn(createDefendantLinkStatus(false));

boolean status = defendantPinToPostLRspecService.isOcmcDefendantLinked("620MC123");

assertFalse(status);
}

@Test
void shouldReturnTrueIfDefendantIsLinked() {
CaseData caseData = new CaseDataBuilder()
.defendantUserDetails(
IdamUserDetails.builder()
.id("1234")
.email("[email protected]")
.build())
.build();
CaseDetails caseDetails = createCaseDetails(caseData);
when(caseDetailsConverter.toCaseData(caseDetails)).thenReturn(caseData);
boolean status = defendantPinToPostLRspecService.isDefendantLinked(caseDetails);

assertTrue(status);
}

@Test
void shouldReturnFalseIfDefendantIsNotLinked() {
CaseData caseData = new CaseDataBuilder().defendantUserDetails(null).build();
CaseDetails caseDetails = createCaseDetails(caseData);
when(caseDetailsConverter.toCaseData(caseDetails)).thenReturn(caseData);
boolean status = defendantPinToPostLRspecService.isDefendantLinked(caseDetails);

assertFalse(status);
}

@Test
void shouldReturnFalseIfUserDetailsEmailIsEmpty() {
CaseData caseData = new CaseDataBuilder().defendantUserDetails(IdamUserDetails.builder().id("1234").build()).build();
CaseDetails caseDetails = createCaseDetails(caseData);
when(caseDetailsConverter.toCaseData(caseDetails)).thenReturn(caseData);
boolean status = defendantPinToPostLRspecService.isDefendantLinked(caseDetails);

assertFalse(status);
}

@Test
void shouldReturnFalseIfUserDetailsIdIsEmpty() {
CaseData caseData = new CaseDataBuilder().defendantUserDetails(
IdamUserDetails.builder().email("[email protected]").build()
).build();
CaseDetails caseDetails = createCaseDetails(caseData);
when(caseDetailsConverter.toCaseData(caseDetails)).thenReturn(caseData);
boolean status = defendantPinToPostLRspecService.isDefendantLinked(caseDetails);

assertFalse(status);
}

private CaseData createCaseDataWithPin(String accessCode, int daysToExpiry) {
return new CaseDataBuilder().atStateClaimSubmitted()
.addRespondent1PinToPostLRspec(createPinToPost(accessCode, LocalDate.now().plusDays(daysToExpiry)))
Expand Down

0 comments on commit b5ef5bb

Please sign in to comment.