/// <summary> /// A fake retrieve method that will query the FakedContext to retrieve the specified /// entity and Guid, or null, if the entity was not found /// </summary> /// <param name="context">The faked context</param> /// <param name="fakedService">The faked service where the Retrieve method will be faked</param> /// <returns></returns> protected static void FakeRetrieve(XrmFakedContext context, IOrganizationService fakedService) { A.CallTo(() => fakedService.Retrieve(A <string> ._, A <Guid> ._, A <ColumnSet> ._)) .ReturnsLazily((string entityName, Guid id, ColumnSet columnSet) => { if (string.IsNullOrWhiteSpace(entityName)) { throw new InvalidOperationException("The entity logical name must not be null or empty."); } if (id == Guid.Empty) { throw new InvalidOperationException("The id must not be empty."); } if (columnSet == null) { throw new InvalidOperationException("The columnset parameter must not be null."); } // Don't fail with invalid operation exception, if no record of this entity exists, but entity is known if (!context.Data.ContainsKey(entityName)) { if (context.ProxyTypesAssembly == null) { throw new InvalidOperationException(string.Format("The entity logical name {0} is not valid.", entityName)); } if (!context.ProxyTypesAssembly.GetTypes().Any(type => context.FindReflectedType(entityName) != null)) { throw new InvalidOperationException(string.Format("The entity logical name {0} is not valid.", entityName)); } } //Entity logical name exists, so , check if the requested entity exists if (context.Data.ContainsKey(entityName) && context.Data[entityName] != null && context.Data[entityName].ContainsKey(id)) { //Entity found => return only the subset of columns specified or all of them if (columnSet.AllColumns) { return(context.Data[entityName][id]); } else { //Return the subset of columns requested only var foundEntity = context.Data[entityName][id]; Entity projected = foundEntity.ProjectAttributes(columnSet, context); return(projected); } } else { //Entity not found in the context => return null throw new FaultException <OrganizationServiceFault>(new OrganizationServiceFault(), string.Format("{0} With Id = {1} Does Not Exist", entityName, id.ToString("D"))); } }); }
/// <summary> /// A fake retrieve method that will query the FakedContext to retrieve the specified /// entity and Guid, or null, if the entity was not found /// </summary> /// <param name="context">The faked context</param> /// <param name="fakedService">The faked service where the Retrieve method will be faked</param> /// <returns></returns> protected static void FakeRetrieve(XrmFakedContext context, IOrganizationService fakedService) { A.CallTo(() => fakedService.Retrieve(A<string>._, A<Guid>._, A<ColumnSet>._)) .ReturnsLazily((string entityName, Guid id, ColumnSet columnSet) => { if (string.IsNullOrWhiteSpace(entityName)) { throw new InvalidOperationException("The entity logical name must not be null or empty."); } if (id == Guid.Empty) { throw new InvalidOperationException("The id must not be empty."); } if (columnSet == null) { throw new InvalidOperationException("The columnset parameter must not be null."); } // Don't fail with invalid operation exception, if no record of this entity exists, but entity is known if (!context.Data.ContainsKey(entityName)) { if (context.ProxyTypesAssembly == null) { throw new InvalidOperationException(string.Format("The entity logical name {0} is not valid.", entityName)); } if (!context.ProxyTypesAssembly.GetTypes().Any(type => context.FindReflectedType(entityName) != null)) { throw new InvalidOperationException(string.Format("The entity logical name {0} is not valid.", entityName)); } } //Entity logical name exists, so , check if the requested entity exists if (context.Data.ContainsKey(entityName) && context.Data[entityName] != null && context.Data[entityName].ContainsKey(id)) { //Entity found => return only the subset of columns specified or all of them if (columnSet.AllColumns) return context.Data[entityName][id]; else { //Return the subset of columns requested only var foundEntity = context.Data[entityName][id]; Entity projected = foundEntity.ProjectAttributes(columnSet,context); return projected; } } else { //Entity not found in the context => return null throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), string.Format("{0} With Id = {1} Does Not Exist", entityName, id.ToString("D"))); } }); }
/// <summary> /// Fakes the delete method. Very similar to the Retrieve one /// </summary> /// <param name="context"></param> /// <param name="fakedService"></param> protected static void FakeDelete(XrmFakedContext context, IOrganizationService fakedService) { A.CallTo(() => fakedService.Delete(A <string> ._, A <Guid> ._)) .Invokes((string entityName, Guid id) => { if (string.IsNullOrWhiteSpace(entityName)) { throw new InvalidOperationException("The entity logical name must not be null or empty."); } if (id == Guid.Empty) { throw new InvalidOperationException("The id must not be empty."); } // Don't fail with invalid operation exception, if no record of this entity exists, but entity is known if (!context.Data.ContainsKey(entityName)) { if (context.ProxyTypesAssembly == null) { throw new InvalidOperationException(string.Format("The entity logical name {0} is not valid.", entityName)); } if (!context.ProxyTypesAssembly.GetTypes().Any(type => context.FindReflectedType(entityName) != null)) { throw new InvalidOperationException(string.Format("The entity logical name {0} is not valid.", entityName)); } } //Entity logical name exists, so , check if the requested entity exists if (context.Data.ContainsKey(entityName) && context.Data[entityName] != null && context.Data[entityName].ContainsKey(id)) { //Entity found => return only the subset of columns specified or all of them context.Data[entityName].Remove(id); } else { //Entity not found in the context => throw not found exception //The entity record was not found, return a CRM-ish update error message throw new FaultException <OrganizationServiceFault>(new OrganizationServiceFault(), string.Format("{0} with Id {1} Does Not Exist", entityName, id)); } }); }