| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | using SimMetrics.Net; |
| | | 6 | | using WireMock.Admin.Mappings; |
| | | 7 | | using WireMock.Matchers; |
| | | 8 | | |
| | | 9 | | namespace WireMock.Serialization |
| | | 10 | | { |
| | | 11 | | internal static class MatcherMapper |
| | | 12 | | { |
| | | 13 | | public static IMatcher Map([CanBeNull] MatcherModel matcher) |
| | 26 | 14 | | { |
| | 26 | 15 | | if (matcher == null) |
| | 2 | 16 | | { |
| | 2 | 17 | | return null; |
| | | 18 | | } |
| | | 19 | | |
| | 24 | 20 | | string[] parts = matcher.Name.Split('.'); |
| | 24 | 21 | | string matcherName = parts[0]; |
| | 24 | 22 | | string matcherType = parts.Length > 1 ? parts[1] : null; |
| | | 23 | | |
| | 24 | 24 | | string[] stringPatterns = matcher.Patterns != null ? matcher.Patterns.Cast<string>().ToArray() : new[] { mat |
| | 24 | 25 | | MatchBehaviour matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviou |
| | | 26 | | |
| | 24 | 27 | | switch (matcherName) |
| | | 28 | | { |
| | | 29 | | case "LinqMatcher": |
| | 2 | 30 | | return new LinqMatcher(matchBehaviour, stringPatterns); |
| | | 31 | | |
| | | 32 | | case "ExactMatcher": |
| | 11 | 33 | | return new ExactMatcher(matchBehaviour, stringPatterns); |
| | | 34 | | |
| | | 35 | | case "RegexMatcher": |
| | 1 | 36 | | return new RegexMatcher(matchBehaviour, stringPatterns, matcher.IgnoreCase == true); |
| | | 37 | | |
| | | 38 | | case "JsonMatcher": |
| | 0 | 39 | | return new JsonMatcher(matchBehaviour, matcher.Pattern); |
| | | 40 | | |
| | | 41 | | case "JsonPathMatcher": |
| | 0 | 42 | | return new JsonPathMatcher(matchBehaviour, stringPatterns); |
| | | 43 | | |
| | | 44 | | case "XPathMatcher": |
| | 0 | 45 | | return new XPathMatcher(matchBehaviour, (string)matcher.Pattern); |
| | | 46 | | |
| | | 47 | | case "WildcardMatcher": |
| | 5 | 48 | | return new WildcardMatcher(matchBehaviour, stringPatterns, matcher.IgnoreCase == true); |
| | | 49 | | |
| | | 50 | | case "SimMetricsMatcher": |
| | 3 | 51 | | SimMetricType type = SimMetricType.Levenstein; |
| | 3 | 52 | | if (!string.IsNullOrEmpty(matcherType) && !Enum.TryParse(matcherType, out type)) |
| | 1 | 53 | | { |
| | 1 | 54 | | throw new NotSupportedException($"Matcher '{matcherName}' with Type '{matcherType}' is not suppo |
| | | 55 | | } |
| | | 56 | | |
| | 2 | 57 | | return new SimMetricsMatcher(matchBehaviour, (string)matcher.Pattern, type); |
| | | 58 | | |
| | | 59 | | default: |
| | 2 | 60 | | throw new NotSupportedException($"Matcher '{matcherName}' is not supported."); |
| | | 61 | | } |
| | 23 | 62 | | } |
| | | 63 | | |
| | | 64 | | public static MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers) |
| | 3 | 65 | | { |
| | 6 | 66 | | return matchers?.Select(Map).Where(x => x != null).ToArray(); |
| | 3 | 67 | | } |
| | | 68 | | |
| | | 69 | | public static MatcherModel Map([CanBeNull] IMatcher matcher) |
| | 8 | 70 | | { |
| | 8 | 71 | | if (matcher == null) |
| | 2 | 72 | | { |
| | 2 | 73 | | return null; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | // If the matcher is a IStringMatcher, get the patterns. |
| | | 77 | | // If the matcher is a IValueMatcher, get the value (can be string or object). |
| | | 78 | | // Else empty array |
| | 6 | 79 | | object[] patterns = matcher is IStringMatcher stringMatcher ? |
| | 6 | 80 | | stringMatcher.GetPatterns().Cast<object>().ToArray() : |
| | 6 | 81 | | matcher is IValueMatcher valueMatcher ? new[] { valueMatcher.Value } : |
| | 6 | 82 | | new object[0]; |
| | 6 | 83 | | bool? ignorecase = matcher is IIgnoreCaseMatcher ignoreCaseMatcher ? ignoreCaseMatcher.IgnoreCase : (bool?)n |
| | 6 | 84 | | bool? rejectOnMatch = matcher.MatchBehaviour == MatchBehaviour.RejectOnMatch ? true : (bool?)null; |
| | | 85 | | |
| | 6 | 86 | | return new MatcherModel |
| | 6 | 87 | | { |
| | 6 | 88 | | RejectOnMatch = rejectOnMatch, |
| | 6 | 89 | | IgnoreCase = ignorecase, |
| | 6 | 90 | | Name = matcher.Name, |
| | 6 | 91 | | Pattern = patterns.Length == 1 ? patterns.First() : null, |
| | 6 | 92 | | Patterns = patterns.Length > 1 ? patterns : null |
| | 6 | 93 | | }; |
| | 8 | 94 | | } |
| | | 95 | | } |
| | | 96 | | } |