Summary

Class:FakeXrmEasy.FakeMessageExecutors.AssociateRequestExecutor
Assembly:FakeXrmEasy
File(s):C:\code\jordimontana82\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\AssociateRequestExecutor.cs
Covered lines:65
Uncovered lines:0
Coverable lines:65
Total lines:103
Line coverage:100%
Branch coverage:100%

Metrics

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

File(s)

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

#LineLine coverage
 1using Microsoft.Xrm.Sdk;
 2using Microsoft.Xrm.Sdk.Messages;
 3using System;
 4using System.Linq;
 5
 6namespace FakeXrmEasy.FakeMessageExecutors
 7{
 8    public class AssociateRequestExecutor : IFakeMessageExecutor
 9    {
 10        public bool CanExecute(OrganizationRequest request)
 3611        {
 3612            return request is AssociateRequest;
 3613        }
 14
 15        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
 18016        {
 18017            var associateRequest = request as AssociateRequest;
 18018            var service = ctx.GetFakedOrganizationService();
 19
 18020             if (associateRequest == null)
 621            {
 622                throw new Exception("Only associate request can be processed!");
 23            }
 24
 17425            var associateRelationship = associateRequest.Relationship;
 17426            var relationShipName = associateRelationship.SchemaName;
 17427            var fakeRelationShip = ctx.GetRelationship(relationShipName);
 28
 17429             if (fakeRelationShip == null)
 630            {
 631                throw new Exception(string.Format("Relationship {0} does not exist in the metadata cache", relationShipN
 32            }
 33
 16834             if (associateRequest.Target == null)
 635            {
 636                throw new Exception("Association without target is invalid!");
 37            }
 38
 149439            foreach (var relatedEntityReference in associateRequest.RelatedEntities)
 51040            {
 51041                 if (fakeRelationShip.RelationshipType == XrmFakedRelationship.enmFakeRelationshipType.ManyToMany)
 49842                {
 49843                    var isFrom1to2 = associateRequest.Target.LogicalName == fakeRelationShip.Entity1LogicalName
 49844                                         || relatedEntityReference.LogicalName != fakeRelationShip.Entity1LogicalName
 49845                                         || String.IsNullOrWhiteSpace(associateRequest.Target.LogicalName);
 49846                     var fromAttribute = isFrom1to2 ? fakeRelationShip.Entity1Attribute : fakeRelationShip.Entity2Attribu
 49847                     var toAttribute = isFrom1to2 ? fakeRelationShip.Entity2Attribute : fakeRelationShip.Entity1Attribute
 49848                     var fromEntityName = isFrom1to2 ? fakeRelationShip.Entity1LogicalName : fakeRelationShip.Entity2Logi
 49849                     var toEntityName = isFrom1to2 ? fakeRelationShip.Entity2LogicalName : fakeRelationShip.Entity1Logica
 50
 51                    //Check records exist
 49852                    var targetExists = ctx.CreateQuery(fromEntityName)
 49853                                                .Where(e => e.Id == associateRequest.Target.Id)
 49854                                                .FirstOrDefault() != null;
 55
 49856                     if (!targetExists)
 657                    {
 658                        throw new Exception(string.Format("{0} with Id {1} doesn't exist", fromEntityName, associateRequ
 59                    }
 60
 49261                    var relatedExists = ctx.CreateQuery(toEntityName)
 49262                                                .Where(e => e.Id == relatedEntityReference.Id)
 49263                                                .FirstOrDefault() != null;
 64
 49265                     if (!relatedExists)
 666                    {
 667                        throw new Exception(string.Format("{0} with Id {1} doesn't exist", toEntityName, relatedEntityRe
 68                    }
 69
 48670                    var association = new Entity(fakeRelationShip.IntersectEntity)
 48671                    {
 48672                        Attributes = new AttributeCollection
 48673                        {
 48674                            { fromAttribute, associateRequest.Target.Id },
 48675                            { toAttribute, relatedEntityReference.Id }
 48676                        }
 48677                    };
 78
 48679                    service.Create(association);
 48680                }
 81                else
 1282                {
 83                    //One to many
 84                    //Get entity to update
 1285                    var entityToUpdate = new Entity(relatedEntityReference.LogicalName)
 1286                    {
 1287                        Id = relatedEntityReference.Id
 1288                    };
 89
 1290                    entityToUpdate[fakeRelationShip.Entity2Attribute] = associateRequest.Target;
 1291                    service.Update(entityToUpdate);
 1292                }
 49893            }
 94
 15095            return new AssociateResponse();
 15096        }
 97
 98        public Type GetResponsibleRequestType()
 427099        {
 4270100            return typeof(AssociateRequest);
 4270101        }
 102    }
 103}