public async Task GatewayBadData()
        {
            var receivePort = 141;

            var testAETConfigModel = GetTestAETConfigModel();

            using (var segmentationClient = GetMockInnerEyeSegmentationClient())
                using (var deleteService = CreateDeleteService())
                    using (var pushService = CreatePushService())
                        using (var downloadService = CreateDownloadService(segmentationClient))
                            using (var uploadService = CreateUploadService(segmentationClient))
                                using (var receiveService = CreateReceiveService(receivePort))
                                    using (var uploadQueue = receiveService.UploadQueue)
                                    {
                                        deleteService.Start();
                                        pushService.Start();
                                        downloadService.Start();
                                        uploadService.Start();
                                        receiveService.Start();

                                        DcmtkHelpers.SendFileUsingDCMTK(
                                            @"Images\LargeSeriesWithContour\rtstruct.dcm",
                                            receivePort,
                                            ScuProfile.LEExplicitCT,
                                            TestContext,
                                            applicationEntityTitle: testAETConfigModel.CallingAET,
                                            calledAETitle: testAETConfigModel.CalledAET);

                                        await Task.Delay(1000).ConfigureAwait(false);

                                        WaitUntilNoMessagesOnQueue(uploadQueue);

                                        Assert.ThrowsException <MessageQueueReadException>(() => TransactionalDequeue <UploadQueueItem>(uploadQueue));
                                    }
        }
Example #2
0
        public void ReceiveServiceAPIDownTest()
        {
            var callingAet = "ProstateRTMl";

            var mockReceiverConfigurationProvider = new MockConfigurationProvider <ReceiveServiceConfig>();
            var client = GetMockInnerEyeSegmentationClient();

            var gatewayReceiveConfig = GetTestGatewayReceiveServiceConfig(140);

            mockReceiverConfigurationProvider.ConfigurationQueue.Clear();
            mockReceiverConfigurationProvider.ConfigurationQueue.Enqueue(gatewayReceiveConfig);

            using (var receiveService = CreateReceiveService(mockReceiverConfigurationProvider.GetConfiguration))
                using (var uploadQueue = receiveService.UploadQueue)
                {
                    receiveService.Start();

                    // This should cause an exception to be raised in ReceiveService.GetAcceptedSopClassesAndTransferSyntaxes
                    mockReceiverConfigurationProvider.TestException = new Exception("A general exception.");

                    uploadQueue.Clear();

                    DcmtkHelpers.SendFileUsingDCMTK(
                        @"Images\1ValidSmall\1.dcm",
                        gatewayReceiveConfig.GatewayDicomEndPoint.Port,
                        ScuProfile.LEExplicitCT,
                        TestContext,
                        applicationEntityTitle: callingAet,
                        calledAETitle: gatewayReceiveConfig.GatewayDicomEndPoint.Title);

                    var receiveQueueItem = TransactionalDequeue <UploadQueueItem>(uploadQueue, 10000);

                    Assert.IsNotNull(receiveQueueItem);
                    Assert.AreEqual(callingAet, receiveQueueItem.CallingApplicationEntityTitle);
                    Assert.AreEqual(gatewayReceiveConfig.GatewayDicomEndPoint.Title, receiveQueueItem.CalledApplicationEntityTitle);

                    Assert.IsFalse(string.IsNullOrEmpty(receiveQueueItem.AssociationFolderPath));

                    var saveDirectoryInfo = new DirectoryInfo(receiveQueueItem.AssociationFolderPath);

                    Assert.IsTrue(saveDirectoryInfo.Exists);

                    var files = saveDirectoryInfo.GetFiles();

                    // Check we received one file over this association
                    Assert.AreEqual(1, files.Length);

                    // Attempt to get another item from the queue
                    Assert.ThrowsException <MessageQueueReadException>(() => TransactionalDequeue <UploadQueueItem>(uploadQueue));
                }
        }
