public void Disassociate_Disassociates_Entities()
        {
            //Arrange
            var fakeRelationship = new XrmFakedRelationship("rob_fake1id", "rob_fake2id", "rob_fake1", "rob_fake2");

            Fixture.Context.AddRelationship("rob_fake1_fake2", fakeRelationship);

            var primary   = GetER("rob_fake1");
            var secondary = GetER("rob_fake2");

            Fixture.Service.Associate("rob_fake1id", primary.Id, new Relationship("rob_fake1_fake2"), new EntityReferenceCollection {
                secondary
            });

            //Act
            var output = Record.Exception(() =>
            {
                Fixture.Helper.Disassociate("rob_fake1_fake2", primary, new EntityReferenceCollection {
                    secondary
                });
            });

            //Assert
            Assert.Null(output);
        }
Example #2
0
        private static object GetRelationshipMetadata(XrmFakedRelationship fakeRelationShip)
        {
            if (fakeRelationShip.RelationshipType == XrmFakedRelationship.enmFakeRelationshipType.ManyToMany)
            {
                var mtm = new Microsoft.Xrm.Sdk.Metadata.ManyToManyRelationshipMetadata();
                mtm.Entity1LogicalName        = fakeRelationShip.Entity1LogicalName;
                mtm.Entity1IntersectAttribute = fakeRelationShip.Entity1Attribute;
                mtm.Entity2LogicalName        = fakeRelationShip.Entity2LogicalName;
                mtm.Entity2IntersectAttribute = fakeRelationShip.Entity2Attribute;
                mtm.SchemaName          = fakeRelationShip.IntersectEntity;
                mtm.IntersectEntityName = fakeRelationShip.IntersectEntity.ToLower();
                return(mtm);
            }
            else
            {
                var otm = new Microsoft.Xrm.Sdk.Metadata.OneToManyRelationshipMetadata();
#if FAKE_XRM_EASY_2013 || FAKE_XRM_EASY_2015 || FAKE_XRM_EASY
#else
                otm.ReferencedEntityNavigationPropertyName = fakeRelationShip.IntersectEntity;
#endif
                otm.ReferencingAttribute = fakeRelationShip.Entity1Attribute;
                otm.ReferencingEntity    = fakeRelationShip.Entity1LogicalName;
                otm.ReferencedAttribute  = fakeRelationShip.Entity2Attribute;
                otm.ReferencedEntity     = fakeRelationShip.Entity2LogicalName;
                otm.SchemaName           = fakeRelationShip.IntersectEntity;
                return(otm);
            }
        }
        public void When_creating_relationship_with_first_constructor_properties_are_set_correctly()
        {
            var rel = new XrmFakedRelationship("entity1Attribute","entity2Attribute","entity1LogicalName","entity2LogicalName");

            Assert.Equal(rel.Entity1LogicalName, "entity1LogicalName");
            Assert.Equal(rel.Entity2LogicalName, "entity2LogicalName");
            Assert.Equal(rel.Entity1Attribute, "entity1Attribute");
            Assert.Equal(rel.Entity2Attribute, "entity2Attribute");
            Assert.Equal(rel.RelationshipType, XrmFakedRelationship.enmFakeRelationshipType.OneToMany);
        }
