Summary

Class:WireMock.Serialization.MappingConverter
Assembly:WireMock.Net
File(s):C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Serialization\MappingConverter.cs
Covered lines:103
Uncovered lines:25
Coverable lines:128
Total lines:162
Line coverage:80.4%
Branch coverage:41.6%

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
ToMappingModel(...)5152428869.7751.28
Map(...)5453.3340
Map(...)4200
Map(...)3200

File(s)

C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Serialization\MappingConverter.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using JetBrains.Annotations;
 5using WireMock.Admin.Mappings;
 6using WireMock.Matchers.Request;
 7using WireMock.RequestBuilders;
 8using WireMock.ResponseBuilders;
 9using WireMock.Util;
 10
 11namespace WireMock.Serialization
 12{
 13    internal static class MappingConverter
 14    {
 15        public static MappingModel ToMappingModel(Mapping mapping)
 116        {
 117            var request = (Request)mapping.RequestMatcher;
 118            var response = (Response)mapping.Provider;
 19
 120            var clientIPMatchers = request.GetRequestMessageMatchers<RequestMessageClientIPMatcher>();
 121            var pathMatchers = request.GetRequestMessageMatchers<RequestMessagePathMatcher>();
 122            var urlMatchers = request.GetRequestMessageMatchers<RequestMessageUrlMatcher>();
 123            var headerMatchers = request.GetRequestMessageMatchers<RequestMessageHeaderMatcher>();
 124            var cookieMatchers = request.GetRequestMessageMatchers<RequestMessageCookieMatcher>();
 125            var paramsMatchers = request.GetRequestMessageMatchers<RequestMessageParamMatcher>();
 126            var bodyMatcher = request.GetRequestMessageMatcher<RequestMessageBodyMatcher>();
 127            var methodMatcher = request.GetRequestMessageMatcher<RequestMessageMethodMatcher>();
 28
 129             var mappingModel = new MappingModel
 130            {
 131                Guid = mapping.Guid,
 132                Title = mapping.Title,
 133                Priority = mapping.Priority,
 134                Scenario = mapping.Scenario,
 135                WhenStateIs = mapping.ExecutionConditionState,
 136                SetStateTo = mapping.NextState,
 137                Request = new RequestModel
 138                {
 139                    ClientIP = clientIPMatchers != null && clientIPMatchers.Any() ? new ClientIPModel
 140                    {
 141                        Matchers = MatcherMapper.Map(clientIPMatchers.Where(m => m.Matchers != null).SelectMany(m => m.M
 142                        Funcs = Map(clientIPMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
 143                    } : null,
 144
 145                    Path = pathMatchers != null && pathMatchers.Any() ? new PathModel
 146                    {
 147                        Matchers = MatcherMapper.Map(pathMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Match
 148                        Funcs = Map(pathMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
 149                    } : null,
 150
 151                    Url = urlMatchers != null && urlMatchers.Any() ? new UrlModel
 152                    {
 153                        Matchers = MatcherMapper.Map(urlMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matche
 154                        Funcs = Map(urlMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
 155                    } : null,
 156
 157                    Methods = methodMatcher?.Methods,
 158
 159                    Headers = headerMatchers != null && headerMatchers.Any() ? headerMatchers.Select(hm => new HeaderMod
 160                    {
 161                        Name = hm.Name,
 162                        Matchers = MatcherMapper.Map(hm.Matchers),
 163                        Funcs = Map(hm.Funcs)
 164                    }).ToList() : null,
 165
 166                    Cookies = cookieMatchers != null && cookieMatchers.Any() ? cookieMatchers.Select(cm => new CookieMod
 167                    {
 168                        Name = cm.Name,
 169                        Matchers = MatcherMapper.Map(cm.Matchers),
 170                        Funcs = Map(cm.Funcs)
 171                    }).ToList() : null,
 172
 173                     Params = paramsMatchers != null && paramsMatchers.Any() ? paramsMatchers.Select(pm => new ParamModel
 174                    {
 175                        Name = pm.Key,
 176                        Values = pm.Values?.ToList(),
 177                        Funcs = Map(pm.Funcs)
 178                    }).ToList() : null,
 179
 180                    Body = methodMatcher?.Methods != null && methodMatcher.Methods.Any(m => m == "get") ? null : new Bod
 181                    {
 182                        Matcher = bodyMatcher != null ? MatcherMapper.Map(bodyMatcher.Matcher) : null,
 183                        Func = bodyMatcher != null ? Map(bodyMatcher.Func) : null,
 184                        DataFunc = bodyMatcher != null ? Map(bodyMatcher.DataFunc) : null
 185                    }
 186                },
 187                Response = new ResponseModel
 188                {
 189                    Delay = response.Delay?.Milliseconds
 190                }
 191            };
 92
 193             if (!string.IsNullOrEmpty(response.ProxyUrl))
 094            {
 095                mappingModel.Response.StatusCode = null;
 096                mappingModel.Response.Headers = null;
 097                mappingModel.Response.BodyDestination = null;
 098                mappingModel.Response.BodyAsJson = null;
 099                mappingModel.Response.Body = null;
 0100                mappingModel.Response.BodyAsBytes = null;
 0101                mappingModel.Response.BodyAsFile = null;
 0102                mappingModel.Response.BodyAsFileIsCached = null;
 0103                mappingModel.Response.UseTransformer = false;
 0104                mappingModel.Response.BodyEncoding = null;
 0105                mappingModel.Response.ProxyUrl = response.ProxyUrl;
 0106            }
 107            else
 1108            {
 1109                mappingModel.Response.BodyDestination = response.ResponseMessage.BodyDestination;
 1110                mappingModel.Response.StatusCode = response.ResponseMessage.StatusCode;
 1111                mappingModel.Response.Headers = Map(response.ResponseMessage.Headers);
 1112                mappingModel.Response.BodyAsJson = response.ResponseMessage.BodyAsJson;
 1113                mappingModel.Response.Body = response.ResponseMessage.Body;
 1114                mappingModel.Response.BodyAsBytes = response.ResponseMessage.BodyAsBytes;
 1115                mappingModel.Response.BodyAsFile = response.ResponseMessage.BodyAsFile;
 1116                mappingModel.Response.BodyAsFileIsCached = response.ResponseMessage.BodyAsFileIsCached;
 1117                mappingModel.Response.UseTransformer = response.UseTransformer;
 118
 1119                 if (response.ResponseMessage.BodyEncoding != null)
 1120                {
 1121                    mappingModel.Response.BodyEncoding = new EncodingModel
 1122                    {
 1123                        EncodingName = response.ResponseMessage.BodyEncoding.EncodingName,
 1124                        CodePage = response.ResponseMessage.BodyEncoding.CodePage,
 1125                        WebName = response.ResponseMessage.BodyEncoding.WebName
 1126                    };
 1127                }
 1128            }
 129
 1130            return mappingModel;
 1131        }
 132
 133        private static IDictionary<string, object> Map(IDictionary<string, WireMockList<string>> dictionary)
 1134        {
 1135             if (dictionary == null)
 0136            {
 0137                return null;
 138            }
 139
 1140            var newDictionary = new Dictionary<string, object>();
 3141            foreach (var entry in dictionary)
 0142            {
 0143                 object value = entry.Value.Count == 1 ? (object)entry.Value.ToString() : entry.Value;
 0144                newDictionary.Add(entry.Key, value);
 0145            }
 146
 1147            return newDictionary;
 1148        }
 149
 150
 151
 152        private static string[] Map<T>([CanBeNull] IEnumerable<Func<T, bool>> funcs)
 0153        {
 0154             return funcs?.Select(Map).Where(x => x != null).ToArray();
 0155        }
 156
 157        private static string Map<T>([CanBeNull] Func<T, bool> func)
 0158        {
 0159             return func?.ToString();
 0160        }
 161    }
 162}