Summary

Class:FakeXrmEasy.FakeMessageExecutors.InsertStatusValueRequestExecutor
Assembly:FakeXrmEasy
File(s):F:\Git\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\InsertStatusValueRequestExecutor.cs
Covered lines:46
Uncovered lines:9
Coverable lines:55
Total lines:95
Line coverage:83.6%
Branch coverage:50%

Metrics

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

File(s)

F:\Git\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\InsertStatusValueRequestExecutor.cs

#LineLine coverage
 1using Microsoft.Xrm.Sdk;
 2using Microsoft.Xrm.Sdk.Messages;
 3using Microsoft.Xrm.Sdk.Metadata;
 4using System;
 5using System.Diagnostics;
 6using System.Linq;
 7using FakeXrmEasy.Extensions;
 8
 9namespace FakeXrmEasy.FakeMessageExecutors
 10{
 11    public class InsertStatusValueRequestExecutor : IFakeMessageExecutor
 12    {
 13        public bool CanExecute(OrganizationRequest request)
 614        {
 615            return request is InsertStatusValueRequest;
 616        }
 17
 18        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
 619        {
 620            var req = request as InsertStatusValueRequest;
 21
 22            Debug.Assert(req != null, nameof(req) + " != null");
 623             if (req.Label == null)
 024                throw new Exception("Label must not be null");
 25
 626             if (string.IsNullOrWhiteSpace(req.Label.LocalizedLabels[0].Label))
 027            {
 028                throw new Exception("Label must not be empty");
 29            }
 30
 631             if (string.IsNullOrEmpty(req.OptionSetName)
 632                && (string.IsNullOrEmpty(req.EntityLogicalName)
 633                || string.IsNullOrEmpty(req.AttributeLogicalName)))
 034            {
 035                throw new Exception("At least OptionSetName or both the EntityName and AttributeName must not be provide
 36            }
 37
 638            var key = !string.IsNullOrWhiteSpace(req.OptionSetName) ? req.OptionSetName : $"{req.EntityLogicalName}#{req
 39
 640             if (!ctx.StatusAttributeMetadata.ContainsKey(key))
 641                ctx.StatusAttributeMetadata.Add(key, new StatusAttributeMetadata());
 42
 643            var statusValuesMetadata = ctx.StatusAttributeMetadata[key];
 44            //statusValuesMetadata.
 645            statusValuesMetadata.OptionSet = new OptionSetMetadata();
 646            statusValuesMetadata.OptionSet.Options.Add(new StatusOptionMetadata()
 647            {
 648                MetadataId = Guid.NewGuid(),
 649                Value = req.Value,
 650                Label = req.Label,
 651                State = req.StateCode,
 652                Description = req.Label
 653            });
 54
 55
 656             if (!string.IsNullOrEmpty(req.EntityLogicalName))
 657            {
 658                var entityMetadata = ctx.GetEntityMetadataByName(req.EntityLogicalName);
 659                 if (entityMetadata != null)
 660                {
 661                    var attribute = entityMetadata
 662                            .Attributes
 1263                            .FirstOrDefault(a => a.LogicalName == req.AttributeLogicalName);
 64
 665                     if (attribute == null)
 066                    {
 067                        throw new Exception($"You are trying to insert an option set value for entity '{req.EntityLogica
 68                    }
 69
 670                     if (!(attribute is EnumAttributeMetadata))
 071                    {
 072                        throw new Exception($"You are trying to insert an option set value for entity '{req.EntityLogica
 73                    }
 74
 675                    var enumAttribute = attribute as EnumAttributeMetadata;
 76
 677                    var options = enumAttribute.OptionSet == null ? new OptionMetadataCollection() : enumAttribute.Optio
 78
 679                    options.Add(new StatusOptionMetadata(){Value = req.Value, Label = req.Label, State = req.StateCode, 
 80
 681                    enumAttribute.OptionSet = new OptionSetMetadata(options);
 82
 683                    entityMetadata.SetAttribute(enumAttribute);
 684                    ctx.SetEntityMetadata(entityMetadata);
 685                }
 686            }
 687            return new InsertStatusValueResponse();
 688        }
 89
 90        public Type GetResponsibleRequestType()
 426491        {
 426492            return typeof(InsertStatusValueRequest);
 426493        }
 94    }
 95}