Skip to content

Commit

Permalink
Added 4 Unit tests...
Browse files Browse the repository at this point in the history
1) Create StringManipulatorEnricher
2) String field too long
3) String field is shortter than max
4) Apply regex manipulator
  • Loading branch information
MrHinsh committed Feb 22, 2024
1 parent 2c21827 commit 9ca3088
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 11 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 @@ -55,24 +55,38 @@ public override void ProcessorExecutionWithFieldItem(IProcessor processor, Field
}
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} with {pattern}", this.GetType().Name, manipulator.Description, manipulator.Pattern);
fieldItem.Value = Regex.Replace((string)fieldItem.Value, manipulator.Pattern, manipulator.Replacement);
if (fieldItem.Value.ToString().Length > 0 && fieldItem.Value.ToString().Length > _options.MaxStringLength)
if (manipulator.Enabled)
{
fieldItem.Value = fieldItem.Value.ToString().Substring(0, Math.Min(fieldItem.Value.ToString().Length, _options.MaxStringLength));
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);
}
}
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));
}
}


}

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 9ca3088

Please sign in to comment.