public void Test_Lookup(Direction direction, string identityType) { string name = "Target" + Guid.NewGuid(); // 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.Fields.Add(stringField); type2.Fields.Add(intField); type2.Name = "Test Type2"; type.Inherits.Add(Entity.Get <EntityType>("core:resource")); type2.Save( ); Relationship relationship = Entity.Create <Relationship>( ); relationship.Name = "Rel1"; relationship.Cardinality_Enum = CardinalityEnum_Enumeration.OneToOne; relationship.FromType = direction == Direction.Forward ? type : type2; relationship.ToType = direction == Direction.Forward ? type2 : type; relationship.Save( ); Resource target = Entity.Create(type2.Id).AsWritable <Resource>(); target.Name = name; target.SetField(stringField.Id, "StringVal"); target.SetField(intField.Id, 101); target.Save( ); 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; string identity; if (identityType == "Name") { identity = "\"" + name + "\""; } else if (identityType == "Guid") { identity = "\"" + target.UpgradeId.ToString() + "\""; } else if (identityType.Contains("Null")) { identity = "null"; } else if (identityType == "StringField") { identity = "\"StringVal\""; } else if (identityType == "IntField") { identity = "101"; } else { throw new InvalidOperationException(); } if (identityType.Contains("StringField")) { lookupField = stringField; } else if (identityType.Contains("IntField")) { lookupField = intField; } // Create JSON string jsonMember = "rel1"; string json = "{\"" + jsonMember + "\":" + identity + "}"; // 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.Contains("Null")) { Assert.That(value, Has.Count.EqualTo(0)); } else { Assert.That(value, Has.Count.EqualTo(1)); Assert.That(value.First( ).Id, Is.EqualTo(target.Id)); } }
public void SetRelationship( ) { var employeeType = Entity.Get <EntityType>("test:employee"); var managerType = Entity.Get <EntityType>("test:manager"); var nameField = Entity.Get <StringField>("core:name"); IEntity emp = new Entity(employeeType); emp.SetField(nameField, "Test Employee"); IEntity emp2 = new Entity(employeeType); emp.SetField(nameField, "Test Employee 2"); IEntity mgr = (new Entity(employeeType)); emp.SetField(nameField, "Test Manager"); mgr.As <Resource>( ).IsOfType.Add(managerType); emp.Save( ); emp2.Save( ); mgr.Save( ); ToDelete.Add(emp.Id); ToDelete.Add(emp2.Id); ToDelete.Add(mgr.Id); var setRel = new SetRelationshipActivity( ); setRel.Save( ); ToDelete.Add(setRel.Id); var setRelAs = setRel.As <WfActivity>( ); ActivityImplementationBase nextActivity = setRelAs.CreateWindowsActivity( ); var args = new Dictionary <string, object> { { "Origin", emp }, { "Relationship", new EntityRef("test:reportsTo").Entity }, { "Destination", mgr }, }; RunActivity(nextActivity, args); emp = Entity.Get(emp.Id); IEntityRelationshipCollection <IEntity> rels = emp.GetRelationships("test:reportsTo"); Assert.AreEqual(1, rels.Count( ), "Ensure the manager has been set"); Assert.AreEqual(rels.First( ).Entity.Id, mgr.Id, "Ensure the manager has been set to the correct value"); // clear relationships args = new Dictionary <string, object> { { "Origin", emp }, { "Relationship", new EntityRef("test:reportsTo").Entity }, { "Destination", null }, { "Replace Existing Destination", true } }; RunActivity(nextActivity, args); emp = Entity.Get(emp.Id); rels = emp.GetRelationships("test:reportsTo"); Assert.AreEqual(0, rels.Count( ), "Ensure the manager has been cleared"); // set the reverse relationship args = new Dictionary <string, object> { { "Origin", mgr }, { "Relationship", new EntityRef("test:directReports").Entity }, { "Destination", emp }, { "(Internal) Is this a reverse relationship", true } }; RunActivity(nextActivity, args); mgr = Entity.Get(mgr.Id); rels = mgr.GetRelationships("test:directReports"); Assert.AreEqual(1, rels.Count( ), "Ensure the employee has been set"); Assert.AreEqual(rels.First( ).Entity.Id, emp.Id, "Ensure the employee has been set to the correct value"); // add a second relationship, clearing the first args = new Dictionary <string, object> { { "Origin", mgr }, { "Relationship", new EntityRef("test:directReports").Entity }, { "Destination", emp2 }, { "(Internal) Is this a reverse relationship", true }, { "Replace Existing Destination", true } }; RunActivity(nextActivity, args); mgr = Entity.Get(mgr.Id); rels = mgr.GetRelationships("test:directReports"); Assert.AreEqual(1, rels.Count( ), "Ensure the new employee has been set and the old cleared"); Assert.AreEqual(rels.First( ).Entity.Id, emp2.Id, "Ensure the manager has been set to the correct value"); // add the first back in args = new Dictionary <string, object> { { "Origin", mgr }, { "Relationship", new EntityRef("test:directReports").Entity }, { "Destination", emp }, { "(Internal) Is this a reverse relationship", true }, { "Replace Existing Destination", false } }; RunActivity(nextActivity, args); mgr = Entity.Get(mgr.Id); rels = mgr.GetRelationships("test:directReports"); Assert.AreEqual(2, rels.Count( ), "Add a second relationship"); }