From 1decdf47d6ec8f496cb8d8deae30d6899d76c337 Mon Sep 17 00:00:00 2001 From: karen-hedges <133129444+karen-hedges@users.noreply.github.com> Date: Thu, 2 Jan 2025 17:05:52 +0000 Subject: [PATCH] DMP-4385 Filter out 0 second audios from ATS processing Updated the ATS filtering to filter out audio files that have less than a second start and end time, start or end time is null or start time is after end time --- .../impl/OutboundFileProcessorImpl.java | 2 + .../impl/AudioTransformationServiceImpl.java | 10 ++ .../AudioTransformationServiceImplTest.java | 128 ++++++++++++++++-- 3 files changed, 132 insertions(+), 8 deletions(-) diff --git a/src/main/java/uk/gov/hmcts/darts/audio/component/impl/OutboundFileProcessorImpl.java b/src/main/java/uk/gov/hmcts/darts/audio/component/impl/OutboundFileProcessorImpl.java index a25e9b18ff..79350cf7df 100644 --- a/src/main/java/uk/gov/hmcts/darts/audio/component/impl/OutboundFileProcessorImpl.java +++ b/src/main/java/uk/gov/hmcts/darts/audio/component/impl/OutboundFileProcessorImpl.java @@ -100,10 +100,12 @@ public List processAudioForPlaybacks(Map media List concatenatedAndMergedAudioFileInfos = new ArrayList<>(); if (isNotEmpty(audioFileInfos)) { + // Used for logging only String audioFilenames = audioFileInfos.stream().map(AudioFileInfo::getMediaFile).collect(Collectors.joining(", ")); List concatenationsList = new ArrayList<>(); + if (isWellFormedAudio(audioFileInfos)) { log.debug("Audio files {} are well formed", audioFilenames); List concatenatedAudios = concatenateByChannelWithGaps(audioFileInfos); diff --git a/src/main/java/uk/gov/hmcts/darts/audio/service/impl/AudioTransformationServiceImpl.java b/src/main/java/uk/gov/hmcts/darts/audio/service/impl/AudioTransformationServiceImpl.java index ce85b40bf7..f315fceb10 100644 --- a/src/main/java/uk/gov/hmcts/darts/audio/service/impl/AudioTransformationServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/darts/audio/service/impl/AudioTransformationServiceImpl.java @@ -36,6 +36,7 @@ import java.nio.file.Path; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -47,6 +48,7 @@ import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; +import static java.util.Objects.nonNull; import static uk.gov.hmcts.darts.audio.enums.MediaRequestStatus.FAILED; import static uk.gov.hmcts.darts.audio.enums.MediaRequestStatus.OPEN; import static uk.gov.hmcts.darts.audiorequests.model.AudioRequestType.DOWNLOAD; @@ -223,7 +225,15 @@ private void processAudioRequest(MediaRequestEntity mediaRequestEntity) { List filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(List mediaEntitiesForRequest, MediaRequestEntity mediaRequestEntity) { return mediaEntitiesForRequest.stream() + .filter(media -> nonNull(media.getStart())) + .filter(media -> nonNull(media.getEnd())) + // Filter out media where the media start and media end times are the same + .filter(media -> !media.getStart().truncatedTo(ChronoUnit.SECONDS).isEqual(media.getEnd().truncatedTo(ChronoUnit.SECONDS))) + // Filter out media where the media start time is after the media end time + .filter(media -> media.getStart().isBefore(media.getEnd())) + // Filter out media where the media end time is after the media request start time .filter(media -> mediaRequestEntity.getStartTime().isBefore(media.getEnd())) + // Filter out media where the media start time is before the media request end time .filter(media -> media.getStart().isBefore(mediaRequestEntity.getEndTime())) .sorted(MEDIA_START_TIME_CHANNEL_COMPARATOR) .collect(Collectors.toList()); diff --git a/src/test/java/uk/gov/hmcts/darts/audio/service/impl/AudioTransformationServiceImplTest.java b/src/test/java/uk/gov/hmcts/darts/audio/service/impl/AudioTransformationServiceImplTest.java index 0e767c119a..9c2abff304 100644 --- a/src/test/java/uk/gov/hmcts/darts/audio/service/impl/AudioTransformationServiceImplTest.java +++ b/src/test/java/uk/gov/hmcts/darts/audio/service/impl/AudioTransformationServiceImplTest.java @@ -246,7 +246,119 @@ void testHandleKedaInvocationForMediaRequestsCaseNull() { } @Test - void filterMediaByMediaRequestDatesWithStartDateExactRequestAndEndDateExactRequest() { + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsMediaWithStartDateAndEndDateTheSame() { + List mediaEntities = createMediaEntities(TIME_12_00, TIME_12_20, TIME_12_20, TIME_12_20, TIME_12_40, TIME_13_00); + MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00); + List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( + mediaEntities, + mediaRequestEntity + ); + + assertEquals(2, mediaEntitiesResult.size()); + assertEquals(TIME_12_00, mediaEntitiesResult.get(0).getStart()); + assertEquals(TIME_12_20, mediaEntitiesResult.get(0).getEnd()); + assertEquals(TIME_12_40, mediaEntitiesResult.get(1).getStart()); + assertEquals(TIME_13_00, mediaEntitiesResult.get(1).getEnd()); + } + + @Test + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsOnlyMediaWithStartDateAndEndDateTheSame() { + List mediaEntities = createMediaEntities(TIME_12_00, TIME_12_00, TIME_12_20, TIME_12_20, TIME_13_00, TIME_13_00); + MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00); + List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( + mediaEntities, + mediaRequestEntity + ); + + assertEquals(0, mediaEntitiesResult.size()); + } + + @Test + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsMediaWithStartDateAfterEndDate() { + List mediaEntities = createMediaEntities(TIME_12_00, TIME_12_20, TIME_12_40, TIME_12_20, TIME_12_40, TIME_13_00); + MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00); + List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( + mediaEntities, + mediaRequestEntity + ); + + assertEquals(2, mediaEntitiesResult.size()); + assertEquals(TIME_12_00, mediaEntitiesResult.get(0).getStart()); + assertEquals(TIME_12_20, mediaEntitiesResult.get(0).getEnd()); + assertEquals(TIME_12_40, mediaEntitiesResult.get(1).getStart()); + assertEquals(TIME_13_00, mediaEntitiesResult.get(1).getEnd()); + } + + @Test + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsOnlyMediaWithStartDateAfterEndDate() { + List mediaEntities = createMediaEntities(TIME_12_20, TIME_12_00, TIME_12_40, TIME_12_20, TIME_13_00, TIME_12_40); + MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00); + List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( + mediaEntities, + mediaRequestEntity + ); + + assertEquals(0, mediaEntitiesResult.size()); + } + + @Test + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsMediaWithNullStartDate() { + List mediaEntities = createMediaEntities(TIME_12_00, TIME_12_20, null, TIME_12_20, TIME_12_40, TIME_13_00); + MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00); + List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( + mediaEntities, + mediaRequestEntity + ); + + assertEquals(2, mediaEntitiesResult.size()); + assertEquals(TIME_12_00, mediaEntitiesResult.get(0).getStart()); + assertEquals(TIME_12_20, mediaEntitiesResult.get(0).getEnd()); + assertEquals(TIME_12_40, mediaEntitiesResult.get(1).getStart()); + assertEquals(TIME_13_00, mediaEntitiesResult.get(1).getEnd()); + } + + @Test + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsOnlyMediaWithNullStartDates() { + List mediaEntities = createMediaEntities(null, TIME_12_00, null, TIME_12_20, null, TIME_12_40); + MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00); + List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( + mediaEntities, + mediaRequestEntity + ); + + assertEquals(0, mediaEntitiesResult.size()); + } + + @Test + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsMediaWithNullEndDate() { + List mediaEntities = createMediaEntities(TIME_12_00, TIME_12_20, TIME_12_40, null, TIME_12_40, TIME_13_00); + MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00); + List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( + mediaEntities, + mediaRequestEntity + ); + + assertEquals(2, mediaEntitiesResult.size()); + assertEquals(TIME_12_00, mediaEntitiesResult.get(0).getStart()); + assertEquals(TIME_12_20, mediaEntitiesResult.get(0).getEnd()); + assertEquals(TIME_12_40, mediaEntitiesResult.get(1).getStart()); + assertEquals(TIME_13_00, mediaEntitiesResult.get(1).getEnd()); + } + + @Test + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsOnlyMediaNullEndDates() { + List mediaEntities = createMediaEntities(TIME_12_20, null, TIME_12_40, null, TIME_13_00, null); + MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00); + List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( + mediaEntities, + mediaRequestEntity + ); + + assertEquals(0, mediaEntitiesResult.size()); + } + + @Test + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithStartDateExactRequestAndEndDateExactRequest() { List mediaEntities = createMediaEntities(TIME_12_00, TIME_12_20, TIME_12_20, TIME_12_40, TIME_12_40, TIME_13_00); MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00); List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( @@ -260,7 +372,7 @@ void filterMediaByMediaRequestDatesWithStartDateExactRequestAndEndDateExactReque } @Test - void filterMediaByMediaRequestDatesWithStartDateAfterRequestAndEndDateBeforeRequest() { + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithStartDateAfterRequestAndEndDateBeforeRequest() { List mediaEntities = createMediaEntities(TIME_12_01, TIME_12_20, TIME_12_20, TIME_12_40, TIME_12_40, TIME_12_59); MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00); List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( @@ -273,7 +385,7 @@ void filterMediaByMediaRequestDatesWithStartDateAfterRequestAndEndDateBeforeRequ } @Test - void filterMediaByMediaRequestDatesWithStartDateBeforeRequestAndEndDateAfterRequest() { + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithStartDateBeforeRequestAndEndDateAfterRequest() { List mediaEntities = createMediaEntities(TIME_11_59, TIME_12_20, TIME_12_20, TIME_12_40, TIME_12_40, TIME_13_01); MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00); List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( @@ -286,7 +398,7 @@ void filterMediaByMediaRequestDatesWithStartDateBeforeRequestAndEndDateAfterRequ } @Test - void filterMediaByMediaRequestDatesWithStartDateAndEndDateExactMiddleMediaMatch() { + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithStartDateAndEndDateExactMiddleMediaMatch() { List mediaEntities = createMediaEntities(TIME_11_59, TIME_12_20, TIME_12_20, TIME_12_40, TIME_12_40, TIME_13_01); MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_20, TIME_12_40); List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( @@ -299,7 +411,7 @@ void filterMediaByMediaRequestDatesWithStartDateAndEndDateExactMiddleMediaMatch( } @Test - void filterMediaByMediaRequestDatesWithStartDateBetweenRequestAndEndDateBetweenRequest() { + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithStartDateBetweenRequestAndEndDateBetweenRequest() { List mediaEntities = createMediaEntities(TIME_12_00, TIME_12_20, TIME_12_20, TIME_12_40, TIME_12_40, TIME_13_00); MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_21, TIME_12_39); List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( @@ -312,7 +424,7 @@ void filterMediaByMediaRequestDatesWithStartDateBetweenRequestAndEndDateBetweenR } @Test - void filterMediaByMediaRequestDatesWithRequestStartAndEndDateOutSideMediaRange() { + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithRequestStartAndEndDateOutSideMediaRange() { List mediaEntities = createMediaEntities(TIME_12_00, TIME_12_20, TIME_12_20, TIME_12_40, TIME_12_40, TIME_12_59); MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_13_00, TIME_13_01); List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( @@ -323,7 +435,7 @@ void filterMediaByMediaRequestDatesWithRequestStartAndEndDateOutSideMediaRange() } @Test - void filterMediaByMediaRequestDatesWithRequestStartTimeMatchesAnAudioEndTime() { + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithRequestStartTimeMatchesAnAudioEndTime() { List mediaEntities = createMediaEntities(TIME_09_59, TIME_10_00, TIME_10_05, TIME_10_10, TIME_10_15, TIME_10_20); MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_10_00, TIME_10_10); List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( @@ -337,7 +449,7 @@ void filterMediaByMediaRequestDatesWithRequestStartTimeMatchesAnAudioEndTime() { } @Test - void filterMediaByMediaRequestDatesWithRequestEndTimeMatchesAnAudioStartTime() { + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithRequestEndTimeMatchesAnAudioStartTime() { List mediaEntities = createMediaEntities(TIME_09_59, TIME_10_00, TIME_10_05, TIME_10_10, TIME_10_15, TIME_10_20); MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_09_59, TIME_10_15); List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(