-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from microsoft/dev
Upgrade to 3.1.0
- Loading branch information
Showing
34 changed files
with
664 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+768 Bytes
DICOM/src/Microsoft.Health.Dicom.Anonymizer.Core.UnitTests/DicomResults/custom.dcm
Binary file not shown.
52 changes: 52 additions & 0 deletions
52
DICOM/src/Microsoft.Health.Dicom.Anonymizer.Core.UnitTests/MaskProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Linq; | ||
using Dicom; | ||
using Microsoft.Health.Dicom.Anonymizer.Core.Models; | ||
using Microsoft.Health.Dicom.Anonymizer.Core.Processors; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Microsoft.Health.Dicom.Anonymizer.Core.UnitTests | ||
{ | ||
public class MaskProcessor : IAnonymizerProcessor | ||
{ | ||
private int _maskedLength; | ||
|
||
public MaskProcessor(JObject setting) | ||
{ | ||
_maskedLength = int.Parse(setting.GetValue("maskedLength", StringComparison.OrdinalIgnoreCase).ToString()); | ||
} | ||
|
||
public bool IsSupported(DicomItem item) | ||
{ | ||
if (item is DicomStringElement) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public static MaskProcessor Create(JObject setting) | ||
{ | ||
return new MaskProcessor(setting); | ||
} | ||
|
||
public void Process(DicomDataset dicomDataset, DicomItem item, ProcessContext context) | ||
{ | ||
var mask = new string('*', this._maskedLength); | ||
if (item is DicomStringElement) | ||
{ | ||
var values = ((DicomStringElement)item).Get<string[]>().Where(x => !string.IsNullOrEmpty(x)).Select(x => x.Length > _maskedLength?mask + x[this._maskedLength..] : mask); | ||
if (values.Count() != 0) | ||
{ | ||
dicomDataset.AddOrUpdate(item.ValueRepresentation, item.Tag, values.ToArray()); | ||
} | ||
} | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...soft.Health.Dicom.Anonymizer.Core.UnitTests/Processors/CustomProcessorFactoryUnitTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using Microsoft.Health.Dicom.Anonymizer.Core.Exceptions; | ||
using Microsoft.Health.Dicom.Anonymizer.Core.Processors; | ||
using Newtonsoft.Json.Linq; | ||
using Xunit; | ||
|
||
namespace Microsoft.Health.Dicom.Anonymizer.Core.UnitTests.Processors | ||
{ | ||
public class CustomProcessorFactoryUnitTests | ||
{ | ||
[Fact] | ||
public void GivenADicomProcessorFactory_GivenMethod_CorrectProcessorWillBeReturned() | ||
{ | ||
var factory = new CustomProcessorFactory(); | ||
Assert.Equal(typeof(PerturbProcessor), factory.CreateProcessor("perturb", new JObject()).GetType()); | ||
Assert.Equal(typeof(EncryptProcessor), factory.CreateProcessor("encrypt", new JObject()).GetType()); | ||
Assert.Equal(typeof(RedactProcessor), factory.CreateProcessor("redact", new JObject()).GetType()); | ||
Assert.Equal(typeof(RefreshUIDProcessor), factory.CreateProcessor("refreshUID", new JObject()).GetType()); | ||
Assert.Equal(typeof(SubstituteProcessor), factory.CreateProcessor("substitute", new JObject()).GetType()); | ||
Assert.Equal(typeof(RemoveProcessor), factory.CreateProcessor("remove", new JObject()).GetType()); | ||
Assert.Equal(typeof(DateShiftProcessor), factory.CreateProcessor("dateshift", new JObject()).GetType()); | ||
Assert.Equal(typeof(CryptoHashProcessor), factory.CreateProcessor("cryptohash", new JObject()).GetType()); | ||
} | ||
|
||
[Fact] | ||
public void GivenADicomProcessorFactory_AddingCustomProcessor_GivenMethod_CorrectProcessorWillBeReturned() | ||
{ | ||
var factory = new CustomProcessorFactory(); | ||
factory.RegisterProcessors(typeof(MaskProcessor), typeof(MockAnonymizerProcessor)); | ||
Assert.Equal(typeof(MaskProcessor), factory.CreateProcessor("mask", JObject.Parse("{\"maskedLength\":\"3\"}")).GetType()); | ||
Assert.Equal(typeof(MockAnonymizerProcessor), factory.CreateProcessor("mockanonymizer", new JObject()).GetType()); | ||
} | ||
|
||
[Fact] | ||
public void GivenADicomProcessorFactory_AddingBuildInProcessor_ExceptionWillBeThrown() | ||
{ | ||
var factory = new CustomProcessorFactory(); | ||
Assert.Throws<AddCustomProcessorException>(() => factory.RegisterProcessors(typeof(RedactProcessor))); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...osoft.Health.Dicom.Anonymizer.Core.UnitTests/Processors/DicomProcessorFactoryUnitTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using Microsoft.Health.Dicom.Anonymizer.Core.Exceptions; | ||
using Microsoft.Health.Dicom.Anonymizer.Core.Processors; | ||
using Newtonsoft.Json.Linq; | ||
using Xunit; | ||
|
||
namespace Microsoft.Health.Dicom.Anonymizer.Core.UnitTests.Processors | ||
{ | ||
public class DicomProcessorFactoryUnitTests | ||
{ | ||
[Fact] | ||
public void GivenADicomProcessorFactory_GivenMethod_CorrectProcessorWillBeReturned() | ||
{ | ||
var factory = new DicomProcessorFactory(); | ||
Assert.Equal(typeof(PerturbProcessor), factory.CreateProcessor("perturb", new JObject()).GetType()); | ||
Assert.Equal(typeof(EncryptProcessor), factory.CreateProcessor("encrypt", new JObject()).GetType()); | ||
Assert.Equal(typeof(RedactProcessor), factory.CreateProcessor("redact", new JObject()).GetType()); | ||
Assert.Equal(typeof(RefreshUIDProcessor), factory.CreateProcessor("refreshUID", new JObject()).GetType()); | ||
Assert.Equal(typeof(SubstituteProcessor), factory.CreateProcessor("substitute", new JObject()).GetType()); | ||
Assert.Equal(typeof(RemoveProcessor), factory.CreateProcessor("remove", new JObject()).GetType()); | ||
Assert.Equal(typeof(DateShiftProcessor), factory.CreateProcessor("dateshift", new JObject()).GetType()); | ||
Assert.Equal(typeof(CryptoHashProcessor), factory.CreateProcessor("cryptohash", new JObject()).GetType()); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...rc/Microsoft.Health.Dicom.Anonymizer.Core.UnitTests/Processors/MockAnonymizerProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using Dicom; | ||
using Microsoft.Health.Dicom.Anonymizer.Core.Models; | ||
using Microsoft.Health.Dicom.Anonymizer.Core.Processors; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Microsoft.Health.Dicom.Anonymizer.Core.UnitTests.Processors | ||
{ | ||
public class MockAnonymizerProcessor : IAnonymizerProcessor | ||
{ | ||
public MockAnonymizerProcessor(JObject settintgs) | ||
{ | ||
} | ||
|
||
public bool IsSupported(DicomItem item) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public void Process(DicomDataset dicomDataset, DicomItem item, ProcessContext context) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...osoft.Health.Dicom.Anonymizer.Core.UnitTests/TestConfigurations/configuration-custom.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"rules": [ | ||
{ | ||
"tag": "RetrieveAETitle", | ||
"method": "mask", | ||
"params": { "maskedLength": "3" } | ||
} | ||
], | ||
"defaultSettings": { | ||
"perturb": { | ||
"span": 1, | ||
"roundTo": 2, | ||
"rangeType": "Proportional" | ||
}, | ||
"dateShift": { | ||
"dateShiftKey": "123", | ||
"dateShiftScope": "SeriesInstance", | ||
"dateShiftRange": 50 | ||
}, | ||
"cryptoHash": { | ||
"cryptoHashKey": "123" | ||
}, | ||
"redact": { | ||
"enablePartialAgesForRedact": false, | ||
"enablePartialDatesForRedact": false | ||
}, | ||
"encrypt": { | ||
"encryptKey": "123456781234567812345678" | ||
}, | ||
"substitute": { | ||
"replaceWith": "ANONYMOUS" | ||
} | ||
}, | ||
"customSettings": { | ||
"perturbCustomerSetting": { | ||
"span": 1, | ||
"roundTo": 2, | ||
"rangeType": "Proportional" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
DICOM/src/Microsoft.Health.Dicom.Anonymizer.Core/Exceptions/AddCustomProcessorException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
|
||
namespace Microsoft.Health.Dicom.Anonymizer.Core.Exceptions | ||
{ | ||
public class AddCustomProcessorException : DicomAnonymizationException | ||
{ | ||
public AddCustomProcessorException(DicomAnonymizationErrorCode errorCode, string message) | ||
: base(errorCode, message) | ||
{ | ||
} | ||
|
||
public AddCustomProcessorException(DicomAnonymizationErrorCode errorCode, string message, Exception innerException) | ||
: base(errorCode, message, innerException) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...src/Microsoft.Health.Dicom.Anonymizer.Core/Processors/Factories/CustomProcessorFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using EnsureThat; | ||
using Microsoft.Health.Dicom.Anonymizer.Core.Exceptions; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Microsoft.Health.Dicom.Anonymizer.Core.Processors | ||
{ | ||
public class CustomProcessorFactory : DicomProcessorFactory | ||
{ | ||
private readonly Dictionary<string, Type> _customProcessors = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase) { }; | ||
|
||
public override IAnonymizerProcessor CreateProcessor(string method, JObject settingObject = null) | ||
{ | ||
EnsureArg.IsNotNullOrEmpty(method, nameof(method)); | ||
|
||
if (Constants.BuiltInMethods.Contains(method)) | ||
{ | ||
return base.CreateProcessor(method, settingObject); | ||
} | ||
|
||
return CreateCustomProcessor(method, settingObject); | ||
} | ||
|
||
public void RegisterProcessors(params Type[] processors) | ||
{ | ||
if (processors != null) | ||
{ | ||
RegisterProcessors(processors.AsEnumerable()); | ||
} | ||
} | ||
|
||
public void RegisterProcessors(IEnumerable<Type> processors) | ||
{ | ||
foreach (Type processor in processors) | ||
{ | ||
var method = GetMethodName(processor.ToString()); | ||
if (Constants.BuiltInMethods.Contains(method)) | ||
{ | ||
throw new AddCustomProcessorException(DicomAnonymizationErrorCode.AddCustomProcessorFailed, $"Anonymization method {method} is a built-in method. Please add custom processor with unique method name."); | ||
} | ||
|
||
_customProcessors.Add(method, processor); | ||
} | ||
} | ||
|
||
private IAnonymizerProcessor CreateCustomProcessor(string method, JObject settingObject = null) | ||
{ | ||
EnsureArg.IsNotNullOrEmpty(method, nameof(method)); | ||
|
||
if (_customProcessors.ContainsKey(method)) | ||
{ | ||
return (IAnonymizerProcessor)Activator.CreateInstance( | ||
_customProcessors[method], | ||
new object[] { settingObject }); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private string GetMethodName(string processor) | ||
{ | ||
return processor.Split(".").Last().Replace("Processor", string.Empty); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.