diff --git a/src/MigrationTools.Tests/ProcessorEnrichers/StringManipulatorEnricherTests.cs b/src/MigrationTools.Tests/ProcessorEnrichers/StringManipulatorEnricherTests.cs new file mode 100644 index 000000000..244c0c18d --- /dev/null +++ b/src/MigrationTools.Tests/ProcessorEnrichers/StringManipulatorEnricherTests.cs @@ -0,0 +1,137 @@ +using System.Collections.Generic; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MigrationTools.DataContracts; +using MigrationTools.Endpoints; +using MigrationTools.Enrichers; +using MigrationTools.ProcessorEnrichers.WorkItemProcessorEnrichers; +using MigrationTools.Processors; +using MigrationTools.Tests; + +namespace MigrationTools.ProcessorEnrichers.Tests +{ + [TestClass()] + public class StringManipulatorEnricherTests + { + private ServiceProvider Services; + + [TestInitialize] + public void Setup() + { + Services = ServiceProviderHelper.GetWorkItemMigrationProcessor(); + } + + [TestMethod(), TestCategory("L0"), TestCategory("Generic.Processor")] + public void StringManipulatorEnricher_ConfigureTest() + { + var y = new StringManipulatorEnricherOptions + { + Enabled = true, + MaxStringLength = 10, + Manipulators = new List + { + new RegexStringManipulator + { + Enabled = true, + Pattern = ".*", + Replacement = "Test", + Description = "Test" + } + } + + }; + var x = Services.GetRequiredService(); + x.Configure(y); + Assert.IsNotNull(x); + } + + [TestMethod(), TestCategory("L1"), TestCategory("ProcessorEnrichers")] + public void StringManipulatorEnricher_RegexTest() + { + var y = new StringManipulatorEnricherOptions + { + Enabled = true, + MaxStringLength = 10, + Manipulators = new List + { + new RegexStringManipulator + { + Enabled = true, + Pattern = "Test", + Replacement = "Test 2", + Description = "Test" + } + } + + }; + var x = Services.GetRequiredService(); + x.Configure(y); + + var fieldItem = new FieldItem + { + FieldType = "String", + internalObject = null, + ReferenceName = "Custom.Test", + Name = "Test", + Value = "Test" + }; + + + x.ProcessorExecutionWithFieldItem(null, fieldItem); + + Assert.AreEqual("Test 2", fieldItem.Value); + } + + [TestMethod(), TestCategory("L1"), TestCategory("ProcessorEnrichers")] + public void StringManipulatorEnricher_LengthShorterThanMaxTest() + { + var y = new StringManipulatorEnricherOptions + { + Enabled = true, + MaxStringLength = 10, + }; + var x = Services.GetRequiredService(); + x.Configure(y); + + var fieldItem = new FieldItem + { + FieldType = "String", + internalObject = null, + ReferenceName = "Custom.Test", + Name = "Test", + Value = "Test" + }; + + + x.ProcessorExecutionWithFieldItem(null, fieldItem); + + Assert.AreEqual(4, fieldItem.Value.ToString().Length); + } + + [TestMethod(), TestCategory("L1"), TestCategory("ProcessorEnrichers")] + public void StringManipulatorEnricher_LengthLongerThanMaxTest() + { + var y = new StringManipulatorEnricherOptions + { + Enabled = true, + MaxStringLength = 10, + }; + var x = Services.GetRequiredService(); + x.Configure(y); + + var fieldItem = new FieldItem + { + FieldType = "String", + internalObject = null, + ReferenceName = "Custom.Test", + Name = "Test", + Value = "Test Test Test Test Test Test Test Test Test Test Test Test Test" + }; + + + x.ProcessorExecutionWithFieldItem(null, fieldItem); + + Assert.AreEqual(10, fieldItem.Value.ToString().Length); + } + } +} \ No newline at end of file diff --git a/src/MigrationTools.Tests/ServiceProviderHelper.cs b/src/MigrationTools.Tests/ServiceProviderHelper.cs index 6dc10d707..c211260bf 100644 --- a/src/MigrationTools.Tests/ServiceProviderHelper.cs +++ b/src/MigrationTools.Tests/ServiceProviderHelper.cs @@ -3,6 +3,7 @@ using MigrationTools.EndpointEnrichers; using MigrationTools.Endpoints; using MigrationTools.Enrichers; +using MigrationTools.ProcessorEnrichers.WorkItemProcessorEnrichers; using MigrationTools.Processors; using MigrationTools.TestExtensions; @@ -29,6 +30,9 @@ internal static ServiceProvider GetWorkItemMigrationProcessor() services.AddSingleton(); services.AddTransient(); + // ProcessorEnrichers + services.AddSingleton(); + //Endpoints services.AddTransient(); services.AddTransient(); diff --git a/src/MigrationTools/ProcessorEnrichers/WorkItemProcessorEnrichers/StringManipulatorEnricher.cs b/src/MigrationTools/ProcessorEnrichers/WorkItemProcessorEnrichers/StringManipulatorEnricher.cs index cb911734d..69ae74d53 100644 --- a/src/MigrationTools/ProcessorEnrichers/WorkItemProcessorEnrichers/StringManipulatorEnricher.cs +++ b/src/MigrationTools/ProcessorEnrichers/WorkItemProcessorEnrichers/StringManipulatorEnricher.cs @@ -53,22 +53,40 @@ public override void ProcessorExecutionWithFieldItem(IProcessor processor, Field Log.LogDebug("{WorkItemProcessorEnricher}::ProcessorExecutionWithFieldItem::Disabled", this.GetType().Name); return; } - if (fieldItem.FieldType == "String") + if (fieldItem.FieldType == "String" && fieldItem.Value !=null) { - foreach (var manipulator in _options.Manipulators) + if (HasManipulators()) { - if (manipulator.Enabled) + foreach (var manipulator in _options.Manipulators) { - Log.LogDebug("{WorkItemProcessorEnricher}::ProcessorExecutionWithFieldItem::Running::{Description}", this.GetType().Name, manipulator.Description); - fieldItem.Value = Regex.Replace((string)fieldItem.Value, manipulator.Pattern, manipulator.Replacement); - } - else - { - Log.LogDebug("{WorkItemProcessorEnricher}::ProcessorExecutionWithFieldItem::Disabled::{Description}", this.GetType().Name, manipulator.Description); + if (manipulator.Enabled) + { + Log.LogDebug("{WorkItemProcessorEnricher}::ProcessorExecutionWithFieldItem::Running::{Description} with {pattern}", this.GetType().Name, manipulator.Description, manipulator.Pattern); + fieldItem.Value = Regex.Replace((string)fieldItem.Value, manipulator.Pattern, manipulator.Replacement); + + } + else + { + Log.LogDebug("{WorkItemProcessorEnricher}::ProcessorExecutionWithFieldItem::Disabled::{Description}", this.GetType().Name, manipulator.Description); + } } } + if (HasStringTooLong(fieldItem)) + { + fieldItem.Value = fieldItem.Value.ToString().Substring(0, Math.Min(fieldItem.Value.ToString().Length, _options.MaxStringLength)); + } } - fieldItem.Value.ToString().Substring(0, Math.Min(fieldItem.Value.ToString().Length-1, _options.MaxStringLength-1)); + + } + + private bool HasStringTooLong(FieldItem fieldItem) + { + return fieldItem.Value.ToString().Length > 0 && fieldItem.Value.ToString().Length > _options.MaxStringLength; + } + + private bool HasManipulators() + { + return _options.Manipulators != null && _options.Manipulators.Count > 0; } }