Example #3
0
        public async Task DicomDataReceiverServerStarts()
        {
            var applicationEntity = new GatewayApplicationEntity("RListenerTest", 191, "127.0.0.1");
            var resultsDirectory  = CreateTemporaryDirectory();

            using (var dicomDataReceiver = new ListenerDataReceiver(new ListenerDicomSaver(resultsDirectory.FullName)))
            {
                StartDicomDataReceiver(dicomDataReceiver, applicationEntity.Port);

                var eventCount = 0;
                var folderPath = string.Empty;

                dicomDataReceiver.DataReceived += (sender, e) =>
                {
                    folderPath = e.FolderPath;
                    Interlocked.Increment(ref eventCount);
                };

                Assert.ThrowsException <DicomNetworkException>(() => StartDicomDataReceiver(dicomDataReceiver, applicationEntity.Port));

                var dataSender = new DicomDataSender();
                var echoResult = await dataSender.DicomEchoAsync(
                    "RListener",
                    applicationEntity.Title,
                    applicationEntity.Port,
                    applicationEntity.IpAddress);

                // Check echo
                Assert.IsTrue(echoResult == DicomOperationResult.Success);

                DcmtkHelpers.SendFileUsingDCMTK(
                    @"Images\1ValidSmall\1.dcm",
                    applicationEntity.Port,
                    ScuProfile.LEExplicitCT,
                    TestContext);

                // Wait for all events to finish on the data received
                SpinWait.SpinUntil(() => eventCount >= 3, TimeSpan.FromSeconds(10));

                // Check the file exists
                Assert.IsTrue(File.Exists(Path.Combine(folderPath, @"1.2.840.113619.2.81.290.1.36662.3.1.20151027.220159.dcm")));

                dicomDataReceiver.StopServer();
            }
        }
        public async Task ReceiveServiceEchoTest()
        {
            var testAETConfigModel = GetTestAETConfigModel();
            var receivePort        = 180;

            using (var receiveService = CreateReceiveService(receivePort))
                using (var uploadQueue = receiveService.UploadQueue)
                {
                    uploadQueue.Clear();
                    receiveService.Start();

                    var sender = new DicomDataSender();

                    await sender.DicomEchoAsync(
                        "Hello",
                        testAETConfigModel.CalledAET,
                        receivePort,
                        "127.0.0.1").ConfigureAwait(false);

                    // Check nothing is added to the message queue
                    Assert.ThrowsException <MessageQueueReadException>(() => TransactionalDequeue <UploadQueueItem>(uploadQueue, timeoutMs: 1000));

                    // Now check when we send a file a message is added
                    DcmtkHelpers.SendFileUsingDCMTK(
                        @"Images\1ValidSmall\1.dcm",
                        receivePort,
                        ScuProfile.LEExplicitCT,
                        TestContext,
                        applicationEntityTitle: testAETConfigModel.CallingAET,
                        calledAETitle: testAETConfigModel.CalledAET);

                    Assert.IsNotNull(TransactionalDequeue <UploadQueueItem>(uploadQueue, timeoutMs: 1000));

                    // Now try another Dicom echo
                    await sender.DicomEchoAsync(
                        "Hello",
                        testAETConfigModel.CalledAET,
                        receivePort,
                        "127.0.0.1").ConfigureAwait(false);

                    // Check nothing is added to the message queue
                    Assert.ThrowsException <MessageQueueReadException>(() => TransactionalDequeue <UploadQueueItem>(uploadQueue, timeoutMs: 1000));
                }
        }
Example #5
0
        public void DicomDataReceiverMutlipleAssociations()
        {
            const int numberOfAssociations = 10;
            const int port = 122;

            var applicationEntity = new GatewayApplicationEntity("RListenerTest", port, "127.0.0.1");
            var resultsDirectory  = CreateTemporaryDirectory();

            using (var dicomDataReceiver = new ListenerDataReceiver(new ListenerDicomSaver(resultsDirectory.FullName)))
            {
                StartDicomDataReceiver(dicomDataReceiver, applicationEntity.Port);

                var associationsReceivedCount = 0;
                var receivedAssociations      = new string[numberOfAssociations];

                dicomDataReceiver.DataReceived += (sender, e) =>
                {
                    if (e.ProgressCode == DicomReceiveProgressCode.AssociationEstablished)
                    {
                        receivedAssociations[int.Parse(e.DicomAssociation.CallingAE)] = e.DicomAssociation.CallingAE;
                        Interlocked.Increment(ref associationsReceivedCount);
                    }
                };

                Parallel.For(0, numberOfAssociations, i =>
                {
                    var dcmtkResult = DcmtkHelpers.SendFileUsingDCMTK(
                        @"Images\1ValidSmall\1.dcm",
                        port,
                        ScuProfile.LEExplicitCT,
                        TestContext,
                        waitForExit: false,
                        applicationEntityTitle: i.ToString());
                });

                SpinWait.SpinUntil(() => associationsReceivedCount == numberOfAssociations, TimeSpan.FromMinutes(1));

                for (var i = 0; i < numberOfAssociations; i++)
                {
                    Assert.IsTrue(receivedAssociations[i] == i.ToString());
                }
            }
        }
