Skip to content

Commit

Permalink
change CaseConcurrencyException message and add unit test (#2446)
Browse files Browse the repository at this point in the history
  • Loading branch information
aktaskaan committed Jul 16, 2024
1 parent d70f4c0 commit 8674dd6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ public CaseDetails set(final CaseDetails caseDetails) {
em.flush();
} catch (StaleObjectStateException | OptimisticLockException e) {
LOG.info("Optimistic Lock Exception: Case data has been altered, UUID={}", caseDetails.getReference(), e);
throw new CaseConcurrencyException("The case data has been altered outside of this transaction.");
throw new CaseConcurrencyException("""
Unfortunately we were unable to save your work to the case as \
another action happened at the same time.
Please review the case and try again.""");
} catch (PersistenceException e) {
if (e.getCause() instanceof ConstraintViolationException && isDuplicateReference(e)) {
LOG.warn("ConstraintViolationException happen for UUID={}. ConstraintName: {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
import uk.gov.hmcts.ccd.domain.service.common.AccessControlService;
import uk.gov.hmcts.ccd.domain.service.security.AuthorisedCaseDefinitionDataService;
import uk.gov.hmcts.ccd.endpoint.exceptions.BadRequestException;
import uk.gov.hmcts.ccd.endpoint.exceptions.CaseConcurrencyException;
import uk.gov.hmcts.ccd.infrastructure.user.UserAuthorisation;
import uk.gov.hmcts.ccd.infrastructure.user.UserAuthorisation.AccessLevel;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.OptimisticLockException;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequestEvent;
import java.io.IOException;
import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
Expand All @@ -57,9 +61,13 @@
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anySet;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static uk.gov.hmcts.ccd.TestFixtures.loadCaseTypeDefinition;
import static uk.gov.hmcts.ccd.domain.service.common.AccessControlService.CAN_READ;
Expand Down Expand Up @@ -125,6 +133,41 @@ public void clearDown() {
listener.requestDestroyed(new ServletRequestEvent(context, request));
}

@Test
public void setShouldThrowCaseConcurrencyException() throws NoSuchFieldException, IllegalAccessException {
EntityManager emMock = mock(EntityManager.class);
CaseDetailsMapper caseDetailsMapper = mock(CaseDetailsMapper.class);

DefaultCaseDetailsRepository defaultCaseDetailsRepository =
new DefaultCaseDetailsRepository(caseDetailsMapper, null, null,
applicationParams);

Field emField = DefaultCaseDetailsRepository.class.getDeclaredField("em");
emField.setAccessible(true);
emField.set(defaultCaseDetailsRepository, emMock);

CaseDetails caseDetails = new CaseDetails();
caseDetails.setReference(1L);

CaseDetailsEntity caseDetailsEntity = new CaseDetailsEntity();
caseDetailsEntity.setReference(1L);
caseDetailsEntity.setLastModified(LocalDateTime.now(ZoneOffset.UTC));
caseDetailsEntity.setVersion(1);

when(caseDetailsMapper.modelToEntity(caseDetails)).thenReturn(caseDetailsEntity);
when(emMock.merge(caseDetailsEntity)).thenReturn(caseDetailsEntity);
doThrow(new OptimisticLockException()).when(emMock).flush();

CaseConcurrencyException exception = assertThrows(CaseConcurrencyException.class,
() -> defaultCaseDetailsRepository.set(caseDetails));

assertThat(exception.getMessage(), is("Unfortunately we were unable to save your work to the case as "
+ "another action happened at the same time.\nPlease review the case and try again."));

verify(emMock).merge(caseDetailsEntity);
verify(emMock).flush();
}

@Test
public void setShouldPersistCaseDetails() {
CaseDetails caseDetails = new CaseDetails();
Expand Down

0 comments on commit 8674dd6

Please sign in to comment.