Skip to content

Commit

Permalink
wip: fix errors & tidy test
Browse files Browse the repository at this point in the history
  • Loading branch information
rkm committed Jan 6, 2025
1 parent 8139feb commit 66b49dd
Showing 1 changed file with 8 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Threading;

Expand All @@ -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()
{
Expand All @@ -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");
Expand Down Expand Up @@ -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<IDicomAnonymiser>(MockBehavior.Strict);
mockAnonymiser
.Setup(
x => x.Anonymise(
It.Is<ExtractFileMessage>(x => x.ExtractionJobIdentifier == testExtractFileMessage.ExtractionJobIdentifier),
It.Is<IFileInfo>(x => x.FullName == _fakeDicom),
It.Is<IFileInfo>(x => x.FullName == Path.Combine(extractDirAbs.FullName, "foo-an.dcm")),
out It.Ref<string>.IsAny
It.Is<FileInfo>(x => x.Name == _fakeDicom),
It.Is<FileInfo>(x => x.Name == Path.Combine(extractDirAbs.FullName, "foo-an.dcm")),
It.IsAny<string>(),
out It.Ref<string?>.IsAny
)
)
.Callback(() => File.Create(expectedAnonPathAbs).Dispose())
Expand All @@ -145,24 +98,18 @@ out It.Ref<string>.IsAny

List<ExtractedFileStatusMessage> statusMessages = [];

using (
var tester = new MicroserviceTester(
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);

var host = new DicomAnonymiserHost(globals, mockAnonymiser.Object);

// Act
// It starts the DicomAnonymiserHost and waits for it to process
//a message.

host.Start();

var timeoutSecs = 10;
Expand All @@ -178,12 +125,7 @@ out It.Ref<string>.IsAny
}

host.Stop("Test end");
}

// 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.
tester.Dispose();

var statusMessage = statusMessages.Single();
Assert.Multiple(() =>
Expand All @@ -192,6 +134,4 @@ out It.Ref<string>.IsAny
Assert.That(File.Exists(expectedAnonPathAbs), Is.True);
});
}

#endregion
}

0 comments on commit 66b49dd

Please sign in to comment.