Summary

Class:FakeXrmEasy.FakeMessageExecutors.DisassociateRequestExecutor
Assembly:FakeXrmEasy
File(s):F:\Git\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\DisassociateRequestExecutor.cs
Covered lines:38
Uncovered lines:6
Coverable lines:44
Total lines:74
Line coverage:86.3%
Branch coverage:66.6%

Metrics

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

File(s)

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

#LineLine coverage
 1using Microsoft.Xrm.Sdk;
 2using Microsoft.Xrm.Sdk.Messages;
 3using Microsoft.Xrm.Sdk.Query;
 4using System;
 5using System.Linq;
 6
 7namespace FakeXrmEasy.FakeMessageExecutors
 8{
 9    public class DisassociateRequestExecutor : IFakeMessageExecutor
 10    {
 11        public bool CanExecute(OrganizationRequest request)
 612        {
 613            return request is DisassociateRequest;
 614        }
 15
 16        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
 2417        {
 2418            var disassociateRequest = request as DisassociateRequest;
 2419            var service = ctx.GetFakedOrganizationService();
 20
 2421             if (disassociateRequest == null)
 022            {
 023                throw new Exception("Only disassociate request can be processed!");
 24            }
 25
 2426            var relationShipName = disassociateRequest.Relationship.SchemaName;
 2427            var relationShip = ctx.GetRelationship(relationShipName);
 28
 2429             if (relationShip == null)
 030            {
 031                throw new Exception(string.Format("Relationship {0} does not exist in the metadata cache", relationShipN
 32            }
 33
 2434             if (disassociateRequest.Target == null)
 035            {
 036                throw new Exception("Disassociation without target is invalid!");
 37            }
 38
 14439            foreach (var relatedEntity in disassociateRequest.RelatedEntities)
 3640            {
 3641                var isFrom1to2 = disassociateRequest.Target.LogicalName == relationShip.Entity1LogicalName
 3642                                      || relatedEntity.LogicalName != relationShip.Entity1LogicalName
 3643                                      || String.IsNullOrWhiteSpace(disassociateRequest.Target.LogicalName);
 3644                 var fromAttribute = isFrom1to2 ? relationShip.Entity1Attribute : relationShip.Entity2Attribute;
 3645                 var toAttribute = isFrom1to2 ? relationShip.Entity2Attribute : relationShip.Entity1Attribute;
 46
 3647                var query = new QueryExpression(relationShip.IntersectEntity)
 3648                {
 3649                    ColumnSet = new ColumnSet(true),
 3650                    Criteria = new FilterExpression(LogicalOperator.And)
 3651                };
 52
 3653                query.Criteria.AddCondition(new ConditionExpression(fromAttribute,
 3654                    ConditionOperator.Equal, disassociateRequest.Target.Id));
 3655                query.Criteria.AddCondition(new ConditionExpression(toAttribute,
 3656                    ConditionOperator.Equal, relatedEntity.Id));
 57
 3658                var results = service.RetrieveMultiple(query);
 59
 3660                 if (results.Entities.Count == 1)
 3661                {
 3662                    service.Delete(relationShip.IntersectEntity, results.Entities.First().Id);
 3663                }
 3664            }
 65
 2466            return new DisassociateResponse();
 2467        }
 68
 69        public Type GetResponsibleRequestType()
 426470        {
 426471            return typeof(DisassociateRequest);
 426472        }
 73    }
 74}