public void NoAlias()
        {
            string aliasName            = "chicken";
            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) =>
                {
                    //Store off Request
                    actualPath           = req.Path;
                    actualRequestOptions = conn.GetRequestPost(req);

                    //Setup Response
                    res.StatusCode = 404;
                });
            }
                );

            string[] actualIndices = service.GetIndicesForAlias();

            //Make sure the Request is the expected request
            Assert.Equal("_alias/" + aliasName, actualPath);
            Assert.Null(actualRequestOptions);

            //Make sure the response is the same.
            Assert.Equal(new string[] { }, actualIndices);
        }
Ejemplo n.º 2
0
        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);
        }
        /// <summary>
        /// Helper function to get an instance of a ESBBIndexerService Only needing to setup
        /// the connection request handler callbacks. (via connSetupCallback)
        /// </summary>
        /// <param name="aliasName"></param>
        /// <param name="connSetupCallback"></param>
        /// <returns></returns>
        protected ESBBIndexerService GetIndexerService(string aliasName, Action <ElasticsearchInterceptingConnection> connSetupCallback)
        {
            //Create connection and handle request.
            ElasticsearchInterceptingConnection interceptConnection = new ElasticsearchInterceptingConnection();

            connSetupCallback(interceptConnection);

            Mock <IOptions <ESBBIndexerServiceOptions> > options = new Mock <IOptions <ESBBIndexerServiceOptions> >();

            options
            .SetupGet(o => o.Value)
            .Returns(new ESBBIndexerServiceOptions()
            {
                AliasName = aliasName
            });

            //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, interceptConnection);

            IElasticClient client = new ElasticClient(connectionSettings);

            ESBBIndexerService service = new ESBBIndexerService(
                client,
                options.Object
                );

            return(service);
        }
        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);
        }
        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);
        }
Ejemplo n.º 6
0
        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
                );
        }
Ejemplo n.º 7
0
        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);
        }