Skip to content

Commit

Permalink
DMP-4388 Removing case list for add audio duplicate check (#2359)
Browse files Browse the repository at this point in the history
  • Loading branch information
davet1985 authored Dec 11, 2024
1 parent 3ae2b14 commit 49bba81
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import static ch.qos.logback.classic.Level.toLevel;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -208,72 +207,6 @@ void addAudioMetadataChecksumsDoNotMatch() throws Exception {
problem.getDetail());
}


@Test
void addAudioMetadataDifferentCases() throws Exception {
superAdminUserStub.givenUserIsAuthorised(mockUserIdentity, SecurityRoleEnum.MID_TIER);

makeAddAudioCall(AUDIO_BINARY_PAYLOAD_1, "case1")
.andExpect(status().isOk());

List<HearingEntity> allHearings = dartsDatabase.getHearingRepository().findByCourthouseCourtroomAndDate("bristol", "1", STARTED_AT.toLocalDate());

List<HearingEntity> addAudioLinkedHearings = new ArrayList<>();
for (HearingEntity hearing : allHearings) {
if (hearing.getCourtCase().getCaseNumber().contains("case1")) {
addAudioLinkedHearings.add(hearing);
}
}
assertEquals(1, addAudioLinkedHearings.size());

MediaEntity mediaFirst = null;
for (HearingEntity hearing : addAudioLinkedHearings) {
List<MediaEntity> mediaEntities = dartsDatabase.getMediaRepository().findAllCurrentMediaByHearingId(hearing.getId());
mediaFirst = mediaEntities.get(0);
assertEquals(1, mediaEntities.size());
assertEquals(STARTED_AT, mediaFirst.getStart());
assertEquals(STARTED_AT, mediaFirst.getEnd());
assertEquals(1, mediaFirst.getChannel());
assertEquals(2, mediaFirst.getTotalChannels());
List<MediaLinkedCaseEntity> mediaLinkedCaseEntities = dartsDatabase.getMediaLinkedCaseRepository().findByMedia(mediaFirst);
assertEquals(1, mediaLinkedCaseEntities.size());
assertEquals("1", dartsDatabase.getCourtroomRepository().findById(mediaFirst.getCourtroom().getId()).get().getName());
assertEquals(mediaFirst.getId().toString(), mediaFirst.getChronicleId());
assertNull(mediaFirst.getAntecedentId());
}

makeAddAudioCall(AUDIO_BINARY_PAYLOAD_1, "case2")
.andExpect(status().isOk());

addAudioLinkedHearings = new ArrayList<>();
allHearings = dartsDatabase.getHearingRepository().findByCourthouseCourtroomAndDate("bristol", "1", STARTED_AT.toLocalDate());

for (HearingEntity hearing : allHearings) {
if (hearing.getCourtCase().getCaseNumber().contains("case2")) {
addAudioLinkedHearings.add(hearing);
}
}
assertEquals(1, addAudioLinkedHearings.size());

MediaEntity mediaSecond = null;
for (HearingEntity hearing : addAudioLinkedHearings) {
List<MediaEntity> mediaEntities = dartsDatabase.getMediaRepository().findAllCurrentMediaByHearingId(hearing.getId());
mediaSecond = mediaEntities.get(0);
assertEquals(1, mediaEntities.size());
assertEquals(STARTED_AT, mediaSecond.getStart());
assertEquals(STARTED_AT, mediaSecond.getEnd());
assertEquals(1, mediaSecond.getChannel());
assertEquals(2, mediaSecond.getTotalChannels());
List<MediaLinkedCaseEntity> mediaLinkedCaseEntities = dartsDatabase.getMediaLinkedCaseRepository().findByMedia(mediaSecond);
assertEquals(1, mediaLinkedCaseEntities.size());
assertEquals("1", dartsDatabase.getCourtroomRepository().findById(mediaSecond.getCourtroom().getId()).get().getName());
assertEquals(mediaSecond.getId().toString(), mediaSecond.getChronicleId());
assertNull(mediaSecond.getAntecedentId());
}

assertNotSame(mediaFirst, mediaSecond);
}