Example #6
0
        public void ReceiveServiceLiveEndToEndTest()
        {
            var receivePort = 160;

            var callingAet = "ProstateRTMl";
            var calledAet  = "testname";

            using (var receiveService = CreateReceiveService(receivePort))
                using (var uploadQueue = receiveService.UploadQueue)
                {
                    uploadQueue.Clear();    // Clear the message queue
                    receiveService.Start(); // Start the service

                    DcmtkHelpers.SendFileUsingDCMTK(
                        @"Images\1ValidSmall\1.dcm",
                        receivePort,
                        ScuProfile.LEExplicitCT,
                        TestContext,
                        applicationEntityTitle: callingAet,
                        calledAETitle: calledAet);

                    var receiveQueueItem = TransactionalDequeue <UploadQueueItem>(uploadQueue, timeoutMs: 20 * 1000);

                    Assert.IsNotNull(receiveQueueItem);
                    Assert.AreEqual(callingAet, receiveQueueItem.CallingApplicationEntityTitle);
                    Assert.AreEqual(calledAet, receiveQueueItem.CalledApplicationEntityTitle);

                    Assert.IsFalse(string.IsNullOrEmpty(receiveQueueItem.AssociationFolderPath));

                    var saveDirectoryInfo = new DirectoryInfo(receiveQueueItem.AssociationFolderPath);

                    Assert.IsTrue(saveDirectoryInfo.Exists);

                    var files = saveDirectoryInfo.GetFiles();

                    // Check we received one file over this association
                    Assert.AreEqual(1, files.Length);

                    // Attempt to get another item from the queue
                    Assert.ThrowsException <MessageQueueReadException>(() => TransactionalDequeue <UploadQueueItem>(uploadQueue));
                }
        }
