Skip to content

Commit

Permalink
CIV-11363 add trigger event (#3633)
Browse files Browse the repository at this point in the history
* CIV-11363 add trigger event
  • Loading branch information
MMNycz authored Nov 27, 2023
1 parent 683792f commit e892ae7
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 27 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ sonarqube {
property "sonar.projectName", "CIVIL :: service"
property "sonar.projectKey", "civil-service"
property "sonar.coverage.jacoco.xmlReportPaths", "${jacocoTestReport.reports.xml.destination.path}"
property "sonar.coverage.exclusions", "**/model/**, **/config/**/*Configuration.java, **/testingsupport/**, **/*ExternalTaskListener.java, **/*BaseExternalTaskHandler.java, **/stereotypes/**, **/*Exception.java, **/EventHistoryMapper*.java, **/model/hearingvalues/**, **/enums/hearing/**, **/fees/client/**, **/enums/sdo/**, **/service/PaymentsService.java, **/RetriggerCases*.java"
property "sonar.coverage.exclusions", "**/model/**, **/config/**/*Configuration.java, **/testingsupport/**, **/*ExternalTaskListener.java, **/*BaseExternalTaskHandler.java, **/stereotypes/**, **/*Exception.java, **/EventHistoryMapper*.java, **/model/hearingvalues/**, **/enums/hearing/**, **/fees/client/**, **/enums/sdo/**, **/service/PaymentsService.java"
property "sonar.cpd.exclusions", "**/*DocumentManagementService.java, **/*Spec*.java, **/*CcdDashboardClaimantClaimMatcher.java"
property "sonar.exclusions", "**/hmc/model/**, **/model/hearingvalues/**"
property "sonar.host.url", "https://sonar.reform.hmcts.net/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import org.apache.commons.io.IOUtils;
import org.camunda.bpm.client.task.ExternalTask;
import org.springframework.stereotype.Component;
import uk.gov.hmcts.reform.ccd.client.model.CaseDataContent;
import uk.gov.hmcts.reform.ccd.client.model.Event;
import uk.gov.hmcts.reform.ccd.client.model.StartEventResponse;
import uk.gov.hmcts.reform.civil.callback.CaseEvent;
import uk.gov.hmcts.reform.civil.handler.tasks.BaseExternalTaskHandler;
import uk.gov.hmcts.reform.civil.service.CoreCaseDataService;
Expand All @@ -18,7 +15,6 @@
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand All @@ -39,26 +35,21 @@ public void handleTask(ExternalTask externalTask) {
updateCaseByEvent(caseIdForNotifyRpaOnCaseHandedOffline, RETRIGGER_CASES);
}

private void updateCaseByEvent(List<String> caseIdList, CaseEvent caseEvent) {
public void updateCaseByEvent(List<String> caseIdList, CaseEvent caseEvent) {
if (caseIdList != null && !caseIdList.isEmpty()) {
log.info("Retrigger cases started for event: {}", caseEvent);
caseIdList.forEach(caseId -> {
try {
log.info("Retrigger CaseId: {} started", caseId);
var startEventResponse = coreCaseDataService.startUpdate(caseId, caseEvent);

Map<String, Object> caseDataMap = coreCaseDataService.getCase(Long.valueOf(caseId)).getData();

coreCaseDataService.submitUpdate(caseId, caseDataContent(startEventResponse, caseDataMap));
coreCaseDataService.triggerEvent(Long.parseLong(caseId), caseEvent);
log.info("Retrigger CaseId: {} finished", caseId);

} catch (FeignException e) {
log.error("ERROR Retrigger CaseId: {}", caseId);
log.error(String.format("Updating case data failed: %s", e.contentUTF8()));
log.error(String.format("Retrigger case failed: %s", e.contentUTF8()));
throw e;
} catch (Exception e) {
log.error("ERROR Retrigger CaseId: {}", caseId);
log.error(String.format("Updating case data failed: %s", e.getMessage()));
throw e;
}
log.info("Retrigger cases Finished for event: {}", caseEvent);
});
Expand All @@ -68,17 +59,6 @@ private void updateCaseByEvent(List<String> caseIdList, CaseEvent caseEvent) {

}

private CaseDataContent caseDataContent(StartEventResponse startEventResponse, Map<String, Object> caseDataMap) {
Map<String, Object> data = startEventResponse.getCaseDetails().getData();
data.putAll(caseDataMap);

return CaseDataContent.builder()
.eventToken(startEventResponse.getToken())
.event(Event.builder().id(startEventResponse.getEventId()).build())
.data(data)
.build();
}

public List<String> readCaseIds(String file) {

String data = readString(file);
Expand All @@ -93,7 +73,7 @@ private String readString(String resourcePath) {
return new String(readBytes(resourcePath), StandardCharsets.UTF_8);
}

private byte[] readBytes(String resourcePath) {
byte[] readBytes(String resourcePath) {
try (InputStream inputStream = RetriggerCasesEventHandler.class.getResourceAsStream(resourcePath)) {
return IOUtils.toByteArray(inputStream);
} catch (IOException e) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/caseIdForRetrigger.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1693404277952499
1694435036274857
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package uk.gov.hmcts.reform.civil.handler;

import feign.FeignException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import uk.gov.hmcts.reform.civil.callback.CaseEvent;
import uk.gov.hmcts.reform.civil.service.CoreCaseDataService;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

class RetriggerCasesEventHandlerTest {

@Mock
private CoreCaseDataService coreCaseDataService;

@InjectMocks
private RetriggerCasesEventHandler retriggerCasesEventHandler;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void testUpdateCaseByEvent() {
List<String> caseIdList = Arrays.asList("1", "2", "3");

retriggerCasesEventHandler.updateCaseByEvent(caseIdList, CaseEvent.RETRIGGER_CASES);

// Assertions
verify(coreCaseDataService, times(caseIdList.size())).triggerEvent(anyLong(), eq(CaseEvent.RETRIGGER_CASES));
}

@Test
void testUpdateCaseByEventWithFeignException() {
List<String> caseIdList = Arrays.asList("1", "2", "3");

// Simulate FeignException
doThrow(FeignException.class).when(coreCaseDataService).triggerEvent(anyLong(), eq(CaseEvent.RETRIGGER_CASES));

// Assertions
FeignException exception = assertThrows(FeignException.class, () ->
retriggerCasesEventHandler.updateCaseByEvent(caseIdList, CaseEvent.RETRIGGER_CASES)
);
}

@Test
void testUpdateCaseByEventWithGenericException() {
List<String> caseIdList = Arrays.asList("1", "2", "3");

doThrow(new RuntimeException("Simulated RuntimeException")).when(coreCaseDataService)
.triggerEvent(anyLong(), eq(CaseEvent.RETRIGGER_CASES));

RuntimeException exception = assertThrows(RuntimeException.class, () ->
retriggerCasesEventHandler.updateCaseByEvent(caseIdList, CaseEvent.RETRIGGER_CASES)
);

assertEquals("Simulated RuntimeException", exception.getMessage());
}

@Test
void testUpdateCaseByEventWithEmptyList() {
List<String> emptyCaseIdList = Collections.emptyList();

retriggerCasesEventHandler.updateCaseByEvent(emptyCaseIdList, CaseEvent.RETRIGGER_CASES);

// Assertions
verify(coreCaseDataService, never()).triggerEvent(anyLong(), eq(CaseEvent.RETRIGGER_CASES));
}
}


0 comments on commit e892ae7

Please sign in to comment.