Example #4
0
        public void When_creating_relationship_with_first_constructor_properties_are_set_correctly()
        {
            var rel = new XrmFakedRelationship("entity1Attribute", "entity2Attribute", "entity1LogicalName", "entity2LogicalName");

            Assert.Equal(rel.Entity1LogicalName, "entity1LogicalName");
            Assert.Equal(rel.Entity2LogicalName, "entity2LogicalName");
            Assert.Equal(rel.Entity1Attribute, "entity1Attribute");
            Assert.Equal(rel.Entity2Attribute, "entity2Attribute");
            Assert.Equal(rel.RelationshipType, XrmFakedRelationship.enmFakeRelationshipType.OneToMany);
        }
        public void When_calling_context_add_addrelated_and_save_changes_entities_are_added_to_the_faked_context()
        {
            var context = new XrmFakedContext();

            var relationship = new XrmFakedRelationship()
            {
                IntersectEntity    = "accountleads",
                Entity1Attribute   = "accountid",
                Entity2Attribute   = "leadid",
                Entity1LogicalName = "account",
                Entity2LogicalName = "lead"
            };

            context.AddRelationship("accountleads", relationship);

            var service = context.GetFakedOrganizationService();

            using (var ctx = new XrmServiceContext(service))
            {
                var account = new Account()
                {
                    Name = "Test account"
                };
                ctx.AddObject(account);

                var contact = new Lead()
                {
                    FirstName = "Jane", LastName = "Doe"
                };
                ctx.AddRelatedObject(account, new Relationship("accountleads"), contact);
                var result = ctx.SaveChanges();

                var resultaccount = ctx.CreateQuery <Account>()
                                    .ToList()
                                    .FirstOrDefault();

                Assert.NotNull(resultaccount);
                Assert.Equal("Test account", resultaccount.Name);

                var reaultlead = ctx.CreateQuery <Lead>()
                                 .ToList()
                                 .FirstOrDefault();

                Assert.NotNull(reaultlead);
                Assert.Equal("Jane", reaultlead.FirstName);
                Assert.Equal("Doe", reaultlead.LastName);

                var relationshipRecords = ctx.CreateQuery("accountleads")
                                          .ToList();
                Assert.NotEmpty(relationshipRecords);
            }
        }
        public void When_calling_context_add_addrelated_and_save_changes_entities_are_added_to_the_faked_context()
        {
            var context = new XrmFakedContext();

            var relationship = new XrmFakedRelationship()
            {
                IntersectEntity = "accountleads",
                Entity1Attribute = "accountid",
                Entity2Attribute = "leadid",
                Entity1LogicalName = "account",
                Entity2LogicalName = "lead"
            };
            context.AddRelationship("accountleads", relationship);

            var service = context.GetFakedOrganizationService();

            using (var ctx = new XrmServiceContext(service))
            {
                var account = new Account() { Name = "Test account" };
                ctx.AddObject(account);

                var contact = new Lead() { FirstName = "Jane", LastName = "Doe" };
                ctx.AddRelatedObject(account, new Relationship("accountleads"), contact);
                var result = ctx.SaveChanges();

                var resultaccount = ctx.CreateQuery<Account>()
                                       .ToList()
                                       .FirstOrDefault();

                Assert.NotNull(resultaccount);
                Assert.Equal("Test account", resultaccount.Name);

                var reaultlead = ctx.CreateQuery<Lead>()
                                    .ToList()
                                    .FirstOrDefault();

                Assert.NotNull(reaultlead);
                Assert.Equal("Jane", reaultlead.FirstName);
                Assert.Equal("Doe", reaultlead.LastName);

                var relationshipRecords = ctx.CreateQuery("accountleads")
                                             .ToList();
                Assert.NotEmpty(relationshipRecords);
            }
        }
