Skip to content

Commit

Permalink
SSCSSI-247: Added new method updateCaseV2WithUnaryFunction (#1665)
Browse files Browse the repository at this point in the history
* SSCSSI-247: Overloaded updateCaseV2 and updated recover methods

* SSCSSI-247: Added new method updateCaseV2WithUnaryFunction

* SSCSSI-247: Updated supressions file

* SSCSSI-247: Reverted updates

* SSCSSI-247: restored updateCaseV2WithUnaryFunction

* SSCSSI-247: Added recover method

* SSCSSI-247: Restored UpdateCcdCaseServiceTest

* SSCSSI-247: Updates to updateCaseV2

* SSCSSI-247: Reverted indenation to master

* SSCSSI-247: Added null check for UpdateResult SscsCaseDetails

* SSCSSI-247: Replaced optional with null

* Revert "SSCSCI-1214 (#1667)"

This reverts commit 2c587eb.

* SSCSCI-1214 (#1667)

* updating country of residence and port of entry Enum's to reflect new values

* updated port of entries

* updating airlookup for new ports of entry and ibc RPCs

* updating airlookup to remove duplicates

* updating AirLookupServiceTest to check for whitespace and different casing

* updating suppressions

(cherry picked from commit 2c587eb)

---------

Co-authored-by: sanjayparekh-hmcts <[email protected]>
Co-authored-by: Nilay <[email protected]>
  • Loading branch information
3 people authored Dec 11, 2024
1 parent 2c587eb commit eaccce6
Show file tree
Hide file tree
Showing 2 changed files with 243 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;

import java.util.function.UnaryOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.annotation.Recover;
Expand Down Expand Up @@ -47,6 +47,23 @@ public SscsCaseDetails updateCaseV2(Long caseId, String eventType, String summar
});
}

@Retryable
public SscsCaseDetails updateCaseV2WithUnaryFunction(Long caseId, String eventType, String summary, String description, IdamTokens idamTokens, UnaryOperator<SscsCaseDetails> mutator) {
return updateCaseV2(caseId, eventType, idamTokens, caseDetails -> {
SscsCaseDetails sscsCaseDetails = mutator.apply(caseDetails);
return new UpdateResult(sscsCaseDetails, summary, description);
});
}

@Recover
public SscsCaseDetails recoverUpdateCaseV2WithUnaryFunction(RuntimeException exception, Long caseId, String eventType, String summary, String description, IdamTokens idamTokens, UnaryOperator<SscsCaseDetails> mutator) {
log.error("In recover method(updateCaseV2WithUnaryFunction) for caseId {} and eventType {}",
caseId,
eventType,
exception);
throw exception;
}

