/// <summary> /// Fakes out calls to RetireveAttribute Requests, using enums to generate the OptionSetMetaData. Userful for mocking out any calls to determine the text values of an optionset. /// </summary> /// <param name="defaultLangaugeCode">The default langauge code. Defaults to reading DefaultLanguageCode from the config, or 1033 if not found</param> /// <returns></returns> public TDerived WithLocalOptionSetsRetrievedFromEnum(int?defaultLangaugeCode = null) { defaultLangaugeCode = defaultLangaugeCode ?? AppConfig.DefaultLanguageCode; ExecuteFuncs.Add((s, r) => { var attRequest = r as RetrieveAttributeRequest; if (attRequest == null) { return(s.Execute(r)); } var response = new RetrieveAttributeResponse(); var optionSet = new PicklistAttributeMetadata { OptionSet = new OptionSetMetadata() }; response.Results["AttributeMetadata"] = optionSet; var enumExpression = CrmServiceUtility.GetEarlyBoundProxyAssembly().GetTypes().Where(t => (t.Name == attRequest.EntityLogicalName + "_" + attRequest.LogicalName || t.Name == attRequest.LogicalName + "_" + attRequest.EntityLogicalName) && t.GetCustomAttributes(typeof(DataContractAttribute), false).Length > 0 && t.GetCustomAttributes(typeof(System.CodeDom.Compiler.GeneratedCodeAttribute), false).Length > 0); // Return EntityLogicalName_Logical Name first var enumType = enumExpression.OrderBy(t => t.Name != attRequest.EntityLogicalName + "_" + attRequest.LogicalName).FirstOrDefault(); if (enumType == null) { throw new Exception($"Unable to find local optionset enum for entity: {attRequest.EntityLogicalName}, attribute: {attRequest.LogicalName}"); } foreach (var value in Enum.GetValues(enumType)) { optionSet.OptionSet.Options.Add( new OptionMetadata { Value = (int)value, Label = new Label { UserLocalizedLabel = new LocalizedLabel(value.ToString(), defaultLangaugeCode.Value), }, }); } return(response); }) ; return(This); }
/// <summary> /// Builds this IOrganizationService. /// </summary> /// <returns></returns> public IOrganizationService Build() { ApplyNewEntityDefaultIds(); ApplyEntityFilter(); return(new FakeIOrganizationService(Service) { AssociateActions = AssociateActions.ToArray(), CreateFuncs = CreateFuncs.ToArray(), DeleteActions = DeleteActions.ToArray(), DisassociateActions = DisassociateActions.ToArray(), ExecuteFuncs = ExecuteFuncs.ToArray(), RetrieveMultipleFuncs = RetrieveMultipleFuncs.ToArray(), RetrieveFuncs = RetrieveFuncs.ToArray(), UpdateActions = UpdateActions.ToArray() }); }
private void ApplyNewEntityDefaultIds() { if (!NewEntityDefaultIds.Any()) { return; } CreateFuncs.Add((s, e) => { DefaultIdForEntity(e); return(s.Create(e)); }); ExecuteFuncs.Add((s, r) => { var email = r as SendEmailFromTemplateRequest; if (email != null) { DefaultIdForEntity(email.Target); } return(s.Execute(r)); }); }
/// <summary> /// Asserts failure whenever an action is requested that would perform an update (Create / Delete / Update) of some sort /// </summary> /// <returns></returns> public TDerived IsReadOnly() { AssociateActions.Add((s, n, i, r, c) => { TestSettings.TestFrameworkProvider.AssertFail("An attempt was made to Associate Entities with a ReadOnly Service"); }); WithNoCreates(); DeleteActions.Add((s, n, i) => { TestSettings.TestFrameworkProvider.AssertFail($"An attempt was made to Delete a(n) {n} Entity with id {i}, using a ReadOnly Service"); }); DisassociateActions.Add((s, n, i, r, c) => { TestSettings.TestFrameworkProvider.AssertFail("An attempt was made to Disassociate Entities with a ReadOnly Service"); }); ExecuteFuncs.Add((s, r) => { var readOnlyStartsWithNames = new List <string> { "CanBe", "CanManyToMany", "Download", "Execute", "Export", "FetchXmlToQueryExpression", "FindParentResourceGroup", "Get", "Is", "LocalTimeFromUtcTime", "Query", "Retrieve", "Search", "UtcTimeFromLocalTime", "WhoAmI" }; if (readOnlyStartsWithNames.Any(n => r.RequestName.StartsWith(n))) { return(s.Execute(r)); } throw TestSettings.TestFrameworkProvider.Value.GetFailedException($"An attempt was made to Execute Request {r.RequestName} with a ReadOnly Service"); }); UpdateActions.Add((s, e) => { throw TestSettings.TestFrameworkProvider.Value.GetFailedException($"An attempt was made to Update a(n) {e.LogicalName} Entity with id {e.Id}, using a ReadOnly Service"); }); return(This); }
/// <summary> /// Adds the fake execute. /// </summary> /// <param name="func">The function.</param> /// <returns></returns> public TDerived WithFakeExecute(params Func <IOrganizationService, OrganizationRequest, OrganizationResponse>[] func) { ExecuteFuncs.AddRange(func); return(This); }