Example #7
0
        private static XrmFakedContext GetDisassosiateContext(Guid businessUnitId, Guid userId, Guid hotelId)
        {
            // role
            var baseRole = new Entity(Entities.Team, Guid.NewGuid());

            baseRole["name"]           = General.RoleTcIdBase;
            baseRole["businessunitid"] = businessUnitId;
            // team
            var team = new Entity(Entities.Team)
            {
                Id = Guid.NewGuid()
            };

            team[Attributes.Team.HotelTeam] = true;
            // hotel
            var hotel = new Entity(Entities.Hotel)
            {
                Id = hotelId
            };

            hotel[Attributes.Hotel.OwningTeam] = new EntityReference(Entities.Team, team.Id);
            // user
            var user = new Entity(Entities.User)
            {
                Id = userId
            };

            user["businessunit"] = businessUnitId;
            var context = new XrmFakedContext();

            context.Initialize(new List <Entity>()
            {
                baseRole, hotel, user, team
            });
            var relationship = new XrmFakedRelationship(Entities.Team, "teamid", "systemuserid", "team", "systemuser");

            context.AddRelationship(Relationships.TeamMembershipAssociation, relationship);
            return(context);
        }
        public static void Should_Not_Fail_On_Conditions_In_Link_Entities()
        {
            var fakedContext = new XrmFakedContext();
            var fakedService = fakedContext.GetFakedOrganizationService();

            var testEntity1 = new Entity("entity1")
            {
                Attributes = new AttributeCollection
                {
                    { "entity1attr", "test1" }
                }
            };
            var testEntity2 = new Entity("entity2")
            {
                Attributes = new AttributeCollection
                {
                    { "entity2attr", "test2" }
                }
            };

            testEntity1.Id = fakedService.Create(testEntity1);
            testEntity2.Id = fakedService.Create(testEntity2);

            var testRelation = new XrmFakedRelationship
            {
                IntersectEntity    = "TestIntersectEntity",
                Entity1LogicalName = "entity1",
                Entity1Attribute   = "entity1attr",
                Entity2LogicalName = "entity2",
                Entity2Attribute   = "entity2attr"
            };

            fakedContext.AddRelationship(testRelation.Entity2LogicalName, testRelation);
            fakedService.Associate(testEntity1.LogicalName, testEntity1.Id,
                                   new Relationship(testRelation.Entity2LogicalName),
                                   new EntityReferenceCollection {
                testEntity2.ToEntityReference()
            });

            var query = new QueryExpression
            {
                EntityName = "entity1",
                Criteria   = new FilterExpression {
                    FilterOperator = LogicalOperator.And
                },
                ColumnSet = new ColumnSet(true)
            };

            var link = new LinkEntity
            {
                JoinOperator          = JoinOperator.Natural,
                LinkFromEntityName    = "entity1",
                LinkFromAttributeName = "entity1attr",
                LinkToEntityName      = "entity2",
                LinkToAttributeName   = "entity2attr",
                LinkCriteria          = new FilterExpression
                {
                    FilterOperator = LogicalOperator.And,
                    Conditions     =
                    {
                        new ConditionExpression
                        {
                            AttributeName = "entity2attr",
                            Operator      = ConditionOperator.Equal,
                            Values        = { "test2" }
                        }
                    }
                }
            };

            query.LinkEntities.Add(link);

            var result = fakedService.RetrieveMultiple(query);

            Assert.NotEmpty(result.Entities);
            Assert.Equal(1, result.Entities.Count);
        }
        public static void Should_Not_Fail_On_Conditions_In_Link_Entities()
        {
            var fakedContext = new XrmFakedContext();
            var fakedService = fakedContext.GetFakedOrganizationService();

            var testEntity1 = new Entity("entity1")
            {
                Attributes = new AttributeCollection
                {
                    {"entity1attr", "test1" }
                }
            };
            var testEntity2 = new Entity("entity2")
            {
                Attributes = new AttributeCollection
                {
                    {"entity2attr", "test2" }
                }
            };

            testEntity1.Id = fakedService.Create(testEntity1);
            testEntity2.Id = fakedService.Create(testEntity2);

            var testRelation = new XrmFakedRelationship
            {
                IntersectEntity = "TestIntersectEntity",
                Entity1LogicalName = "entity1",
                Entity1Attribute = "entity1attr",
                Entity2LogicalName = "entity2",
                Entity2Attribute = "entity2attr"
            };
            fakedContext.AddRelationship(testRelation.Entity2LogicalName, testRelation);
            fakedService.Associate(testEntity1.LogicalName, testEntity1.Id, new Relationship(testRelation.Entity2LogicalName), new EntityReferenceCollection { testEntity2.ToEntityReference() });

            var query = new QueryExpression
            {
                EntityName = "entity1",
                Criteria = new FilterExpression { FilterOperator = LogicalOperator.And },
                ColumnSet = new ColumnSet(true)
            };

            var link = new LinkEntity
            {
                JoinOperator = JoinOperator.Natural,
                LinkFromEntityName = "entity1",
                LinkFromAttributeName = "entity1attr",
                LinkToEntityName = "entity2",
                LinkToAttributeName = "entity2attr",
                LinkCriteria = new FilterExpression
                {
                    FilterOperator = LogicalOperator.And,
                    Conditions = { new ConditionExpression { AttributeName = "entity2attr", Operator = ConditionOperator.Equal, Values = { "test2" } } }
                }
            };
            query.LinkEntities.Add(link);

            var result = fakedService.RetrieveMultiple(query);
            Assert.NotEmpty(result.Entities);
            Assert.Equal(1, result.Entities.Count);
        }
