Ejemplo n.º 1
0
        public void TestMongoExpectedFilesDoc_GetHashCode()
        {
            var expectedFiles = new HashSet <MongoExpectedFileInfoDoc>
            {
                new MongoExpectedFileInfoDoc(Guid.NewGuid(), "anon1.dcm"),
                new MongoExpectedFileInfoDoc(Guid.NewGuid(), "anon2.dcm"),
            };
            Guid jobId        = Guid.NewGuid();
            var  rejectedKeys = new MongoRejectedKeyInfoDoc(
                MongoExtractionMessageHeaderDoc.FromMessageHeader(jobId, _testHeader, _dateTimeProvider),
                new Dictionary <string, int>
            {
                { "reject-1", 1 },
                { "reject-2", 2 },
            }
                );

            var doc1 = new MongoExpectedFilesDoc(
                MongoExtractionMessageHeaderDoc.FromMessageHeader(jobId, _testHeader, _dateTimeProvider),
                "TestKey",
                expectedFiles,
                rejectedKeys);
            var doc2 = new MongoExpectedFilesDoc(
                MongoExtractionMessageHeaderDoc.FromMessageHeader(jobId, _testHeader, _dateTimeProvider),
                "TestKey",
                expectedFiles,
                rejectedKeys);

            Assert.AreEqual(doc1.GetHashCode(), doc2.GetHashCode());
        }
Ejemplo n.º 2
0
        protected override void PersistMessageToStoreImpl(ExtractFileCollectionInfoMessage message, IMessageHeader header)
        {
            if (InCompletedJobCollection(message.ExtractionJobIdentifier))
            {
                throw new ApplicationException("Received an ExtractFileCollectionInfoMessage for a job that is already completed");
            }

            MongoExpectedFilesDoc expectedFilesForKey = MongoExpectedFilesDoc.FromMessage(message, header, _dateTimeProvider);

            _database
            .GetCollection <MongoExpectedFilesDoc>(ExpectedFilesCollectionName(message.ExtractionJobIdentifier))
            .InsertOne(expectedFilesForKey);
        }
Ejemplo n.º 3
0
        public void TestMongoExpectedFilesDoc_FromMessage()
        {
            var mockMessage = new Mock <ExtractFileCollectionInfoMessage>();

            mockMessage.Object.KeyValue = "TestKey";
            Guid jobId = Guid.NewGuid();

            mockMessage.Object.ExtractionJobIdentifier = jobId;
            var header1 = new MessageHeader();
            var header2 = new MessageHeader();

            mockMessage.Object.ExtractFileMessagesDispatched = new JsonCompatibleDictionary <MessageHeader, string>
            {
                { header1, "AnonFile1.dcm" },
                { header2, "AnonFile2.dcm" },
            };
            mockMessage.Object.RejectionReasons = new Dictionary <string, int>
            {
                { "Reject1", 1 },
                { "Reject2", 2 },
            };

            MongoExpectedFilesDoc doc = MongoExpectedFilesDoc.FromMessage(mockMessage.Object, _testHeader, _dateTimeProvider);

            var expected = new MongoExpectedFilesDoc(
                MongoExtractionMessageHeaderDoc.FromMessageHeader(jobId, _testHeader, _dateTimeProvider),
                "TestKey",
                new HashSet <MongoExpectedFileInfoDoc>
            {
                new MongoExpectedFileInfoDoc(header1.MessageGuid, "AnonFile1.dcm"),
                new MongoExpectedFileInfoDoc(header2.MessageGuid, "AnonFile2.dcm"),
            },
                MongoRejectedKeyInfoDoc.FromMessage(mockMessage.Object, _testHeader, _dateTimeProvider));

            Assert.AreEqual(expected, doc);
        }
