Skip to content

Commit

Permalink
only increment case data version when data changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex McAusland committed Nov 8, 2024
1 parent c43bb7e commit ae7fdcf
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void getEventHistory() throws Exception {

@Order(4)
@Test
public void testOptimisticLock() throws Exception {
public void testAddNoteRunsConcurrently() throws Exception {
var firstEvent = ccdApi.startEvent(
getAuthorisation("[email protected]"),
getServiceAuth(), String.valueOf(caseRef), "caseworker-add-note").getToken();
Expand All @@ -151,9 +151,44 @@ public void testOptimisticLock() throws Exception {
"ignore_warning", false
);

// Conflicting change!
// Concurrent change to case notes should be allowed without raising a conflict
addNote();

var e =
buildRequest("[email protected]",
"http://localhost:4452/cases/" + caseRef + "/events", HttpPost::new);
e.addHeader("experimental", "true");
e.addHeader("Accept",
"application/vnd.uk.gov.hmcts.ccd-data-store-api.create-event.v2+json;charset=UTF-8");

e.setEntity(new StringEntity(new Gson().toJson(body), ContentType.APPLICATION_JSON));
var response = HttpClientBuilder.create().build().execute(e);
assertThat(response.getStatusLine().getStatusCode(), equalTo(201));
}

@Order(5)
@Test
public void testOptimisticLockOnJsonBlob() throws Exception {
var firstEvent = ccdApi.startEvent(
getAuthorisation("[email protected]"),
getServiceAuth(), String.valueOf(caseRef), "caseworker-update-due-date").getToken();

var body = Map.of(
"data", Map.of(
"dueDate", "2020-01-01"
),
"event", Map.of(
"id", "caseworker-update-due-date",
"summary", "summary",
"description", "description"
),
"event_token", firstEvent,
"ignore_warning", false
);

// Concurrent change to json blob should be rejected
updateDueDate();

var e =
buildRequest("[email protected]",
"http://localhost:4452/cases/" + caseRef + "/events", HttpPost::new);
Expand All @@ -166,6 +201,37 @@ public void testOptimisticLock() throws Exception {
assertThat(response.getStatusLine().getStatusCode(), equalTo(409));
}

private void updateDueDate() throws Exception {

var token = ccdApi.startEvent(
getAuthorisation("[email protected]"),
getServiceAuth(), String.valueOf(caseRef), "caseworker-update-due-date").getToken();

var body = Map.of(
"data", Map.of(
"dueDate", "2020-01-01"
),
"event", Map.of(
"id", "caseworker-update-due-date",
"summary", "summary",
"description", "description"
),
"event_token", token,
"ignore_warning", false
);

var e =
buildRequest("[email protected]",
"http://localhost:4452/cases/" + caseRef + "/events", HttpPost::new);
e.addHeader("experimental", "true");
e.addHeader("Accept",
"application/vnd.uk.gov.hmcts.ccd-data-store-api.create-event.v2+json;charset=UTF-8");

e.setEntity(new StringEntity(new Gson().toJson(body), ContentType.APPLICATION_JSON));
var response = HttpClientBuilder.create().build().execute(e);
assertThat(response.getStatusLine().getStatusCode(), equalTo(201));
}


private void addNote() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void configure(ConfigBuilder<CaseData, State, UserRole> configBuilder) {

if (env.contains(ENVIRONMENT_AAT)) {
roles.add(SOLICITOR);
roles.add(CASE_WORKER);
}

new PageBuilder(configBuilder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package uk.gov.hmcts.divorce.sow014;

import ch.qos.logback.core.util.StringUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.sql.Types;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import uk.gov.hmcts.ccd.sdk.api.callback.AboutToStartOrSubmitResponse;
import uk.gov.hmcts.ccd.sdk.runtime.CallbackController;
import uk.gov.hmcts.reform.ccd.client.model.CallbackRequest;
import uk.gov.hmcts.reform.ccd.client.model.CaseDetails;
Expand Down Expand Up @@ -116,24 +112,24 @@ on conflict (reference)

@SneakyThrows
private POCCaseDetails aboutToSubmit(POCCaseDetails event) {
var req = CallbackRequest.builder()
.caseDetails(toCaseDetails(event.getCaseDetails()))
.caseDetailsBefore(toCaseDetails(event.getCaseDetailsBefore()))
.eventId(event.getEventDetails().getEventId())
.build();
var cb = runtime.aboutToSubmit(req);

if (event.getCaseDetailsBefore() != null) {
var before = mapper.writeValueAsString(event.getCaseDetailsBefore().get("case_data"));
var after = mapper.writeValueAsString(cb.getData());
Files.writeString(Paths.get("before.json"), before);
Files.writeString(Paths.get("after.json"), after);
}
event.getCaseDetails().put("case_data", mapper.readValue(mapper.writeValueAsString(cb.getData()), Map.class));
if (cb.getState() != null) {
event.getEventDetails().setStateId(cb.getState().toString());
try {
var req = CallbackRequest.builder()
.caseDetails(toCaseDetails(event.getCaseDetails()))
.caseDetailsBefore(toCaseDetails(event.getCaseDetailsBefore()))
.eventId(event.getEventDetails().getEventId())
.build();
var cb = runtime.aboutToSubmit(req);

event.getCaseDetails().put("case_data", mapper.readValue(mapper.writeValueAsString(cb.getData()), Map.class));
if (cb.getState() != null) {
event.getEventDetails().setStateId(cb.getState().toString());
}
return event;
} catch (NoSuchMethodError e) {
// TODO: There is a config generator classpath bug - this exception is thrown when a callback doesn't exist for an event!
// There's nothing to do anyway if there's no callback so deferred for now.
return event;
}
return event;
}

@GetMapping(
Expand Down

0 comments on commit ae7fdcf

Please sign in to comment.