Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit 2f112a2

Browse files
Add subclasses for transformation result (#36)
1 parent 8930cee commit 2f112a2

File tree

11 files changed

+124
-82
lines changed

11 files changed

+124
-82
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ to it:
3232

3333
The handler will create a case in CCD for you and return the ID of the new case.
3434

35+
You can check a sample implementation in
36+
[this](https://github.com/hmcts/bulk-scan-ccd-event-handler-sample-app) repository.
37+
3538
## Developing
3639

3740
### Prerequisites

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ plugins {
1010
}
1111

1212
group 'uk.gov.hmcts.reform'
13-
version '0.0.1'
13+
version '0.1.0'
1414

1515
sourceCompatibility = 1.8
1616

src/main/java/uk/gov/hmcts/reform/bulkscanccdeventhandler/ccd/CcdClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import uk.gov.hmcts.reform.bulkscanccdeventhandler.ccd.api.model.Event;
99
import uk.gov.hmcts.reform.bulkscanccdeventhandler.ccd.api.model.StartEventResponse;
1010
import uk.gov.hmcts.reform.bulkscanccdeventhandler.handler.model.CaseCreationRequest;
11-
import uk.gov.hmcts.reform.bulkscanccdeventhandler.transformer.model.TransformationResult;
11+
import uk.gov.hmcts.reform.bulkscanccdeventhandler.transformer.model.OkTransformationResult;
1212

1313
import java.util.function.Supplier;
1414

@@ -26,7 +26,7 @@ public CcdClient(CcdApi api, Supplier<String> s2sTokenSupplier) {
2626
}
2727
// endregion
2828

29-
public String createCase(CaseCreationRequest req, TransformationResult tr) {
29+
public String createCase(CaseCreationRequest req, OkTransformationResult tr) {
3030
log.info("Starting CCD event. CaseType: {}, EventId: {}", tr.caseTypeId, tr.eventId);
3131

3232
StartEventResponse startEventResponse = null;

src/main/java/uk/gov/hmcts/reform/bulkscanccdeventhandler/handler/ExceptionRecordEventHandler.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package uk.gov.hmcts.reform.bulkscanccdeventhandler.handler;
22

33
import uk.gov.hmcts.reform.bulkscanccdeventhandler.ccd.CcdClient;
4+
import uk.gov.hmcts.reform.bulkscanccdeventhandler.handler.exceptions.InvalidTransformationResultTypeException;
45
import uk.gov.hmcts.reform.bulkscanccdeventhandler.handler.model.CaseCreationRequest;
56
import uk.gov.hmcts.reform.bulkscanccdeventhandler.handler.model.CaseCreationResult;
67
import uk.gov.hmcts.reform.bulkscanccdeventhandler.handler.validation.CaseCreationRequestValidator;
78
import uk.gov.hmcts.reform.bulkscanccdeventhandler.transformer.ExceptionRecordToCaseTransformer;
9+
import uk.gov.hmcts.reform.bulkscanccdeventhandler.transformer.model.ErrorTransformationResult;
10+
import uk.gov.hmcts.reform.bulkscanccdeventhandler.transformer.model.OkTransformationResult;
811
import uk.gov.hmcts.reform.bulkscanccdeventhandler.transformer.model.TransformationResult;
912

1013
import java.util.List;
14+
import java.util.function.Function;
1115

1216
import static java.util.Collections.emptyList;
1317

@@ -31,16 +35,33 @@ public ExceptionRecordEventHandler(
3135

3236
public CaseCreationResult handle(CaseCreationRequest req) {
3337
validator.validate(req);
34-
TransformationResult result = transformer.transform(req.exceptionRecord);
3538

36-
boolean shouldCreateCase = result.errors.isEmpty() && (result.warnings.isEmpty() || req.ignoreWarnings);
39+
return handleTransformationResult(
40+
transformer.transform(req.exceptionRecord),
41+
okRes -> {
42+
if (okRes.warnings.isEmpty() || req.ignoreWarnings) {
43+
// TODO: handle exceptions
44+
String caseId = ccdClient.createCase(req, okRes);
45+
return ok(caseId);
46+
} else {
47+
return errors(emptyList(), okRes.warnings);
48+
}
49+
},
50+
errRes -> errors(errRes.errors, errRes.warnings)
51+
);
52+
}
3753

38-
if (shouldCreateCase) {
39-
// TODO: handle exceptions
40-
String caseId = ccdClient.createCase(req, result);
41-
return ok(caseId);
54+
private <T> T handleTransformationResult(
55+
TransformationResult result,
56+
Function<OkTransformationResult, T> okAction,
57+
Function<ErrorTransformationResult, T> errorAction
58+
) {
59+
if (result instanceof OkTransformationResult) {
60+
return okAction.apply((OkTransformationResult) result);
61+
} else if (result instanceof ErrorTransformationResult) {
62+
return errorAction.apply((ErrorTransformationResult) result);
4263
} else {
43-
return errors(result.errors, result.warnings);
64+
throw new InvalidTransformationResultTypeException("Invalid type: " + result.getClass().getName());
4465
}
4566
}
4667

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package uk.gov.hmcts.reform.bulkscanccdeventhandler.handler.exceptions;
2+
3+
public class InvalidTransformationResultTypeException extends RuntimeException {
4+
public InvalidTransformationResultTypeException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package uk.gov.hmcts.reform.bulkscanccdeventhandler.transformer.model;
2+
3+
import java.util.List;
4+
5+
public class ErrorTransformationResult implements TransformationResult {
6+
7+
/**
8+
* Warnings that occurred during case mapping or validation.
9+
*/
10+
public final List<String> warnings;
11+
12+
/**
13+
* Errors that occurred during case mapping or validation.
14+
*/
15+
public final List<String> errors;
16+
17+
public ErrorTransformationResult(List<String> warnings, List<String> errors) {
18+
this.warnings = warnings;
19+
this.errors = errors;
20+
}
21+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package uk.gov.hmcts.reform.bulkscanccdeventhandler.transformer.model;
2+
3+
import java.util.List;
4+
5+
public class OkTransformationResult implements TransformationResult {
6+
7+
/**
8+
* Warnings that occurred during case mapping or validation.
9+
*/
10+
public final List<String> warnings;
11+
12+
/**
13+
* Object representing the case that should be created in CCD.
14+
* It will be serialised to json. It can be a map or a domain object, as long as it serialises to expected json.
15+
*/
16+
public final Object data;
17+
18+
/**
19+
* CCD jurisdiction in which case should be created.
20+
*/
21+
public final String jurisdiction;
22+
23+
/**
24+
* CCD CaseTypeId that should be used when creating a case.
25+
*/
26+
public final String caseTypeId;
27+
28+
/**
29+
* CCD Event Id that should be used when creating a case.
30+
*/
31+
public final String eventId;
32+
33+
public OkTransformationResult(
34+
List<String> warnings,
35+
Object data,
36+
String jurisdiction,
37+
String caseTypeId,
38+
String eventId
39+
) {
40+
this.warnings = warnings;
41+
this.data = data;
42+
this.jurisdiction = jurisdiction;
43+
this.caseTypeId = caseTypeId;
44+
this.eventId = eventId;
45+
}
46+
}
Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,5 @@
11
package uk.gov.hmcts.reform.bulkscanccdeventhandler.transformer.model;
22

3-
import java.util.List;
3+
public interface TransformationResult {
44

5-
public class TransformationResult {
6-
7-
/**
8-
* Warnings that occurred during case mapping or validation.
9-
*/
10-
public final List<String> warnings;
11-
12-
/**
13-
* Errors that occurred during case mapping or validation.
14-
*/
15-
public final List<String> errors;
16-
17-
/**
18-
* Object representing the case that should be created in CCD.
19-
* It will be serialised to json. It can be a map or a domain object, as long as it serialises to expected json.
20-
*/
21-
public final Object data;
22-
23-
/**
24-
* CCD jurisdiction in which case should be created.
25-
*/
26-
public final String jurisdiction;
27-
28-
/**
29-
* CCD CaseTypeId that should be used when creating a case.
30-
*/
31-
public final String caseTypeId;
32-
33-
/**
34-
* CCD Event Id that should be used when creating a case.
35-
*/
36-
public final String eventId;
37-
38-
// region constructor
39-
public TransformationResult(
40-
List<String> warnings,
41-
List<String> errors,
42-
Object data,
43-
String jurisdiction,
44-
String caseTypeId,
45-
String eventId
46-
) {
47-
this.warnings = warnings;
48-
this.errors = errors;
49-
this.data = data;
50-
this.jurisdiction = jurisdiction;
51-
this.caseTypeId = caseTypeId;
52-
this.eventId = eventId;
53-
}
54-
// endregion
555
}

src/test/java/uk/gov/hmcts/reform/bulkscanccdeventhandler/ccd/CcdClientTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import uk.gov.hmcts.reform.bulkscanccdeventhandler.ccd.api.model.CaseDataResp;
1313
import uk.gov.hmcts.reform.bulkscanccdeventhandler.ccd.api.model.StartEventResponse;
1414
import uk.gov.hmcts.reform.bulkscanccdeventhandler.handler.model.CaseCreationRequest;
15+
import uk.gov.hmcts.reform.bulkscanccdeventhandler.transformer.model.OkTransformationResult;
1516
import uk.gov.hmcts.reform.bulkscanccdeventhandler.transformer.model.TransformationResult;
1617

1718
import java.util.function.Supplier;
@@ -43,7 +44,7 @@ public void setUp() {
4344
public void should_call_api_with_correct_params() {
4445
// given
4546
CaseCreationRequest req = caseCreationRequest();
46-
TransformationResult tr = okResult();
47+
OkTransformationResult tr = okResult();
4748
String s2s = "s2s-token";
4849
given(s2sTokenSupplier.get())
4950
.willReturn(s2s);

src/test/java/uk/gov/hmcts/reform/bulkscanccdeventhandler/handler/ExceptionRecordEventHandlerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import uk.gov.hmcts.reform.bulkscanccdeventhandler.handler.validation.CaseCreationRequestValidator;
1212
import uk.gov.hmcts.reform.bulkscanccdeventhandler.testutils.sampledata.SampleCaseCreationRequest;
1313
import uk.gov.hmcts.reform.bulkscanccdeventhandler.transformer.ExceptionRecordToCaseTransformer;
14-
import uk.gov.hmcts.reform.bulkscanccdeventhandler.transformer.model.TransformationResult;
14+
import uk.gov.hmcts.reform.bulkscanccdeventhandler.transformer.model.OkTransformationResult;
1515

1616
import static java.util.Arrays.asList;
1717
import static org.assertj.core.api.Assertions.assertThat;
@@ -43,7 +43,7 @@ private void setUp() {
4343
public void should_handle_successful_transformation_result() {
4444
// given
4545
CaseCreationRequest req = SampleCaseCreationRequest.caseCreationRequest();
46-
TransformationResult transformationResult = okResult();
46+
OkTransformationResult transformationResult = okResult();
4747

4848
given(ccdClient.createCase(any(), any())).willReturn("new-case-id");
4949
given(transformer.transform(req.exceptionRecord)).willReturn(transformationResult);
@@ -106,7 +106,7 @@ public void should_handle_transformation_result_with_warnings_when_errors_should
106106
// given
107107
CaseCreationRequest req = caseCreationRequest(true); // ignore warnings
108108

109-
TransformationResult transformationResult = warningResult(asList("warn1", "warn2"));
109+
OkTransformationResult transformationResult = warningResult(asList("warn1", "warn2"));
110110

111111
given(ccdClient.createCase(any(), any())).willReturn("new-case-id");
112112
given(transformer.transform(req.exceptionRecord)).willReturn(transformationResult);

0 commit comments

Comments
 (0)