Summary

Class:FakeXrmEasy.FakeMessageExecutors.LoseOpportunityRequestExecutor
Assembly:FakeXrmEasy
File(s):C:\code\jordimontana82\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\LoseOpportunityRequestExecutor.cs
Covered lines:24
Uncovered lines:2
Coverable lines:26
Total lines:56
Line coverage:92.3%
Branch coverage:50%

Metrics

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

File(s)

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

#LineLine coverage
 1using System;
 2using System.Linq;
 3using Microsoft.Xrm.Sdk;
 4using Microsoft.Crm.Sdk.Messages;
 5
 6namespace FakeXrmEasy.FakeMessageExecutors
 7{
 8    public class LoseOpportunityRequestExecutor : IFakeMessageExecutor
 9    {
 10        public bool CanExecute(OrganizationRequest request)
 611        {
 612            return request is LoseOpportunityRequest;
 613        }
 14
 15        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
 616        {
 617            var req = request as LoseOpportunityRequest;
 18
 19            // Check if OpportunityClose and Status were passed to request
 620             if (req.OpportunityClose != null &&
 621                req.Status != null)
 622            {
 23                // LoseOpportunityRequest.OpportunityClose.OpportunityId
 624                var opportunityReference = req.OpportunityClose.GetAttributeValue<EntityReference>("opportunityid");
 625                var opportunityId = opportunityReference.Id;
 26
 27                // Get Opportunities (in good scenario, should return 1 record)
 628                var opportunities = (from op in ctx.CreateQuery("opportunity")
 629                                     where op.Id == opportunityId
 630                                     select op);
 31
 32                // More than one if to check and give better feedback to user
 633                 if (opportunities.Count() < 1) throw new Exception(string.Format("No Opportunity found with Id = {0}", o
 634                 else if (opportunities.Count() > 1) throw new Exception(string.Format("More than one Opportunity found w
 35                else
 636                {
 637                    var opportunity = opportunities.FirstOrDefault();
 638                    opportunity.Attributes["statuscode"] = req.Status;
 39
 640                    ctx.GetOrganizationService().Update(opportunity);
 41
 642                    return new LoseOpportunityResponse();
 43                }
 44            }
 45            else
 046            {
 047                throw new Exception("OpportunityClose or Status was not passed to request.");
 48            }
 649        }
 50
 51        public Type GetResponsibleRequestType()
 427052        {
 427053            return typeof(LoseOpportunityRequest);
 427054        }
 55    }
 56}