public async void Search_TestRequestSetup(Terms_Search_Request_Base data) { JObject actualRequest = null; ElasticsearchInterceptingConnection conn = new ElasticsearchInterceptingConnection(); conn.RegisterRequestHandlerForType <Nest.SearchResponse <GlossaryTerm> >((req, res) => { // We don't really care about the ES response for this test. res.Stream = MockEmptyResponse; res.StatusCode = 200; actualRequest = conn.GetRequestPost(req); }); // While this has a URI, it 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> gTermsClientOptions = GetMockOptions(); ESTermsQueryService termsClient = new ESTermsQueryService(client, gTermsClientOptions, new NullLogger <ESTermsQueryService>()); try { var results = await termsClient.Search(data.Dictionary, data.Audience, data.LangCode, data.SearchTerm, data.MatchType, data.Size, data.From, data.IncludeAdditionalInfo); } catch (Exception) { } Assert.Equal(data.ExpectedRequest, actualRequest, new JTokenEqualityComparer()); }
public async void GetCount_InvalidResponse() { ElasticsearchInterceptingConnection conn = new ElasticsearchInterceptingConnection(); conn.RegisterRequestHandlerForType <Nest.CountResponse>((req, res) => { string partial = @"{ ""count"": 8458, ""_shards"": {"; 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(); ESTermsQueryService query = new ESTermsQueryService(client, clientOptions, new NullLogger <ESTermsQueryService>()); // We don't care about the inputs, only in the error response. APIErrorException ex = await Assert.ThrowsAsync <APIErrorException>( () => query.GetCount("Cancer.gov", AudienceType.HealthProfessional, "es") ); Assert.Equal(500, ex.HttpStatusCode); }
public async void Search_TestAPIConnectionFailure() { ElasticsearchInterceptingConnection conn = new ElasticsearchInterceptingConnection(); conn.RegisterRequestHandlerForType <Nest.SearchResponse <GlossaryTerm> >((req, res) => { res.StatusCode = 500; }); // While this has a URI, it 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> gTermsClientOptions = GetMockOptions(); ESTermsQueryService termsClient = new ESTermsQueryService(client, gTermsClientOptions, new NullLogger <ESTermsQueryService>()); APIErrorException ex = await Assert.ThrowsAsync <APIErrorException>(() => termsClient.Search("Cancer.gov", AudienceType.Patient, "en", "chicken", MatchType.Begins, 10, 0, false)); Assert.Equal(500, ex.HttpStatusCode); }
public async void GetByName_DataLoading(GetByNameTermsQueryTestData data) { IElasticClient client = GetByName_GetElasticClientWithData(data.PrettyUrlName); // Setup the mocked Options IOptions <GlossaryAPIOptions> gTermsClientOptions = GetMockOptions(); ESTermsQueryService termsClient = new ESTermsQueryService(client, gTermsClientOptions, new NullLogger <ESTermsQueryService>()); GlossaryTerm glossaryTerm = await termsClient.GetByName("Cancer.gov", AudienceType.Patient, "en", "s-1"); Assert.Equal(data.ExpectedData, glossaryTerm, new GlossaryTermComparer()); }
public async void GetByName_TestNoOrIncorrectResults(string file, int expectedStatusCode, string expectedMessage) { IElasticClient client = GetByName_GetElasticClientWithData(file); // Setup the mocked Options IOptions <GlossaryAPIOptions> gTermsClientOptions = GetMockOptions(); ESTermsQueryService termsClient = new ESTermsQueryService(client, gTermsClientOptions, new NullLogger <ESTermsQueryService>()); APIErrorException ex = await Assert.ThrowsAsync <APIErrorException>(() => termsClient.GetByName("Cancer.gov", AudienceType.Patient, "en", "s-1")); Assert.Equal(expectedStatusCode, ex.HttpStatusCode); Assert.Equal(expectedMessage, ex.Message); }
public async void Search_DataLoading(SearchTermsQueryTestData data) { IElasticClient client = Search_GetElasticClientWithData(data.SearchTestType); // Setup the mocked Options IOptions <GlossaryAPIOptions> gTermsClientOptions = GetMockOptions(); ESTermsQueryService termsClient = new ESTermsQueryService(client, gTermsClientOptions, new NullLogger <ESTermsQueryService>()); GlossaryTermResults glossaryTermResults = await termsClient.Search("Cancer.gov", AudienceType.Patient, "en", "chicken", MatchType.Begins, 5, 0, false); Assert.Equal(data.ExpectedData.Results, glossaryTermResults.Results, new GlossaryTermComparer()); Assert.Equal(data.ExpectedData.Meta.TotalResults, glossaryTermResults.Meta.TotalResults); Assert.Equal(data.ExpectedData.Meta.From, glossaryTermResults.Meta.From); }
public async void GetCount_Request(BaseTermsQueryCountTestData 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.CountResponse>((req, res) => { // We don't really care about the response for this test. res.Stream = GetMockCountResponse(); 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(); ESTermsQueryService query = new ESTermsQueryService(client, clientOptions, new NullLogger <ESTermsQueryService>()); // We don't really care that this returns anything (for this test), only that the intercepting connection // sets up the request correctly. long result = await query.GetCount(data.DictionaryName, data.Audience, data.Language); Assert.Equal("/glossaryv1/terms/_count", esURI.AbsolutePath); Assert.Equal("application/json", esContentType); Assert.Equal(HttpMethod.POST, esMethod); Assert.Equal(data.ExpectedData, requestBody, new JTokenEqualityComparer()); }
public async void GetById_TestUriSetup(BaseTermsQueryTestData data) { Uri esURI = null; ElasticsearchInterceptingConnection conn = new ElasticsearchInterceptingConnection(); conn.RegisterRequestHandlerForType <Nest.GetResponse <GlossaryTerm> >((req, res) => { //Get the file name for this round res.Stream = TestingTools.GetTestFileAsStream("ESTermsQueryData/GetById/" + data.ESTermID + ".json"); res.StatusCode = 200; esURI = req.Uri; }); // While this has a URI, it 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> gTermsClientOptions = GetMockOptions(); ESTermsQueryService termsClient = new ESTermsQueryService(client, gTermsClientOptions, new NullLogger <ESTermsQueryService>()); // We don't actually care that this returns anything - only that the intercepting connection // sets up the request URI correctly. GlossaryTerm actDisplay = await termsClient.GetById( data.DictionaryName, data.Audience, data.Language, data.TermID ); Assert.Equal($"/glossaryv1/terms/{data.ESTermID}", esURI.AbsolutePath); }
public async void GetCount_Response(BaseTermsCountResponseData 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.CountResponse>((req, res) => { res.Stream = TestingTools.GetTestFileAsStream("ESTermsQueryData/GetCount/" + 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(); ESTermsQueryService query = new ESTermsQueryService(client, clientOptions, new NullLogger <ESTermsQueryService>()); // We don't really care about the inputs. What matters is the return, which is controlled by the mock ES connection. long result = await query.GetCount("Cancer.gov", AudienceType.Patient, "es"); Assert.Equal(data.ExpectedCount, result); }
public async void GetByName_TestRequestSetup() { JObject actualRequest = null; JObject expectedRequest = JObject.Parse(@" { ""sort"": [ { ""term_name"": {} } ], ""query"": { ""bool"": { ""must"": [ { ""term"": { ""language"": { ""value"": ""en"" } } }, { ""term"": { ""audience"": { ""value"": ""Patient"" } } }, { ""term"": { ""dictionary"": { ""value"": ""Cancer.gov"" } } }, { ""term"": { ""pretty_url_name"": { ""value"": ""s-1"" } } } ] } } }" ); ElasticsearchInterceptingConnection conn = new ElasticsearchInterceptingConnection(); conn.RegisterRequestHandlerForType <Nest.SearchResponse <GlossaryTerm> >((req, res) => { res.Stream = TestingTools.GetTestFileAsStream("ESTermsQueryData/GetByName/s-1.json"); res.StatusCode = 200; actualRequest = conn.GetRequestPost(req); }); // While this has a URI, it 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> gTermsClientOptions = GetMockOptions(); ESTermsQueryService termsClient = new ESTermsQueryService(client, gTermsClientOptions, new NullLogger <ESTermsQueryService>()); try { var results = await termsClient.GetByName("Cancer.gov", AudienceType.Patient, "en", "s-1"); } catch (Exception) { } Assert.Equal(expectedRequest, actualRequest, new JTokenEqualityComparer()); }