Example #10
0
        private static XrmFakedContext GetAssosiateContext(Guid businessUnitId, Guid userId, Guid hotelId, bool createOwningTeam = false, bool createBusinessUnitTeam = false, bool createSecurityRole = true, bool createBuSecurityRole = true)
        {
            var entities = new List <Entity>();

            // role
            if (createSecurityRole)
            {
                var baseRole = new Entity(Entities.Role, Guid.NewGuid());
                baseRole[Attributes.Role.Name]           = General.RoleTcIdBase;
                baseRole[Attributes.Role.BusinessUnitId] = businessUnitId;
                entities.Add(baseRole);
            }
            // business unit
            var businessUnit = new Entity(Entities.BusinessUnit, Guid.NewGuid());

            businessUnit[Attributes.BusinessUnit.Name] = "GB";
            entities.Add(businessUnit);
            // roles for unit
            if (createBuSecurityRole)
            {
                var role = new Entity(Entities.Role, Guid.NewGuid());
                role[Attributes.Role.Name]           = General.RoleTcIdBase;
                role[Attributes.Role.BusinessUnitId] = businessUnit.Id;
                entities.Add(role);
                role = new Entity(Entities.Role, Guid.NewGuid());
                role[Attributes.Role.Name]           = General.RoleTcIdRep;
                role[Attributes.Role.BusinessUnitId] = businessUnit.Id;
                entities.Add(role);
            }
            // hotel
            var hotel = new Entity(Entities.Hotel)
            {
                Id = hotelId
            };

            hotel[Attributes.Hotel.Name]          = "Hotel XXX";
            hotel[Attributes.Hotel.MasterHotelId] = "1002";
            hotel[Attributes.Hotel.OwningTeam]    = null;
            entities.Add(hotel);
            if (createOwningTeam)
            {
                var team = new Entity(Entities.Team)
                {
                    Id = Guid.NewGuid()
                };
                team[Attributes.Team.Name]         = "Hotel Team: Hotel XXX - 1002";
                team[Attributes.Team.HotelTeam]    = true;
                hotel[Attributes.Hotel.OwningTeam] = new EntityReference(Entities.Team, team.Id);
                entities.Add(team);
                if (createBusinessUnitTeam)
                {
                    var buTeam = new Entity(Entities.Team)
                    {
                        Id = Guid.NewGuid()
                    };
                    buTeam[Attributes.Team.Name]           = "GB: Hotel Team: Hotel XXX - 1002";
                    buTeam[Attributes.Team.HotelTeam]      = true;
                    buTeam[Attributes.Team.BusinessUnitId] = new EntityReference(Entities.BusinessUnit, businessUnit.Id);
                    entities.Add(buTeam);
                }
            }
            // user
            var user = new Entity(Entities.User)
            {
                Id = userId
            };

            user["businessunit"] = businessUnitId;
            entities.Add(user);
            var context = new XrmFakedContext();

            context.Initialize(entities);
            var relationship = new XrmFakedRelationship(Entities.Team, "teamid", "systemuserid", "team", "systemuser");

            context.AddRelationship(Relationships.TeamMembershipAssociation, relationship);
            relationship = new XrmFakedRelationship(Entities.Team, "teamid", "roleid", "team", "role");
            context.AddRelationship(Relationships.TeamRolesAssociation, relationship);
            return(context);
        }