Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DMP-4381: Automatically update createdBy and lastModifiedBy when persisting entities to the database #2308

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1677f48
Updated lastModifiedBy and createdBy to auto update based on entity l…
Ben-Edwards-cgi Nov 22, 2024
3098af6
Merge branch 'master' into DMP-4381
Ben-Edwards-cgi Nov 22, 2024
c9fa327
Applied review comments
Ben-Edwards-cgi Nov 25, 2024
e4ab4b1
Merge branch 'master' into DMP-4381
Ben-Edwards-cgi Nov 25, 2024
001009b
Merge branch 'master' into DMP-4381
Ben-Edwards-cgi Nov 26, 2024
50fe7a6
Merge branch 'master' into DMP-4381
Ben-Edwards-cgi Nov 26, 2024
0c05c24
Merge branch 'master' into DMP-4381
Ben-Edwards-cgi Nov 27, 2024
b5e877c
Merge branch 'master' into DMP-4381
Ben-Edwards-cgi Nov 27, 2024
eaa05ad
Merge branch 'master' into DMP-4381
Ben-Edwards-cgi Nov 28, 2024
d8b4f06
Merge branch 'master' into DMP-4381
Ben-Edwards-cgi Nov 29, 2024
01db930
Merge branch 'master' into DMP-4381
Ben-Edwards-cgi Dec 2, 2024
90c1ad2
Applied review comments
Ben-Edwards-cgi Dec 4, 2024
531a450
Fixed typo
Ben-Edwards-cgi Dec 5, 2024
3ce05ef
Merge branch 'master' into DMP-4381
Ben-Edwards-cgi Dec 6, 2024
5ea0748
Merge branch 'master' into DMP-4381
Ben-Edwards-cgi Dec 6, 2024
7ce5bbc
Merge branch 'master' into DMP-4381
Ben-Edwards-cgi Dec 6, 2024
4cf8620
Added integration test
Ben-Edwards-cgi Dec 9, 2024
2a0110e
Updated how we update createdBy and LastModifiedBy
Ben-Edwards-cgi Dec 9, 2024
7914ce0
Fixed circular reference
Ben-Edwards-cgi Dec 9, 2024
08545fc
Merge branch 'master' into DMP-4381
Ben-Edwards-cgi Dec 9, 2024
02606b7
Fixed test
Ben-Edwards-cgi Dec 9, 2024
f5989d0
Merge branch 'master' into DMP-4381
Ben-Edwards-cgi Dec 10, 2024
7f37da8
Fixed issue with integration tests
Ben-Edwards-cgi Dec 10, 2024
c94111e
Fixed issue with integration tests
Ben-Edwards-cgi Dec 10, 2024
80aa299
Test fixes
Ben-Edwards-cgi Dec 10, 2024
6aed390
Fixed style
Ben-Edwards-cgi Dec 10, 2024
ff6d5a6
Disabled beforeUpdate to check tests
Ben-Edwards-cgi Dec 10, 2024
66589bd
Tmp changes to verify integration tests
Ben-Edwards-cgi Dec 10, 2024
17bd661
Tmp changes to verify integration tests
Ben-Edwards-cgi Dec 10, 2024
895e710
Reverted a few tmp changes
Ben-Edwards-cgi Dec 10, 2024
3b32154
Merge branch 'master' into DMP-4381
Ben-Edwards-cgi Dec 11, 2024
8d11520
Merge branch 'master' into DMP-4381
Ben-Edwards-cgi Dec 11, 2024
f6af92b
Merge branch 'master' into DMP-4381
Ben-Edwards-cgi Dec 11, 2024
62dc14d
Merge branch 'master' into DMP-4381
Ben-Edwards-cgi Dec 12, 2024
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 @@ -8,11 +8,13 @@
import uk.gov.hmcts.darts.audiorequests.model.AudioRequestType;
import uk.gov.hmcts.darts.common.entity.TransformedMediaEntity;
import uk.gov.hmcts.darts.common.entity.TransientObjectDirectoryEntity;
import uk.gov.hmcts.darts.common.entity.UserAccountEntity;
import uk.gov.hmcts.darts.testutils.IntegrationBase;

