public static void InitializeIndex_Should_Not_Throw_An_Exception()
        {
            var serviceLocatorMock = new Mock<IServiceLocator>();
            serviceLocatorMock.Setup(x => x.GetInstance<IIndexableTypeMapperResolver>())
                .Returns(new TypeScanIndexableTypeMapperResolver());
            ServiceLocator.SetLocator(serviceLocatorMock.Object);

            var languageBranchRepositoryMock = new Mock<ILanguageBranchRepository>();
            languageBranchRepositoryMock.Setup(x => x.ListEnabled())
                .Returns(new[] {new LanguageBranch(new CultureInfo("en")), new LanguageBranch(new CultureInfo("nl"))});

            var pageDataIndexer = new PageDataIndexer(languageBranchRepositoryMock.Object, new ElasticClient(new Uri("http://localhost:9200")),
                new CmsElasticSearchOptions(), null, null);

            pageDataIndexer.InitializeIndex();
        }
        public static void IndexPageTree_With_BulkSize_2_And_3_Records_Should_BulkIndex_Twice()
        {
            var contentRepositoryMock = new Mock<IContentRepository>();
            contentRepositoryMock.Setup(x => x.GetDescendents(ContentReference.RootPage))
                .Returns(new[] { new ContentReference(1), new ContentReference(1), new ContentReference(1) });
            contentRepositoryMock.Setup(x => x.Get<PageData>(new ContentReference(1), new CultureInfo("en")))
                .Returns(new TestPage());

            var elasticClientMock = CreateElasticClientMock();

            var pageDataIndexer = new PageDataIndexer(CreateLanguageBranchRepositoryMock().Object,
                elasticClientMock.Object, new CmsElasticSearchOptions {BulkSize = 2}, contentRepositoryMock.Object,
                new Mock<ILogger>().Object);

            pageDataIndexer.IndexPageTree().ToList();

            elasticClientMock.Verify(x => x.Bulk(It.IsAny<IBulkRequest>()), Times.Exactly(2));
            elasticClientMock.Verify(x => x.Bulk(It.Is<IBulkRequest>(b => b.Operations.Count == 2)), Times.Exactly(1));
            elasticClientMock.Verify(x => x.Bulk(It.Is<IBulkRequest>(b => b.Operations.Count == 1)), Times.Exactly(1));
        }
        public static void IndexPageTree_Should_Not_Throw_An_Exception()
        {
            var serviceLocatorMock = new Mock<IServiceLocator>();
            serviceLocatorMock.Setup(x => x.GetInstance<IIndexableTypeMapperResolver>())
                .Returns(new TypeScanIndexableTypeMapperResolver());
            ServiceLocator.SetLocator(serviceLocatorMock.Object);

            var languageBranchRepositoryMock = new Mock<ILanguageBranchRepository>();
            languageBranchRepositoryMock.Setup(x => x.ListEnabled())
                .Returns(new[] { new LanguageBranch(new CultureInfo("en")), new LanguageBranch(new CultureInfo("nl")) });

            var contentRepositoryMock = new Mock<IContentRepository>();
            contentRepositoryMock.Setup(x => x.GetDescendents(It.IsAny<PageReference>()))
                .Returns(new[] {new ContentReference(1)});
            contentRepositoryMock.Setup(x => x.Get<PageData>(It.IsAny<ContentReference>(), It.IsAny<CultureInfo>()))
                .Returns(new TestPage());

            var pageDataIndexer = new PageDataIndexer(languageBranchRepositoryMock.Object, new ElasticClient(new Uri("http://localhost:9200")),
                new CmsElasticSearchOptions(), contentRepositoryMock.Object, null);

            var indexPageTree = pageDataIndexer.IndexPageTree().ToArray();
        }
        public static void IndexPageTree_Should_Swap_Correct_Indices()
        {
            var contentRepositoryMock = new Mock<IContentRepository>();
            contentRepositoryMock.Setup(x => x.GetDescendents(ContentReference.RootPage))
                .Returns(new[] { new ContentReference(1)});
            contentRepositoryMock.Setup(x => x.Get<PageData>(new ContentReference(1), new CultureInfo("en")))
                .Returns(new TestPage());

            var elasticClientMock = CreateElasticClientMock();

            var pageDataIndexer = new PageDataIndexer(CreateLanguageBranchRepositoryMock().Object,
                elasticClientMock.Object, new CmsElasticSearchOptions(), contentRepositoryMock.Object,
                new Mock<ILogger>().Object);

            var indexPageTree = pageDataIndexer.IndexPageTree();
            indexPageTree.ToList();

            // verify create index is not called when re-creating the re-indexed indice
            elasticClientMock.Verify(
                x =>
                    x.CreateIndex(It.Is<IndexName>(s => s.Name.Equals("site_en_1")),
                        It.IsAny<Func<CreateIndexDescriptor, ICreateIndexRequest>>()), Times.Once);

            elasticClientMock.Verify(x=>x.Alias(It.Is<Func<BulkAliasDescriptor, IBulkAliasRequest>>(f=> f(new BulkAliasDescriptor()).Actions.Any(a=> a.GetType() == typeof(AliasAddDescriptor)))));

            elasticClientMock.Verify(
                x =>
                    x.Alias(
                        It.Is<Func<BulkAliasDescriptor, IBulkAliasRequest>>(
                            f =>
                                f(new BulkAliasDescriptor())
                                    .Actions.OfType<AliasAddDescriptor>()
                                    .Any(a => ((IAliasAddAction)a).Add.Index.Name.Equals("site_en_1")) &&
                                    f(new BulkAliasDescriptor())
                                    .Actions.OfType<AliasRemoveDescriptor>()
                                    .Any(a => ((IAliasRemoveAction)a).Remove.Index.Name.Equals("site_en_2")))), Times.Once);
        }