Exemple #1
0
        public void MatcherModelMapper_Map_ExactMatcher_Pattern()
        {
            // Assign
            var model = new MatcherModel
            {
                Name     = "ExactMatcher",
                Patterns = new[] { "x" }
            };

            // Act
            var matcher = (ExactMatcher)_sut.Map(model);

            // Assert
            matcher.GetPatterns().Should().ContainSingle("x");
            matcher.ThrowException.Should().BeFalse();
        }
        public void MatcherMapper_Map_MatcherModel_LinqMatcher_Patterns()
        {
            // Assign
            var model = new MatcherModel
            {
                Name     = "LinqMatcher",
                Patterns = new[] { "p1", "p2" }
            };

            // Act
            var matcher = (LinqMatcher)_sut.Map(model);

            // Assert
            matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
            matcher.GetPatterns().Should().Contain("p1", "p2");
        }
        public void MatcherMapper_Map_MatcherModel_LinqMatcher_Patterns()
        {
            // Assign
            var model = new MatcherModel
            {
                Name     = "LinqMatcher",
                Patterns = new[] { "p1", "p2" }
            };

            // Act
            var matcher = (LinqMatcher)_sut.Map(model);

            // Assert
            Check.That(matcher.MatchBehaviour).IsEqualTo(MatchBehaviour.AcceptOnMatch);
            Check.That(matcher.GetPatterns()).ContainsExactly("p1", "p2");
        }
        public static IMatcher Map([CanBeNull] MatcherModel matcher)
        {
            if (matcher == null)
            {
                return(null);
            }

            string[] parts       = matcher.Name.Split('.');
            string   matcherName = parts[0];
            string   matcherType = parts.Length > 1 ? parts[1] : null;

            string[]       patterns       = matcher.Patterns ?? new[] { matcher.Pattern };
            MatchBehaviour matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch;

            switch (matcherName)
            {
            case "ExactMatcher":
                return(new ExactMatcher(matchBehaviour, patterns));

            case "RegexMatcher":
                return(new RegexMatcher(matchBehaviour, patterns, matcher.IgnoreCase == true));

            case "JsonMatcher":
                return(new JsonMatcher(matchBehaviour, matcher.Pattern));

            case "JsonPathMatcher":
                return(new JsonPathMatcher(matchBehaviour, patterns));

            case "XPathMatcher":
                return(new XPathMatcher(matchBehaviour, matcher.Pattern));

            case "WildcardMatcher":
                return(new WildcardMatcher(matchBehaviour, patterns, matcher.IgnoreCase == true));

            case "SimMetricsMatcher":
                SimMetricType type = SimMetricType.Levenstein;
                if (!string.IsNullOrEmpty(matcherType) && !Enum.TryParse(matcherType, out type))
                {
                    throw new NotSupportedException($"Matcher '{matcherName}' with Type '{matcherType}' is not supported.");
                }

                return(new SimMetricsMatcher(matchBehaviour, matcher.Pattern, type));

            default:
                throw new NotSupportedException($"Matcher '{matcherName}' is not supported.");
            }
        }
Exemple #5
0
        public void MatcherModelMapper_Map_RegexMatcher()
        {
            // Assign
            var model = new MatcherModel
            {
                Name       = "RegexMatcher",
                Patterns   = new[] { "x", "y" },
                IgnoreCase = true
            };

            // Act
            var matcher = (RegexMatcher)_sut.Map(model);

            // Assert
            Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
            Check.That(matcher.IsMatch("X")).IsEqualTo(0.5d);
        }