import java.time.OffsetDateTime;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand All @@ -37,14 +39,17 @@ void shouldSaveTransientDataLocation() {

MediaRequestEntity mediaRequestEntity = mediaRequestService.getMediaRequestEntityById(mediaRequestEntity1.getId());
UUID blobId = UUID.fromString("f744a74f-83c0-47e4-8bb2-2fd4d2b68647");

UserAccountEntity userAccount = dartsDatabase.getUserAccountStub().createAuthorisedIntegrationTestUser(false, "NEWCASTLE");
chrisbellingham-hmcts marked this conversation as resolved.
Show resolved Hide resolved
givenBearerTokenExists(userAccount.getEmailAddress());
TransformedMediaEntity transformedMediaEntity = transformedMediaHelper.createTransformedMediaEntity(
mediaRequestEntity,
"aFilename",
mediaRequestEntity.getStartTime(),
mediaRequestEntity.getEndTime(),
1000L
);
assertThat(transformedMediaEntity.getCreatedBy()).isEqualTo(mediaRequestEntity.getCreatedBy());
assertThat(transformedMediaEntity.getLastModifiedBy()).isEqualTo(mediaRequestEntity.getCreatedBy());
TransientObjectDirectoryEntity transientObjectDirectoryEntity = transientObjectDirectoryService.saveTransientObjectDirectoryEntity(
transformedMediaEntity,
blobId
Expand All @@ -62,5 +67,4 @@ void shouldSaveTransientDataLocation() {
.isAfter(OffsetDateTime.parse("2023-07-06T16:05:00.000Z")));
assertNotNull(transientObjectDirectoryEntity.getLastModifiedBy());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,18 @@ public TransformedMediaEntity createTransformedMediaEntity(MediaRequestEntity me
entity.setOutputFilename(filename);
entity.setStartTime(startTime);
entity.setEndTime(endTime);
entity.setCreatedBy(mediaRequest.getCreatedBy());
entity.setLastModifiedBy(mediaRequest.getCreatedBy());
entity.setOutputFormat(audioRequestOutputFormat);
if (nonNull(fileSize)) {
entity.setOutputFilesize(fileSize.intValue());
}
transformedMediaRepository.save(entity);
return entity;
//Ensures createdBy / LastModified does not get overridden by the @CreatedBy / @LastModifiedBy annotaiton
jackmaloney marked this conversation as resolved.
Show resolved Hide resolved
chrisbellingham-hmcts marked this conversation as resolved.
Show resolved Hide resolved
TransformedMediaEntity savedTM = transformedMediaRepository.save(entity);
savedTM.setLastModifiedBy(mediaRequest.getCreatedBy());
savedTM.setCreatedBy(mediaRequest.getCreatedBy());
transformedMediaRepository.updateCreatedByLastModifiedBy(entity.getId(),
mediaRequest.getCreatedBy().getId(),
mediaRequest.getCreatedBy().getId());
return savedTM;
chrisbellingham-hmcts marked this conversation as resolved.
Show resolved Hide resolved
}

@SuppressWarnings({"PMD.CognitiveComplexity"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import uk.gov.hmcts.darts.authentication.exception.AuthenticationError;
import uk.gov.hmcts.darts.authorisation.component.UserIdentity;
import uk.gov.hmcts.darts.common.entity.UserAccountEntity;
import uk.gov.hmcts.darts.common.exception.DartsApiException;

import java.util.Optional;

@Configuration
public class AuthenticationConfiguration {

Expand All @@ -14,4 +19,15 @@ public AuthConfigFallback getNoAuthConfigurationFallback(DefaultAuthConfiguratio
throw new DartsApiException(AuthenticationError.FAILED_TO_OBTAIN_AUTHENTICATION_CONFIG);
};
}

@Bean
public AuditorAware<UserAccountEntity> auditorAware(UserIdentity userIdentity) {
chrisbellingham-hmcts marked this conversation as resolved.
Show resolved Hide resolved
return () -> {
try {
return Optional.ofNullable(userIdentity.getUserAccount());
} catch (Exception e) {
return Optional.empty();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.envers.NotAudited;
import org.springframework.data.annotation.CreatedBy;
import uk.gov.hmcts.darts.common.entity.UserAccountEntity;

import java.time.OffsetDateTime;
Expand All @@ -24,6 +25,7 @@ public class CreatedBaseEntity {
@NotAudited
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "created_by")
@CreatedBy
chrisbellingham-hmcts marked this conversation as resolved.
Show resolved Hide resolved
private UserAccountEntity createdBy;

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.Setter;
import org.hibernate.annotations.UpdateTimestamp;
import org.hibernate.envers.NotAudited;
import org.springframework.data.annotation.LastModifiedBy;
import uk.gov.hmcts.darts.common.entity.UserAccountEntity;

import java.time.OffsetDateTime;
Expand All @@ -25,5 +26,6 @@ public class CreatedModifiedBaseEntity extends CreatedBaseEntity {
@NotAudited
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "last_modified_by")
@LastModifiedBy
private UserAccountEntity lastModifiedBy;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.envers.NotAudited;
import org.springframework.data.annotation.CreatedBy;
import uk.gov.hmcts.darts.common.entity.UserAccountEntity;

import java.time.OffsetDateTime;
Expand All @@ -29,6 +30,7 @@ public class MandatoryCreatedBaseEntity {
@NotAudited
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "created_by", nullable = false)
@CreatedBy
private UserAccountEntity createdBy;

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.Setter;
import org.hibernate.annotations.UpdateTimestamp;
import org.hibernate.envers.NotAudited;
import org.springframework.data.annotation.LastModifiedBy;
import uk.gov.hmcts.darts.common.entity.UserAccountEntity;

import java.time.OffsetDateTime;
Expand All @@ -26,5 +27,6 @@ public class MandatoryCreatedModifiedBaseEntity extends MandatoryCreatedBaseEnti
@NotAudited
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "last_modified_by", nullable = false)
@LastModifiedBy
private UserAccountEntity lastModifiedBy;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.data.domain.Limit;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import uk.gov.hmcts.darts.audio.model.TransformedMediaDetailsDto;
Expand Down Expand Up @@ -93,4 +94,12 @@ List<TransformedMediaEntity> findTransformedMedia(Integer mediaId,
OffsetDateTime requestedAtTo);


@Query(value = """
UPDATE darts.transformed_media
set created_by = :id1,
last_modified_by = :id2
where trm_id = :id
""", nativeQuery = true)
@Modifying
void updateCreatedByLastModifiedBy(Integer id, Integer id1, Integer id2);
chrisbellingham-hmcts marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -177,9 +178,12 @@ void getMediaByHearingIdShouldReturnRepositoryResultsUnmodifiedWhenRepositoryRes
}

@Test
void saveProcessedDataShouldSaveBlobAndSetStatus() {
void saveProcessedData_shouldSaveBlobAndSetStatus() {
final MediaRequestEntity mediaRequestEntity = new MediaRequestEntity();
mediaRequestEntity.setRequestType(DOWNLOAD);
UserAccountEntity userAccount = new UserAccountEntity();
userAccount.setId(1);
mediaRequestEntity.setCreatedBy(userAccount);

BlobClientUploadResponse blobClientUploadResponse = mock(BlobClientUploadResponseImpl.class);
UUID blobName = UUID.randomUUID();
Expand All @@ -198,9 +202,10 @@ void saveProcessedDataShouldSaveBlobAndSetStatus() {

TransformedMediaEntity transformedMediaEntity = new TransformedMediaEntity();
transformedMediaEntity.setId(1);
when(transformedMediaRepository.save(
any()
)).thenReturn(transformedMediaEntity);
doAnswer(invocation -> invocation.getArgument(0)).when(transformedMediaRepository).save(any());

when(mockTransientObjectDirectoryEntity.getTransformedMedia())
.thenReturn(transformedMediaEntity);

when(mockTransientObjectDirectoryEntity.getTransformedMedia(
)).thenReturn(transformedMediaEntity);
Expand Down Expand Up @@ -543,7 +548,10 @@ void whenCreateTransformMediaEntityIsCalled_thenFilenameFormatSizeShouldBeSet()

MediaRequestEntity mediaRequest = new MediaRequestEntity();
mediaRequest.setRequestType(AudioRequestType.PLAYBACK);

UserAccountEntity userAccount = new UserAccountEntity();
userAccount.setId(1);
mediaRequest.setCreatedBy(userAccount);
doAnswer(invocation -> invocation.getArgument(0)).when(transformedMediaRepository).save(any());
TransformedMediaEntity transformedMediaEntity = transformedMediaHelper.createTransformedMediaEntity(
mediaRequest,
"case1_23_Nov_2023.mp3",
Expand All @@ -552,6 +560,8 @@ void whenCreateTransformMediaEntityIsCalled_thenFilenameFormatSizeShouldBeSet()
BINARY_DATA.getLength()
);

assertEquals(userAccount, transformedMediaEntity.getCreatedBy());
assertEquals(userAccount, transformedMediaEntity.getLastModifiedBy());
assertEquals(TEST_FILE_NAME, transformedMediaEntity.getOutputFilename());
assertEquals(TEST_EXTENSION, transformedMediaEntity.getOutputFormat().getExtension());
assertEquals(TEST_BINARY_STRING.length(), transformedMediaEntity.getOutputFilesize());
Expand Down
Loading