/// <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); }
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); }