Ejemplo n.º 1
0
        public void Test_Relationship(Direction direction, string identityType, int count)
        {
            // Create schema
            EntityType type = Entity.Create <EntityType>( );

            type.Name = "Test Type";
            type.Inherits.Add(Entity.Get <EntityType>("core:resource"));
            type.Save( );
            Field      stringField = new StringField( ).As <Field>( );
            Field      intField    = new IntField( ).As <Field>( );
            EntityType type2       = Entity.Create <EntityType>( );

            type2.Name = "Test Type2";
            type2.Fields.Add(stringField);
            type2.Fields.Add(intField);
            type.Inherits.Add(Entity.Get <EntityType>("core:resource"));
            type2.Save( );
            Relationship relationship = Entity.Create <Relationship>( );

            relationship.Name             = "Rel1";
            relationship.Cardinality_Enum = CardinalityEnum_Enumeration.ManyToMany;
            relationship.FromType         = direction == Direction.Forward ? type : type2;
            relationship.ToType           = direction == Direction.Forward ? type2 : type;
            relationship.Save( );

            var targets = new List <Resource>( );

            for (int i = 0; i < count; i++)
            {
                string name      = "Target" + Guid.NewGuid( );
                string stringVal = "StringVal" + i;
                int    intVal    = 100 + i;

                Resource target = Entity.Create(type2.Id).AsWritable <Resource>( );
                target.SetField(stringField.Id, stringVal);
                target.SetField(intField.Id, intVal);
                target.Name = name;
                target.Save( );
                targets.Add(target);
            }


            using (new SecurityBypassContext( ))
            {
                new AccessRuleFactory( ).AddAllowByQuery(Entity.Get <Subject>("core:everyoneRole").AsWritable <Subject>( ), type.As <SecurableEntity>( ), new [] { Permissions.Read, Permissions.Modify }, TestQueries.Entities(type.Id).ToReport( ));
                new AccessRuleFactory( ).AddAllowByQuery(Entity.Get <Subject>("core:everyoneRole").AsWritable <Subject>( ), type2.As <SecurableEntity>( ), new [] { Permissions.Read, Permissions.Modify }, TestQueries.Entities(type2.Id).ToReport( ));
            }

            Field lookupField = null;

            if (identityType.Contains("StringField"))
            {
                lookupField = stringField;
            }
            else if (identityType.Contains("IntField"))
            {
                lookupField = intField;
            }

            var identities = new List <string>( );

            for (int i = 0; i < count; i++)
            {
                var    target = targets[i];
                string identity;
                if (identityType.Contains("Null"))
                {
                    continue;
                }
                if (identityType == "Name")
                {
                    identity = "\"" + target.Name + "\"";
                }
                else if (identityType == "Guid")
                {
                    identity = "\"" + target.UpgradeId + "\"";
                }
                else if (identityType == "StringField")
                {
                    identity = "\"StringVal" + i + "\"";
                }
                else if (identityType == "IntField")
                {
                    identity = (100 + i).ToString( );
                }
                else
                {
                    throw new InvalidOperationException( );
                }
                identities.Add(identity);
            }

            // Create JSON
            string jsonMember = "rel1";
            string json       = "{\"" + jsonMember + "\":[" + string.Join(", ", identities) + "]}";

            // Create a mapping
            var mapping    = CreateApiResourceMapping(new EntityRef(type.Id));
            var relMapping = CreateApiRelationshipMapping(mapping, new EntityRef(relationship.Id), jsonMember, direction == Direction.Reverse);

            relMapping.MappedRelationshipLookupField = lookupField;
            relMapping.Save( );

            // Fill entity
            IEntity entity = RunTest(json, mapping);
            IEntityRelationshipCollection <IEntity> value = entity.GetRelationships(relationship.Id, direction);

            // Assert mapping
            Assert.That(value, Is.Not.Null);
            if (identityType == "Null")
            {
                Assert.That(value, Has.Count.EqualTo(0));
            }
            else
            {
                Assert.That(value, Has.Count.EqualTo(targets.Count));
                var actual   = value.Select(e => e.Id).OrderBy(id => id).ToList( );
                var expected = targets.Select(e => e.Id).OrderBy(id => id).ToList( );

                Assert.That(actual, Is.EquivalentTo(expected));
            }
        }