public void Export_SingleComponent(bool parent, bool reverse) { IEntityXmlExporter exporter = Factory.EntityXmlExporter; IEntityRepository repository = Factory.EntityRepository; Definition def1 = new Definition( ); def1.Inherits.Add(UserResource.UserResource_Type); def1.Name = "End1"; def1.Save( ); Definition def2 = new Definition( ); def2.Inherits.Add(UserResource.UserResource_Type); def2.Name = "End2"; def2.Save( ); Relationship rel = new Relationship( ); rel.Name = "Rel321"; rel.FromType = def1.As <EntityType>( ); rel.ToType = def2.As <EntityType>( ); rel.Cardinality_Enum = CardinalityEnum_Enumeration.OneToOne; rel.RelType_Enum = reverse ? RelTypeEnum_Enumeration.RelSingleComponent : RelTypeEnum_Enumeration.RelSingleComponentOf; rel.CloneAction_Enum = reverse ? CloneActionEnum_Enumeration.CloneEntities : CloneActionEnum_Enumeration.Drop; rel.ReverseCloneAction_Enum = reverse ? CloneActionEnum_Enumeration.Drop : CloneActionEnum_Enumeration.CloneEntities; rel.Save( ); Guid relType = Entity.GetUpgradeId(rel.Id); try { def1.Save( ); def2.Save( ); rel.Save( ); IEntity e2 = Entity.Create(def2); e2.Save( ); IEntity e1 = Entity.Create(def1); e1.GetRelationships(rel.Id, Direction.Forward).Add(e2); e1.Save( ); string e1id = Entity.GetUpgradeId(e1.Id).ToString(); string e2id = Entity.GetUpgradeId(e2.Id).ToString( ); string rootId = parent ? e2id : e1id; string childId = parent ? e1id : e2id; bool isNested = parent != reverse; string relTag = reverse ? "revRel" : "rel"; string revRelTag = reverse ? "rel" : "revRel"; long id = parent ? e2.Id : e1.Id; string xml; using (RunAsImportExportRole( )) { xml = exporter.GenerateXml(id, EntityXmlExportSettings.Default); } XmlDocument doc = new XmlDocument( ); doc.LoadXml(xml); XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); // Note: can't get xpath to work with default namespace ns.AddNamespace("c", "core"); ns.AddNamespace("k", "console"); XmlElement e = doc.SelectSingleNode("/c:xml/c:entities/c:group/c:entity", ns) as XmlElement; Assert.That(e, Is.Not.Null, "e"); Assert.That(e.SelectSingleNode("@id", ns).Value, Is.EqualTo(rootId)); if (isNested) { XmlElement re = e.SelectSingleNode($"c:{revRelTag}[@id='{relType}']/c:entity", ns) as XmlElement; Assert.That(re.GetAttribute("id"), Is.EqualTo(childId)); } else { XmlElement re = e.SelectSingleNode($"c:{relTag}[@id='{relType}']", ns) as XmlElement; Assert.That(re.InnerText, Is.EqualTo(childId)); } } finally { rel.AsWritable( ).Delete( ); def1.AsWritable( ).Delete( ); def2.AsWritable( ).Delete( ); } }
public void RelNameChangeDoesNotBreakExistingPolicy() { var toDelete = new List <long>(); try { var parentType = new EntityType { Name = "dummy parent type" }; parentType.Save(); toDelete.Add(parentType.Id); var childType = new EntityType { Name = "dummy child type" }; childType.Save(); toDelete.Add(childType.Id); var rel = new Relationship { Name = "test rel", FromType = parentType, ToType = childType, Cardinality_Enum = CardinalityEnum_Enumeration.OneToOne }; rel.Save(); toDelete.Add(rel.Id); var policy = new RecordChangeAuditPolicy() { Name = "TEST ResourceAuditEventHandlerTests CascadeDeleteOfRelated", TriggeredOnType = parentType, TriggerEnabled = true }; policy.UpdatedRelationshipsToTriggerOn.Add(rel); policy.Save(); toDelete.Add(policy.Id); rel = rel.AsWritable <Relationship>(); rel.Name = "renamed rel"; rel.Save(); var child = Entity.Create(childType); child.SetField(Resource.Name_Field, "child"); child.Save(); toDelete.Add(child.Id); var parent = Entity.Create(parentType); parent.Save(); toDelete.Add(parent.Id); var log = RunAndLog(() => { parent = parent.AsWritable(); parent.GetRelationships(rel).Add(child); parent.Save(); }); Assert.That(log.Count, Is.EqualTo(1)); var logEntry = log[0].As <RecordChangeLogEntry>(); Assert.That(logEntry.Description, Is.EqualTo("[renamed rel] set to 'child'")); } finally { Entity.Delete(toDelete); } }
public void Export_MultipleParents_ObjectWithRel( ) { IEntityXmlExporter exporter = Factory.EntityXmlExporter; IEntityRepository repository = Factory.EntityRepository; Definition def1 = new Definition( ); def1.Name = "End1"; def1.Save( ); Definition def2 = new Definition( ); def2.Name = "End2"; def2.Save( ); Relationship rel = new Relationship( ); rel.Name = "Rel321"; rel.FromType = def1.As <EntityType>( ); rel.ToType = def2.As <EntityType>( ); rel.Cardinality_Enum = CardinalityEnum_Enumeration.ManyToMany; rel.Save( ); try { def1.Save( ); def2.Save( ); rel.Save( ); string xml; using (RunAsImportExportRole( )) { xml = exporter.GenerateXml(def1.Id, EntityXmlExportSettings.Default); } XmlDocument doc = new XmlDocument( ); doc.LoadXml(xml); XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); // Note: can't get xpath to work with default namespace ns.AddNamespace("c", "core"); ns.AddNamespace("k", "console"); XmlElement e = doc.SelectSingleNode("/c:xml/c:entities/c:group/c:definition", ns) as XmlElement; Assert.That(e, Is.Not.Null, "e"); Assert.That(e.SelectSingleNode("@id", ns).Value, Is.Not.Null); Assert.That(e.SelectSingleNode("c:name", ns).InnerText, Is.EqualTo("End1")); XmlElement re = e.SelectSingleNode("c:relationships/c:relationship", ns) as XmlElement; Assert.That(re, Is.Not.Null, "re"); Assert.That(re.SelectSingleNode("@id", ns).Value, Is.Not.Null); Assert.That(re.SelectSingleNode("c:name", ns).InnerText, Is.EqualTo("Rel321")); Assert.That(re.SelectSingleNode("c:toType", ns).InnerText, Is.Not.Null); Guid guid = Guid.Parse(re.SelectSingleNode("c:toType", ns).InnerText); Assert.That(guid, Is.EqualTo(Entity.GetUpgradeId(def2.Id))); } finally { rel.AsWritable( ).Delete( ); def1.AsWritable( ).Delete( ); def2.AsWritable( ).Delete( ); } }