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..a50fd3aeb2 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,7 +100,11 @@ public List processAudioForPlaybacks(Map media List concatenatedAndMergedAudioFileInfos = new ArrayList<>(); if (isNotEmpty(audioFileInfos)) { - String audioFilenames = audioFileInfos.stream().map(AudioFileInfo::getMediaFile).collect(Collectors.joining(", ")); + // Used for logging only + String audioFilenames = null; + if (log.isDebugEnabled()) { + audioFilenames = audioFileInfos.stream().map(AudioFileInfo::getMediaFile).collect(Collectors.joining(", ")); + } List concatenationsList = new ArrayList<>(); 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..5a5d38e954 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,6 +225,11 @@ 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 and not less than a second apart as trim works against seconds + .filter(media -> !media.getStart().truncatedTo(ChronoUnit.SECONDS).isEqual(media.getEnd().truncatedTo(ChronoUnit.SECONDS))) + .filter(media -> media.getStart().isBefore(media.getEnd())) .filter(media -> mediaRequestEntity.getStartTime().isBefore(media.getEnd())) .filter(media -> media.getStart().isBefore(mediaRequestEntity.getEndTime())) .sorted(MEDIA_START_TIME_CHANNEL_COMPARATOR) 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..4e8bd01549 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 @@ -74,6 +74,7 @@ class AudioTransformationServiceImplTest { private static final OffsetDateTime TIME_12_00 = OffsetDateTime.parse("2023-01-01T12:00Z"); private static final OffsetDateTime TIME_12_01 = OffsetDateTime.parse("2023-01-01T12:01Z"); private static final OffsetDateTime TIME_12_20 = OffsetDateTime.parse("2023-01-01T12:20Z"); + private static final OffsetDateTime TIME_12_20_00_900 = OffsetDateTime.parse("2023-01-01T12:20:00.900Z"); private static final OffsetDateTime TIME_12_21 = OffsetDateTime.parse("2023-01-01T12:21Z"); private static final OffsetDateTime TIME_12_39 = OffsetDateTime.parse("2023-01-01T12:39Z"); private static final OffsetDateTime TIME_12_40 = OffsetDateTime.parse("2023-01-01T12:40Z"); @@ -246,105 +247,299 @@ void testHandleKedaInvocationForMediaRequestsCaseNull() { } @Test - void filterMediaByMediaRequestDatesWithStartDateExactRequestAndEndDateExactRequest() { + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsMediaWithStartDateAndEndDateTheSame() { + // given + 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); + + // when + List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( + mediaEntities, + mediaRequestEntity + ); + + // then + 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_WhichContainsMediaWithStartDateAndEndDateLessThan1Second() { + // given + List mediaEntities = createMediaEntities(TIME_12_00, TIME_12_20, TIME_12_20, TIME_12_20_00_900, TIME_12_40, TIME_13_00); + MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00); + + // when + List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( + mediaEntities, + mediaRequestEntity + ); + + // then + 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() { + // given + 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); + + // when + List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( + mediaEntities, + mediaRequestEntity + ); + + // then + assertEquals(0, mediaEntitiesResult.size()); + } + + + @Test + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsMediaWithStartDateAfterEndDate() { + // given + 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); + + // when + List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( + mediaEntities, + mediaRequestEntity + ); + + // then + 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() { + // given + 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); + + // when + List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( + mediaEntities, + mediaRequestEntity + ); + + // then + assertEquals(0, mediaEntitiesResult.size()); + } + + @Test + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsMediaWithNullStartDate() { + // given + 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); + + // when + List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( + mediaEntities, + mediaRequestEntity + ); + + // then + 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() { + // given + 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); + + // when + List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( + mediaEntities, + mediaRequestEntity + ); + + // then + 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() { + // given 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); + + // when List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( mediaEntities, mediaRequestEntity ); + // then assertEquals(3, mediaEntitiesResult.size()); assertEquals(TIME_12_00, mediaEntitiesResult.get(0).getStart()); assertEquals(TIME_13_00, mediaEntitiesResult.get(2).getEnd()); } @Test - void filterMediaByMediaRequestDatesWithStartDateAfterRequestAndEndDateBeforeRequest() { + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithStartDateAfterRequestAndEndDateBeforeRequest() { + // given 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); + + // when List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( mediaEntities, mediaRequestEntity ); + + // then assertEquals(3, mediaEntitiesResult.size()); assertEquals(TIME_12_01, mediaEntitiesResult.get(0).getStart()); assertEquals(TIME_12_59, mediaEntitiesResult.get(2).getEnd()); } @Test - void filterMediaByMediaRequestDatesWithStartDateBeforeRequestAndEndDateAfterRequest() { + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithStartDateBeforeRequestAndEndDateAfterRequest() { + // given 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); + + // when List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( mediaEntities, mediaRequestEntity ); + + // then assertEquals(3, mediaEntitiesResult.size()); assertEquals(TIME_11_59, mediaEntitiesResult.get(0).getStart()); assertEquals(TIME_13_01, mediaEntitiesResult.get(2).getEnd()); } @Test - void filterMediaByMediaRequestDatesWithStartDateAndEndDateExactMiddleMediaMatch() { + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithStartDateAndEndDateExactMiddleMediaMatch() { + // given 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); + + // when List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( mediaEntities, mediaRequestEntity ); + + // then assertEquals(1, mediaEntitiesResult.size()); assertEquals(TIME_12_20, mediaEntitiesResult.get(0).getStart()); assertEquals(TIME_12_40, mediaEntitiesResult.get(0).getEnd()); } @Test - void filterMediaByMediaRequestDatesWithStartDateBetweenRequestAndEndDateBetweenRequest() { + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithStartDateBetweenRequestAndEndDateBetweenRequest() { + // given 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); + + // when List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( mediaEntities, mediaRequestEntity ); + + // then assertEquals(1, mediaEntitiesResult.size()); assertEquals(TIME_12_20, mediaEntitiesResult.get(0).getStart()); assertEquals(TIME_12_40, mediaEntitiesResult.get(0).getEnd()); } @Test - void filterMediaByMediaRequestDatesWithRequestStartAndEndDateOutSideMediaRange() { + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithRequestStartAndEndDateOutSideMediaRange() { + // given 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); + + // when List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( mediaEntities, mediaRequestEntity ); + + // then assertEquals(0, mediaEntitiesResult.size()); } @Test - void filterMediaByMediaRequestDatesWithRequestStartTimeMatchesAnAudioEndTime() { + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithRequestStartTimeMatchesAnAudioEndTime() { + // given 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); + + // when List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( mediaEntities, mediaRequestEntity ); + // then assertEquals(1, mediaEntitiesResult.size()); assertEquals(TIME_10_05, mediaEntitiesResult.get(0).getStart()); assertEquals(TIME_10_10, mediaEntitiesResult.get(0).getEnd()); } @Test - void filterMediaByMediaRequestDatesWithRequestEndTimeMatchesAnAudioStartTime() { + void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithRequestEndTimeMatchesAnAudioStartTime() { + // given 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); + + // when List mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel( mediaEntities, mediaRequestEntity ); + // then assertEquals(2, mediaEntitiesResult.size()); assertEquals(TIME_09_59, mediaEntitiesResult.get(0).getStart()); assertEquals(TIME_10_10, mediaEntitiesResult.get(1).getEnd());