예제 #1
0
        public void Upsert_Creates_Record_When_It_Does_Not_Exist_Using_Alternate_Key()
        {
            var context = new XrmFakedContext();

            context.ProxyTypesAssembly = Assembly.GetExecutingAssembly();
            context.InitializeMetadata(Assembly.GetExecutingAssembly());
            var service = context.GetOrganizationService();

            var metadata = context.GetEntityMetadataByName("contact");

            metadata.SetFieldValue("_keys", new EntityKeyMetadata[]
            {
                new EntityKeyMetadata()
                {
                    KeyAttributes = new string[] { "firstname" }
                }
            });
            context.SetEntityMetadata(metadata);
            var contact = new Contact()
            {
                FirstName = "FakeXrm",
                LastName  = "Easy"
            };

            contact.KeyAttributes.Add("firstname", contact.FirstName);

            var request = new UpsertRequest()
            {
                Target = contact
            };

            var response = (UpsertResponse)service.Execute(request);

            Assert.Equal(true, response.RecordCreated);
        }
예제 #2
0
 private void SetPrimaryIdAttributes(XrmFakedContext context)
 {
     foreach (var entity in context.CreateMetadataQuery())
     {
         typeof(EntityMetadata).GetProperty(nameof(EntityMetadata.PrimaryIdAttribute)).SetValue(entity, entity.LogicalName + "id");
         var attr = entity.Attributes.Single(a => a.LogicalName == entity.LogicalName + "id");
         typeof(AttributeMetadata).GetProperty(nameof(AttributeMetadata.IsPrimaryId)).SetValue(attr, true);
         context.SetEntityMetadata(entity);
     }
 }