Ejemplo n.º 4
0
        public void TestCompleteJobImpl()
        {
            Guid jobId   = Guid.NewGuid();
            var  testJob = new MongoExtractJobDoc(
                jobId,
                MongoExtractionMessageHeaderDoc.FromMessageHeader(jobId, new MessageHeader(), _dateTimeProvider),
                "1234",
                ExtractJobStatus.Failed,
                "test/dir",
                _dateTimeProvider.UtcNow(),
                "SeriesInstanceUID",
                1,
                "MR",
                isIdentifiableExtraction: true,
                isNoFilterExtraction: true,
                null);
            var testMongoExpectedFilesDoc = new MongoExpectedFilesDoc(
                MongoExtractionMessageHeaderDoc.FromMessageHeader(jobId, new MessageHeader(), _dateTimeProvider),
                "1.2.3.4",
                new HashSet <MongoExpectedFileInfoDoc>
            {
                new MongoExpectedFileInfoDoc(Guid.NewGuid(), "anon1.dcm"),
            },
                new MongoRejectedKeyInfoDoc(
                    MongoExtractionMessageHeaderDoc.FromMessageHeader(jobId, new MessageHeader(), _dateTimeProvider),
                    new Dictionary <string, int>())
                );
            var testMongoFileStatusDoc = new MongoFileStatusDoc(
                MongoExtractionMessageHeaderDoc.FromMessageHeader(jobId, new MessageHeader(), _dateTimeProvider),
                "input.dcm",
                "anon1.dcm",
                true,
                false,
                ExtractedFileStatus.Anonymised,
                "Verified");

            var client = new TestMongoClient();
            var store  = new MongoExtractJobStore(client, ExtractionDatabaseName, _dateTimeProvider);

            // Assert that an exception is thrown for a non-existent job
            Assert.Throws <ApplicationException>(() => store.MarkJobCompleted(Guid.NewGuid()));
            client.MockSessionHandle.Verify(x => x.AbortTransaction(It.IsAny <CancellationToken>()), Times.Once);
            client.MockSessionHandle.Verify(x => x.CommitTransaction(It.IsAny <CancellationToken>()), Times.Never);

            // Assert that an exception is thrown for a job which is marked as failed
            client = new TestMongoClient();
            store  = new MongoExtractJobStore(client, ExtractionDatabaseName, _dateTimeProvider);
            client.ExtractionDatabase.InProgressCollection.InsertOne(testJob);
            client.MockSessionHandle.Reset();
            Assert.Throws <ApplicationException>(() => store.MarkJobCompleted(Guid.NewGuid()));
            client.MockSessionHandle.Verify(x => x.AbortTransaction(It.IsAny <CancellationToken>()), Times.Once);
            client.MockSessionHandle.Verify(x => x.CommitTransaction(It.IsAny <CancellationToken>()), Times.Never);

            // Check that we handle a failed insertion
            client            = new TestMongoClient();
            store             = new MongoExtractJobStore(client, ExtractionDatabaseName, _dateTimeProvider);
            testJob.JobStatus = ExtractJobStatus.Completed;
            client.ExtractionDatabase.InProgressCollection.InsertOne(testJob);
            client.ExtractionDatabase.CompletedJobCollection.RejectChanges = true;
            client.MockSessionHandle.Reset();
            Assert.Throws <Exception>(() => store.MarkJobCompleted(jobId));
            client.MockSessionHandle.Verify(x => x.AbortTransaction(It.IsAny <CancellationToken>()), Times.Once);
            client.MockSessionHandle.Verify(x => x.CommitTransaction(It.IsAny <CancellationToken>()), Times.Never);

            // Check we handle a bad DeleteResult
            client = new TestMongoClient();
            store  = new MongoExtractJobStore(client, ExtractionDatabaseName, _dateTimeProvider);
            client.ExtractionDatabase.InProgressCollection.RejectChanges = true;
            client.MockSessionHandle.Reset();
            Assert.Throws <ApplicationException>(() => store.MarkJobCompleted(jobId));
            client.MockSessionHandle.Verify(x => x.AbortTransaction(It.IsAny <CancellationToken>()), Times.Once);
            client.MockSessionHandle.Verify(x => x.CommitTransaction(It.IsAny <CancellationToken>()), Times.Never);

            // Check we handle missing expectedFiles collection
            client = new TestMongoClient();
            store  = new MongoExtractJobStore(client, ExtractionDatabaseName, _dateTimeProvider);
            client.ExtractionDatabase.InProgressCollection.InsertOne(testJob);
            client.MockSessionHandle.Reset();
            Assert.Throws <ApplicationException>(() => store.MarkJobCompleted(jobId));
            client.MockSessionHandle.Verify(x => x.AbortTransaction(It.IsAny <CancellationToken>()), Times.Once);
            client.MockSessionHandle.Verify(x => x.CommitTransaction(It.IsAny <CancellationToken>()), Times.Never);

            // Check we handle missing statuses collection
            client = new TestMongoClient();
            store  = new MongoExtractJobStore(client, ExtractionDatabaseName, _dateTimeProvider);
            client.ExtractionDatabase.InProgressCollection.InsertOne(testJob);
            client.ExtractionDatabase.ExpectedFilesCollections[$"expectedFiles_{jobId}"] = new MockExtractCollection <Guid, MongoExpectedFilesDoc>();
            client.ExtractionDatabase.ExpectedFilesCollections[$"expectedFiles_{jobId}"].InsertOne(testMongoExpectedFilesDoc);
            client.MockSessionHandle.Reset();
            Assert.Throws <ApplicationException>(() => store.MarkJobCompleted(jobId));
            client.MockSessionHandle.Verify(x => x.AbortTransaction(It.IsAny <CancellationToken>()), Times.Once);
            client.MockSessionHandle.Verify(x => x.CommitTransaction(It.IsAny <CancellationToken>()), Times.Never);

            // Check happy path
            client = new TestMongoClient();
            store  = new MongoExtractJobStore(client, ExtractionDatabaseName, _dateTimeProvider);
            client.ExtractionDatabase.InProgressCollection.InsertOne(testJob);
            client.ExtractionDatabase.ExpectedFilesCollections[$"expectedFiles_{jobId}"] = new MockExtractCollection <Guid, MongoExpectedFilesDoc>();
            client.ExtractionDatabase.ExpectedFilesCollections[$"expectedFiles_{jobId}"].InsertOne(testMongoExpectedFilesDoc);
            client.ExtractionDatabase.StatusCollections[$"statuses_{jobId}"] = new MockExtractCollection <Guid, MongoFileStatusDoc>();
            client.ExtractionDatabase.StatusCollections[$"statuses_{jobId}"].InsertOne(testMongoFileStatusDoc);
            client.MockSessionHandle.Reset();
            store.MarkJobCompleted(jobId);
            client.MockSessionHandle.Verify(x => x.AbortTransaction(It.IsAny <CancellationToken>()), Times.Never);
            client.MockSessionHandle.Verify(x => x.CommitTransaction(It.IsAny <CancellationToken>()), Times.Once);
            Assert.AreEqual(1, client.ExtractionDatabase.ExpectedFilesCollections.Count);
            Assert.AreEqual(1, client.ExtractionDatabase.StatusCollections.Count);
        }
