private void SetupContentTest()
        {
            AttributeTypeRegistry.SetCurrent(new CmsAttributeTypeRegistry());

            // Ensure parent schema and content root exists for this test
            var contentVirtualRoot = FixedEntities.ContentVirtualRoot;
            var systemRoot = new SystemRoot();
            var contentRootSchema = new ContentRootSchema();
            Hive.AutoCommitTo<IContentStore>(
                x =>
                {
                    x.Repositories.AddOrUpdate(systemRoot);
                    x.Repositories.AddOrUpdate(contentVirtualRoot);
                    x.Repositories.Schemas.AddOrUpdate(contentRootSchema);
                });
        }
        public void SaveAndLoad_InbuiltSchemas()
        {
            var originalSecurity = new UserGroupSchema();
            var systemRoot = new SystemRoot();

            using (var writer = ProviderSetup.UnitFactory.Create())
            {
                writer.EntityRepository.AddOrUpdate(systemRoot); // Need to add a system root because UserGroupSchema advertises that it's related to it
                writer.EntityRepository.Schemas.AddOrUpdate(originalSecurity);
                writer.Complete();
            }

            // Clear session to avoid caching when testing readbacks
            PostWriteCallback.Invoke();

            using (var reader = ReadonlyProviderSetup.ReadonlyUnitFactory.CreateReadonly())
            {
                // Reload. Shouldn't matter that we're newing up the schemas to get the Id as Id should 
                // have been left alone when saving, so leave it here as a test case
                var securitySchema = reader.EntityRepository.Schemas.Get<EntitySchema>(originalSecurity.Id);

                Assert.IsNotNull(securitySchema, "Security schema");

                Assert.AreEqual(originalSecurity.AttributeDefinitions.Count, securitySchema.AttributeDefinitions.Count, "Security attrib defs count differ");
                Assert.AreEqual(originalSecurity.AttributeGroups.Count, securitySchema.AttributeGroups.Count, "Security attrib groups count differ");
                Assert.AreEqual(originalSecurity.AttributeTypes.Count(), securitySchema.AttributeTypes.Count(), "Security attrib types count differ");
            }
        }
        public void Create_Typed_Entity_Under_Root_Then_Copy_To_Another_Parent()
        {
            //Arrange

            var root = new SystemRoot();
            Revision<TypedEntity> content = HiveModelCreationHelper.MockVersionedTypedEntity();
            Revision<TypedEntity> newParent = HiveModelCreationHelper.MockVersionedTypedEntity();
            AssignFakeIdsIfPassthrough(ProviderSetup.ProviderMetadata, content, newParent);

            root.RelationProxies.EnlistChild(content.Item, FixedRelationTypes.DefaultRelationType);

            using (var uow = ProviderSetup.UnitFactory.Create())
            {
                uow.EntityRepository.AddOrUpdate(root);
                uow.EntityRepository.Revisions.AddOrUpdate(content);
                uow.EntityRepository.Revisions.AddOrUpdate(newParent);
                uow.Complete();

                // Guard for the fact that the relation proxy must have had its lazyload delegate set as a result of the
                // above calls
                Assert.True(root.RelationProxies.IsConnected);
                Assert.True(content.Item.RelationProxies.IsConnected);
                Assert.True(newParent.Item.RelationProxies.IsConnected);
            }
            PostWriteCallback.Invoke();

            //Act

            TypedEntity copied;
            using (var uow = ProviderSetup.UnitFactory.Create())
            {
                //create a new copied entity
                copied = content.Item.CreateDeepCopyToNewParent(newParent.Item, FixedRelationTypes.DefaultRelationType, 0);
                AssignFakeIdsIfPassthrough(ProviderSetup.ProviderMetadata, copied);

                uow.EntityRepository.AddOrUpdate(copied);
                uow.Complete();
            }
            PostWriteCallback.Invoke();

            //Assert

            using (var uow = ProviderSetup.UnitFactory.Create())
            {
                var queriesCopiedContent = uow.EntityRepository.Get<TypedEntity>(copied.Id);
                Assert.IsNotNull(queriesCopiedContent);
                Assert.IsTrue(queriesCopiedContent.RelationProxies.IsConnected);
                Assert.AreEqual(FixedRelationTypes.DefaultRelationType.RelationName, queriesCopiedContent.RelationProxies.Single().Item.Type.RelationName);
                Assert.AreEqual(newParent.Item.Id, queriesCopiedContent.RelationProxies.Single().Item.SourceId);
            }
            PostWriteCallback.Invoke();
        }
        private void AddParentRelation(Action<ProviderUnit, SystemRoot, TypedEntity> addRelation)
        {
            // Arrange
            var parentRoot = new SystemRoot();
            var contentChild = HiveModelCreationHelper.MockTypedEntity();
            AssignFakeIdsIfPassthrough(ProviderSetup.ProviderMetadata, contentChild);

            // Act
            using (var writer = ProviderSetup.UnitFactory.Create())
            {
                writer.EntityRepository.AddOrUpdate(parentRoot);
                writer.EntityRepository.AddOrUpdate(contentChild);
                addRelation.Invoke(writer, parentRoot, contentChild);
                writer.Complete();
            }

            PostWriteCallback.Invoke();

            // Assert
            Assert.NotNull(parentRoot.RelationProxies.LazyLoadDelegate);
            Assert.NotNull(parentRoot.RelationProxies.FirstOrDefault());

            // Check loading the relation from the repo
            using (var writer = ProviderSetup.UnitFactory.Create())
            {
                var ancestorRelations = writer.EntityRepository.GetLazyAncestorRelations(contentChild.Id, FixedRelationTypes.DefaultRelationType);
                var ancestor = ancestorRelations.Where(x => x.SourceId == parentRoot.Id).FirstOrDefault();
                Assert.NotNull(ancestor);
                Assert.NotNull(ancestor.Source);
                Assert.AreEqual(ancestor.SourceId, parentRoot.Id);
                Assert.AreEqual(ancestor.DestinationId, contentChild.Id);

                var descendentRelations = writer.EntityRepository.GetLazyDescendentRelations(parentRoot.Id, FixedRelationTypes.DefaultRelationType);
                var descendent = descendentRelations.Where(x => x.DestinationId == contentChild.Id).FirstOrDefault();
                Assert.NotNull(descendent);
                Assert.NotNull(descendent.Source);
                Assert.AreEqual(descendent.SourceId, parentRoot.Id);
                Assert.AreEqual(descendent.DestinationId, contentChild.Id);
            }

            // Check loading the relation from the lazy-loaded collection on the entity
            using (var writer = ProviderSetup.UnitFactory.Create())
            {
                var reloadedRoot = writer.EntityRepository.Get<TypedEntity>(parentRoot.Id);
                var child = reloadedRoot.RelationProxies.FirstOrDefault();
                Assert.NotNull(child);
            }
        }
        public void Create_Typed_Entity_Under_Root_Then_Move_To_Another_Parent()
        {
            //Arrange

            var root = new SystemRoot();
            Revision<TypedEntity> content = HiveModelCreationHelper.MockVersionedTypedEntity();
            Revision<TypedEntity> newParent = HiveModelCreationHelper.MockVersionedTypedEntity();
            AssignFakeIdsIfPassthrough(ProviderSetup.ProviderMetadata, content, newParent);

            using (var uow = ProviderSetup.UnitFactory.Create())
            {
                uow.EntityRepository.AddOrUpdate(root);
                uow.EntityRepository.Revisions.AddOrUpdate(content);
                uow.EntityRepository.Revisions.AddOrUpdate(newParent);
                uow.EntityRepository.AddRelation(root, content.Item, FixedRelationTypes.DefaultRelationType, 0);
                uow.Complete();

                // Guard for the fact that the relation proxy must have had its lazyload delegate set as a result of the
                // above calls
                Assert.True(root.RelationProxies.IsConnected);
                Assert.True(content.Item.RelationProxies.IsConnected);
                Assert.True(newParent.Item.RelationProxies.IsConnected);
            }
            PostWriteCallback.Invoke();

            //Act

            using (var uow = ProviderSetup.UnitFactory.Create())
            {
                var reloadedContent = uow.EntityRepository.Revisions.Get<TypedEntity>(content.Item.Id, content.MetaData.Id);
                Assert.NotNull(reloadedContent);

                var rootRelations = uow.EntityRepository
                    .GetChildRelations(root, FixedRelationTypes.DefaultRelationType);

                var rootRelation = rootRelations
                    .Where(x => x.DestinationId == reloadedContent.Item.Id)
                    .FirstOrDefault();
                Assert.NotNull(rootRelation);

                uow.EntityRepository.RemoveRelation(rootRelation);
                uow.EntityRepository.AddRelation(newParent.Item, content.Item, FixedRelationTypes.DefaultRelationType, 0);
                uow.Complete();
            }
            PostWriteCallback.Invoke();

            //Assert

            using (var uow = ReadonlyProviderSetup.ReadonlyUnitFactory.CreateReadonly())
            {
                var contentReloaded = uow.EntityRepository.Get<TypedEntity>(content.Item.Id);
                Assert.IsNotNull(contentReloaded);
                var relations = contentReloaded.RelationProxies.ToList();
                Assert.That(relations.Count, Is.EqualTo(1));
                var relation = relations.Single().Item;
                Assert.AreEqual(FixedRelationTypes.DefaultRelationType.RelationName, relation.Type.RelationName);
                Assert.AreEqual(newParent.Item.Id, relation.SourceId);

                var rootReloaded = uow.EntityRepository.Get<TypedEntity>(root.Id);
                Assert.NotNull(rootReloaded);
                var rootRelations = uow.EntityRepository.GetDescendentRelations(root, FixedRelationTypes.DefaultRelationType);
                Assert.IsFalse(rootRelations.Any());
            }
            PostWriteCallback.Invoke();
        }
        public void From_Inherited_TypedEntity_To_IndexOperation()
        {
            //Arrange

            var systemRoot = new SystemRoot();


            //Act

            var result1 = _mapper.Map<SystemRoot, NestedHiveIndexOperation>(systemRoot);
            var result2 = _mapper.Map<TypedEntity, NestedHiveIndexOperation>(systemRoot);

            //Assert

            Assert.AreEqual("TypedEntity", result1.ItemCategory);
            Assert.AreEqual(systemRoot.EntitySchema.SchemaType, result1.Fields[FixedIndexedFields.SchemaType].FieldValue);
            Assert.AreEqual(IndexOperationType.Add, result1.OperationType);
            Assert.AreEqual(systemRoot.Id.Value.ToString(), result1.Id.Value);
            Assert.AreEqual(systemRoot.Id.Value.ToString(), result1.Fields[FixedIndexedFields.EntityId].FieldValue);
            Assert.AreEqual(systemRoot.EntitySchema.Alias, result1.Fields[FixedIndexedFields.SchemaAlias].FieldValue);
            Assert.AreEqual(systemRoot.EntitySchema.Name, result1.Fields[FixedIndexedFields.SchemaName].FieldValue);
            Assert.AreEqual(systemRoot.EntitySchema.Id.Value.ToString(), result1.Fields[FixedIndexedFields.SchemaId].FieldValue);

            VerifyIEntityFields(result1, systemRoot);
        }
        public virtual void EntityRelations_CachePopulates()
        {
            TypedEntity entityParent = HiveModelCreationHelper.MockTypedEntity();
            TypedEntity entityChild = HiveModelCreationHelper.MockTypedEntity();
            TypedEntity entitySibling = HiveModelCreationHelper.MockTypedEntity();

            var root = new SystemRoot();
            root.Relations.Add(FixedRelationTypes.ContentTreeRelationType, entityParent);
            entityParent.Relations.Add(FixedRelationTypes.ContentTreeRelationType, entityChild);
            entityParent.Relations.Add(FixedRelationTypes.ContentTreeRelationType, entitySibling);

            using (IReadWriteUnitOfWork writer = DirectReadWriteProvider.CreateReadWriteUnitOfWork())
            {
                writer.ReadWriteRepository.AddOrUpdate(root);
                writer.Commit();
            }

            PostWriteCallback.Invoke();

            using (IReadOnlyUnitOfWork reader = DirectReaderProvider.CreateReadOnlyUnitOfWork())
            {
                IEnumerable<TypedEntity> getEntities = reader.ReadRepository.GetEntities<TypedEntity>();
                Assert.AreEqual(4, getEntities.Count());
            }

            Assert.Inconclusive("Not finished");
        }
        public virtual void EntityRelations_GetByRoute()
        {
            TypedEntity entityParent = HiveModelCreationHelper.MockTypedEntity();
            TypedEntity entityChild = HiveModelCreationHelper.MockTypedEntity();
            TypedEntity entityGrandchild = HiveModelCreationHelper.MockTypedEntity();

            entityParent.Attributes.SetValueOrAdd(new NodeNameAttribute("homepage"));
            entityChild.Attributes.SetValueOrAdd(new NodeNameAttribute("news"));
            entityGrandchild.Attributes.SetValueOrAdd(new NodeNameAttribute("newsitem1"));

            var root = new SystemRoot();
            root.Relations.Add(FixedRelationTypes.ContentTreeRelationType, entityParent);
            entityParent.Relations.Add(FixedRelationTypes.ContentTreeRelationType, entityChild);
            entityChild.Relations.Add(FixedRelationTypes.ContentTreeRelationType, entityGrandchild);

            using (IReadWriteUnitOfWork writer = DirectReadWriteProvider.CreateReadWriteUnitOfWork())
            {
                writer.ReadWriteRepository.AddOrUpdate(root);
                writer.Commit();
            }

            using (DisposableTimer timer = DisposableTimer.TraceDuration<AbstractHivePersistenceTest>("Start perf test", "End perf test"))
            {
                for (int i = 0; i < 5; i++)
                {
                    PostWriteCallback.Invoke();

                    using (IReadOnlyUnitOfWork reader = DirectReaderProvider.CreateReadOnlyUnitOfWork())
                    {
                        var nhReader = reader.ReadRepository as RepositoryReader;
                        var hp = nhReader.GetByPath<TypedEntity>(new HiveId("/homepage/"), FixedRelationTypes.ContentTreeRelationType, FixedStatusTypes.Created);
                        var news = nhReader.GetByPath<TypedEntity>(new HiveId("/homepage/news"), FixedRelationTypes.ContentTreeRelationType, FixedStatusTypes.Created);
                        var newsitem1 = nhReader.GetByPath<TypedEntity>(new HiveId("/homepage/news/newsitem1"), FixedRelationTypes.ContentTreeRelationType, FixedStatusTypes.Created);
                        Assert.IsNotNull(hp);
                        Assert.IsNotNull(news);
                        Assert.IsNotNull(newsitem1);

                        var newsitem1_bad1 = nhReader.GetByPath<TypedEntity>(new HiveId("/homepage/newsitem1"));
                        var newsitem1_bad2 = nhReader.GetByPath<TypedEntity>(new HiveId("newsitem1"));
                        Assert.IsNull(newsitem1_bad1);
                        Assert.IsNull(newsitem1_bad2);
                    }
                }

                LogHelper.TraceIfEnabled<AbstractHivePersistenceTest>("Avg of {0}ms per run", () => timer.Stopwatch.ElapsedMilliseconds / 5);
            }
        }