public SscsCaseDetails updateCaseV2WithoutRetry(Long caseId, String eventType, String summary, String description, IdamTokens idamTokens, Consumer<SscsCaseDetails> mutator) {
return updateCaseV2(caseId, eventType, idamTokens, caseDetails -> {
mutator.accept(caseDetails);
Expand All @@ -59,7 +76,11 @@ public SscsCaseDetails triggerCaseEventV2(Long caseId, String eventType, String
return updateCaseV2(caseId, eventType, idamTokens, caseDetails -> new UpdateResult(summary, description));
}

public record UpdateResult(String summary, String description) { }
public record UpdateResult(SscsCaseDetails sscsCaseDetails, String summary, String description) {
public UpdateResult(String summary, String description) {
this(null, summary, description);
}
}

/**
* Update a case while making correct use of CCD's optimistic locking.
Expand All @@ -81,7 +102,13 @@ public SscsCaseDetails updateCaseV2(Long caseId, String eventType, IdamTokens id
data.sortCollections();

var result = mutator.apply(caseDetails);
CaseDataContent caseDataContent = sscsCcdConvertService.getCaseDataContent(caseDetails.getData(), startEventResponse, result.summary, result.description);
SscsCaseData sscsCaseData = caseDetails.getData();
if (result.sscsCaseDetails != null) {
log.info("Result contains sscsCaseDetails for caseId {}", caseId);
sscsCaseData = result.sscsCaseDetails.getData();
}

CaseDataContent caseDataContent = sscsCcdConvertService.getCaseDataContent(sscsCaseData, startEventResponse, result.summary, result.description);

return sscsCcdConvertService.getCaseDetails(ccdClient.submitEventForCaseworker(idamTokens, caseId, caseDataContent));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
package uk.gov.hmcts.reform.sscs.ccd.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static uk.gov.hmcts.reform.sscs.ccd.domain.EventType.POST_HEARING_REQUEST;
import static uk.gov.hmcts.reform.sscs.ccd.domain.YesNo.NO;
import static uk.gov.hmcts.reform.sscs.ccd.domain.YesNo.YES;

import java.util.function.UnaryOperator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import uk.gov.hmcts.reform.ccd.client.model.StartEventResponse;
import uk.gov.hmcts.reform.sscs.ccd.client.CcdClient;
import uk.gov.hmcts.reform.sscs.ccd.domain.SscsCaseData;
import uk.gov.hmcts.reform.sscs.ccd.domain.SscsCaseDetails;
import uk.gov.hmcts.reform.sscs.idam.IdamService;
import uk.gov.hmcts.reform.sscs.idam.IdamTokens;

@ExtendWith(MockitoExtension.class)
public class UpdateCcdCaseServiceTest {

@Mock
private IdamService idamService;
@Mock
private SscsCcdConvertService sscsCcdConvertService;
@Mock
private CcdClient ccdClient;

@Captor
private ArgumentCaptor<SscsCaseData> sscsCaseDataArgumentCaptor;

@InjectMocks
private UpdateCcdCaseService updateCcdCaseService;

@Test
void handleUpdateCaseV2WithUnaryFunction() {
long caseId = 1234L;
String benefitCode = "200DD";
UnaryOperator<SscsCaseDetails> unaryOperator = sscsCaseDetails -> sscsCaseDetails.toBuilder()
.data(SscsCaseData.builder()
.addDocuments(YES)
.benefitCode(benefitCode)
.build())
.build();

when(ccdClient.startEvent(isA(IdamTokens.class), anyLong(), eq(POST_HEARING_REQUEST.getType())))
.thenReturn(StartEventResponse.builder().build());

when(sscsCcdConvertService.getCaseDetails(isA(StartEventResponse.class)))
.thenReturn(SscsCaseDetails.builder()
.data(SscsCaseData.builder()
.addDocuments(NO)
.build())
.build());

String postHearingSummary = "Post hearing Summary";
String postHearingDescription = "Post hearing Description";

updateCcdCaseService.updateCaseV2WithUnaryFunction(
caseId,
POST_HEARING_REQUEST.getType(),
postHearingSummary,
postHearingDescription,
IdamTokens.builder().build(),
unaryOperator);

verify(sscsCcdConvertService).getCaseDataContent(
sscsCaseDataArgumentCaptor.capture(),
any(),
eq(postHearingSummary),
eq(postHearingDescription));

SscsCaseData sscsCaseData = sscsCaseDataArgumentCaptor.getValue();
assertThat(sscsCaseData.getAddDocuments())
.isEqualTo(YES);
assertThat(sscsCaseData.getBenefitCode())
.isEqualTo(benefitCode);
}

@Test
void handleUpdateCaseV2() {
long caseId = 1234L;
String benefitCode = "200DD";

when(ccdClient.startEvent(isA(IdamTokens.class), anyLong(), eq(POST_HEARING_REQUEST.getType())))
.thenReturn(StartEventResponse.builder().build());

when(sscsCcdConvertService.getCaseDetails(isA(StartEventResponse.class)))
.thenReturn(SscsCaseDetails.builder()
.data(SscsCaseData.builder()
.addDocuments(NO)
.build())
.build());

String postHearingSummary = "Post hearing Summary";
String postHearingDescription = "Post hearing Description";

updateCcdCaseService.updateCaseV2(
caseId,
POST_HEARING_REQUEST.getType(),
postHearingSummary,
postHearingDescription,
IdamTokens.builder().build(),
sscsCaseDetails -> {
sscsCaseDetails.getData().setAddDocuments(YES);
sscsCaseDetails.getData().setBenefitCode(benefitCode);
});

verify(sscsCcdConvertService).getCaseDataContent(
sscsCaseDataArgumentCaptor.capture(),
any(),
eq(postHearingSummary),
eq(postHearingDescription));

SscsCaseData sscsCaseData = sscsCaseDataArgumentCaptor.getValue();
assertThat(sscsCaseData.getAddDocuments())
.isEqualTo(YES);
assertThat(sscsCaseData.getBenefitCode())
.isEqualTo(benefitCode);
}

@Test
void handleUpdateCaseV2WithUpdateResult() {
long caseId = 1234L;
String benefitCode = "200DD";

when(ccdClient.startEvent(isA(IdamTokens.class), anyLong(), eq(POST_HEARING_REQUEST.getType())))
.thenReturn(StartEventResponse.builder().build());

when(sscsCcdConvertService.getCaseDetails(isA(StartEventResponse.class)))
.thenReturn(SscsCaseDetails.builder()
.data(SscsCaseData.builder()
.addDocuments(NO)
.build())
.build());

String postHearingSummary = "Post hearing Summary";
String postHearingDescription = "Post hearing Description";

updateCcdCaseService.updateCaseV2(
caseId,
POST_HEARING_REQUEST.getType(),
IdamTokens.builder().build(),
sscsCaseDetails -> {
sscsCaseDetails.getData().setAddDocuments(YES);
sscsCaseDetails.getData().setBenefitCode(benefitCode);
return new UpdateCcdCaseService.UpdateResult(postHearingSummary, postHearingDescription);
});

verify(sscsCcdConvertService).getCaseDataContent(
sscsCaseDataArgumentCaptor.capture(),
any(),
eq(postHearingSummary),
eq(postHearingDescription));

SscsCaseData sscsCaseData = sscsCaseDataArgumentCaptor.getValue();
assertThat(sscsCaseData.getAddDocuments())
.isEqualTo(YES);
assertThat(sscsCaseData.getBenefitCode())
.isEqualTo(benefitCode);
}

@Test
void handleUpdateCaseV2WithUpdateResultSetWithNullSccsCaseDetails() {
long caseId = 1234L;
String benefitCode = "200DD";

when(ccdClient.startEvent(isA(IdamTokens.class), anyLong(), eq(POST_HEARING_REQUEST.getType())))
.thenReturn(StartEventResponse.builder().build());

when(sscsCcdConvertService.getCaseDetails(isA(StartEventResponse.class)))
.thenReturn(SscsCaseDetails.builder()
.data(SscsCaseData.builder()
.addDocuments(NO)
.build())
.build());

String postHearingSummary = "Post hearing Summary";
String postHearingDescription = "Post hearing Description";

updateCcdCaseService.updateCaseV2(
caseId,
POST_HEARING_REQUEST.getType(),
IdamTokens.builder().build(),
sscsCaseDetails -> {
sscsCaseDetails.getData().setAddDocuments(YES);
sscsCaseDetails.getData().setBenefitCode(benefitCode);
return new UpdateCcdCaseService.UpdateResult(null, postHearingSummary, postHearingDescription);
});

verify(sscsCcdConvertService).getCaseDataContent(
sscsCaseDataArgumentCaptor.capture(),
any(),
eq(postHearingSummary),
eq(postHearingDescription));

SscsCaseData sscsCaseData = sscsCaseDataArgumentCaptor.getValue();
assertThat(sscsCaseData.getAddDocuments())
.isEqualTo(YES);
assertThat(sscsCaseData.getBenefitCode())
.isEqualTo(benefitCode);
}
}

0 comments on commit eaccce6

Please sign in to comment.