Ejemplo n.º 5
0
        public void TestGetReadJobsImpl()
        {
            Guid jobId   = Guid.NewGuid();
            var  testJob = new MongoExtractJobDoc(
                jobId,
                MongoExtractionMessageHeaderDoc.FromMessageHeader(jobId, new MessageHeader(), _dateTimeProvider),
                "1234",
                ExtractJobStatus.Failed,
                "test/dir",
                _dateTimeProvider.UtcNow(),
                "SeriesInstanceUID",
                1,
                "MR",
                isIdentifiableExtraction: true,
                isNoFilterExtraction: true,
                null);
            var testMongoExpectedFilesDoc = new MongoExpectedFilesDoc(
                MongoExtractionMessageHeaderDoc.FromMessageHeader(jobId, new MessageHeader(), _dateTimeProvider),
                "1.2.3.4",
                new HashSet <MongoExpectedFileInfoDoc>
            {
                new MongoExpectedFileInfoDoc(Guid.NewGuid(), "anon1.dcm"),
            },
                new MongoRejectedKeyInfoDoc(
                    MongoExtractionMessageHeaderDoc.FromMessageHeader(jobId, new MessageHeader(), _dateTimeProvider),
                    new Dictionary <string, int>())
                );
            var testMongoFileStatusDoc = new MongoFileStatusDoc(
                MongoExtractionMessageHeaderDoc.FromMessageHeader(jobId, new MessageHeader(), _dateTimeProvider),
                "input.dcm",
                "anon1.dcm",
                true,
                false,
                ExtractedFileStatus.Anonymised,
                "Verified");

            var client = new TestMongoClient();
            var store  = new MongoExtractJobStore(client, ExtractionDatabaseName, _dateTimeProvider);

            // Assert that jobs marked as failed are not returned
            client.ExtractionDatabase.InProgressCollection.InsertOne(testJob);
            client.ExtractionDatabase.InProgressCollection.RejectChanges = true;
            Assert.AreEqual(0, store.GetReadyJobs().Count);

            // Assert that an in progress job is not returned
            client            = new TestMongoClient();
            store             = new MongoExtractJobStore(client, ExtractionDatabaseName, _dateTimeProvider);
            testJob.JobStatus = ExtractJobStatus.WaitingForCollectionInfo;
            client.ExtractionDatabase.InProgressCollection.InsertOne(testJob);
            client.ExtractionDatabase.InProgressCollection.RejectChanges = true;
            Assert.AreEqual(0, store.GetReadyJobs().Count);

            // Check we handle a bad ReplaceOneResult
            client            = new TestMongoClient();
            store             = new MongoExtractJobStore(client, ExtractionDatabaseName, _dateTimeProvider);
            testJob.JobStatus = ExtractJobStatus.WaitingForCollectionInfo;
            client.ExtractionDatabase.InProgressCollection.InsertOne(testJob);
            client.ExtractionDatabase.InProgressCollection.RejectChanges = true;
            client.ExtractionDatabase.ExpectedFilesCollections[$"expectedFiles_{jobId}"] = new MockExtractCollection <Guid, MongoExpectedFilesDoc>();
            client.ExtractionDatabase.ExpectedFilesCollections[$"expectedFiles_{jobId}"].InsertOne(testMongoExpectedFilesDoc);
            Assert.Throws <ApplicationException>(() => store.GetReadyJobs());

            // Check happy path
            client            = new TestMongoClient();
            store             = new MongoExtractJobStore(client, ExtractionDatabaseName, _dateTimeProvider);
            testJob.JobStatus = ExtractJobStatus.WaitingForCollectionInfo;
            client.ExtractionDatabase.InProgressCollection.InsertOne(testJob);
            client.ExtractionDatabase.ExpectedFilesCollections[$"expectedFiles_{jobId}"] = new MockExtractCollection <Guid, MongoExpectedFilesDoc>();
            client.ExtractionDatabase.ExpectedFilesCollections[$"expectedFiles_{jobId}"].InsertOne(testMongoExpectedFilesDoc);
            Assert.AreEqual(0, store.GetReadyJobs().Count);
            Assert.AreEqual(ExtractJobStatus.WaitingForStatuses, client.ExtractionDatabase.InProgressCollection.Documents.Single().Value.JobStatus);
            client.ExtractionDatabase.StatusCollections[$"statuses_{jobId}"] = new MockExtractCollection <Guid, MongoFileStatusDoc>();
            client.ExtractionDatabase.StatusCollections[$"statuses_{jobId}"].InsertOne(testMongoFileStatusDoc);
            ExtractJobInfo job = store.GetReadyJobs().Single();

            Assert.AreEqual(ExtractJobStatus.ReadyForChecks, job.JobStatus);
            Assert.AreEqual(ExtractJobStatus.ReadyForChecks, client.ExtractionDatabase.InProgressCollection.Documents.Single().Value.JobStatus);
        }
