Summary

Class:WireMock.Serialization.MappingConverter
Assembly:WireMock.Net
File(s):C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Serialization\MappingConverter.cs
Covered lines:87
Uncovered lines:29
Coverable lines:116
Total lines:142
Line coverage:75%
Branch coverage:48.5%

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
ToMappingModel(...)000.7880.516
Map(...)000.4170.25

File(s)

C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Serialization\MappingConverter.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Linq;
 3using WireMock.Admin.Mappings;
 4using WireMock.Matchers.Request;
 5using WireMock.RequestBuilders;
 6using WireMock.ResponseBuilders;
 7using WireMock.Util;
 8
 9namespace WireMock.Serialization
 10{
 11    internal static class MappingConverter
 12    {
 13        public static MappingModel ToMappingModel(IMapping mapping)
 214        {
 215            var request = (Request)mapping.RequestMatcher;
 216            var response = (Response)mapping.Provider;
 17
 218            var clientIPMatchers = request.GetRequestMessageMatchers<RequestMessageClientIPMatcher>();
 219            var pathMatchers = request.GetRequestMessageMatchers<RequestMessagePathMatcher>();
 220            var urlMatchers = request.GetRequestMessageMatchers<RequestMessageUrlMatcher>();
 221            var headerMatchers = request.GetRequestMessageMatchers<RequestMessageHeaderMatcher>();
 222            var cookieMatchers = request.GetRequestMessageMatchers<RequestMessageCookieMatcher>();
 223            var paramsMatchers = request.GetRequestMessageMatchers<RequestMessageParamMatcher>();
 224            var bodyMatcher = request.GetRequestMessageMatcher<RequestMessageBodyMatcher>();
 225            var methodMatcher = request.GetRequestMessageMatcher<RequestMessageMethodMatcher>();
 26
 227            var mappingModel = new MappingModel
 228            {
 229                Guid = mapping.Guid,
 230                Title = mapping.Title,
 231                Priority = mapping.Priority,
 232                Scenario = mapping.Scenario,
 233                WhenStateIs = mapping.ExecutionConditionState,
 234                SetStateTo = mapping.NextState,
 235                Request = new RequestModel
 236                {
 237                    ClientIP = clientIPMatchers != null && clientIPMatchers.Any() ? new ClientIPModel
 238                    {
 239                        Matchers = MatcherMapper.Map(clientIPMatchers.Where(m => m.Matchers != null).SelectMany(m => m.M
 240                    } : null,
 241
 242                    Path = pathMatchers != null && pathMatchers.Any() ? new PathModel
 243                    {
 444                        Matchers = MatcherMapper.Map(pathMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Match
 245                    } : null,
 246
 247                    Url = urlMatchers != null && urlMatchers.Any() ? new UrlModel
 248                    {
 249                        Matchers = MatcherMapper.Map(urlMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matche
 250                    } : null,
 251
 252                    Methods = methodMatcher?.Methods,
 253
 254                    Headers = headerMatchers != null && headerMatchers.Any() ? headerMatchers.Select(hm => new HeaderMod
 255                    {
 256                        Name = hm.Name,
 257                        Matchers = MatcherMapper.Map(hm.Matchers)
 258                    }).ToList() : null,
 259
 260                    Cookies = cookieMatchers != null && cookieMatchers.Any() ? cookieMatchers.Select(cm => new CookieMod
 261                    {
 262                        Name = cm.Name,
 263                        Matchers = MatcherMapper.Map(cm.Matchers)
 264                    }).ToList() : null,
 265
 266                    Params = paramsMatchers != null && paramsMatchers.Any() ? paramsMatchers.Select(pm => new ParamModel
 267                    {
 268                        Name = pm.Key,
 269                        Matchers = MatcherMapper.Map(pm.Matchers)
 270                    }).ToList() : null,
 271
 272                    Body = methodMatcher?.Methods != null && methodMatcher.Methods.Any(m => m == "get") ? null : new Bod
 273                    {
 274                        Matcher = bodyMatcher != null ? MatcherMapper.Map(bodyMatcher.Matcher) : null
 275                    }
 276                },
 277                Response = new ResponseModel
 278                {
 279                    Delay = response.Delay?.Milliseconds
 280                }
 281            };
 82
 283            if (!string.IsNullOrEmpty(response.ProxyUrl))
 084            {
 085                mappingModel.Response.StatusCode = null;
 086                mappingModel.Response.Headers = null;
 087                mappingModel.Response.BodyDestination = null;
 088                mappingModel.Response.BodyAsJson = null;
 089                mappingModel.Response.BodyAsJsonIndented = null;
 090                mappingModel.Response.Body = null;
 091                mappingModel.Response.BodyAsBytes = null;
 092                mappingModel.Response.BodyAsFile = null;
 093                mappingModel.Response.BodyAsFileIsCached = null;
 094                mappingModel.Response.UseTransformer = false;
 095                mappingModel.Response.BodyEncoding = null;
 096                mappingModel.Response.ProxyUrl = response.ProxyUrl;
 097            }
 98            else
 299            {
 2100                mappingModel.Response.BodyDestination = response.ResponseMessage.BodyDestination;
 2101                mappingModel.Response.StatusCode = response.ResponseMessage.StatusCode;
 2102                mappingModel.Response.Headers = Map(response.ResponseMessage.Headers);
 2103                mappingModel.Response.BodyAsJson = response.ResponseMessage.BodyAsJson;
 2104                mappingModel.Response.BodyAsJsonIndented = response.ResponseMessage.BodyAsJsonIndented;
 2105                mappingModel.Response.Body = response.ResponseMessage.Body;
 2106                mappingModel.Response.BodyAsBytes = response.ResponseMessage.BodyAsBytes;
 2107                mappingModel.Response.BodyAsFile = response.ResponseMessage.BodyAsFile;
 2108                mappingModel.Response.BodyAsFileIsCached = response.ResponseMessage.BodyAsFileIsCached;
 2109                mappingModel.Response.UseTransformer = response.UseTransformer;
 110
 2111                if (response.ResponseMessage.BodyEncoding != null && response.ResponseMessage.BodyEncoding.WebName != "u
 0112                {
 0113                    mappingModel.Response.BodyEncoding = new EncodingModel
 0114                    {
 0115                        EncodingName = response.ResponseMessage.BodyEncoding.EncodingName,
 0116                        CodePage = response.ResponseMessage.BodyEncoding.CodePage,
 0117                        WebName = response.ResponseMessage.BodyEncoding.WebName
 0118                    };
 0119                }
 2120            }
 121
 2122            return mappingModel;
 2123        }
 124
 125        private static IDictionary<string, object> Map(IDictionary<string, WireMockList<string>> dictionary)
 2126        {
 2127            if (dictionary == null || dictionary.Count == 0)
 2128            {
 2129                return null;
 130            }
 131
 0132            var newDictionary = new Dictionary<string, object>();
 0133            foreach (var entry in dictionary)
 0134            {
 0135                object value = entry.Value.Count == 1 ? (object)entry.Value.ToString() : entry.Value;
 0136                newDictionary.Add(entry.Key, value);
 0137            }
 138
 0139            return newDictionary;
 2140        }
 141    }
 142}