public void CreateIndex_QueryValidation() { string aliasName = "TestAlias"; ESBBIndexerService service = this.GetIndexerService( aliasName, conn => { conn.RegisterRequestHandlerForType <Nest.CreateIndexResponse>((req, res) => { res.StatusCode = 200; res.Stream = TestingTools.GetTestFileAsStream("ES_Acknowledged_True_Response.json"); }); } ); string currTimeStamp = DateTime.Now.ToString("yyyyMMddHHmmss"); string actualIndexName = service.CreateTimeStampedIndex(); string actualTimeStamp = actualIndexName.Replace(aliasName, ""); long currAsLong = long.Parse(currTimeStamp); long actualAsLong = long.Parse(actualTimeStamp); //Give it up to a 5 sec difference. Assert.InRange(actualAsLong, currAsLong - 5, currAsLong); }
public async void GetAll_TestRequestSetup(GetAll_Request_Base data) { JObject actualRequest = null; ElasticsearchInterceptingConnection conn = new ElasticsearchInterceptingConnection(); conn.RegisterRequestHandlerForType <Nest.SearchResponse <GlossaryTerm> >((req, res) => { res.Stream = TestingTools.GetTestFileAsStream("ESTermsQueryData/GetAll/getall_response_results.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.GetAll(data.Dictionary, data.Audience, data.LangCode, data.Size, data.From, data.IncludeAdditionalInfo); } catch (Exception) { } Assert.Equal(data.ExpectedRequest, actualRequest, new JTokenEqualityComparer()); }
public void OptimizedSucceeded() { string aliasName = "bestbets"; string indexName = aliasName + "20161207125500"; string actualPath = string.Empty; object actualRequestOptions = null; ESBBIndexerService service = this.GetIndexerService( aliasName, conn => { //This is the handler for the Optimize call conn.RegisterRequestHandlerForType <Nest.ForceMergeResponse>((req, res) => { //Store off Request actualPath = req.Path; actualRequestOptions = conn.GetRequestPost(req); //Setup Response res.StatusCode = 200; res.Stream = TestingTools.GetTestFileAsStream("ForceMerge_Good_Response.json"); }); } ); bool succeeded = service.OptimizeIndex(indexName); //Make sure the Request is the expected request //Params for this are actual query params. // Hostname doesn't matter, just needed so we have an absolute Uri (relative Uri allows very few operations). Uri actualReqUri = new Uri( "http://localhost" + actualPath, UriKind.Absolute); Uri expectedUri = new Uri( "http://localhost" + indexName + "/_forcemerge?wait_for_merge=true&max_num_segments=1", UriKind.Absolute); //Check the path/parameters are as expected Assert.Equal(expectedUri, actualReqUri, new UriComparer()); //Check that there was not request body Assert.Null(actualRequestOptions); //Technically, there is no condition where false would be returned as //an exception would be thrown. Assert.True(succeeded); }
/// <summary> /// Creates a new instance of the ESResAggSvcConnection class /// </summary> /// <param name="testFile">The JSON file for the test response</param> public ESResQSvcGetConn(string testFile, int status = 200) { this.TestFile = testFile; //This section is for registering the intercepters for the request. //Add Handlers this.RegisterRequestHandlerForType <Nest.GetResponse <Resource> >((req, res) => { //Get the file name for this round res.Stream = TestingTools.GetTestFileAsStream(GetTestFileName()); res.StatusCode = status; }); }
/// <summary> /// Creates a new instance of the ESMatchTokenizerConnection class /// </summary> /// <param name="testFilePrefix">The prefix of the test files</param> public ESHealthTokenizerConnection(string testFilePrefix) { this.TestFilePrefix = testFilePrefix; //Add Handlers this.RegisterRequestHandlerForType <Nest.ClusterHealthResponse>((req, res) => { //I don't care about the request for this... for now. //Get the file name for this round res.Stream = TestingTools.GetTestFileAsStream(GetTestFileName()); res.StatusCode = 200; }); }
/// <summary> /// Creates a new instance of the ESMatchConnection class /// </summary> /// <param name="testFilePrefix">The prefix of the test files</param> public ESHealthConnection(string testFilePrefix) { this.TestFilePrefix = testFilePrefix; //Add Handlers this.RegisterRequestHandlerForType <Nest.ClusterHealthResponse>((req, res) => { // Health check is a GET request (e.g. https://localhost:9299/_cluster/health/bestbets?pretty) // so we don't need to do anything special, just load the data file. //Get the file name for this round res.Stream = TestingTools.GetTestFileAsStream(GetTestFileName()); res.StatusCode = 200; }); }
public async void GetByIdForGlossaryTerm(BaseTermQueryTestData 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("ESTermQueryData/" + 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> gTermClientOptions = GetMockOptions(); ESTermQueryService termClient = new ESTermQueryService(client, gTermClientOptions, new NullLogger <ESTermQueryService>()); // We don't actually care that this returns anything - only that the intercepting connection // sets up the request URI correctly. GlossaryTerm actDisplay = await termClient.GetById( data.DictionaryName, data.Audience, data.Language, data.TermID, new string[] {} ); Assert.Equal( esURI.Segments, new string[] { "/", "glossaryv1/", "terms/", data.ESTermID }, new ArrayComparer() ); }
/// <summary> /// Creates a new instance of the ESMatchConnection class /// </summary> /// <param name="testFilePrefix">The prefix of the test files</param> public ESMatchConnection(string testFilePrefix) { this.TestFilePrefix = testFilePrefix; //Add Handlers this.RegisterRequestHandlerForType <Nest.SearchResponse <BestBetsMatch> >((req, res) => { //Get the request parameters dynamic postObj = this.GetRequestPost(req); //Determine which round we are performing int numTokens = postObj["params"].matchedtokencount; //Get the file name for this round res.Stream = TestingTools.GetTestFileAsStream(GetTestFileName(numTokens)); res.StatusCode = 200; }); }
/// <summary> /// Creates a new instance of the ESMatchConnection class /// </summary> /// <param name="testFilePrefix">The prefix of the test files</param> public ESMatchConnection(string testFilePrefix) { this.TestFilePrefix = testFilePrefix; //Add Handlers this.RegisterRequestHandlerForType <Nest.SearchResponse <BestBetsMatch> >((req, res) => { //Get the request parameters dynamic postObj = this.GetRequestPost(req); //Determine which round we are performing //int numTokens = postObj["params"].matchedtokencount; //If this is one item the bool node will be the nested match //if it is both exact and matches, then the bool node will //be a should. This code is tightly matched to the built query //in the implementation int numTokens = -1; var boolNode = postObj["query"]["bool"]; if (boolNode["should"] != null) { numTokens = boolNode["should"][0] ["bool"]["must"][3] ["match"]["synonym"] ["minimum_should_match"].ToObject <int>(); } else { numTokens = boolNode["must"][3] ["match"]["synonym"] ["minimum_should_match"].ToObject <int>(); } //Get the file name for this round res.Stream = TestingTools.GetTestFileAsStream(GetTestFileName(numTokens)); res.StatusCode = 200; }); }
public void SingleAlias() { string aliasName = "bestbets"; string actualPath = string.Empty; object actualRequestOptions = null; ESBBIndexerService service = this.GetIndexerService( aliasName, conn => { //This is the handler for AliasExists calls conn.RegisterRequestHandlerForType <Nest.ExistsResponse>((req, res) => { //Just need to return 200 to say alias does exist res.StatusCode = 200; res.Stream = null; res.Exception = null; }); conn.RegisterRequestHandlerForType <Nest.GetAliasesResponse>((req, res) => { //Store off Request actualPath = req.Path; actualRequestOptions = conn.GetRequestPost(req); //Setup Response res.StatusCode = 200; res.Stream = TestingTools.GetTestFileAsStream("GetIndicesForAlias_SingleItemResponse.json"); }); } ); string[] actualIndices = service.GetIndicesForAlias(); //Make sure the Request is the expected request Assert.Equal(aliasName + "/_alias", actualPath); Assert.Null(actualRequestOptions); //Make sure the response is the same. Assert.Equal(new string[] { "bestbets20161202152737" }, actualIndices); }
///<summary> ///A private method to enrich data from file for Search ///</summary> private IElasticClient Search_GetElasticClientWithData(string searchTestType) { ElasticsearchInterceptingConnection conn = new ElasticsearchInterceptingConnection(); conn.RegisterRequestHandlerForType <Nest.SearchResponse <GlossaryTerm> >((req, res) => { //Get the file name for this round res.Stream = TestingTools.GetTestFileAsStream("ESTermsQueryData/Search/search_response_" + searchTestType + ".json"); res.StatusCode = 200; }); //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); return(client); }
/// <summary> /// Creates a new instance of the ESResAggSvcConnection class /// </summary> /// <param name="testFile">The JSON file for the test response</param> public ESResAggSvcConnection(string testFile) { this.TestFile = testFile; //This section is for registering the intercepters for the request. //Add Handlers this.RegisterRequestHandlerForType <Nest.SearchResponse <Resource> >((req, res) => { //Get the request parameters //dynamic postObj = this.GetRequestPost(req); //Determine which round we are performing //int numTokens = postObj["params"].matchedtokencount; //Get the file name for this round res.Stream = TestingTools.GetTestFileAsStream(GetTestFileName()); res.StatusCode = 200; }); }
private IElasticClient GetElasticClientWithData(BaseDisplayTestData data) { ElasticsearchInterceptingConnection conn = new ElasticsearchInterceptingConnection(); conn.RegisterRequestHandlerForType <Nest.GetResponse <BestBetsCategoryDisplay> >((req, res) => { //Get the file name for this round res.Stream = TestingTools.GetTestFileAsStream("ESDisplayData/" + data.TestFilePath); res.StatusCode = 200; }); //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); return(client); }
public async void GetBestBetForDisplay_TestURISetup(BaseDisplayTestData data) { Uri esURI = null; ElasticsearchInterceptingConnection conn = new ElasticsearchInterceptingConnection(); conn.RegisterRequestHandlerForType <Nest.GetResponse <BestBetsCategoryDisplay> >((req, res) => { //Get the file name for this round res.Stream = TestingTools.GetTestFileAsStream("ESDisplayData/" + data.TestFilePath); 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 <CGBBIndexOptions> bbClientOptions = GetMockOptions(); ESBestBetsDisplayService bbClient = new ESBestBetsDisplayService(client, bbClientOptions, new NullLogger <ESBestBetsDisplayService>()); // We don't actually care that this returns anything - only that the intercepting connection // sets up the request URI correctly. IBestBetDisplay actDisplay = await bbClient.GetBestBetForDisplay("preview", "431121"); Assert.Equal(esURI.Segments, new string[] { "/", "bestbets_preview_v1/", "categorydisplay/", "431121" }, new ArrayComparer()); actDisplay = await bbClient.GetBestBetForDisplay("live", "431121"); Assert.Equal(esURI.Segments, new string[] { "/", "bestbets_live_v1/", "categorydisplay/", "431121" }, new ArrayComparer()); }
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 void MakeCurrentManyExisting() { string aliasName = "bestbets"; string newIndexName = aliasName + "20161207125500"; string actualPath = string.Empty; JObject actualRequestOptions = null; ESBBIndexerService service = this.GetIndexerService( aliasName, conn => { //This is the handler for AliasExists calls conn.RegisterRequestHandlerForType <Nest.ExistsResponse>((req, res) => { //Just need to return 200 to say alias does exist res.StatusCode = 200; res.Stream = null; res.Exception = null; }); //This is the handler for getting the indices associated with the alias conn.RegisterRequestHandlerForType <Nest.GetAliasesResponse>((req, res) => { //Store off Request actualPath = req.Path; actualRequestOptions = conn.GetRequestPost(req); //Setup Response res.StatusCode = 200; res.Stream = TestingTools.GetTestFileAsStream("GetIndicesForAlias_MultiItemResponse.json"); }); //This is the handler for Alias, and the focus of this test. conn.RegisterRequestHandlerForType <BulkAliasResponse>((req, res) => { actualPath = req.Path; actualRequestOptions = actualRequestOptions = conn.GetRequestPost(req); res.StatusCode = 200; res.Stream = TestingTools.GetTestFileAsStream("ES_Acknowledged_True_Response.json"); }); } ); service.MakeIndexCurrentAlias(newIndexName); // Make sure the Request is the expected request // Index being added should be newIndexName // Alias being addded should be aliasName // Values for expectedRemoveIndices MUST match GetIndicesForAlias_MultiItemResponse.json string[] expectedAddIndices = new string[] { newIndexName }; string[] expectedRemoveIndices = new string[] { "bestbets20161202152737", "bestbets20161201165226" }; // Pull out the lists of aliases and indexes being added/removed. string[] actualAddedAliases = (from act in actualRequestOptions["actions"] where act["add"] != null select(string) act["add"]["alias"]).ToArray(); string[] actualAddedIndices = (from act in actualRequestOptions["actions"] where act["add"] != null select(string) act["add"]["index"]).ToArray(); string[] actualRemovedAliases = (from act in actualRequestOptions["actions"] where act["remove"] != null select(string) act["remove"]["alias"]).ToArray(); string[] actualRemovedIndices = (from act in actualRequestOptions["actions"] where act["remove"] != null select(string) act["remove"]["index"]).ToArray(); // Items where both actions are missing, or both are present. (List should be empty.) var unexpectedList = from act in actualRequestOptions["actions"] where (act["add"] != null && act["remove"] != null) || (act["add"] == null && act["remove"] == null) select act; // Check list lengths. bool correctListSizes = actualAddedAliases.Length == 1 && actualAddedIndices.Length == 1 && actualRemovedAliases.Length == 2 && actualRemovedIndices.Length == 2 && unexpectedList.Count() == 0; // Check that all expected indices are accounted for. bool expectedAddIndicesPresent = expectedAddIndices.All(index => actualAddedIndices.Contains(index)); bool expectedRemoveIndicesPresent = expectedRemoveIndices.All(index => actualRemovedIndices.Contains(index)); // All aliases should match aliasName. bool addAliasCorrect = actualAddedAliases.All(alias => alias == aliasName); bool removeAliasCorrect = actualRemovedAliases.All(alias => alias == aliasName); Assert.True(correctListSizes && expectedAddIndicesPresent && expectedRemoveIndicesPresent && addAliasCorrect && removeAliasCorrect); }
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()); }
public void MakeCurrentNoExisting() { string aliasName = "bestbets"; string newIndexName = aliasName + "20161207125500"; string actualPath = string.Empty; JObject actualRequestOptions = null; ESBBIndexerService service = this.GetIndexerService( aliasName, conn => { //This is the handler for AliasExists calls. This handles no existing alias. conn.RegisterRequestHandlerForType <Nest.ExistsResponse>((req, res) => { //Setup Response res.StatusCode = 404; }); //This is the handler for Alias, and the focus of this test. conn.RegisterRequestHandlerForType <BulkAliasResponse>((req, res) => { actualPath = req.Path; actualRequestOptions = actualRequestOptions = conn.GetRequestPost(req); res.StatusCode = 200; res.Stream = TestingTools.GetTestFileAsStream("ES_Acknowledged_True_Response.json"); }); } ); service.MakeIndexCurrentAlias(newIndexName); //Make sure the Request is the expected request //Assert.Equal("_alias/" + aliasName, actualPath); string[] expectedAddIndices = new string[] { newIndexName }; string[] expectedRemoveIndices = new string[] { }; // Pull out the lists of aliases and indexes being added/removed. string[] actualAddedAliases = (from act in actualRequestOptions["actions"] where act["add"] != null select(string) act["add"]["alias"]).ToArray(); string[] actualAddedIndices = (from act in actualRequestOptions["actions"] where act["add"] != null select(string) act["add"]["index"]).ToArray(); // Items where the add action is missing, or remove is present. (List should be empty.) var unexpectedList = from act in actualRequestOptions["actions"] where act["add"] == null || act["remove"] != null select act; // Check list lengths. bool correctListSizes = actualAddedAliases.Length == 1 && actualAddedIndices.Length == 1 && unexpectedList.Count() == 0; // Check that all expected indices are accounted for. bool expectedAddIndicesPresent = expectedAddIndices.All(index => actualAddedIndices.Contains(index)); // All aliases should match aliasName. bool addAliasCorrect = actualAddedAliases.All(alias => alias == aliasName); //Are assertions will be on the actual request options. Assert.True( correctListSizes && expectedAddIndicesPresent && addAliasCorrect ); }