From e411aaffe73f3340b5aee132eba8be9e3ac5e637 Mon Sep 17 00:00:00 2001 From: Ruairidh MacLeod Date: Tue, 17 Dec 2024 22:20:15 +0000 Subject: [PATCH] wip: fix errors & tidy test --- .../DicomAnonymiserHostTests.cs | 106 ++++-------------- 1 file changed, 23 insertions(+), 83 deletions(-) diff --git a/tests/SmiServices.IntegrationTests/Microservices/DicomAnonymiser/DicomAnonymiserHostTests.cs b/tests/SmiServices.IntegrationTests/Microservices/DicomAnonymiser/DicomAnonymiserHostTests.cs index 8d9798c21..ad5fa4b4c 100644 --- a/tests/SmiServices.IntegrationTests/Microservices/DicomAnonymiser/DicomAnonymiserHostTests.cs +++ b/tests/SmiServices.IntegrationTests/Microservices/DicomAnonymiser/DicomAnonymiserHostTests.cs @@ -10,7 +10,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.IO.Abstractions; using System.Linq; using System.Threading; @@ -19,34 +18,10 @@ namespace SmiServices.IntegrationTests.Microservices.DicomAnonymiser [RequiresRabbit] public class DicomAnonymiserHostTests { - #region Fixture Methods - - // Private fields (_tempTestDir, _dicomRoot, _fakeDicom) that - // are used in the setup and teardown of each test. private DirectoryInfo _tempTestDir = null!; private DirectoryInfo _dicomRoot = null!; private string _fakeDicom = null!; - - // [OneTimeSetUp] and [OneTimeTearDown] methods are run once - // before and after all the tests in the class, respectively. - // In this case, the setup method is used to set up a logger. - [OneTimeSetUp] - public void OneTimeSetUp() - { - } - - [OneTimeTearDown] - public void OneTimeTearDown() { } - - #endregion - - #region Test Methods - - // [SetUp] and [TearDown] methods are run before and after each - // test, respectively. In this case, the setup method creates a - // temporary directory and a fake DICOM file, and the teardown - // method deletes the temporary directory. [SetUp] public void SetUp() { @@ -64,33 +39,15 @@ public void TearDown() _tempTestDir.Delete(recursive: true); } - #endregion - - #region Tests - - // The Integration_HappyPath_MockAnonymiser method is a test case - // structured in the Arrange-Act-Assert pattern. It tests for the - // scenario where the DICOM Anonymiser successfully anonymises a - // DICOM file. [Test] public void Integration_HappyPath_MockAnonymiser() { - // Arrange - // It sets up the necessary objects and state for the test. This - // includes creating a mock DICOM Anonymiser, setting file paths, - // and creating a DicomAnonymiserHost. - GlobalOptions globals = new GlobalOptionsFactory().Load(nameof(Integration_HappyPath_MockAnonymiser)); globals.FileSystemOptions!.FileSystemRoot = _dicomRoot.FullName; var extractRoot = Directory.CreateDirectory(Path.Combine(_tempTestDir.FullName, "extractRoot")); globals.FileSystemOptions.ExtractRoot = extractRoot.FullName; - // NOTE: The commented out code below is an alternative way to create - // a fake DICOM file, however, it is not used in this test. - // File.Create(_fakeDicom).Dispose(); - // File.SetAttributes(_fakeDicom, File.GetAttributes(_fakeDicom) | FileAttributes.ReadOnly); - var dicomFile = new DicomFile(); dicomFile.Dataset.Add(DicomTag.PatientID, "12345678"); dicomFile.Dataset.Add(DicomTag.Modality, "CT"); @@ -122,18 +79,14 @@ public void Integration_HappyPath_MockAnonymiser() OutputPath = "foo-an.dcm", }; - // The test uses the Moq library to create a mock implementation of - // the IDicomAnonymiser interface. This allows the test to control the - // behavior of the DICOM Anonymiser and verify that it is called with - // the correct arguments. var mockAnonymiser = new Mock(MockBehavior.Strict); mockAnonymiser .Setup( x => x.Anonymise( - It.Is(x => x.ExtractionJobIdentifier == testExtractFileMessage.ExtractionJobIdentifier), - It.Is(x => x.FullName == _fakeDicom), - It.Is(x => x.FullName == Path.Combine(extractDirAbs.FullName, "foo-an.dcm")), - out It.Ref.IsAny + It.Is(x => x.Name == _fakeDicom), + It.Is(x => x.Name == Path.Combine(extractDirAbs.FullName, "foo-an.dcm")), + It.IsAny(), + out It.Ref.IsAny ) ) .Callback(() => File.Create(expectedAnonPathAbs).Dispose()) @@ -145,45 +98,34 @@ out It.Ref.IsAny List statusMessages = []; - using ( - var tester = new MicroserviceTester( - globals.RabbitOptions!, - globals.DicomAnonymiserOptions.AnonFileConsumerOptions! - ) - ) - { - tester.CreateExchange(statusExchange, successQueue, isSecondaryBinding: false, routingKey: "verify"); - tester.CreateExchange(statusExchange, failureQueue, isSecondaryBinding: true, routingKey: "noverify"); - - tester.SendMessage(globals.DicomAnonymiserOptions.AnonFileConsumerOptions!, new MessageHeader(), testExtractFileMessage); + using var tester = new MicroserviceTester( + globals.RabbitOptions!, + globals.DicomAnonymiserOptions.AnonFileConsumerOptions! + ); - var host = new DicomAnonymiserHost(globals, mockAnonymiser.Object); + tester.CreateExchange(statusExchange, successQueue, isSecondaryBinding: false, routingKey: "verify"); + tester.CreateExchange(statusExchange, failureQueue, isSecondaryBinding: true, routingKey: "noverify"); - // Act - // It starts the DicomAnonymiserHost and waits for it to process - //a message. + tester.SendMessage(globals.DicomAnonymiserOptions.AnonFileConsumerOptions!, new MessageHeader(), testExtractFileMessage); - host.Start(); + var host = new DicomAnonymiserHost(globals, mockAnonymiser.Object); - var timeoutSecs = 10; + host.Start(); - while (statusMessages.Count == 0 && timeoutSecs > 0) - { - statusMessages.AddRange(tester.ConsumeMessages(successQueue).Select(x => x.Item2)); - statusMessages.AddRange(tester.ConsumeMessages(failureQueue).Select(x => x.Item2)); + var timeoutSecs = 10; - --timeoutSecs; - if (statusMessages.Count == 0) - Thread.Sleep(TimeSpan.FromSeconds(1)); - } + while (statusMessages.Count == 0 && timeoutSecs > 0) + { + statusMessages.AddRange(tester.ConsumeMessages(successQueue).Select(x => x.Item2)); + statusMessages.AddRange(tester.ConsumeMessages(failureQueue).Select(x => x.Item2)); - host.Stop("Test end"); + --timeoutSecs; + if (statusMessages.Count == 0) + Thread.Sleep(TimeSpan.FromSeconds(1)); } - // Assert - // It checks that the expected outcome has occurred. In this case, it - // checks that the status message indicates that the file was anonymised - // and that the anonymised file exists. + host.Stop("Test end"); + tester.Dispose(); var statusMessage = statusMessages.Single(); Assert.Multiple(() => @@ -192,7 +134,5 @@ out It.Ref.IsAny Assert.That(File.Exists(expectedAnonPathAbs), Is.True); }); } - - #endregion } }