Summary

Class:WireMock.Transformers.HandleBarsJsonPath
Assembly:WireMock.Net
File(s):C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Transformers\HandleBarsJsonPath.cs
Covered lines:46
Uncovered lines:1
Coverable lines:47
Total lines:75
Line coverage:97.8%
Branch coverage:71.4%

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
Register()0011
ParseArguments(...)000.9170.6

File(s)

C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Transformers\HandleBarsJsonPath.cs

#LineLine coverage
 1using System;
 2using System.Linq;
 3using HandlebarsDotNet;
 4using Newtonsoft.Json;
 5using Newtonsoft.Json.Linq;
 6using WireMock.Validation;
 7
 8namespace WireMock.Transformers
 9{
 10    internal static class HandleBarsJsonPath
 11    {
 12        public static void Register()
 113        {
 114            Handlebars.RegisterHelper("JsonPath.SelectToken", (writer, context, arguments) =>
 515            {
 516                (JObject valueToProcess, string jsonpath) = ParseArguments(arguments);
 117
 118                try
 519                {
 520                    var result = valueToProcess.SelectToken(jsonpath);
 521                    writer.WriteSafeString(result);
 522                }
 123                catch (JsonException)
 124                {
 125                    // Ignore JsonException and return
 126                    return;
 127                }
 528            });
 29
 130            Handlebars.RegisterHelper("JsonPath.SelectTokens", (writer, options, context, arguments) =>
 431            {
 432                (JObject valueToProcess, string jsonpath) = ParseArguments(arguments);
 133
 134                try
 335                {
 336                    var values = valueToProcess.SelectTokens(jsonpath);
 337                    if (values != null)
 338                    {
 1139                        options.Template(writer, values.ToDictionary(value => value.Path, value => value));
 340                    }
 341                }
 142                catch (JsonException)
 143                {
 144                    // Ignore JsonException and return
 145                    return;
 146                }
 347            });
 148        }
 49
 50        private static (JObject valueToProcess, string jsonpath) ParseArguments(object[] arguments)
 751        {
 1452            Check.Condition(arguments, args => args.Length == 2, nameof(arguments));
 753            Check.NotNull(arguments[0], "arguments[0]");
 654            Check.NotNullOrEmpty(arguments[1] as string, "arguments[1]");
 55
 56            JObject valueToProcess;
 57
 658            switch (arguments[0])
 59            {
 60                case string jsonAsString:
 461                    valueToProcess = JObject.Parse(jsonAsString);
 462                    break;
 63
 64                case JObject jsonAsJObject:
 265                    valueToProcess = jsonAsJObject;
 266                    break;
 67
 68                default:
 069                    throw new NotSupportedException($"The value '{arguments[0]}' with type '{arguments[0]?.GetType()}' c
 70            }
 71
 672            return (valueToProcess, arguments[1] as string);
 673        }
 74    }
 75}