//public static async Task<Guid> ProcessFile(this OsdrTestHarness harness, string bucket, string fileName, IDictionary<string, object> metadata = null)
        //{
        //    Log.Information($"Processing started [{fileName}]");

        //    var blobId = await harness.AddBlob(bucket, fileName, metadata);

        //    //var blobInfo = await harness.BlobStorage.GetFileInfo(blobId, bucket);

        //    //await harness.BusControl.Publish<BlobLoaded>(new
        //    //{
        //    //    BlobInfo = new LoadedBlobInfo(blobId, fileName, blobInfo.Length, harness.JohnId, blobInfo.UploadDateTime, blobInfo.MD5, bucket, blobInfo.Metadata),
        //    //    TimeStamp = DateTimeOffset.UtcNow
        //    //});

        //    if (!harness.Received.Select<Generic.Sagas.Events.FileProcessed>(m => m.Context.Message.BlobId == blobId).Any())
        //    {
        //        throw new TimeoutException();
        //    }
        //    var fileProcessed = harness.Received.Select<Generic.Sagas.Events.FileProcessed>(m => m.Context.Message.BlobId == blobId).First();
        //    var fileId = fileProcessed.Context.Message.Id;

        //    if (!harness.Received.Select<Generic.Domain.Events.Files.NodeStatusPersisted>(m => (m.Context.Message.Status == Generic.Domain.FileStatus.Processed || m.Context.Message.Status == Generic.Domain.FileStatus.Failed) && m.Context.Message.Id == fileId).Any())
        //    {
        //        throw new TimeoutException();
        //    }
        //    if (!harness.Received.Select<Generic.Domain.Events.Files.StatusPersisted>(m => (m.Context.Message.Status == Generic.Domain.FileStatus.Processed || m.Context.Message.Status == Generic.Domain.FileStatus.Failed) && m.Context.Message.Id == fileId).Any())
        //    {
        //        throw new TimeoutException();
        //    }

        //    var events = await harness.CqrsEventStore.Get(fileId, 0);

        //    Log.Information($"Aggregate-{fileId}: {string.Join(", ", events.Select(e => $"{e.GetType().Name} ({e.Version})"))}");

        //    Log.Information($"Processing finished [{fileName}]");

        //    return fileId;
        //}

        //public static async Task<Guid> ProcessRecordsFile(this OsdrTestHarness harness, string bucket, string fileName, IDictionary<string, object> metadata = null)
        //{
        //    Log.Information($"Processing started [{fileName}]");

        //    var blobId = await harness.AddBlob(bucket, fileName, metadata.ToDictionary(c => c.Key, c => c.Value, StringComparer.OrdinalIgnoreCase));

        //    var blobInfo = await harness.BlobStorage.GetFileInfo(blobId, bucket);

        //    await harness.BusControl.Publish<BlobLoaded>(new
        //    {
        //        BlobInfo = new LoadedBlobInfo(blobId, fileName, blobInfo.Length, (Guid)metadata["parentId"], blobInfo.UploadDateTime, blobInfo.MD5, bucket, blobInfo.Metadata),
        //        TimeStamp = DateTimeOffset.UtcNow
        //    });

        //    if (!harness.Received.Select<RecordsFile.Sagas.Events.RecordsFileProcessed>(m => m.Context.Message.BlobId == blobId).Any())
        //    {
        //        throw new TimeoutException();
        //    }
        //    var fileProcessed = harness.Received.Select<RecordsFile.Sagas.Events.RecordsFileProcessed>(m => m.Context.Message.BlobId == blobId).First();
        //    var fileId = fileProcessed.Context.Message.Id;

        //    if (fileProcessed.Context.Message.ProcessedRecords != harness.GetProcessedRecords(fileId).Count())
        //    {
        //        throw new Exception($"fileProcessed.Context.Message.ProcessedRecords ({fileProcessed.Context.Message.ProcessedRecords}) != Fixture.GetProcessedRecords(fileId).Count() ({harness.GetProcessedRecords(fileId).Count()})");
        //    }

        //    foreach (var recordId in harness.GetProcessedRecords(fileId))
        //    {
        //        if (!harness.Received.Select<RecordsFile.Domain.Events.Records.NodeStatusPersisted>(m => m.Context.Message.Status == RecordsFile.Domain.RecordStatus.Processed && m.Context.Message.Id == recordId).Any())
        //        {
        //            throw new TimeoutException();
        //        }
        //        if (!harness.Received.Select<RecordsFile.Domain.Events.Records.StatusPersisted>(m => m.Context.Message.Status == RecordsFile.Domain.RecordStatus.Processed && m.Context.Message.Id == recordId).Any())
        //        {
        //            throw new TimeoutException();
        //        }
        //    }

        //    if (fileProcessed.Context.Message.FailedRecords != harness.GetInvalidRecords(fileId).Count())
        //    {
        //        throw new Exception("fileProcessed.Context.Message.FailedRecords != Fixture.GetInvalidRecords(fileId).Count()");
        //    }

        //    foreach (var recordId in harness.GetInvalidRecords(fileId))
        //    {
        //        if (!harness.Received.Select<RecordsFile.Domain.Events.Records.RecordPersisted>(m => m.Context.Message.Id == recordId).Any())
        //        {
        //            throw new TimeoutException();
        //        }
        //        if (!harness.Received.Select<RecordsFile.Domain.Events.Records.NodeRecordPersisted>(m => m.Context.Message.Id == recordId).Any())
        //        {
        //            throw new TimeoutException();
        //        }
        //    }

        //    if (!harness.Received.Select<Generic.Domain.Events.Files.NodeStatusPersisted>(m => (m.Context.Message.Status == Generic.Domain.FileStatus.Processed || m.Context.Message.Status == Generic.Domain.FileStatus.Failed) && m.Context.Message.Id == fileId).Any())
        //    {
        //        throw new TimeoutException();
        //    }
        //    if (!harness.Received.Select<Generic.Domain.Events.Files.StatusPersisted>(m => (m.Context.Message.Status == Generic.Domain.FileStatus.Processed || m.Context.Message.Status == Generic.Domain.FileStatus.Failed) && m.Context.Message.Id == fileId).Any())
        //    {
        //        throw new TimeoutException();
        //    }

        //    var events = await harness.CqrsEventStore.Get(fileId, 0);

        //    Log.Information($"Aggregate-{fileId}: {string.Join(", ", events.Select(e => $"{e.GetType().Name} ({e.Version})"))}");

        //    Log.Information($"Processing finished [{fileName}]");

        //    return fileId;
        //}

        public static Guid WaitWhileRecordsFileProcessed(this OsdrTestHarness harness, Guid blobId)
        {
            if (!harness.Received.Select <RecordsFile.Sagas.Events.RecordsFileProcessed>(m => m.Context.Message.BlobId == blobId).Any())
            {
                throw new TimeoutException();
            }
            var fileProcessed = harness.Received.Select <RecordsFile.Sagas.Events.RecordsFileProcessed>(m => m.Context.Message.BlobId == blobId).First();
            var fileId        = fileProcessed.Context.Message.Id;

            if (fileProcessed.Context.Message.ProcessedRecords != harness.GetProcessedRecords(fileId).Count())
            {
                throw new Exception($"fileProcessed.Context.Message.ProcessedRecords ({fileProcessed.Context.Message.ProcessedRecords}) != Fixture.GetProcessedRecords(fileId).Count() ({harness.GetProcessedRecords(fileId).Count()})");
            }

            foreach (var recordId in harness.GetProcessedRecords(fileId))
            {
                if (!harness.Received.Select <RecordsFile.Domain.Events.Records.NodeStatusPersisted>(m => m.Context.Message.Status == RecordsFile.Domain.RecordStatus.Processed && m.Context.Message.Id == recordId).Any())
                {
                    throw new TimeoutException();
                }
                if (!harness.Received.Select <RecordsFile.Domain.Events.Records.StatusPersisted>(m => m.Context.Message.Status == RecordsFile.Domain.RecordStatus.Processed && m.Context.Message.Id == recordId).Any())
                {
                    throw new TimeoutException();
                }
            }

            if (fileProcessed.Context.Message.FailedRecords != harness.GetInvalidRecords(fileId).Count())
            {
                throw new Exception("fileProcessed.Context.Message.FailedRecords != Fixture.GetInvalidRecords(fileId).Count()");
            }

            foreach (var recordId in harness.GetInvalidRecords(fileId))
            {
                if (!harness.Received.Select <RecordsFile.Domain.Events.Records.RecordPersisted>(m => m.Context.Message.Id == recordId).Any())
                {
                    throw new TimeoutException();
                }
                if (!harness.Received.Select <RecordsFile.Domain.Events.Records.NodeRecordPersisted>(m => m.Context.Message.Id == recordId).Any())
                {
                    throw new TimeoutException();
                }
            }

            if (!harness.Received.Select <Generic.Domain.Events.Files.NodeStatusPersisted>(m => (m.Context.Message.Status == Generic.Domain.FileStatus.Processed || m.Context.Message.Status == Generic.Domain.FileStatus.Failed) && m.Context.Message.Id == fileId).Any())
            {
                throw new TimeoutException();
            }
            if (!harness.Received.Select <Generic.Domain.Events.Files.StatusPersisted>(m => (m.Context.Message.Status == Generic.Domain.FileStatus.Processed || m.Context.Message.Status == Generic.Domain.FileStatus.Failed) && m.Context.Message.Id == fileId).Any())
            {
                throw new TimeoutException();
            }

            return(fileId);
        }
        public static async Task <Guid> ProcessRecordsFile(this OsdrTestHarness harness, string bucket, string fileName, IDictionary <string, object> metadata = null)
        {
            Log.Information($"Processing started [{fileName}]");

            var blobId = await harness.AddBlob(bucket, fileName, metadata.ToDictionary(c => c.Key, c => c.Value, StringComparer.OrdinalIgnoreCase));

            var blobInfo = await harness.BlobStorage.GetFileInfo(blobId, bucket);

            await harness.BusControl.Publish <BlobLoaded>(new
            {
                BlobInfo  = new LoadedBlobInfo(blobId, fileName, blobInfo.Length, (Guid)metadata["parentId"], blobInfo.UploadDateTime, blobInfo.MD5, bucket, blobInfo.Metadata),
                TimeStamp = DateTimeOffset.UtcNow
            });

            if (!harness.Received.Select <RecordsFile.Sagas.Events.RecordsFileProcessed>(m => m.Context.Message.BlobId == blobId).Any())
            {
                throw new TimeoutException();
            }
            var fileProcessed = harness.Received.Select <RecordsFile.Sagas.Events.RecordsFileProcessed>(m => m.Context.Message.BlobId == blobId).First();
            var fileId        = fileProcessed.Context.Message.Id;

            if (fileProcessed.Context.Message.ProcessedRecords != harness.GetProcessedRecords(fileId).Count())
            {
                throw new Exception($"fileProcessed.Context.Message.ProcessedRecords ({fileProcessed.Context.Message.ProcessedRecords}) != Fixture.GetProcessedRecords(fileId).Count() ({harness.GetProcessedRecords(fileId).Count()})");
            }

            foreach (var recordId in harness.GetProcessedRecords(fileId))
            {
                if (!harness.Received.Select <RecordsFile.Domain.Events.Records.NodeStatusPersisted>(m => m.Context.Message.Status == RecordsFile.Domain.RecordStatus.Processed && m.Context.Message.Id == recordId).Any())
                {
                    throw new TimeoutException();
                }
                if (!harness.Received.Select <RecordsFile.Domain.Events.Records.StatusPersisted>(m => m.Context.Message.Status == RecordsFile.Domain.RecordStatus.Processed && m.Context.Message.Id == recordId).Any())
                {
                    throw new TimeoutException();
                }
            }

            if (fileProcessed.Context.Message.FailedRecords != harness.GetInvalidRecords(fileId).Count())
            {
                throw new Exception("fileProcessed.Context.Message.FailedRecords != Fixture.GetInvalidRecords(fileId).Count()");
            }

            foreach (var recordId in harness.GetInvalidRecords(fileId))
            {
                if (!harness.Received.Select <RecordsFile.Domain.Events.Records.RecordPersisted>(m => m.Context.Message.Id == recordId).Any())
                {
                    throw new TimeoutException();
                }
                if (!harness.Received.Select <RecordsFile.Domain.Events.Records.NodeRecordPersisted>(m => m.Context.Message.Id == recordId).Any())
                {
                    throw new TimeoutException();
                }
            }

            if (!harness.Received.Select <Generic.Domain.Events.Files.NodeStatusPersisted>(m => (m.Context.Message.Status == Generic.Domain.FileStatus.Processed || m.Context.Message.Status == Generic.Domain.FileStatus.Failed) && m.Context.Message.Id == fileId).Any())
            {
                throw new TimeoutException();
            }
            if (!harness.Received.Select <Generic.Domain.Events.Files.StatusPersisted>(m => (m.Context.Message.Status == Generic.Domain.FileStatus.Processed || m.Context.Message.Status == Generic.Domain.FileStatus.Failed) && m.Context.Message.Id == fileId).Any())
            {
                throw new TimeoutException();
            }

            var events = await harness.CqrsEventStore.Get(fileId, 0);

            Log.Information($"Aggregate-{fileId}: {string.Join(", ", events.Select(e => $"{e.GetType().Name} ({e.Version})"))}");

            Log.Information($"Processing finished [{fileName}]");

            return(fileId);
        }