@Test
void addAudioMetadataDuplicate() throws Exception {
superAdminUserStub.givenUserIsAuthorised(mockUserIdentity, SecurityRoleEnum.MID_TIER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import static ch.qos.logback.classic.Level.toLevel;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -196,71 +195,6 @@ void addAudioMetadata() throws Exception {
assertEquals(0, mediaEntities.size());//shouldn't have any as no audio in that courtroom
}

@Test
void addAudioMetadataDifferentCases() throws Exception {
superAdminUserStub.givenUserIsAuthorised(mockUserIdentity, SecurityRoleEnum.MID_TIER);

makeAddAudioCall(AUDIO_BINARY_PAYLOAD_1, "case1")
.andExpect(status().isOk());

List<HearingEntity> allHearings = dartsDatabase.getHearingRepository().findByCourthouseCourtroomAndDate("bristol", "1", STARTED_AT.toLocalDate());

List<HearingEntity> addAudioLinkedHearings = new ArrayList<>();
for (HearingEntity hearing : allHearings) {
if (hearing.getCourtCase().getCaseNumber().contains("case1")) {
addAudioLinkedHearings.add(hearing);
}
}
assertEquals(1, addAudioLinkedHearings.size());

MediaEntity mediaFirst = null;
for (HearingEntity hearing : addAudioLinkedHearings) {
List<MediaEntity> mediaEntities = dartsDatabase.getMediaRepository().findAllCurrentMediaByHearingId(hearing.getId());
mediaFirst = mediaEntities.get(0);
assertEquals(1, mediaEntities.size());
assertEquals(STARTED_AT, mediaFirst.getStart());
assertEquals(STARTED_AT, mediaFirst.getEnd());
assertEquals(1, mediaFirst.getChannel());
assertEquals(2, mediaFirst.getTotalChannels());
List<MediaLinkedCaseEntity> mediaLinkedCaseEntities = dartsDatabase.getMediaLinkedCaseRepository().findByMedia(mediaFirst);
assertEquals(1, mediaLinkedCaseEntities.size());
assertEquals("1", dartsDatabase.getCourtroomRepository().findById(mediaFirst.getCourtroom().getId()).get().getName());
assertEquals(mediaFirst.getId().toString(), mediaFirst.getChronicleId());
assertNull(mediaFirst.getAntecedentId());
}

makeAddAudioCall(AUDIO_BINARY_PAYLOAD_1, "case2")
.andExpect(status().isOk());

addAudioLinkedHearings = new ArrayList<>();
allHearings = dartsDatabase.getHearingRepository().findByCourthouseCourtroomAndDate("bristol", "1", STARTED_AT.toLocalDate());

for (HearingEntity hearing : allHearings) {
if (hearing.getCourtCase().getCaseNumber().contains("case2")) {
addAudioLinkedHearings.add(hearing);
}
}
assertEquals(1, addAudioLinkedHearings.size());

MediaEntity mediaSecond = null;
for (HearingEntity hearing : addAudioLinkedHearings) {
List<MediaEntity> mediaEntities = dartsDatabase.getMediaRepository().findAllCurrentMediaByHearingId(hearing.getId());
mediaSecond = mediaEntities.get(0);
assertEquals(1, mediaEntities.size());
assertEquals(STARTED_AT, mediaSecond.getStart());
assertEquals(STARTED_AT, mediaSecond.getEnd());
assertEquals(1, mediaSecond.getChannel());
assertEquals(2, mediaSecond.getTotalChannels());
List<MediaLinkedCaseEntity> mediaLinkedCaseEntities = dartsDatabase.getMediaLinkedCaseRepository().findByMedia(mediaSecond);
assertEquals(1, mediaLinkedCaseEntities.size());
assertEquals("1", dartsDatabase.getCourtroomRepository().findById(mediaSecond.getCourtroom().getId()).get().getName());
assertEquals(mediaSecond.getId().toString(), mediaSecond.getChronicleId());
assertNull(mediaSecond.getAntecedentId());
}

assertNotSame(mediaFirst, mediaSecond);
}

@Test
void addAudioMetadataDuplicate() throws Exception {
superAdminUserStub.givenUserIsAuthorised(mockUserIdentity, SecurityRoleEnum.MID_TIER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import uk.gov.hmcts.darts.audio.component.AddAudioRequestMapper;
Expand All @@ -12,15 +11,12 @@
import uk.gov.hmcts.darts.audio.service.AudioUploadService;
import uk.gov.hmcts.darts.authorisation.component.UserIdentity;
import uk.gov.hmcts.darts.common.datamanagement.enums.DatastoreContainerType;
import uk.gov.hmcts.darts.common.entity.CourtCaseEntity;
import uk.gov.hmcts.darts.common.entity.CourtroomEntity;
import uk.gov.hmcts.darts.common.entity.ExternalObjectDirectoryEntity;
import uk.gov.hmcts.darts.common.entity.HearingEntity;
import uk.gov.hmcts.darts.common.entity.MediaEntity;
import uk.gov.hmcts.darts.common.entity.MediaLinkedCaseEntity;
import uk.gov.hmcts.darts.common.entity.ObjectRecordStatusEntity;
import uk.gov.hmcts.darts.common.entity.UserAccountEntity;
import uk.gov.hmcts.darts.common.enums.MediaLinkedCaseSourceType;
import uk.gov.hmcts.darts.common.exception.DartsApiException;
import uk.gov.hmcts.darts.common.repository.ExternalLocationTypeRepository;
import uk.gov.hmcts.darts.common.repository.ExternalObjectDirectoryRepository;
Expand All @@ -41,10 +37,8 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static org.apache.commons.collections4.CollectionUtils.isEmpty;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
Expand Down Expand Up @@ -189,28 +183,6 @@ private List<MediaEntity> filterForMediaWithMismatchingChecksum(List<MediaEntity
.toList();
}

private List<MediaEntity> filterMediaEntitiesWithIdenticalCaseList(List<String> caseNumbersToLookFor, List<MediaEntity> mediaEntities) {
ArrayList<MediaEntity> resultList = new ArrayList<>();
for (MediaEntity mediaEntity : mediaEntities) {
List<MediaLinkedCaseEntity> mediaLinkedCaseEntities =
mediaLinkedCaseRepository.findByMediaAndSource(mediaEntity, MediaLinkedCaseSourceType.ADD_AUDIO_METADATA);
List<String> mediaCaseNumbers = mediaLinkedCaseEntities.stream()
.map(MediaLinkedCaseEntity::getCourtCase)
.filter(Objects::nonNull)
.map(CourtCaseEntity::getCaseNumber)
.collect(Collectors.toList());
mediaCaseNumbers.addAll(mediaLinkedCaseEntities.stream()
.map(MediaLinkedCaseEntity::getCaseNumber)
.filter(Objects::nonNull)
.toList());
if (caseNumbersToLookFor.size() == mediaCaseNumbers.size() && CollectionUtils.containsAll(mediaCaseNumbers, caseNumbersToLookFor)) {
resultList.add(mediaEntity);
}
}

return resultList;
}


private List<MediaEntity> getLatestDuplicateMediaFiles(AddAudioMetadataRequest addAudioMetadataRequest) {
CourtroomEntity courtroomEntity = retrieveCoreObjectService.retrieveOrCreateCourtroom(addAudioMetadataRequest.getCourthouse(),
Expand All @@ -223,9 +195,6 @@ private List<MediaEntity> getLatestDuplicateMediaFiles(AddAudioMetadataRequest a
addAudioMetadataRequest.getStartedAt(),
addAudioMetadataRequest.getEndedAt());

// filter on any media entities that relate to the same cases.
identicalMediaEntities = filterMediaEntitiesWithIdenticalCaseList(addAudioMetadataRequest.getCases(), identicalMediaEntities);

// now lets get the lowest level media objects so that they can act as a basis for the antecedent
Tree<MediaEntityTreeNodeImpl> tree = new Tree<>();
identicalMediaEntities.stream().forEach(entry ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.springframework.mock.web.MockMultipartFile;
import uk.gov.hmcts.darts.audio.component.AddAudioRequestMapper;
import uk.gov.hmcts.darts.audio.component.impl.AddAudioRequestMapperImpl;
import uk.gov.hmcts.darts.audio.config.AudioConfigurationProperties;
import uk.gov.hmcts.darts.audio.exception.AudioApiError;
import uk.gov.hmcts.darts.audio.model.AddAudioMetadataRequest;
import uk.gov.hmcts.darts.audio.service.AudioAsyncService;
Expand All @@ -24,10 +23,8 @@
import uk.gov.hmcts.darts.common.entity.HearingEntity;
import uk.gov.hmcts.darts.common.entity.MediaEntity;
import uk.gov.hmcts.darts.common.entity.UserAccountEntity;
import uk.gov.hmcts.darts.common.enums.MediaLinkedCaseSourceType;
import uk.gov.hmcts.darts.common.exception.DartsApiException;
import uk.gov.hmcts.darts.common.helper.MediaLinkedCaseHelper;
import uk.gov.hmcts.darts.common.repository.CourtLogEventRepository;
import uk.gov.hmcts.darts.common.repository.ExternalLocationTypeRepository;
import uk.gov.hmcts.darts.common.repository.ExternalObjectDirectoryRepository;
import uk.gov.hmcts.darts.common.repository.HearingRepository;
Expand All @@ -42,6 +39,7 @@

import java.io.InputStream;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

Expand All @@ -54,6 +52,7 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
Expand All @@ -78,10 +77,6 @@ class AudioUploadServiceImplTest {
@Mock
private UserIdentity userIdentity;
@Mock
private CourtLogEventRepository courtLogEventRepository;
@Mock
private AudioConfigurationProperties audioConfigurationProperties;
@Mock
private ExternalObjectDirectoryRepository externalObjectDirectoryRepository;
@Mock
private ExternalLocationTypeRepository externalLocationTypeRepository;
Expand All @@ -98,8 +93,6 @@ class AudioUploadServiceImplTest {
private MediaLinkedCaseHelper mediaLinkedCaseHelper;
@Mock
private MediaLinkedCaseRepository mediaLinkedCaseRepository;
@Mock
private MediaEntity mediaEntityMock;

@BeforeEach
void setUp() {
Expand All @@ -122,7 +115,7 @@ void setUp() {
}

@Test
void addAudio() {
void addAudio_shouldUploadToInboundAndSave_whenFileAndMetadataUsed() {
UserAccountEntity userAccount = new UserAccountEntity();
userAccount.setId(10);
when(userIdentity.getUserAccount()).thenReturn(userAccount);
Expand All @@ -137,7 +130,7 @@ void addAudio() {
any()
)).thenReturn(hearingEntity);
when(mediaRepository.findMediaByDetails(any(), any(), any(), any(), any()))
.thenReturn(List.of(mediaEntityMock));
.thenReturn(Collections.emptyList());

CourthouseEntity courthouse = new CourthouseEntity();
courthouse.setCourthouseName("SWANSEA");
Expand Down Expand Up @@ -184,12 +177,10 @@ void addAudio() {
assertEquals(savedMedia.getChecksum(), externalObjectDirectoryEntity.getChecksum());
assertNotNull(externalObjectDirectoryEntity.getChecksum());
assertEquals(externalLocation, externalObjectDirectoryEntity.getExternalLocation());

verify(mediaLinkedCaseRepository).findByMediaAndSource(mediaEntityMock, MediaLinkedCaseSourceType.ADD_AUDIO_METADATA);
}

@Test
void addAudioNoUpload() {
void addAudio_shouldSaveMediaAndEod_whenMetadataOnly() {
UserAccountEntity userAccount = new UserAccountEntity();
userAccount.setId(10);
when(userIdentity.getUserAccount()).thenReturn(userAccount);
Expand Down Expand Up @@ -245,6 +236,47 @@ void addAudioNoUpload() {
assertEquals(externalLocation, externalObjectDirectoryEntity.getExternalLocation());
}

@Test
void addAudio_shouldDoNothing_whenMetadataOnlyAndAudioIsADuplicate() {
UserAccountEntity userAccount = new UserAccountEntity();
userAccount.setId(10);
when(userIdentity.getUserAccount()).thenReturn(userAccount);

// Given
CourthouseEntity courthouse = new CourthouseEntity();
courthouse.setCourthouseName("SWANSEA");
CourtroomEntity courtroomEntity = new CourtroomEntity(1, "1", courthouse);
when(retrieveCoreObjectService.retrieveOrCreateCourtroom(eq("SWANSEA"), eq("1"), any(UserAccountEntity.class)))
.thenReturn(courtroomEntity);

OffsetDateTime startedAt = OffsetDateTime.now().minusHours(1);
OffsetDateTime endedAt = OffsetDateTime.now();
AddAudioMetadataRequest addAudioMetadataRequest = createAddAudioRequest(startedAt, endedAt);

MediaEntity mediaEntity = createMediaEntity(startedAt, endedAt);
mediaEntity.setChecksum(addAudioMetadataRequest.getChecksum());
mediaEntity.setId(10);

when(mediaRepository.findMediaByDetails(any(), any(), any(), any(), any()))
.thenReturn(List.of(mediaEntity));

when(dataManagementApi.getChecksum(any(), any())).thenReturn(addAudioMetadataRequest.getChecksum());

// When
UUID externalLocation = UUID.randomUUID();
audioService.addAudio(externalLocation, addAudioMetadataRequest);

// Then
verify(mediaRepository, times(1))
.findMediaByDetails(courtroomEntity, mediaEntity.getChannel(),mediaEntity.getMediaFile(), startedAt, endedAt);
verifyNoMoreInteractions(mediaRepository);
verifyNoInteractions(hearingRepository);
verifyNoInteractions(logApi);
verifyNoInteractions(externalObjectDirectoryRepository);
verify(dataManagementApi, times(1)).getChecksum(DatastoreContainerType.INBOUND, externalLocation);
verifyNoMoreInteractions(dataManagementApi);
}

@Test
void addAudioNoUploadChecksumDoNotMatch() {
OffsetDateTime startedAt = OffsetDateTime.now().minusHours(1);
Expand Down Expand Up @@ -273,6 +305,7 @@ private MediaEntity createMediaEntity(OffsetDateTime startedAt, OffsetDateTime e
mediaEntity.setStart(startedAt);
mediaEntity.setEnd(endedAt);
mediaEntity.setChannel(1);
mediaEntity.setMediaFile("test");
mediaEntity.setTotalChannels(2);
CourthouseEntity courthouse = new CourthouseEntity();
courthouse.setCourthouseName("SWANSEA");
Expand Down

0 comments on commit 49bba81

Please sign in to comment.