-
Notifications
You must be signed in to change notification settings - Fork 0
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 #186 from buhlergroup/feature/regex-match
Value Matches Condition
- Loading branch information
Showing
10 changed files
with
294 additions
and
50 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
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
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
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,34 @@ | ||
namespace Buhlergroup.DataMapper.Condition | ||
{ | ||
using System; | ||
using System.Text.RegularExpressions; | ||
using Buhlergroup.DataMapper.Model; | ||
using Newtonsoft.Json.Linq; | ||
|
||
public class ValueMatchesCondition : ICondition | ||
{ | ||
public string Apply(FieldConditionModel condition, JObject model) | ||
{ | ||
if (model == null) | ||
{ | ||
throw new ArgumentNullException(nameof(model)); | ||
} | ||
|
||
if (condition == null) | ||
{ | ||
throw new ArgumentNullException(nameof(condition)); | ||
} | ||
|
||
var fieldValue = (string)model[condition.Field]; | ||
|
||
if (fieldValue == null) | ||
{ | ||
return null; | ||
} | ||
|
||
var matches = Regex.IsMatch(fieldValue, condition.EqualsValue); | ||
|
||
return Regex.IsMatch(fieldValue, condition.EqualsValue) ? condition.Value : null; | ||
} | ||
} | ||
} |
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
116 changes: 116 additions & 0 deletions
116
source/tests/data-mapper-test/Condition/ValueMatchesConditionTest.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,116 @@ | ||
namespace Buhlergroup.DataMapper.Condition.Test | ||
{ | ||
using System; | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Model; | ||
using Newtonsoft.Json.Linq; | ||
|
||
[TestClass] | ||
public class ValueMatchesConditionTest | ||
{ | ||
[TestMethod] | ||
public void Apply_ThrowOnNullCondition() | ||
{ | ||
var jsonObject = new JObject | ||
{ | ||
{ "bpmnr", "123" } | ||
}; | ||
|
||
var handler = new ValueMatchesCondition(); | ||
|
||
Action act = () => handler.Apply(null, jsonObject); | ||
act.Should().Throw<ArgumentNullException>(); | ||
} | ||
|
||
[TestMethod] | ||
public void Apply_ThrowOnNullObject() | ||
{ | ||
var condition = new FieldConditionModel | ||
{ | ||
Type = ConditionType.VALUE_MATCHES, | ||
Field = "ms13actual", | ||
Value = "ms13", | ||
EqualsValue = "123" | ||
}; | ||
|
||
var handler = new ValueMatchesCondition(); | ||
|
||
Action act = () => handler.Apply(condition, null); | ||
act.Should().Throw<ArgumentNullException>(); | ||
} | ||
|
||
[TestMethod] | ||
public void Apply_Correctly() | ||
{ | ||
var jsonObject = new JObject | ||
{ | ||
{ "bpmnr", "123" } | ||
}; | ||
var condition = new FieldConditionModel | ||
{ | ||
Type = ConditionType.VALUE_MATCHES, | ||
Field = "bpmnr", | ||
Value = "1234", | ||
EqualsValue = "[1-9]{3}" | ||
}; | ||
|
||
var result = new ValueMatchesCondition().Apply(condition, jsonObject); | ||
result.Should().Be("1234"); | ||
} | ||
|
||
[TestMethod] | ||
public void Apply_DoesNotEqual() | ||
{ | ||
var jsonObject = new JObject | ||
{ | ||
{ "bpmnr", "1234" } | ||
}; | ||
var condition = new FieldConditionModel | ||
{ | ||
Type = ConditionType.VALUE_MATCHES, | ||
Field = "bpmnr", | ||
Value = "1234", | ||
EqualsValue = "[A-Z]" | ||
}; | ||
|
||
var result = new ValueMatchesCondition().Apply(condition, jsonObject); | ||
result.Should().BeNull(); | ||
} | ||
|
||
[TestMethod] | ||
public void Apply_EmptyField() | ||
{ | ||
var jsonObject = new JObject | ||
{ | ||
{ "bpmnr", "" } | ||
}; | ||
var condition = new FieldConditionModel | ||
{ | ||
Type = ConditionType.VALUE_MATCHES, | ||
Field = "bpmnr", | ||
Value = "1234", | ||
EqualsValue = "[1-9]" | ||
}; | ||
|
||
var result = new ValueMatchesCondition().Apply(condition, jsonObject); | ||
result.Should().BeNull(); | ||
} | ||
|
||
[TestMethod] | ||
public void Apply_NonExistingField() | ||
{ | ||
var jsonObject = new JObject(); | ||
var condition = new FieldConditionModel | ||
{ | ||
Type = ConditionType.VALUE_MATCHES, | ||
Field = "bpmnr", | ||
Value = "1234", | ||
EqualsValue = "123" | ||
}; | ||
|
||
var result = new ValueMatchesCondition().Apply(condition, jsonObject); | ||
result.Should().BeNull(); | ||
} | ||
} | ||
} |
Oops, something went wrong.