Example #7
0
        public async Task GenerateAndTestDeAnonymisedStructureSetFile()
        {
            var image      = @"Images\LargeSeriesWithContour";
            var tempFolder = CreateTemporaryDirectory();

            var segmentationClient = GetMockInnerEyeSegmentationClient();

            var configType   = AETConfigType.ModelWithResultDryRun;
            var dryRunFolder = DryRunFolders.GetFolder(configType);

            var testAETConfigModel = GetTestAETConfigModel();

            var newTestAETConfigModel = testAETConfigModel.With(
                aetConfig: new ClientAETConfig(
                    new AETConfig(
                        configType,
                        testAETConfigModel.AETConfig.Config.ModelsConfig),
                    testAETConfigModel.AETConfig.Destination,
                    false));

            var aetConfigProvider = new MockAETConfigProvider(newTestAETConfigModel);

            var receivePort = 160;

            using (var deleteService = CreateDeleteService())
                using (var pushService = CreatePushService(aetConfigProvider.AETConfigModels))
                    using (var downloadService = CreateDownloadService(segmentationClient))
                        using (var uploadService = CreateUploadService(segmentationClient, aetConfigProvider.AETConfigModels))
                            using (var receiveService = CreateReceiveService(receivePort, tempFolder))
                            {
                                deleteService.Start();
                                pushService.Start();
                                downloadService.Start();
                                uploadService.Start();
                                receiveService.Start();

                                DcmtkHelpers.SendFolderUsingDCMTK(
                                    image,
                                    receivePort,
                                    ScuProfile.LEExplicitCT,
                                    TestContext,
                                    applicationEntityTitle: newTestAETConfigModel.CallingAET,
                                    calledAETitle: newTestAETConfigModel.CalledAET);

                                SpinWait.SpinUntil(() => tempFolder.GetDirectories().FirstOrDefault(x => x.FullName.Contains(dryRunFolder)) != null);

                                var dryRunFolderDirectory = tempFolder.GetDirectories().First(x => x.FullName.Contains(dryRunFolder)).GetDirectories().First();

                                // Wait until we have all image files.
                                SpinWait.SpinUntil(() => dryRunFolderDirectory.GetFiles().Length == 1);

                                // Wait for all files to save.
                                await Task.Delay(1000);

                                var originalSlice = DicomFile.Open(new DirectoryInfo(image).GetFiles().First().FullName);

                                Assert.IsNotNull(originalSlice);

                                foreach (var file in dryRunFolderDirectory.GetFiles())
                                {
                                    var dicomFile = DicomFile.Open(file.FullName, FileReadOption.ReadAll);

                                    Assert.AreEqual(originalSlice.Dataset.GetSingleValue <string>(DicomTag.StudyDate), dicomFile.Dataset.GetSingleValue <string>(DicomTag.StudyDate));
                                    Assert.AreEqual(originalSlice.Dataset.GetSingleValue <string>(DicomTag.AccessionNumber), dicomFile.Dataset.GetSingleValue <string>(DicomTag.AccessionNumber));
                                    Assert.AreEqual("RTSTRUCT", dicomFile.Dataset.GetSingleValue <string>(DicomTag.Modality));
                                    Assert.AreEqual("Microsoft Corporation", dicomFile.Dataset.GetSingleValue <string>(DicomTag.Manufacturer));
                                    Assert.AreEqual(originalSlice.Dataset.GetSingleValue <string>(DicomTag.ReferringPhysicianName), dicomFile.Dataset.GetSingleValue <string>(DicomTag.ReferringPhysicianName));
                                    Assert.AreEqual(originalSlice.Dataset.GetSingleValueOrDefault(DicomTag.StudyDescription, string.Empty), dicomFile.Dataset.GetSingleValueOrDefault(DicomTag.StudyDescription, string.Empty));
                                    Assert.AreEqual(originalSlice.Dataset.GetSingleValue <string>(DicomTag.PatientName), dicomFile.Dataset.GetSingleValue <string>(DicomTag.PatientName));
                                    Assert.AreEqual(originalSlice.Dataset.GetSingleValue <string>(DicomTag.PatientID), dicomFile.Dataset.GetSingleValue <string>(DicomTag.PatientID));
                                    Assert.AreEqual(originalSlice.Dataset.GetSingleValue <string>(DicomTag.PatientBirthDate), dicomFile.Dataset.GetSingleValue <string>(DicomTag.PatientBirthDate));
                                    Assert.AreEqual(originalSlice.Dataset.GetSingleValue <string>(DicomTag.StudyInstanceUID), dicomFile.Dataset.GetSingleValue <string>(DicomTag.StudyInstanceUID));
                                    Assert.AreEqual(originalSlice.Dataset.GetSingleValue <string>(DicomTag.StudyID), dicomFile.Dataset.GetSingleValue <string>(DicomTag.StudyID));

                                    Assert.IsTrue(dicomFile.Dataset.GetString(DicomTag.SoftwareVersions).StartsWith("Microsoft InnerEye Gateway:"));
                                    Assert.IsTrue(dicomFile.Dataset.GetValue <string>(DicomTag.SoftwareVersions, 1).StartsWith("InnerEye AI Model:"));
                                    Assert.IsTrue(dicomFile.Dataset.GetValue <string>(DicomTag.SoftwareVersions, 2).StartsWith("InnerEye AI Model ID:"));
                                    Assert.IsTrue(dicomFile.Dataset.GetValue <string>(DicomTag.SoftwareVersions, 3).StartsWith("InnerEye Model Created:"));
                                    Assert.IsTrue(dicomFile.Dataset.GetValue <string>(DicomTag.SoftwareVersions, 4).StartsWith("InnerEye Version:"));

                                    Assert.AreEqual("1.2.840.10008.5.1.4.1.1.481.3", dicomFile.Dataset.GetSingleValue <string>(DicomTag.SOPClassUID));
                                    Assert.AreEqual($"{DateTime.UtcNow.Year}{DateTime.UtcNow.Month.ToString("D2")}{DateTime.UtcNow.Day.ToString("D2")}", dicomFile.Dataset.GetSingleValue <string>(DicomTag.SeriesDate));
                                    Assert.IsTrue(dicomFile.Dataset.GetSingleValueOrDefault(DicomTag.SeriesInstanceUID, string.Empty).StartsWith("1.2.826.0.1.3680043.2"));
                                    Assert.AreEqual("511091532", dicomFile.Dataset.GetSingleValueOrDefault(DicomTag.SeriesNumber, string.Empty));
                                    Assert.AreEqual("NOT FOR CLINICAL USE", dicomFile.Dataset.GetSingleValueOrDefault(DicomTag.SeriesDescription, string.Empty));
                                    Assert.IsTrue(dicomFile.Dataset.GetSingleValueOrDefault(DicomTag.SOPInstanceUID, string.Empty).StartsWith("1.2.826.0.1.3680043.2"));
                                    Assert.AreEqual("ANONYM", dicomFile.Dataset.GetSingleValueOrDefault(DicomTag.OperatorsName, string.Empty));

                                    VerifyDicomFile(file.FullName);
                                }
                            }
        }
