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-4385 Filter out 0 second audios from ATS processing #2408

Merged
merged 6 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -100,7 +100,11 @@ public List<AudioFileInfo> processAudioForPlaybacks(Map<MediaEntity, Path> media

List<AudioFileInfo> 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<ChannelAudio> concatenationsList = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -223,6 +225,11 @@ private void processAudioRequest(MediaRequestEntity mediaRequestEntity) {
List<MediaEntity> filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(List<MediaEntity> 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)))
Ben-Edwards-cgi marked this conversation as resolved.
Show resolved Hide resolved
.filter(media -> media.getStart().isBefore(media.getEnd()))
.filter(media -> mediaRequestEntity.getStartTime().isBefore(media.getEnd()))
karen-hedges marked this conversation as resolved.
Show resolved Hide resolved
.filter(media -> media.getStart().isBefore(mediaRequestEntity.getEndTime()))
.sorted(MEDIA_START_TIME_CHANNEL_COMPARATOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -246,105 +247,299 @@ void testHandleKedaInvocationForMediaRequestsCaseNull() {
}

@Test
void filterMediaByMediaRequestDatesWithStartDateExactRequestAndEndDateExactRequest() {
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsMediaWithStartDateAndEndDateTheSame() {
// given
List<MediaEntity> 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<MediaEntity> 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<MediaEntity> 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<MediaEntity> 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<MediaEntity> 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<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
mediaEntities,
mediaRequestEntity
);

// then
assertEquals(0, mediaEntitiesResult.size());
}


@Test
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsMediaWithStartDateAfterEndDate() {
// given
List<MediaEntity> 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<MediaEntity> 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<MediaEntity> 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<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
mediaEntities,
mediaRequestEntity
);

// then
assertEquals(0, mediaEntitiesResult.size());
}

@Test
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsMediaWithNullStartDate() {
// given
List<MediaEntity> 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<MediaEntity> 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<MediaEntity> mediaEntities = createMediaEntities(null, TIME_12_00, null, TIME_12_20, null, TIME_12_40);
MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00);
List<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
mediaEntities,
mediaRequestEntity
);

assertEquals(0, mediaEntitiesResult.size());
}

@Test
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WhichContainsMediaWithNullEndDate() {
// given
List<MediaEntity> 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<MediaEntity> 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<MediaEntity> mediaEntities = createMediaEntities(TIME_12_20, null, TIME_12_40, null, TIME_13_00, null);
MediaRequestEntity mediaRequestEntity = createMediaRequest(TIME_12_00, TIME_13_00);
List<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
mediaEntities,
mediaRequestEntity
);

assertEquals(0, mediaEntitiesResult.size());
}

@Test
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithStartDateExactRequestAndEndDateExactRequest() {
// given
List<MediaEntity> 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<MediaEntity> 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<MediaEntity> 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<MediaEntity> 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<MediaEntity> 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<MediaEntity> 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<MediaEntity> 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<MediaEntity> 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<MediaEntity> 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<MediaEntity> 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<MediaEntity> 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<MediaEntity> mediaEntitiesResult = audioTransformationService.filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel(
mediaEntities,
mediaRequestEntity
);

// then
assertEquals(0, mediaEntitiesResult.size());
}

@Test
void filterMediaByMediaRequestDatesWithRequestStartTimeMatchesAnAudioEndTime() {
void filterMediaByMediaRequestTimeframeAndSortByStartTimeAndChannel_ReturnsFilteredData_WithRequestStartTimeMatchesAnAudioEndTime() {
// given
List<MediaEntity> 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<MediaEntity> 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<MediaEntity> 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<MediaEntity> 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());
Expand Down
Loading