Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Map;
import java.util.Optional;

import static java.util.stream.Collectors.toList;
import static uk.gov.hmcts.ccd.config.JacksonUtils.MAPPER;

// TODO CaseService and CaseDataService could probably be merged together.
Expand Down Expand Up @@ -76,12 +75,24 @@ public CaseDetails createNewCaseDetails(String caseTypeId, String jurisdictionId
* @return <code>Optional&lt;CaseDetails&gt;</code> - CaseDetails wrapped in Optional
*/
public CaseDetails populateCurrentCaseDetailsWithEventFields(CaseDataContent content, CaseDetails caseDetails) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look OK, but I'm not sure how SSCS invokes this method. I think it might be via the CaseDataValidatorController or CaseDetailsEndpoint endpoints, but I can't identify route. It might be something from XUI.

I would suggest renaming the method it's now populating with whatever is returned by getData() as well as getEventData(), but only do this if the change fixes the issue. Try it out as a proof of concept first.

if (content.getEventData() != null) {
content.getEventData().forEach((key, value) -> caseDetails.getData().put(key, value));
}
applyEventData(caseDetails, content.getEventData());
applyEventData(caseDetails, content.getData());
return caseDetails;
}

private void applyEventData(CaseDetails caseDetails, Map<String, JsonNode> updates) {
if (updates == null || updates.isEmpty()) {
return;
}
updates.forEach((key, value) -> {
if (value == null || value.isNull()) {
caseDetails.getData().remove(key);
} else {
caseDetails.getData().put(key, value);
}
});
}

public CaseDetails clone(CaseDetails source) {
final CaseDetails clone;

Expand Down Expand Up @@ -135,7 +146,7 @@ public Map<String, JsonNode> buildJsonFromCaseFieldsWithDefaultValue(
.filter(e -> !e.getReference().isBlank())
.map(caseEventFieldComplex -> JacksonUtils
.buildFromDottedPath(caseEventFieldComplex.getReference(),
caseEventFieldComplex.getDefaultValue())).collect(toList());
caseEventFieldComplex.getDefaultValue())).toList();

if (!collect.isEmpty()) { // to prevent construct like "FieldA": {}
ObjectNode objectNode = MAPPER.getNodeFactory().objectNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,23 @@ void populateCurrentCaseDetailsWithEventFields() throws Exception {


Map<String, JsonNode> eventData = JacksonUtils.convertValue(MAPPER.readTree(
"{\n"
+ " \"PersonFirstName\": \"First Name\",\n"
+ " \"PersonLastName\": \"Last Name\"\n"
+ "}"));
"""
{
"PersonFirstName": "First Name",
"PersonLastName": "Last Name"
}
"""
));

Map<String, JsonNode> resultData = JacksonUtils.convertValue(MAPPER.readTree(
"{\n"
+ " \"PersonFirstName\": \"First Name\",\n"
+ " \"PersonLastName\": \"Last Name\",\n"
+ " \"Person\":{\"Names\":{\"FirstName\":\"Jack\"}}\n"
+ "}"));
"""
{
"PersonFirstName": "First Name",
"PersonLastName": "Last Name",
"Person":{"Names":{"FirstName":"Jack"}}
}
"""
));

CaseDataContent caseDataContent = newCaseDataContent()
.withCaseReference(CASE_REFERENCE)
Expand All @@ -262,6 +268,47 @@ void populateCurrentCaseDetailsWithEventFields() throws Exception {
);
}

@Test
@DisplayName("should remove deleted fields from event data when latest data contains nulls")
void shouldRemoveFieldsDeletedOnPage() throws Exception {
CaseDetails caseDetails = buildCaseDetails();
caseDetails.setId("299");
caseDetails.getData().put("TextField", MAPPER.getNodeFactory().textNode("old"));
caseDetails.getData().put("OtherField", MAPPER.getNodeFactory().textNode("old2"));

Map<String, JsonNode> eventData = JacksonUtils.convertValue(MAPPER.readTree(
"""
{
"TextField": "value",
"OtherField": "event"
}
"""
));

Map<String, JsonNode> pageData = JacksonUtils.convertValue(MAPPER.readTree(
"""
{
"TextField": null,
"NewField": "new"
}
"""
));

CaseDataContent caseDataContent = newCaseDataContent()
.withCaseReference(CASE_REFERENCE)
.withEventData(eventData)
.withData(pageData)
.build();

CaseDetails result = caseService.populateCurrentCaseDetailsWithEventFields(caseDataContent, caseDetails);

assertAll(
() -> assertThat(result.getData().containsKey("TextField"), is(false)),
() -> assertThat(result.getData().get("OtherField").asText(), is("event")),
() -> assertThat(result.getData().get("NewField").asText(), is("new"))
);
}

@Test
@DisplayName("should fail for bad CASE_REFERENCE")
void shouldThrowBadRequestException() {
Expand Down