Example #8
0
        public async Task GenerateAndTestAnonymisedRTFile()
        {
            var tempFolder = CreateTemporaryDirectory();

            {
                var configType   = AETConfigType.ModelDryRun;
                var dryRunFolder = DryRunFolders.GetFolder(configType);

                var segmentationClient = GetMockInnerEyeSegmentationClient();

                var testAETConfigModel    = GetTestAETConfigModel();
                var newTestAETConfigModel = testAETConfigModel.With(
                    aetConfig: new ClientAETConfig(
                        new AETConfig(
                            configType,
                            null),
                        testAETConfigModel.AETConfig.Destination,
                        false));

                var aetConfigProvider = new MockAETConfigProvider(newTestAETConfigModel);

                var receivePort = 162;

                using (var deleteService = CreateDeleteService())
                    using (var pushService = CreatePushService(aetConfigProvider.AETConfigModels))
                        using (var downloadService = CreateDownloadService(segmentationClient))
                            using (var uploadService = CreateUploadService(segmentationClient, aetConfigProvider.AETConfigModels))
                                using (var receiveService = CreateReceiveService(receivePort, tempFolder))
                                {
                                    deleteService.Start();
                                    pushService.Start();
                                    downloadService.Start();
                                    uploadService.Start();
                                    receiveService.Start();

                                    DcmtkHelpers.SendFolderUsingDCMTK(
                                        @"Images\1ValidSmall",
                                        receivePort,
                                        ScuProfile.LEExplicitRTCT,
                                        TestContext,
                                        applicationEntityTitle: newTestAETConfigModel.CallingAET,
                                        calledAETitle: newTestAETConfigModel.CalledAET);

                                    SpinWait.SpinUntil(() => tempFolder.GetDirectories().FirstOrDefault(x => x.FullName.Contains(dryRunFolder)) != null);

                                    var dryRunFolderDirectory = tempFolder.GetDirectories().First(x => x.FullName.Contains(dryRunFolder)).GetDirectories().First();

                                    // Wait until we have all image files.
                                    SpinWait.SpinUntil(() => dryRunFolderDirectory.GetFiles().Length == 1);

                                    // Wait for all files to save.
                                    await Task.Delay(1000);

                                    var savedSampleFile = false;

                                    foreach (var file in dryRunFolderDirectory.GetFiles())
                                    {
                                        var dicomFile = DicomFile.Open(file.FullName);

                                        AssertDicomFileIsAnonymised(dicomFile);

                                        if (!savedSampleFile)
                                        {
                                            WriteDicomFileForBuildPackage("AnonymisedRT.dcm", dicomFile);
                                            savedSampleFile = true;
                                        }
                                    }
                                }
            }
        }
        public void ReceiveServiceRestartTest()
        {
            var testAETConfigModel = GetTestAETConfigModel();

            var configurationDirectory = CreateTemporaryDirectory().FullName;

            var expectedGatewayReceiveConfig1 = TestGatewayReceiveConfigProvider.Config.With(
                receiveServiceConfig: GetTestGatewayReceiveServiceConfig(110),
                configurationServiceConfig: new ConfigurationServiceConfig(
                    configurationRefreshDelaySeconds: 1));

            ConfigurationProviderTests.Serialise(expectedGatewayReceiveConfig1, configurationDirectory, GatewayReceiveConfigProvider.GatewayReceiveConfigFileName);

            using (var client = GetMockInnerEyeSegmentationClient())
                using (var gatewayReceiveConfigProvider = CreateGatewayReceiveConfigProvider(configurationDirectory))
                    using (var receiveService = CreateReceiveService(gatewayReceiveConfigProvider.ReceiveServiceConfig))
                        using (var uploadQueue = receiveService.UploadQueue)
                            using (var configurationService = CreateConfigurationService(
                                       client,
                                       gatewayReceiveConfigProvider.ConfigurationServiceConfig,
                                       receiveService))
                            {
                                // Start the service
                                configurationService.Start();

                                uploadQueue.Clear(); // Clear the message queue

                                var expectedGatewayReceiveConfig2 = TestGatewayReceiveConfigProvider.Config.With(
                                    receiveServiceConfig: GetTestGatewayReceiveServiceConfig(111),
                                    configurationServiceConfig: new ConfigurationServiceConfig(
                                        expectedGatewayReceiveConfig1.ConfigurationServiceConfig.ConfigCreationDateTime.AddSeconds(5),
                                        expectedGatewayReceiveConfig1.ConfigurationServiceConfig.ApplyConfigDateTime.AddSeconds(10)));

                                ConfigurationProviderTests.Serialise(expectedGatewayReceiveConfig2, configurationDirectory, GatewayReceiveConfigProvider.GatewayReceiveConfigFileName);

                                SpinWait.SpinUntil(() => receiveService.StartCount == 2);

                                // Send on the old config
                                var result = DcmtkHelpers.SendFileUsingDCMTK(
                                    @"Images\1ValidSmall\1.dcm",
                                    expectedGatewayReceiveConfig1.ReceiveServiceConfig.GatewayDicomEndPoint.Port,
                                    ScuProfile.LEExplicitCT,
                                    TestContext,
                                    applicationEntityTitle: testAETConfigModel.CallingAET,
                                    calledAETitle: testAETConfigModel.CalledAET);

                                // Check this did not send on the old config
                                Assert.IsFalse(string.IsNullOrWhiteSpace(result));

                                // Send on the new config
                                result = DcmtkHelpers.SendFileUsingDCMTK(
                                    @"Images\1ValidSmall\1.dcm",
                                    expectedGatewayReceiveConfig2.ReceiveServiceConfig.GatewayDicomEndPoint.Port,
                                    ScuProfile.LEExplicitCT,
                                    TestContext,
                                    applicationEntityTitle: testAETConfigModel.CallingAET,
                                    calledAETitle: testAETConfigModel.CalledAET);

                                // Check this did send on the new config
                                Assert.IsTrue(string.IsNullOrWhiteSpace(result));

                                var receiveQueueItem = TransactionalDequeue <UploadQueueItem>(uploadQueue);

                                Assert.IsNotNull(receiveQueueItem);
                                Assert.AreEqual(testAETConfigModel.CallingAET, receiveQueueItem.CallingApplicationEntityTitle);
                                Assert.AreEqual(testAETConfigModel.CalledAET, receiveQueueItem.CalledApplicationEntityTitle);

                                Assert.IsFalse(string.IsNullOrEmpty(receiveQueueItem.AssociationFolderPath));

                                var saveDirectoryInfo = new DirectoryInfo(receiveQueueItem.AssociationFolderPath);

                                Assert.IsTrue(saveDirectoryInfo.Exists);

                                var files = saveDirectoryInfo.GetFiles();

                                // Check we received one file over this association
                                Assert.AreEqual(1, files.Length);

                                // Attempt to get another item from the queue
                                Assert.ThrowsException <MessageQueueReadException>(() => TransactionalDequeue <UploadQueueItem>(uploadQueue));
                            }
        }
        public async Task GatewayLiveEndToEndTest()
        {
            var testAETConfigModel = GetTestAETConfigModel();

            var resultDirectory = CreateTemporaryDirectory();

            using (var dicomDataReceiver = new ListenerDataReceiver(new ListenerDicomSaver(resultDirectory.FullName)))
            {
                var eventCount = 0;
                var folderPath = string.Empty;

                dicomDataReceiver.DataReceived += (sender, e) =>
                {
                    folderPath = e.FolderPath;
                    Interlocked.Increment(ref eventCount);
                };

                StartDicomDataReceiver(dicomDataReceiver, testAETConfigModel.AETConfig.Destination.Port);

                var receivePort = 141;

                using (var segmentationClient = GetMockInnerEyeSegmentationClient())
                    using (var deleteService = CreateDeleteService())
                        using (var pushService = CreatePushService())
                            using (var downloadService = CreateDownloadService(segmentationClient))
                                using (var uploadService = CreateUploadService(segmentationClient))
                                    using (var receiveService = CreateReceiveService(receivePort))
                                    {
                                        deleteService.Start();
                                        pushService.Start();
                                        downloadService.Start();
                                        uploadService.Start();
                                        receiveService.Start();

                                        var dicomDataSender = new DicomDataSender();
                                        var echoResult      = await dicomDataSender.DicomEchoAsync(
                                            testAETConfigModel.CallingAET,
                                            testAETConfigModel.CalledAET,
                                            receivePort,
                                            "127.0.0.1").ConfigureAwait(false);

                                        Assert.IsTrue(echoResult == DicomOperationResult.Success);

                                        DcmtkHelpers.SendFolderUsingDCMTK(
                                            @"Images\1ValidSmall",
                                            receivePort,
                                            ScuProfile.LEExplicitCT,
                                            TestContext,
                                            applicationEntityTitle: testAETConfigModel.CallingAET,
                                            calledAETitle: testAETConfigModel.CalledAET);

                                        // Wait for all events to finish on the data received
                                        SpinWait.SpinUntil(() => eventCount >= 3, TimeSpan.FromMinutes(3));

#pragma warning disable CA1508 // Avoid dead conditional code
                                        Assert.IsFalse(string.IsNullOrWhiteSpace(folderPath));
#pragma warning restore CA1508 // Avoid dead conditional code

                                        var dicomFile = await DicomFile.OpenAsync(new DirectoryInfo(folderPath).GetFiles()[0].FullName).ConfigureAwait(false);

                                        Assert.IsNotNull(dicomFile);
                                    }
            }
        }
