public void ShouldThrowAnExceptionIfCantCreateAnIndex()
        {
            var response = new StubResponse(400);

            MockElasticClient.Setup(x => x.CreateIndex(It.IsAny <IndexName>(), It.IsAny <Func <CreateIndexDescriptor, ICreateIndexRequest> >(), It.IsAny <string>())).Returns(response);

            var indexMaintainer = new ElasticsearchApprenticeshipIndexMaintainer(MockElasticClient.Object, Mock.Of <IElasticsearchMapper>(), Mock.Of <ILog>(), Mock.Of <IElasticsearchConfiguration>());

            Assert.Throws <ConnectionException>(() => indexMaintainer.CreateIndex("testindex"));
        }
        public async Task Should_find_all_posts_by_author(string author, int expectCount)
        {
            var mockClient = new MockElasticClient <PostSource>(
                new[] {
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        author = "Mozilla"
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        author = "Mozilla"
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        author = "Mozilla"
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        author = "Google"
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        author = "Google"
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        author = "Apple"
                    }
                },
            }, "posts");
            var repository = new ReadPostRepository(mockClient.ElasticClient);

            var result = await repository.SearchAsync(new PostFilter(),
                                                      CancellationToken.None);

            result.Count(x => x.Author == author).Should().Be(expectCount);
        }
        public async Task Should_find_all_posts_by_the_content(string term, int expectCount)
        {
            var mockClient = new MockElasticClient <PostSource>(
                new[] {
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        content = "Firefox"
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        content = "Firefox"
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        content = "Firefox"
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        content = "Chrome"
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        content = "Chrome"
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        content = "Safari"
                    }
                },
            }, "posts");
            var repository = new ReadPostRepository(mockClient.ElasticClient);

            var result = await repository.SearchAsync(new PostFilter(),
                                                      CancellationToken.None);

            result.Count(x => x.Content == term).Should().Be(expectCount);
        }
        public async Task Should_find_all_posts_by_link(string link, int expectCount)
        {
            var mockClient = new MockElasticClient <PostSource>(
                new[] {
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        link = "twitter.com"
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        link = "linkedin.com"
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        link = "twitter.com"
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        link = "facebook.com"
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        link = "twitter.com"
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        link = "linkedin.com"
                    }
                },
            }, "posts");
            var repository = new ReadPostRepository(mockClient.ElasticClient);

            var result = await repository.SearchAsync(new PostFilter(),
                                                      CancellationToken.None);

            result.Count(x => x.Link == link).Should().Be(expectCount);
        }
        public async Task Should_find_all_posts_by_date(string dateStr, int expectCount)
        {
            var mockClient = new MockElasticClient <PostSource>(
                new[] {
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        date = DateTime.Parse("2020-05-31")
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        date = DateTime.Parse("2020-05-31")
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        date = DateTime.Parse("2019-05-31")
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        date = DateTime.Parse("2018-05-31")
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        date = DateTime.Parse("2018-05-31")
                    }
                },
                new BaseIndexedEntry <PostSource> {
                    id = Guid.NewGuid(), source = new PostSource {
                        date = DateTime.Parse("2018-05-31")
                    }
                },
            }, "posts");
            var repository = new ReadPostRepository(mockClient.ElasticClient);

            var result = await repository.SearchAsync(new PostFilter(),
                                                      CancellationToken.None);

            result.Count(x => x.Date == DateTime.Parse(dateStr)).Should().Be(expectCount);
        }
        public async Task ShouldFilterOutStandardsThatAreNotValid()
        {
            var response     = new StubResponse(400);
            var mockResponse = new Mock <IBulkResponse> {
                DefaultValue = DefaultValue.Mock
            };
            var mockSettings = new Mock <IInfrastructureSettings>();

            mockSettings.SetupGet(x => x.FrameworkIdFormat).Returns("{0}{1}{2}");
            MockElasticClient.Setup(x => x.CreateIndex(It.IsAny <IndexName>(), It.IsAny <Func <CreateIndexDescriptor, ICreateIndexRequest> >(), It.IsAny <string>())).Returns(response);
            MockElasticClient.Setup(x => x.BulkAsync(It.IsAny <IBulkRequest>(), It.IsAny <string>())).Returns(Task.FromResult <IBulkResponse>(mockResponse.Object));

            var indexMaintainer = new ElasticsearchApprenticeshipIndexMaintainer(
                MockElasticClient.Object,
                new ElasticsearchMapper(mockSettings.Object, Mock.Of <IOrganisationTypeProcessor>()),
                Mock.Of <ILog>(),
                Mock.Of <IElasticsearchConfiguration>());

            await indexMaintainer.IndexStandards("testindex", StandardsTestData4001WithOneNull());

            MockElasticClient.Verify(x => x.BulkAsync(It.IsAny <IBulkRequest>(), It.IsAny <string>()), Times.Exactly(1));
        }