public async Task GivenARequest_WhenNoFormatsAreSet_ThenHardcodedDefaultIsReturned(bool containerSpecified, string expectedFormat)
        {
            _exportJobConfiguration.Formats = null;

            ExportJobRecord actualRecord = null;
            await _fhirOperationDataStore.CreateExportJobAsync(
                Arg.Do <ExportJobRecord>(record =>
            {
                actualRecord = record;
            }),
                Arg.Any <CancellationToken>());

            var request = new CreateExportRequest(RequestUrl, ExportJobType.All, containerName: containerSpecified ? "test" : null);
            CreateExportResponse response = await _createExportRequestHandler.Handle(request, _cancellationToken);

            Assert.Equal(expectedFormat, actualRecord.ExportFormat);
        }
        public async Task GivenDifferentRequestor_WhenCreatingAnExportJob_ThenNewJobShouldBeCreated()
        {
            _claimsExtractor.ExtractImpl = () => new[] { KeyValuePair.Create("oid", "user1") };

            var request = new CreateExportRequest(RequestUrl, ExportJobType.All);

            CreateExportResponse response = await _createExportRequestHandler.Handle(request, _cancellationToken);

            _claimsExtractor.ExtractImpl = () => new[] { KeyValuePair.Create("oid", "user2") };

            var newRequest = new CreateExportRequest(RequestUrl, ExportJobType.All);

            CreateExportResponse newResponse = await _createExportRequestHandler.Handle(newRequest, _cancellationToken);

            Assert.NotNull(newResponse);
            Assert.NotEqual(response.JobId, newResponse.JobId);
        }
        public async Task GivenSetSecretFails_WhenCreatingAnExportJob_ThenThrowsOperationFailedException()
        {
            // Set up create export request handler with mock secret store.
            ISecretStore   mockSecretStore = Substitute.For <ISecretStore>();
            HttpStatusCode errorStatusCode = HttpStatusCode.InternalServerError;

            mockSecretStore.SetSecretAsync(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <CancellationToken>())
            .Returns <SecretWrapper>(_ => throw new SecretStoreException(SecretStoreErrors.SetSecretError, innerException: null, statusCode: errorStatusCode));

            _createExportRequestHandler = new CreateExportRequestHandler(_claimsExtractor, _fhirOperationDataStore, mockSecretStore);

            var request = new CreateExportRequest(RequestUrl, DestinationType, ConnectionString);

            OperationFailedException ofe = await Assert.ThrowsAsync <OperationFailedException>(() => _createExportRequestHandler.Handle(request, _cancellationToken));

            Assert.NotNull(ofe);
            Assert.Equal(errorStatusCode, ofe.ResponseStatusCode);
        }
        public static async Task <CreateExportResponse> ExportAsync(
            this IMediator mediator,
            Uri requestUri,
            string destinationType,
            string destinationConnectionString,
            CancellationToken cancellationToken)
        {
            EnsureArg.IsNotNull(mediator, nameof(mediator));
            EnsureArg.IsNotNull(requestUri, nameof(requestUri));
            EnsureArg.IsNotNullOrWhiteSpace(destinationType, nameof(destinationType));
            EnsureArg.IsNotNullOrWhiteSpace(destinationConnectionString, nameof(destinationConnectionString));

            var request = new CreateExportRequest(requestUri, destinationType, destinationConnectionString);

            var response = await mediator.Send(request, cancellationToken);

            return(response);
        }
        public static async Task <CreateExportResponse> ExportAsync(
            this IMediator mediator,
            Uri requestUri,
            ExportJobType requestType,
            string resourceType,
            PartialDateTime since,
            string groupId,
            CancellationToken cancellationToken)
        {
            EnsureArg.IsNotNull(mediator, nameof(mediator));
            EnsureArg.IsNotNull(requestUri, nameof(requestUri));

            var request = new CreateExportRequest(requestUri, requestType, resourceType, since, groupId);

            CreateExportResponse response = await mediator.Send(request, cancellationToken);

            return(response);
        }
        public async Task GivenThereIsAMatchingJob_WhenRequestorClaimsInDifferentOrder_ThenExistingJobShouldBeReturned()
        {
            var claim1 = KeyValuePair.Create("oid", "user1");
            var claim2 = KeyValuePair.Create("iss", "http://localhost/authority");

            _claimsExtractor.ExtractImpl = () => new[] { claim1, claim2 };

            var request = new CreateExportRequest(RequestUrl, DestinationType, ConnectionString);

            CreateExportResponse response = await _createExportRequestHandler.Handle(request, _cancellationToken);

            _claimsExtractor.ExtractImpl = () => new[] { claim2, claim1 };

            var newRequest = new CreateExportRequest(RequestUrl, DestinationType, ConnectionString);

            CreateExportResponse newResponse = await _createExportRequestHandler.Handle(newRequest, _cancellationToken);

            Assert.Equal(response.JobId, newResponse.JobId);
        }
        public async Task GivenARequestWithFilters_WhenConverted_ThenTheFiltersArePopulated(string filters, IList <ExportJobFilter> expectedFilters)
        {
            ExportJobRecord actualRecord = null;
            await _fhirOperationDataStore.CreateExportJobAsync(
                Arg.Do <ExportJobRecord>(record =>
            {
                actualRecord = record;
            }), Arg.Any <CancellationToken>());

            var request = new CreateExportRequest(RequestUrl, ExportJobType.All, filters: filters);
            CreateExportResponse response = await _createExportRequestHandler.Handle(request, _cancellationToken);

            Assert.Collection(
                actualRecord.Filters,
                expectedFilters.Select((actFilter) => new Action <ExportJobFilter>((expFilter) =>
            {
                Assert.Equal(expFilter.ResourceType, actFilter.ResourceType);
                Assert.Equal(expFilter.Parameters, actFilter.Parameters);
            })).ToArray());
        }
