@ApiOperation(
            value = "Export messages as CSV",
            notes = "Use this endpoint, if you want to configure export parameters freely instead of relying on an existing Search"
    )
    @POST
    @Produces(MoreMediaTypes.TEXT_CSV)
    @NoAuditEvent("Has custom audit events")
    public ChunkedOutput<SimpleMessageChunk> retrieve(@ApiParam @Valid MessagesRequest rawrequest,
                                                      @Context SearchUser searchUser) {

        final MessagesRequest request = fillInIfNecessary(rawrequest, searchUser);

        final ValidationRequest.Builder validationReq = ValidationRequest.builder();
        Optional.ofNullable(rawrequest.queryString()).ifPresent(validationReq::query);
        Optional.ofNullable(rawrequest.timeRange()).ifPresent(validationReq::timerange);
        Optional.ofNullable(rawrequest.streams()).ifPresent(validationReq::streams);
        final ValidationResponse validationResponse = queryValidationService.validate(validationReq.build());
        if (validationResponse.status().equals(ValidationStatus.ERROR)) {
            validationResponse.explanations().stream().findFirst().map(ValidationMessage::errorMessage).ifPresent(message -> {
                throw new BadRequestException("Request validation failed: " + message);
            });
        }

        executionGuard.checkUserIsPermittedToSeeStreams(request.streams(), searchUser::canReadStream);

        ExportMessagesCommand command = commandFactory.buildFromRequest(request);

        return asyncRunner.apply(chunkConsumer -> exporter().export(command, chunkConsumer));
    }
@Test
    void appliesDefaultStreamsToRequestIfOmitted() {
        MessagesRequest request = validRequest();

        final SearchUser searchUser = TestSearchUser.builder()
                .allowStream("stream-1")
                .allowStream("stream-2")
                .build();


        ArgumentCaptor<MessagesRequest> captor = ArgumentCaptor.forClass(MessagesRequest.class);
        when(commandFactory.buildFromRequest(captor.capture())).thenReturn(ExportMessagesCommand.withDefaults());

        sut.retrieve(request, searchUser);

        MessagesRequest value = captor.getValue();
        assertThat(value.streams())
                .containsExactly("stream-1", "stream-2");
    }