Ejemplo n.º 6
0
        public void TestPersistMessageToStoreImpl_ExtractFileCollectionInfoMessage()
        {
            Guid jobId   = Guid.NewGuid();
            var  header1 = new MessageHeader();
            var  header2 = new MessageHeader();
            var  testExtractFileCollectionInfoMessage = new ExtractFileCollectionInfoMessage
            {
                ExtractionJobIdentifier = jobId,
                ProjectNumber           = "1234-5678",
                RejectionReasons        = new Dictionary <string, int>
                {
                    { "reject1", 1 },
                    { "reject2", 2 },
                },
                JobSubmittedAt                = DateTime.UtcNow,
                ExtractionDirectory           = "1234-5678/testExtract",
                ExtractFileMessagesDispatched = new JsonCompatibleDictionary <MessageHeader, string>
                {
                    { header1, "file1" },
                    { header2, "file2" }
                },
                KeyValue = "series-1",
            };
            var header = new MessageHeader
            {
                MessageGuid = Guid.NewGuid(),
                OriginalPublishTimestamp = MessageHeader.UnixTimeNow(),
                Parents = new[] { Guid.NewGuid(), },
                ProducerExecutableName = "MongoExtractStoreTests",
                ProducerProcessID      = 1234,
            };

            var client = new TestMongoClient();
            var store  = new MongoExtractJobStore(client, ExtractionDatabaseName, _dateTimeProvider);

            store.PersistMessageToStore(testExtractFileCollectionInfoMessage, header);

            Dictionary <Guid, MongoExpectedFilesDoc> docs = client.ExtractionDatabase.ExpectedFilesCollections[$"expectedFiles_{jobId}"].Documents;

            Assert.AreEqual(docs.Count, 1);
            MongoExpectedFilesDoc extractJob = docs.Values.ToList()[0];

            var expected = new MongoExpectedFilesDoc(
                MongoExtractionMessageHeaderDoc.FromMessageHeader(jobId, header, _dateTimeProvider),
                "series-1",
                new HashSet <MongoExpectedFileInfoDoc>
            {
                new MongoExpectedFileInfoDoc(header1.MessageGuid, "file1"),
                new MongoExpectedFileInfoDoc(header2.MessageGuid, "file2"),
            },
                new MongoRejectedKeyInfoDoc(
                    MongoExtractionMessageHeaderDoc.FromMessageHeader(jobId, header, _dateTimeProvider),
                    new Dictionary <string, int>
            {
                { "reject1", 1 },
                { "reject2", 2 },
            })
                );

            Assert.True(extractJob.Equals(expected));
        }