Example #11
0
        public void ReceiveServiceRestartTest()
        {
            var callingAet = "ProstateRTMl";

            var client = GetMockInnerEyeSegmentationClient();


            var mockConfigurationServiceConfigProvider = new MockConfigurationProvider <ConfigurationServiceConfig>();

            var configurationServiceConfig1 = new ConfigurationServiceConfig(
                configurationRefreshDelaySeconds: 1);

            var configurationServiceConfig2 = new ConfigurationServiceConfig(
                configurationServiceConfig1.ConfigCreationDateTime.AddSeconds(5),
                configurationServiceConfig1.ApplyConfigDateTime.AddSeconds(10));

            mockConfigurationServiceConfigProvider.ConfigurationQueue.Clear();
            mockConfigurationServiceConfigProvider.ConfigurationQueue.Enqueue(configurationServiceConfig1);
            mockConfigurationServiceConfigProvider.ConfigurationQueue.Enqueue(configurationServiceConfig2);

            var mockReceiverConfigurationProvider2 = new MockConfigurationProvider <ReceiveServiceConfig>();

            var testReceiveServiceConfig1 = GetTestGatewayReceiveServiceConfig(110);
            var testReceiveServiceConfig2 = GetTestGatewayReceiveServiceConfig(111);

            mockReceiverConfigurationProvider2.ConfigurationQueue.Clear();
            mockReceiverConfigurationProvider2.ConfigurationQueue.Enqueue(testReceiveServiceConfig1);
            mockReceiverConfigurationProvider2.ConfigurationQueue.Enqueue(testReceiveServiceConfig2);

            using (var receiveService = CreateReceiveService(mockReceiverConfigurationProvider2.GetConfiguration))
                using (var uploadQueue = receiveService.UploadQueue)
                    using (var configurationService = CreateConfigurationService(
                               client,
                               mockConfigurationServiceConfigProvider.GetConfiguration,
                               receiveService))
                    {
                        // Start the service
                        configurationService.Start();

                        uploadQueue.Clear(); // Clear the message queue

                        SpinWait.SpinUntil(() => receiveService.StartCount == 2);

                        // Send on the new config
                        var result = DcmtkHelpers.SendFileUsingDCMTK(
                            @"Images\1ValidSmall\1.dcm",
                            testReceiveServiceConfig1.GatewayDicomEndPoint.Port,
                            ScuProfile.LEExplicitCT,
                            TestContext,
                            applicationEntityTitle: callingAet,
                            calledAETitle: testReceiveServiceConfig1.GatewayDicomEndPoint.Title);

                        // Check this did send on the old config
                        Assert.IsFalse(string.IsNullOrWhiteSpace(result));

                        // Send on the new config
                        result = DcmtkHelpers.SendFileUsingDCMTK(
                            @"Images\1ValidSmall\1.dcm",
                            testReceiveServiceConfig2.GatewayDicomEndPoint.Port,
                            ScuProfile.LEExplicitCT,
                            TestContext,
                            applicationEntityTitle: callingAet,
                            calledAETitle: testReceiveServiceConfig2.GatewayDicomEndPoint.Title);

                        // Check this did send on the new config
                        Assert.IsTrue(string.IsNullOrWhiteSpace(result));

                        var receiveQueueItem = TransactionalDequeue <UploadQueueItem>(uploadQueue);

                        Assert.IsNotNull(receiveQueueItem);
                        Assert.AreEqual(callingAet, receiveQueueItem.CallingApplicationEntityTitle);
                        Assert.AreEqual(testReceiveServiceConfig2.GatewayDicomEndPoint.Title, receiveQueueItem.CalledApplicationEntityTitle);

                        Assert.IsFalse(string.IsNullOrEmpty(receiveQueueItem.AssociationFolderPath));

                        var saveDirectoryInfo = new DirectoryInfo(receiveQueueItem.AssociationFolderPath);

                        Assert.IsTrue(saveDirectoryInfo.Exists);

                        var files = saveDirectoryInfo.GetFiles();

                        // Check we received one file over this association
                        Assert.AreEqual(1, files.Length);

                        // Attempt to get another item from the queue
                        Assert.ThrowsException <MessageQueueReadException>(() => TransactionalDequeue <UploadQueueItem>(uploadQueue));
                    }
        }