예제 #3
0
        public ModelIntegrationTests()
        {
            fakedContext = new XrmFakedContext();
            fakedContext.SetEntityMetadata(BusinessUnitMetadata.GetMetadata());
            //fakedContext.ProxyTypesAssembly = Assembly.GetExecutingAssembly();

            organizationService = fakedContext.GetFakedOrganizationService();
            testContext         = new TestModelContext(organizationService);

            bu = new BusinessUnit()
            {
                _name = "GG"
            };
            bu.Id = testContext.Create(bu);

            t = new Team()
            {
                _name = "GUGI"
            };
            t.Id = testContext.Create(t);

            Agust = new User()
            {
                _name   = "Agust",
                _bu     = new EntityReference("", bu.Id),
                _teamid = new EntityReference("", t.Id),
            };

            Agust.Id = testContext.Create(Agust);

            Andrea = new User()
            {
                _name   = "Andrea",
                _bu     = new EntityReference("", bu.Id),
                _teamid = new EntityReference("", t.Id),
            };

            Andrea.Id = testContext.Create(Andrea);
        }
        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
        {
            var req = request as InsertOptionValueRequest;

            if (req.Label == null)
            {
                throw new Exception("Label must not be null");
            }

            if (string.IsNullOrWhiteSpace(req.Label.LocalizedLabels[0].Label))
            {
                throw new Exception("Label must not be empty");
            }

            if (string.IsNullOrEmpty(req.OptionSetName) &&
                (string.IsNullOrEmpty(req.EntityLogicalName) ||
                 string.IsNullOrEmpty(req.AttributeLogicalName)))
            {
                throw new Exception("At least OptionSetName or both the EntityName and AttributeName must not be provided");
            }

            string key = "";

            if (!string.IsNullOrWhiteSpace(req.OptionSetName))
            {
                key = req.OptionSetName;
            }
            else
            {
                key = string.Format("{0}#{1}", req.EntityLogicalName, req.AttributeLogicalName);
            }

            if (!ctx.OptionSetValuesMetadata.ContainsKey(key))
            {
                ctx.OptionSetValuesMetadata.Add(key, new OptionSetMetadata());
            }

            var optionSetMetadata = ctx.OptionSetValuesMetadata[key];

            optionSetMetadata.Options.Add(new OptionMetadata()
            {
                MetadataId = Guid.NewGuid(),
                Value      = req.Value,
                Label      = req.Label
            });

            if (!string.IsNullOrEmpty(req.EntityLogicalName))
            {
                var entityMetadata = ctx.GetEntityMetadataByName(req.EntityLogicalName);
                if (entityMetadata != null)
                {
                    var attribute = entityMetadata
                                    .Attributes
                                    .FirstOrDefault(a => a.LogicalName == req.AttributeLogicalName);

                    if (attribute == null)
                    {
                        throw new Exception($"You are trying to insert an option set value for entity '{req.EntityLogicalName}' with entity metadata associated but the attribute '{req.AttributeLogicalName}' doesn't exist in metadata");
                    }

                    if (!(attribute is EnumAttributeMetadata))
                    {
                        throw new Exception($"You are trying to insert an option set value for entity '{req.EntityLogicalName}' with entity metadata associated but the attribute '{req.AttributeLogicalName}' is not a valid option set field (not a subtype of EnumAttributeMetadata)");
                    }

                    var enumAttribute = attribute as EnumAttributeMetadata;

                    var options = enumAttribute.OptionSet == null ? new OptionMetadataCollection() : enumAttribute.OptionSet.Options;

                    options.Add(new OptionMetadata(req.Label, req.Value));
                    enumAttribute.OptionSet = new OptionSetMetadata(options);

                    entityMetadata.SetAttribute(enumAttribute);
                    ctx.SetEntityMetadata(entityMetadata);
                }
            }
            return(new InsertOptionValueResponse());
        }
        private string ConvertFetchToOData(string fetch)
        {
            var context = new XrmFakedContext();

            // Add basic metadata
            var relationships = new[]
            {
                new OneToManyRelationshipMetadata
                {
                    SchemaName           = "contact_customer_accounts",
                    ReferencedEntity     = "account",
                    ReferencedAttribute  = "accountid",
                    ReferencingEntity    = "contact",
                    ReferencingAttribute = "parentcustomerid"
                },
                new OneToManyRelationshipMetadata
                {
                    SchemaName           = "account_primarycontact",
                    ReferencedEntity     = "contact",
                    ReferencedAttribute  = "contactid",
                    ReferencingEntity    = "account",
                    ReferencingAttribute = "primarycontactid"
                }
            };

            var entities = new[]
            {
                new EntityMetadata
                {
                    LogicalName   = "account",
                    EntitySetName = "accounts"
                },
                new EntityMetadata
                {
                    LogicalName   = "contact",
                    EntitySetName = "contacts"
                },
                new EntityMetadata
                {
                    LogicalName   = "connection",
                    EntitySetName = "connections"
                },
                new EntityMetadata
                {
                    LogicalName   = "webresource",
                    EntitySetName = "webresourceset"
                },
                new EntityMetadata
                {
                    LogicalName   = "stringmap",
                    EntitySetName = "stringmaps"
                },
                new EntityMetadata
                {
                    LogicalName   = "incident",
                    EntitySetName = "incidents"
                }
            };

            var attributes = new Dictionary <string, AttributeMetadata[]>
            {
                ["account"] = new AttributeMetadata[]
                {
                    new UniqueIdentifierAttributeMetadata
                    {
                        LogicalName = "accountid"
                    },
                    new StringAttributeMetadata
                    {
                        LogicalName = "name"
                    },
                    new StringAttributeMetadata
                    {
                        LogicalName = "websiteurl"
                    },
                    new LookupAttributeMetadata
                    {
                        LogicalName = "primarycontactid",
                        Targets     = new[] { "contact" }
                    }
                },
                ["contact"] = new AttributeMetadata[]
                {
                    new UniqueIdentifierAttributeMetadata
                    {
                        LogicalName = "contactid"
                    },
                    new StringAttributeMetadata
                    {
                        LogicalName = "firstname"
                    },
                    new LookupAttributeMetadata
                    {
                        LogicalName = "parentcustomerid",
                        Targets     = new[] { "account", "contact" }
                    },
                    new DateTimeAttributeMetadata
                    {
                        LogicalName = "createdon"
                    }
                },
                ["connection"] = new AttributeMetadata[]
                {
                    new UniqueIdentifierAttributeMetadata
                    {
                        LogicalName = "connectionid"
                    },
                    new PicklistAttributeMetadata
                    {
                        LogicalName = "record1objecttypecode"
                    }
                },
                ["incident"] = new AttributeMetadata[]
                {
                    new UniqueIdentifierAttributeMetadata
                    {
                        LogicalName = "incidentid"
                    }
                },
                ["stringmap"] = new AttributeMetadata[]
                {
                    new UniqueIdentifierAttributeMetadata
                    {
                        LogicalName = "stringmapid"
                    },
                    new EntityNameAttributeMetadata
                    {
                        LogicalName = "objecttypecode"
                    },
                    new StringAttributeMetadata
                    {
                        LogicalName = "attributename"
                    },
                    new IntegerAttributeMetadata
                    {
                        LogicalName = "attributevalue"
                    },
                    new StringAttributeMetadata
                    {
                        LogicalName = "value"
                    }
                },
                ["webresource"] = new AttributeMetadata[]
                {
                    new UniqueIdentifierAttributeMetadata
                    {
                        LogicalName = "webresourceid"
                    },
                    new StringAttributeMetadata
                    {
                        LogicalName = "name"
                    },
                    new ManagedPropertyAttributeMetadata
                    {
                        LogicalName = "iscustomizable"
                    }
                }
            };

            SetSealedProperty(attributes["webresource"].Single(a => a.LogicalName == "iscustomizable"), nameof(ManagedPropertyAttributeMetadata.ValueAttributeTypeCode), AttributeTypeCode.Boolean);
            SetRelationships(entities, relationships);
            SetAttributes(entities, attributes);
            SetSealedProperty(entities.Single(e => e.LogicalName == "incident"), nameof(EntityMetadata.ObjectTypeCode), 112);

            foreach (var entity in entities)
            {
                context.SetEntityMetadata(entity);
            }

            context.AddFakeMessageExecutor <RetrieveAllEntitiesRequest>(new RetrieveAllEntitiesRequestExecutor(entities));
            var org       = context.GetOrganizationService();
            var converter = new FetchXmlToWebAPIConverter(new MetadataProvider(org), $"https://example.crm.dynamics.com/api/data/v9.0");

            return(converter.ConvertFetchXmlToWebAPI(fetch));
        }