Exemple #6
0
        public void MatcherMapper_Map_MatcherModel_LinqMatcher_Pattern()
        {
            // Assign
            var model = new MatcherModel
            {
                Name    = "LinqMatcher",
                Pattern = "p"
            };

            // Act
            var matcher = MatcherMapper.Map(model) as LinqMatcher;

            // Assert
            Check.That(matcher).IsNotNull();
            Check.That(matcher.MatchBehaviour).IsEqualTo(MatchBehaviour.AcceptOnMatch);
            Check.That(matcher.GetPatterns()).ContainsExactly("p");
        }
        public void MatcherMapper_Map_MatcherModel_JsonMatcher_Pattern_As_Object()
        {
            // Assign
            var pattern = new { AccountIds = new[] { 1, 2, 3 } };
            var model   = new MatcherModel
            {
                Name    = "JsonMatcher",
                Pattern = pattern
            };

            // Act
            var matcher = (JsonMatcher)_sut.Map(model);

            // Assert
            matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
            matcher.Value.Should().BeEquivalentTo(pattern);
        }
        public void MatcherMapper_Map_MatcherModel_JsonPartialWilcardMatcher_Patterns_As_Object()
        {
            // Assign
            object pattern = new { X = "*" };
            var    model   = new MatcherModel
            {
                Name    = "JsonPartialWildcardMatcher",
                Pattern = pattern
            };

            // Act
            var matcher = (JsonPartialWildcardMatcher)_sut.Map(model);

            // Assert
            matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
            matcher.Value.Should().BeEquivalentTo(pattern);
        }
        public void MatcherMapper_Map_MatcherModel_JsonPartialMatcher_Pattern_As_String()
        {
            // Assign
            var pattern = "{ \"AccountIds\": [ 1, 2, 3 ] }";
            var model   = new MatcherModel
            {
                Name    = "JsonPartialMatcher",
                Pattern = pattern
            };

            // Act
            var matcher = (JsonPartialMatcher)_sut.Map(model);

            // Assert
            matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
            matcher.Value.Should().BeEquivalentTo(pattern);
        }
Exemple #10
0
        public void MatcherModelMapper_Map_CSharpCodeMatcher_NotAllowed_ThrowsException()
        {
            // Assign
            var model = new MatcherModel
            {
                Name     = "CSharpCodeMatcher",
                Patterns = new[] { "x" }
            };
            var sut = new MatcherMapper(new WireMockServerSettings {
                AllowCSharpCodeMatcher = false
            });

            // Act
            Action action = () => sut.Map(model);

            // Assert
            action.Should().Throw <NotSupportedException>();
        }
        public void MatcherMapper_Map_MatcherModel_JsonPartialMatcher_Patterns_As_Object()
        {
            // Assign
            object pattern1 = new { AccountIds = new[] { 1, 2, 3 } };
            object pattern2 = new { X = "x" };
            var    patterns = new[] { pattern1, pattern2 };
            var    model    = new MatcherModel
            {
                Name     = "JsonPartialMatcher",
                Patterns = patterns
            };

            // Act
            var matcher = (JsonMatcher)_sut.Map(model);

            // Assert
            matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch);
            matcher.Value.Should().BeEquivalentTo(patterns);
        }
Exemple #12
0
        public void MatcherModelMapper_Map_ThrowExceptionWhenMatcherFails_True(string name)
        {
            // Assign
            var settings = new WireMockServerSettings
            {
                ThrowExceptionWhenMatcherFails = true
            };
            var sut   = new MatcherMapper(settings);
            var model = new MatcherModel
            {
                Name     = name,
                Patterns = new[] { "" }
            };

            // Act
            var matcher = sut.Map(model);

            // Assert
            matcher.ThrowException.Should().BeTrue();
        }
        public IMatcher Map([CanBeNull] MatcherModel matcher)
        {
            if (matcher == null)
            {
                return(null);
            }

            string[] parts       = matcher.Name.Split('.');
            string   matcherName = parts[0];
            string   matcherType = parts.Length > 1 ? parts[1] : null;

            string[]       stringPatterns = (matcher.Patterns != null ? matcher.Patterns : new[] { matcher.Pattern }).OfType <string>().ToArray();
            MatchBehaviour matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch;
            bool           ignoreCase     = matcher.IgnoreCase == true;
            bool           throwExceptionWhenMatcherFails = _settings.ThrowExceptionWhenMatcherFails == true;

            switch (matcherName)
            {
            case "NotNullOrEmptyMatcher":
                return(new NotNullOrEmptyMatcher(matchBehaviour));

            case "CSharpCodeMatcher":
                if (_settings.AllowCSharpCodeMatcher == true)
                {
                    return(PluginLoader.Load <ICSharpCodeMatcher>(matchBehaviour, stringPatterns));
                }

                throw new NotSupportedException("It's not allowed to use the 'CSharpCodeMatcher' because IWireMockServerSettings.AllowCSharpCodeMatcher is not set to 'true'.");

            case "LinqMatcher":
                return(new LinqMatcher(matchBehaviour, throwExceptionWhenMatcherFails, stringPatterns));

            case "ExactMatcher":
                return(new ExactMatcher(matchBehaviour, throwExceptionWhenMatcherFails, stringPatterns));

            case "ExactObjectMatcher":
                return(CreateExactObjectMatcher(matchBehaviour, stringPatterns[0], throwExceptionWhenMatcherFails));

            case "RegexMatcher":
                return(new RegexMatcher(matchBehaviour, stringPatterns, ignoreCase, throwExceptionWhenMatcherFails));

            case "JsonMatcher":
                object value = matcher.Pattern ?? matcher.Patterns;
                return(new JsonMatcher(matchBehaviour, value, ignoreCase, throwExceptionWhenMatcherFails));

            case "JsonPartialMatcher":
                object matcherValue = matcher.Pattern ?? matcher.Patterns;
                return(new JsonPartialMatcher(matchBehaviour, matcherValue, ignoreCase, throwExceptionWhenMatcherFails));

            case "JsonPathMatcher":
                return(new JsonPathMatcher(matchBehaviour, throwExceptionWhenMatcherFails, stringPatterns));

            case "JmesPathMatcher":
                return(new JmesPathMatcher(matchBehaviour, throwExceptionWhenMatcherFails, stringPatterns));

            case "XPathMatcher":
                return(new XPathMatcher(matchBehaviour, throwExceptionWhenMatcherFails, stringPatterns));

            case "WildcardMatcher":
                return(new WildcardMatcher(matchBehaviour, stringPatterns, ignoreCase, throwExceptionWhenMatcherFails));

            case "ContentTypeMatcher":
                return(new ContentTypeMatcher(matchBehaviour, stringPatterns, ignoreCase, throwExceptionWhenMatcherFails));

            case "SimMetricsMatcher":
                SimMetricType type = SimMetricType.Levenstein;
                if (!string.IsNullOrEmpty(matcherType) && !Enum.TryParse(matcherType, out type))
                {
                    throw new NotSupportedException($"Matcher '{matcherName}' with Type '{matcherType}' is not supported.");
                }

                return(new SimMetricsMatcher(matchBehaviour, stringPatterns, type, throwExceptionWhenMatcherFails));

            default:
                throw new NotSupportedException($"Matcher '{matcherName}' is not supported.");
            }
        }
