public async void GetSuggestions_TestInvalidResponse()
        {
            ElasticsearchInterceptingConnection conn = new ElasticsearchInterceptingConnection();

            conn.RegisterRequestHandlerForType <Nest.SearchResponse <Suggestion> >((req, res) =>
            {
                string partial   = @"{
                    ""took"": 223,
                    ""timed_out"": false,
                    ""_shards"": {
                                ""total"": 1,
                        ""successful"": 1,";
                byte[] byteArray = Encoding.UTF8.GetBytes(partial);
                res.Stream       = new MemoryStream(byteArray);
                res.StatusCode   = 200;
            });

            // The URI does not matter, an InMemoryConnection never requests from the server.
            var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

            var            connectionSettings = new ConnectionSettings(pool, conn);
            IElasticClient client             = new ElasticClient(connectionSettings);

            // Setup the mocked Options
            IOptions <GlossaryAPIOptions> clientOptions = GetMockOptions();

            ESAutosuggestQueryService query = new ESAutosuggestQueryService(client, clientOptions, new NullLogger <ESAutosuggestQueryService>());

            // We don't care about the inputs, only in the error response.
            APIErrorException ex = await Assert.ThrowsAsync <APIErrorException>(
                () => query.GetSuggestions("Cancer.gov", AudienceType.HealthProfessional, "es", "chicken", MatchType.Contains, 200)
                );

            Assert.Equal(500, ex.HttpStatusCode);
        }
        public async void GetSuggestions_TestEmptyESResults()
        {
            ElasticsearchInterceptingConnection conn = new ElasticsearchInterceptingConnection();

            conn.RegisterRequestHandlerForType <Nest.SearchResponse <Suggestion> >((req, res) =>
            {
                // We don't really care about the response for this test.
                res.Stream     = GetMockEmptyResponse();
                res.StatusCode = 200;
            });

            // The URI does not matter, an InMemoryConnection never requests from the server.
            var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

            var            connectionSettings = new ConnectionSettings(pool, conn);
            IElasticClient client             = new ElasticClient(connectionSettings);

            // Setup the mocked Options
            IOptions <GlossaryAPIOptions> clientOptions = GetMockOptions();

            ESAutosuggestQueryService query = new ESAutosuggestQueryService(client, clientOptions, new NullLogger <ESAutosuggestQueryService>());

            // We don't really care about the inputs, only the result.
            Suggestion[] result = await query.GetSuggestions("Cancer.gov", AudienceType.HealthProfessional, "es", "chicken", MatchType.Contains, 200);

            Assert.Empty(result);
        }
        public async void GetSuggestions_TestErrorResponse()
        {
            ElasticsearchInterceptingConnection conn = new ElasticsearchInterceptingConnection();

            conn.RegisterRequestHandlerForType <Nest.SearchResponse <Suggestion> >((req, res) =>
            {
                // Simulate an error.
                res.Stream     = null;
                res.StatusCode = 500;
            });

            // The URI does not matter, an InMemoryConnection never requests from the server.
            var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

            var            connectionSettings = new ConnectionSettings(pool, conn);
            IElasticClient client             = new ElasticClient(connectionSettings);

            // Setup the mocked Options
            IOptions <GlossaryAPIOptions> clientOptions = GetMockOptions();

            ESAutosuggestQueryService query = new ESAutosuggestQueryService(client, clientOptions, new NullLogger <ESAutosuggestQueryService>());

            // We don't care about the inputs, only in the error response.
            APIErrorException ex = await Assert.ThrowsAsync <APIErrorException>(
                () => query.GetSuggestions("Cancer.gov", AudienceType.HealthProfessional, "es", "chicken", MatchType.Contains, 200)
                );

            Assert.Equal(500, ex.HttpStatusCode);
        }
        public async void GetSuggestions_TestRequestSetup(BaseAutosuggestServiceScenario data)
        {
            Uri        esURI         = null;
            string     esContentType = String.Empty;
            HttpMethod esMethod      = HttpMethod.DELETE; // Basically, something other than the expected value.

            JObject requestBody = null;

            ElasticsearchInterceptingConnection conn = new ElasticsearchInterceptingConnection();

            conn.RegisterRequestHandlerForType <Nest.SearchResponse <Suggestion> >((req, res) =>
            {
                // We don't really care about the response for this test.
                res.Stream     = MockEmptyResponse;
                res.StatusCode = 200;

                esURI         = req.Uri;
                esContentType = req.ContentType;
                esMethod      = req.Method;
                requestBody   = conn.GetRequestPost(req);
            });

            // The URI does not matter, an InMemoryConnection never requests from the server.
            var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

            var            connectionSettings = new ConnectionSettings(pool, conn);
            IElasticClient client             = new ElasticClient(connectionSettings);

            // Setup the mocked Options
            IOptions <DrugDictionaryAPIOptions> clientOptions = GetMockOptions();

            clientOptions.Value.Autosuggest.MaxSuggestionLength = data.MaxSuggestionLength;

            ESAutosuggestQueryService query = new ESAutosuggestQueryService(client, clientOptions, new NullLogger <ESAutosuggestQueryService>());

            // We don't really care that this returns anything (for this test), only that the intercepting connection
            // sets up the request correctly.
            Suggestion[] result = await query.GetSuggestions(data.SearchText, data.MatchType, data.Size,
                                                             data.IncludeResourceTypes, data.IncludeNameTypes, data.ExcludeNameTypes);

            Assert.Equal("/drugv1/terms/_search", esURI.AbsolutePath);
            Assert.Equal("application/json", esContentType);
            Assert.Equal(HttpMethod.POST, esMethod);
            Assert.Equal(data.ExpectedData, requestBody, new JTokenEqualityComparer());
        }
        public async void GetSuggestions_TestBeginsRequestSetup(BaseAutosuggestTestData data)
        {
            Uri        esURI         = null;
            string     esContentType = String.Empty;
            HttpMethod esMethod      = HttpMethod.DELETE; // Basically, something other than the expected value.

            JObject requestBody = null;

            ElasticsearchInterceptingConnection conn = new ElasticsearchInterceptingConnection();

            conn.RegisterRequestHandlerForType <Nest.SearchResponse <Suggestion> >((req, res) =>
            {
                res.Stream     = TestingTools.GetTestFileAsStream("ESAutosuggestQueryResponse/" + data.TestFilename);
                res.StatusCode = 200;

                esURI         = req.Uri;
                esContentType = req.ContentType;
                esMethod      = req.Method;
                requestBody   = conn.GetRequestPost(req);
            });

            // The URI does not matter, an InMemoryConnection never requests from the server.
            var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

            var            connectionSettings = new ConnectionSettings(pool, conn);
            IElasticClient client             = new ElasticClient(connectionSettings);

            // Setup the mocked Options
            IOptions <GlossaryAPIOptions> clientOptions = GetMockOptions();

            ESAutosuggestQueryService query = new ESAutosuggestQueryService(client, clientOptions, new NullLogger <ESAutosuggestQueryService>());

            // We don't really care about the inputs. What matters is the return, which is controlled by the mock ES connection.
            Suggestion[] result = await query.GetSuggestions("Cancer.gov", AudienceType.Patient, "es", "chicken", MatchType.Contains, 200);

            Assert.Equal(data.ExpectedData, result, new ArrayComparer <Suggestion, SuggestionComparer>());
        }