Exemple #8
0
        public static async Task <CreateExportResponse> ExportAsync(
            this IMediator mediator,
            Uri requestUri,
            ExportJobType requestType,
            string resourceType,
            PartialDateTime since,
            string groupId,
            string containerName,
            string formatName,
            string anonymizationConfigLocation,
            string anonymizationConfigFileETag,
            CancellationToken cancellationToken)
        {
            EnsureArg.IsNotNull(mediator, nameof(mediator));
            EnsureArg.IsNotNull(requestUri, nameof(requestUri));

            var request = new CreateExportRequest(requestUri, requestType, resourceType, since, groupId, containerName, formatName, anonymizationConfigLocation, anonymizationConfigFileETag);

            CreateExportResponse response = await mediator.Send(request, cancellationToken);

            return(response);
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            CreateExportRequest request;

            try
            {
                request = new CreateExportRequest
                {
                    CreateExportDetails = CreateExportDetails,
                    OpcRetryToken       = OpcRetryToken,
                    OpcRequestId        = OpcRequestId
                };

                response = client.CreateExport(request).GetAwaiter().GetResult();
                WriteOutput(response, response.Export);
                FinishProcessing(response);
            }
            catch (Exception ex)
            {
                TerminatingErrorDuringExecution(ex);
            }
        }
Exemple #10
0
        private ExportJobOutcome CreateExportJobOutcome()
        {
            var exportRequest = new CreateExportRequest(new Uri($"http://localhost/ExportJob/"), "destinationType", "destinationConnection");

            return(new ExportJobOutcome(new ExportJobRecord(exportRequest.RequestUri, "Patient", "hash"), WeakETag.FromVersionId("0")));
        }
Exemple #11
0
        private ExportJobOutcome CreateExportJobOutcome()
        {
            var exportRequest = new CreateExportRequest(new Uri($"http://localhost/ExportJob/"), ExportJobType.All);

            return(new ExportJobOutcome(new ExportJobRecord(exportRequest.RequestUri, exportRequest.RequestType, ExportFormatTags.ResourceName, null, null, "hash", rollingFileSizeInMB: 64), WeakETag.FromVersionId("0")));
        }