Example #12
0
        public async Task IntegrationTestEndToEnd()
        {
            var sourceDirectory = CreateTemporaryDirectory();
            var resultDirectory = CreateTemporaryDirectory();

            var random = new Random();

            // Get file names for all in directory
            var sourceDicomFileNames = new DirectoryInfo(@"Images\HN").GetFiles().ToArray();
            // Load all DICOM files
            var sourceDicomFiles = sourceDicomFileNames.Select(f => DicomFile.Open(f.FullName, FileReadOption.ReadAll)).ToArray();

            // Add/Update random tags for the source DICOM files.
            DicomAnonymisationTests.AddRandomTags(random, sourceDicomFiles);

            // Save them all to the sourceDirectory.
            var sourcePairs = sourceDicomFileNames.Zip(sourceDicomFiles, (f, d) => Tuple.Create(f, d)).ToArray();

            foreach (var sourcePair in sourcePairs)
            {
                var sourceImageFilePath = Path.Combine(sourceDirectory.FullName, sourcePair.Item1.Name);

                sourcePair.Item2.Save(sourceImageFilePath);
            }

            // Keep the first as a reference for deanonymization later.
            var originalSlice = sourceDicomFiles.First();

            var testAETConfigModel = GetTestAETConfigModel();

            var receivePort = 160;

            using (var dicomDataReceiver = new ListenerDataReceiver(new ListenerDicomSaver(resultDirectory.FullName)))
                using (var deleteService = CreateDeleteService())
                    using (var pushService = CreatePushService())
                        using (var downloadService = CreateDownloadService())
                            using (var uploadService = CreateUploadService())
                                using (var receiveService = CreateReceiveService(receivePort))
                                {
                                    // Start a DICOM receiver for the final DICOM-RT file
                                    var eventCount = new ConcurrentDictionary <DicomReceiveProgressCode, int>();
                                    var folderPath = string.Empty;

                                    dicomDataReceiver.DataReceived += (sender, e) =>
                                    {
                                        folderPath = e.FolderPath;
                                        eventCount.AddOrUpdate(e.ProgressCode, 1, (k, v) => v + 1);
                                    };

                                    StartDicomDataReceiver(dicomDataReceiver, testAETConfigModel.AETConfig.Destination.Port);

                                    // Start the services.
                                    deleteService.Start();
                                    pushService.Start();
                                    downloadService.Start();
                                    uploadService.Start();
                                    receiveService.Start();

                                    // Try a DICOM C-ECHO
                                    var dicomDataSender = new DicomDataSender();

                                    var echoResult = await dicomDataSender.DicomEchoAsync(
                                        testAETConfigModel.CallingAET,
                                        testAETConfigModel.CalledAET,
                                        receivePort,
                                        "127.0.0.1").ConfigureAwait(false);

                                    Assert.IsTrue(echoResult == DicomOperationResult.Success);

                                    // Send the image stack
                                    DcmtkHelpers.SendFolderUsingDCMTK(
                                        sourceDirectory.FullName,
                                        receivePort,
                                        ScuProfile.LEExplicitCT,
                                        TestContext,
                                        applicationEntityTitle: testAETConfigModel.CallingAET,
                                        calledAETitle: testAETConfigModel.CalledAET);

                                    // Wait for DICOM-RT file to be received.
                                    Func <DicomReceiveProgressCode, int, bool> TestEventCount = (progressCode, count) =>
                                                                                                eventCount.ContainsKey(progressCode) && eventCount[progressCode] == count;

                                    SpinWait.SpinUntil(() => TestEventCount(DicomReceiveProgressCode.AssociationEstablished, 1));
                                    SpinWait.SpinUntil(() => TestEventCount(DicomReceiveProgressCode.FileReceived, 1));
                                    SpinWait.SpinUntil(() => TestEventCount(DicomReceiveProgressCode.AssociationReleased, 1));
                                    SpinWait.SpinUntil(() => TestEventCount(DicomReceiveProgressCode.ConnectionClosed, 1));

                                    Assert.IsTrue(eventCount[DicomReceiveProgressCode.AssociationEstablished] == 1);
                                    Assert.IsTrue(eventCount[DicomReceiveProgressCode.FileReceived] == 1);
                                    Assert.IsTrue(eventCount[DicomReceiveProgressCode.AssociationReleased] == 1);
                                    Assert.IsTrue(eventCount[DicomReceiveProgressCode.ConnectionClosed] == 1);

                                    var receivedFiles = new DirectoryInfo(folderPath).GetFiles();
                                    Assert.AreEqual(1, receivedFiles.Length);

                                    var receivedFilePath = receivedFiles.First().FullName;

                                    var dicomFile = await DicomFile.OpenAsync(receivedFilePath, FileReadOption.ReadAll).ConfigureAwait(false);

                                    Assert.IsNotNull(dicomFile);

                                    var matchedModel = ApplyAETModelConfigProvider.ApplyAETModelConfig(testAETConfigModel.AETConfig.Config.ModelsConfig, sourceDicomFiles);

                                    var segmentationClient = TestGatewayProcessorConfigProvider.CreateInnerEyeSegmentationClient().Invoke();

                                    DicomAnonymisationTests.AssertDeanonymizedFile(
                                        originalSlice,
                                        dicomFile,
                                        segmentationClient.TopLevelReplacements,
                                        matchedModel.Result.TagReplacements,
                                        false);

                                    AssertIsDicomRtFile(DateTime.UtcNow, dicomFile, matchedModel.Result.ModelId);
                                }
        }