Summary

Class:FakeXrmEasy.FakeMessageExecutors.InsertOptionValueRequestExecutor
Assembly:FakeXrmEasy
File(s):C:\code\jordimontana82\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\InsertOptionValueRequestExecutor.cs
Covered lines:50
Uncovered lines:5
Coverable lines:55
Total lines:91
Line coverage:90.9%
Branch coverage:77.7%

Metrics

MethodCyclomatic ComplexitySequence CoverageBranch Coverage
CanExecute(...)1100100
Execute(...)1487.578.95
GetResponsibleRequestType()1100100

File(s)

C:\code\jordimontana82\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\InsertOptionValueRequestExecutor.cs

#LineLine coverage
 1using Microsoft.Xrm.Sdk;
 2using Microsoft.Xrm.Sdk.Messages;
 3using Microsoft.Xrm.Sdk.Metadata;
 4using System;
 5using System.Linq;
 6using FakeXrmEasy.Extensions;
 7
 8namespace FakeXrmEasy.FakeMessageExecutors
 9{
 10    public class InsertOptionValueRequestExecutor : IFakeMessageExecutor
 11    {
 12        public bool CanExecute(OrganizationRequest request)
 5413        {
 5414            return request is InsertOptionValueRequest;
 5415        }
 16
 17        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
 5418        {
 5419            var req = request as InsertOptionValueRequest;
 20
 5421             if (req.Label == null)
 022                throw new Exception("Label must not be null");
 23
 5424             if (string.IsNullOrWhiteSpace(req.Label.LocalizedLabels[0].Label))
 625            {
 626                throw new Exception("Label must not be empty");
 27            }
 28
 4829             if (string.IsNullOrEmpty(req.OptionSetName)
 4830                && (string.IsNullOrEmpty(req.EntityLogicalName)
 4831                || string.IsNullOrEmpty(req.AttributeLogicalName)))
 1832            {
 1833                throw new Exception("At least OptionSetName or both the EntityName and AttributeName must not be provide
 34            }
 35
 3036            string key = "";
 3037             if (!string.IsNullOrWhiteSpace(req.OptionSetName))
 638                key = req.OptionSetName;
 39            else
 2440                key = string.Format("{0}#{1}", req.EntityLogicalName, req.AttributeLogicalName);
 41
 3042             if (!ctx.OptionSetValuesMetadata.ContainsKey(key))
 3043                ctx.OptionSetValuesMetadata.Add(key, new OptionSetMetadata());
 44
 3045            var optionSetMetadata = ctx.OptionSetValuesMetadata[key];
 3046            optionSetMetadata.Options.Add(new OptionMetadata()
 3047            {
 3048                MetadataId = Guid.NewGuid(),
 3049                Value = req.Value,
 3050                Label = req.Label
 3051            });
 52
 3053             if (!string.IsNullOrEmpty(req.EntityLogicalName))
 2454            {
 2455                var entityMetadata = ctx.GetEntityMetadataByName(req.EntityLogicalName);
 2456                 if (entityMetadata != null)
 1857                {
 1858                    var attribute = entityMetadata
 1859                            .Attributes
 3660                            .FirstOrDefault(a => a.LogicalName == req.AttributeLogicalName);
 61
 1862                     if (attribute == null)
 063                    {
 064                        throw new Exception($"You are trying to insert an option set value for entity '{req.EntityLogica
 65                    }
 66
 1867                     if (!(attribute is EnumAttributeMetadata))
 068                    {
 069                        throw new Exception($"You are trying to insert an option set value for entity '{req.EntityLogica
 70                    }
 71
 1872                    var enumAttribute = attribute as EnumAttributeMetadata;
 73
 1874                    var options = enumAttribute.OptionSet == null ? new OptionMetadataCollection() : enumAttribute.Optio
 75
 1876                    options.Add(new OptionMetadata(req.Label, req.Value));
 1877                    enumAttribute.OptionSet = new OptionSetMetadata(options);
 78
 1879                    entityMetadata.SetAttribute(enumAttribute);
 1880                    ctx.SetEntityMetadata(entityMetadata);
 1881                }
 2482            }
 3083            return new InsertOptionValueResponse();
 3084        }
 85
 86        public Type GetResponsibleRequestType()
 427087        {
 427088            return typeof(InsertOptionValueRequest);
 427089        }
 90    }
 91}