public void Test_CountEntitiesWithReference_OneWayReference()
        {
            MockEntity entity = new MockEntity();
            entity.ID = Guid.NewGuid();

            MockPublicEntity publicEntity = new MockPublicEntity();
            publicEntity.ID = Guid.NewGuid();

            entity.PublicEntities = new MockPublicEntity[]{
                publicEntity
            };

            int before = DataAccess.Data.Counter.CountEntitiesWithReference(typeof(MockPublicEntity),
                                                                            publicEntity.ID,
                                                                            "",
                                                                            typeof(MockEntity),
                                                                            "PublicEntities");

            Assert.AreEqual(0, before);

            DataAccess.Data.Saver.Save(publicEntity);
            DataAccess.Data.Saver.Save(entity);

            int after = DataAccess.Data.Counter.CountEntitiesWithReference(typeof(MockPublicEntity),
                                                                            publicEntity.ID,
                                                                            "",
                                                                            typeof(MockEntity),
                                                                            "PublicEntities");

            Assert.AreEqual(1, after);
        }
        public void Test_GetMirrorPropertyNameReverse_Multiple_Implicit_Async()
        {
            MockEntity entity = new MockEntity();
            MockPublicEntity publicEntity = new MockPublicEntity();

            string mirrorPropertyName = EntitiesUtilities.GetMirrorPropertyNameReverse(publicEntity, "", typeof(MockEntity));

            Assert.AreEqual("PublicEntities", mirrorPropertyName, "The mirror property name wasn't determined correctly.");
        }
        public void Test_Authorise_Entity_PropertyName()
        {
            MockEntity entity = new MockEntity();

            MockRestrictedEntity restrictedEntity = new MockRestrictedEntity();

            entity.RestrictedEntities = new MockRestrictedEntity[]{
                restrictedEntity
            };

            AuthoriseReferenceStrategy.New(entity, "RestrictedEntities").Authorise();

            Assert.AreEqual(0, entity.RestrictedEntities.Length, "Restricted entity wasn't removed from reference property.");
        }
        public void Test_Authorise_RemovesUnauthorisedReferences()
        {
            MockEntity entity = new MockEntity();
            entity.ID = Guid.NewGuid();

            MockRestrictedEntity restrictedEntity = new MockRestrictedEntity();
            restrictedEntity.ID = Guid.NewGuid();

            entity.RestrictedEntities = new MockRestrictedEntity[]{
                restrictedEntity
            };

            AuthoriseReferencesStrategy.New(entity).Authorise(entity);

            Assert.AreEqual(0, entity.RestrictedEntities.Length, "Invalid number of restricted entities found.");
        }
        public void Test_Authorise_KeepsAuthorisedReferences()
        {
            MockEntity entity = new MockEntity();
            entity.ID = Guid.NewGuid();

            MockPublicEntity publicEntity = new MockPublicEntity();
            publicEntity.ID = Guid.NewGuid();

            entity.PublicEntities = new MockPublicEntity[]{
                publicEntity
            };

            AuthoriseReferencesStrategy.New(entity).Authorise(entity);

            Assert.AreEqual(1, entity.PublicEntities.Length, "Invalid number of public entities found.");
        }
        public virtual void Test_Delete_EntityAndReference_Async()
        {
            MockEntity entity = new MockEntity();
            entity.ID = Guid.NewGuid();

            MockPublicEntity referencedEntity = new MockPublicEntity();
            referencedEntity.ID = Guid.NewGuid();

            entity.PublicEntities = new MockPublicEntity[]{referencedEntity};

            DataAccess.Data.Saver.Save(referencedEntity);
            DataAccess.Data.Saver.Save(entity);

            EntityReferenceCollection references = DataAccess.Data.Referencer.GetReferences(typeof(MockEntity).Name, typeof(MockPublicEntity).Name);

            Assert.AreEqual(1, references.Count, "Incorrect number of references found.");

            DataAccess.Data.Deleter.Delete(entity);

            EntityReferenceCollection references2 = DataAccess.Data.Referencer.GetReferences(typeof(MockEntity).Name, typeof(MockPublicEntity).Name);

            Assert.AreEqual(0, references2.Count, "Reference not deleted.");
        }
        public void Test_Save_RemovesUnauthorisedReferences()
        {
            MockEntity entity = new MockEntity();
            entity.ID = Guid.NewGuid();

            MockRestrictedEntity restrictedEntity = new MockRestrictedEntity();
            restrictedEntity.ID = Guid.NewGuid();

            entity.RestrictedEntities = new MockRestrictedEntity[]{
                restrictedEntity
            };

            SaveStrategy.New(restrictedEntity, false).Save(restrictedEntity);
            SaveStrategy.New(entity).Save(entity);

            MockEntity foundEntity = RetrieveStrategy.New<MockEntity>(false).Retrieve<MockEntity>("ID", entity.ID);

            Assert.IsNotNull(foundEntity);

            foundEntity.Activate();

            Assert.AreEqual(0, foundEntity.RestrictedEntities.Length, "Restricted entity wasn't removed from reference property.");
        }
        public virtual void Test_Update_SetsCountPropertyForReference_TwoWay()
        {
            MockEntity entity = new MockEntity();
            entity.ID = Guid.NewGuid();

            MockSyncEntity referencedEntity = new MockSyncEntity();
            referencedEntity.ID = Guid.NewGuid();

            entity.SyncEntities = new MockSyncEntity[]{
                referencedEntity
            };

            DataAccess.Data.Saver.Save(referencedEntity);
            DataAccess.Data.Saver.Save(entity);

            DataAccess.Data.Updater.Update(entity);

            MockEntity foundEntity = DataAccess.Data.Reader.GetEntity<MockEntity>("ID", entity.ID);
            MockSyncEntity foundReferencedEntity = DataAccess.Data.Reader.GetEntity<MockSyncEntity>("ID", referencedEntity.ID);

            DataAccess.Data.Activator.Activate(foundEntity);
            DataAccess.Data.Activator.Activate(foundReferencedEntity);

            Assert.AreEqual(1, foundEntity.TotalSyncEntities, "The TotalSyncEntities property didn't have the expected value.");
            Assert.AreEqual(1, foundEntity.SyncEntities.Length, "The SyncEntities property didn't have the expected length.");

            Assert.AreEqual(1, foundReferencedEntity.TotalEntities, "The TotalEntities property didn't have the expected value.");
            Assert.AreEqual(1, foundReferencedEntity.Entities.Length, "The Entities property didn't have the expected length.");
        }
        public virtual void Test_GetActiveReferences_ReferenceWithInterfaceType()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing the GetDataStoreName function with a provided entity reference.", NLog.LogLevel.Debug))
            {

                MockEntity referencedEntity = new MockEntity();
                referencedEntity.ID = Guid.NewGuid();

                MockInterfaceReferencePropertyEntity entity = new MockInterfaceReferencePropertyEntity();
                entity.ID = Guid.NewGuid();
                entity.ReferencedEntity = referencedEntity;

                EntityReferenceCollection references = DataAccess.Data.Referencer.GetActiveReferences(entity);

                EntityReference reference = references[0];

                string type1Name = reference.Type1Name;
                string type2Name = reference.Type2Name;

                Assert.AreEqual("MockInterfaceReferencePropertyEntity", type1Name, "Type 1 name is invalid.");
                Assert.AreEqual("MockEntity", type2Name, "Type 2 name is invalid.");

            }
        }
        public virtual void Test_SetCountProperty_TwoWayReference()
        {
            MockEntity entity = new MockEntity();
            entity.ID = Guid.NewGuid();

            MockSyncEntity referencedEntity = new MockSyncEntity();
            referencedEntity.ID = Guid.NewGuid();

            entity.SyncEntities = new MockSyncEntity[]{
                referencedEntity
            };

            EntityReferenceCollection references = DataAccess.Data.Referencer.GetActiveReferences(entity);

            DataAccess.Data.Saver.Save(referencedEntity);
            DataAccess.Data.Saver.Save(entity);

            DataAccess.Data.Referencer.SetCountProperty(entity, "SyncEntities", referencedEntity.ID);

            Assert.AreEqual(1, entity.TotalSyncEntities, "The TotalSyncEntities property didn't have the expected value.");
        }
        public virtual void Test_GetReferences_InterfaceReferenceProperty()
        {
            using (LogGroup logGroup = LogGroup.StartDebug("Testing the GetReferences function."))
            {
                MockEntity referencedEntity = new MockEntity();
                referencedEntity.ID = Guid.NewGuid();

                MockInterfaceReferencePropertyEntity entity = new MockInterfaceReferencePropertyEntity();
                entity.ID = Guid.NewGuid();
                entity.ReferencedEntity = referencedEntity;

                DataAccess.Data.Saver.Save(referencedEntity);
                DataAccess.Data.Saver.Save(entity);

                EntityReferenceCollection references = DataAccess.Data.Referencer.GetReferences(entity);

                Assert.AreEqual(1, references.Count, "Wrong number of references found.");

                string[] names = DataAccess.Data.GetDataStoreNames();

                bool interfaceStoreFound = Array.IndexOf(names, "IEntity-MockInterfaceReferencePropertyEntity") > -1;
                bool concreteStoreFound = Array.IndexOf(names, "MockEntity") > -1;
                bool referenceStoreFound = Array.IndexOf(names, "MockEntity-MockInterfaceReferencePropertyEntity") > -1;

                // Ensure that the interface name wasn't used as a store name
                Assert.IsFalse(interfaceStoreFound);

                // Ensure that the concrete class name was used as a store name
                Assert.IsTrue(concreteStoreFound);

                // Ensure that the reference store was named properly
                Assert.IsTrue(referenceStoreFound);
            }
        }
        public void Test_SwitchFor()
        {
            MockEntity entity = new MockEntity();
            entity.ID = Guid.NewGuid();

            MockSyncEntity otherEntity = new MockSyncEntity();
            otherEntity.ID = Guid.NewGuid();

            entity.SyncEntities = new MockSyncEntity[]{
                otherEntity
            };

            EntityReference reference = new EntityReference();
            reference.SourceEntity = entity;
            reference.ReferenceEntity = otherEntity;
            reference.Property1Name = "SyncEntities";
            reference.Property2Name = "Entities";

            EntityReference switchedReference = reference.SwitchFor(otherEntity);

            Assert.AreEqual(reference.Property1Name, "Entities", "Property1Name doesn't match what's expected.");
            Assert.AreEqual(reference.Property2Name, "SyncEntities", "Property2Name doesn't match what's expected.");

            Assert.AreEqual(reference.Type1Name.ToString(), otherEntity.ShortTypeName, "Type1Name doesn't match what's expected.");
            Assert.AreEqual(reference.Type2Name.ToString(), entity.ShortTypeName, "Type2Name doesn't match what's expected.");

            Assert.AreEqual(reference.Entity1ID.ToString(), otherEntity.ID.ToString(), "Entity1ID doesn't match what's expected.");
            Assert.AreEqual(reference.Entity2ID.ToString(), entity.ID.ToString(), "Entity2ID doesn't match what's expected.");
        }
        public virtual void Test_Save_SetsCountPropertyForReference()
        {
            using (LogGroup logGroup = LogGroup.StartDebug("Testing the Save function to ensure it sets the reference count properties."))
            {
                MockEntity entity = new MockEntity();
                entity.ID = Guid.NewGuid();

                MockSyncEntity referencedEntity = new MockSyncEntity();
                referencedEntity.ID = Guid.NewGuid();

                entity.SyncEntities = new MockSyncEntity[]{
                    referencedEntity
                };

                DataAccess.Data.Saver.Save(referencedEntity);
                DataAccess.Data.Saver.Save(entity);

                MockEntity foundEntity = DataAccess.Data.Reader.GetEntity<MockEntity>("ID", entity.ID);
                MockSyncEntity foundReferencedEntity = DataAccess.Data.Reader.GetEntity<MockSyncEntity>("ID", referencedEntity.ID);

                DataAccess.Data.Activator.Activate(foundEntity);
                DataAccess.Data.Activator.Activate(foundReferencedEntity);

                Assert.AreEqual(1, foundEntity.TotalSyncEntities, "The TotalSyncEntities property didn't have the expected value.");
                Assert.AreEqual(1, foundEntity.SyncEntities.Length, "The SyncEntities property didn't have the expected length.");

                Assert.AreEqual(1, foundReferencedEntity.TotalEntities, "The TotalEntities property didn't have the expected value.");
                Assert.AreEqual(1, foundReferencedEntity.Entities.Length, "The Entities property didn't have the expected length.");
            }
        }