Exemple #14
0
        public IMatcher Map([CanBeNull] MatcherModel matcher)
        {
            if (matcher == null)
            {
                return(null);
            }

            string[] parts       = matcher.Name.Split('.');
            string   matcherName = parts[0];
            string   matcherType = parts.Length > 1 ? parts[1] : null;

            string[]       stringPatterns = matcher.Patterns != null?matcher.Patterns.OfType <string>().ToArray() : new[] { matcher.Pattern as string };
            MatchBehaviour matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch;

            switch (matcherName)
            {
            case "CSharpCodeMatcher":
                if (_settings.AllowCSharpCodeMatcher == true)
                {
                    return(new CSharpCodeMatcher(matchBehaviour, stringPatterns));
                }

                throw new NotSupportedException("It's not allowed to use the 'CSharpCodeMatcher' because FluentMockServerSettings.AllowCSharpCodeMatcher is not set to 'true'.");

            case "LinqMatcher":
                return(new LinqMatcher(matchBehaviour, stringPatterns));

            case "ExactMatcher":
                return(new ExactMatcher(matchBehaviour, stringPatterns));

            case "ExactObjectMatcher":
                return(CreateExactObjectMatcher(matchBehaviour, stringPatterns[0]));

            case "RegexMatcher":
                return(new RegexMatcher(matchBehaviour, stringPatterns, matcher.IgnoreCase == true));

            case "JsonMatcher":
                return(new JsonMatcher(matchBehaviour, matcher.Pattern, matcher.IgnoreCase == true));

            case "JsonPathMatcher":
                return(new JsonPathMatcher(matchBehaviour, stringPatterns));

            case "JmesPathMatcher":
                return(new JmesPathMatcher(matchBehaviour, stringPatterns));

            case "XPathMatcher":
                return(new XPathMatcher(matchBehaviour, stringPatterns));

            case "WildcardMatcher":
                return(new WildcardMatcher(matchBehaviour, stringPatterns, matcher.IgnoreCase == true));

            case "SimMetricsMatcher":
                SimMetricType type = SimMetricType.Levenstein;
                if (!string.IsNullOrEmpty(matcherType) && !Enum.TryParse(matcherType, out type))
                {
                    throw new NotSupportedException($"Matcher '{matcherName}' with Type '{matcherType}' is not supported.");
                }

                return(new SimMetricsMatcher(matchBehaviour, stringPatterns, type));

            default:
                throw new NotSupportedException($"Matcher '{matcherName}' is not supported.");
            }
        }