Exemple #12
0
 public async Task GivenARequestWithANonexistantFormatName_WhenConverted_ThenABadRequestIsReturned()
 {
     var request = new CreateExportRequest(RequestUrl, ExportJobType.All, formatName: "invalid");
     await Assert.ThrowsAsync <BadRequestException>(() => _createExportRequestHandler.Handle(request, _cancellationToken));
 }
Exemple #13
0
        private ExportJobOutcome CreateExportJobOutcome()
        {
            var exportRequest = new CreateExportRequest(new Uri($"http://localhost/ExportJob/"), ExportJobType.All);

            return(new ExportJobOutcome(new ExportJobRecord(exportRequest.RequestUri, exportRequest.RequestType, null, "hash"), WeakETag.FromVersionId("0")));
        }
Exemple #14
0
    public override async Task CreateExport(CreateExportRequest options, IServerStreamWriter <CreateExportResponse> responseStream, ServerCallContext context)
    {
        var ef           = options.ExportFormat;
        var exportFormat = (DiscordChatExporter.Core.Exporting.ExportFormat)ef;

        var parsed    = Snowflake.TryParse(options.ChannelId);
        var channelId = parsed ?? Snowflake.Zero;

        var client = new DiscordClient(options.Token);

        client._tokenKind = TokenKind.Bot;
        Channel channel;

        try
        {
            channel = await client.GetChannelAsync(channelId);
        }
        catch (DiscordChatExporterException e)
        {
            if (e.Message.Contains("Authentication"))
            {
                throw new RpcException(new Status(StatusCode.PermissionDenied, "An invalid Discord token was provided."));
            }
            if (e.Message.Contains("Requested resource does not exist"))
            {
                throw new RpcException(new Status(StatusCode.NotFound, "A channel with the provided ID was not found."));
            }
            throw new RpcException(new Status(StatusCode.Unknown, $"An unknown error occurred: {e.Message}"));
        }

        var guild = await client.GetGuildAsync(channel.GuildId);

        var res = await client.GetJsonResponseAsync("users/@me");

        var me = DiscordChatExporter.Core.Discord.Data.User.Parse(res);

        var path = GetPath(channel.Id.ToString(), exportFormat);

        _logger.LogInformation($"[{me.FullName} ({me.Id})] Exporting #{channel.Name} ({channel.Id}) within {guild.Name} ({guild.Id}) to {path}");
        var request = new ExportRequest(
            guild,
            channel,
            path,
            exportFormat,
            Snowflake.TryParse(options.After),
            Snowflake.TryParse(options.Before),
            PartitionLimit.Null,
            MessageFilter.Null,
            false,
            false,
            options.DateFormat
            );

        var exporter = new ChannelExporter(client);

        _logger.LogInformation("Starting export");
        var progress = new Progress <double>(p => responseStream.WriteAsync(new CreateExportResponse {
            Progress = p
        }));
        var messageCount = await exporter.ExportChannelAsync(request, progress);

        _logger.LogInformation("Finished exporting");


        var buffer = new byte[ChunkSize];

        await using var readStream = File.OpenRead(path);
        while (true)
        {
            var count = await readStream.ReadAsync(buffer);

            if (count == 0)
            {
                break;
            }

            Console.WriteLine("Sending file data chunk of length " + count);
            await responseStream.WriteAsync(new CreateExportResponse
            {
                Data = new ExportComplete {
                    MessageCount = messageCount,
                    Data         = UnsafeByteOperations.UnsafeWrap(buffer.AsMemory(0, count))
                }
            });
        }

        deleteFile(path);
    }
 [InlineData("Observation?status:final")]                      // Incorrect divider
 public async Task GivenARequestWithIncorectFilters_WhenConverted_ThenABadRequestIsReturned(string filters)
 {
     var request = new CreateExportRequest(RequestUrl, ExportJobType.All, filters: filters);
     await Assert.ThrowsAsync <BadRequestException>(() => _createExportRequestHandler.Handle(request, _cancellationToken));
 }