Skip to content

Commit

Permalink
[preview] fix string bug (#1951)
Browse files Browse the repository at this point in the history
Added 4 Unit tests...

1) Create StringManipulatorEnricher
2) String field too long
3) String field is shortter than max
4) Apply regex manipulator

Should resolve #1950
  • Loading branch information
MrHinsh authored Feb 22, 2024
1 parent 1a3311a commit 933bc00
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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<RegexStringManipulator>
{
new RegexStringManipulator
{
Enabled = true,
Pattern = ".*",
Replacement = "Test",
Description = "Test"
}
}

};
var x = Services.GetRequiredService<StringManipulatorEnricher>();
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<RegexStringManipulator>
{
new RegexStringManipulator
{
Enabled = true,
Pattern = "Test",
Replacement = "Test 2",
Description = "Test"
}
}

};
var x = Services.GetRequiredService<StringManipulatorEnricher>();
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<StringManipulatorEnricher>();
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<StringManipulatorEnricher>();
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);
}
}
}
4 changes: 4 additions & 0 deletions src/MigrationTools.Tests/ServiceProviderHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using MigrationTools.EndpointEnrichers;
using MigrationTools.Endpoints;
using MigrationTools.Enrichers;
using MigrationTools.ProcessorEnrichers.WorkItemProcessorEnrichers;
using MigrationTools.Processors;
using MigrationTools.TestExtensions;

Expand All @@ -29,6 +30,9 @@ internal static ServiceProvider GetWorkItemMigrationProcessor()
services.AddSingleton<WorkItemTrackingProcessor>();
services.AddTransient<ProcessorEnricherContainer>();

// ProcessorEnrichers
services.AddSingleton<StringManipulatorEnricher>();

//Endpoints
services.AddTransient<InMemoryWorkItemEndpoint>();
services.AddTransient<EndpointEnricherContainer>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 933bc00

Please sign in to comment.