Esempio n. 1
0
        /// <summary>
        /// Verify that controller throws the correct exception when no collection is specified.
        /// </summary>
        /// <param name="collectionValue">A string specifying the collection to search.</param>
        public void Get_EmptyCollection_ReturnsError(String collectionValue)
        {
            // The file needs to exist so it can be deserialized, but we don't make
            // use of the actual content.
            string testFile = "Search.CGov.En.BreastCancer.json";

            IOptions <AutosuggestIndexOptions> config = GetMockedAutosuggestIndexOptions();
            AutosuggestController ctrl = new AutosuggestController(
                ElasticTools.GetInMemoryElasticClient(testFile),
                config,
                NullLogger <AutosuggestController> .Instance
                );

            Exception ex = Assert.Throws <APIErrorException>(
                // Parameters don't matter, and for this test we don't care about saving the results
                () =>
                ctrl.Get(
                    collectionValue,
                    "en",
                    "some term"
                    )
                );

            // Search without a collection should report bad request (400)
            Assert.Equal(400, ((APIErrorException)ex).HttpStatusCode);
        }
        /// <summary>
        /// Verify that the request sent to ES for a single term is being set up correctly.
        /// </summary>
        public void Check_For_Correct_Request_Data()
        {
            string term = "Breast Cancer";

            ISearchTemplateRequest actualReq = null;

            //Setup the client with the request handler callback to be executed later.
            IElasticClient client =
                ElasticTools.GetMockedSearchTemplateClient <Suggestion>(
                    req => actualReq = req,
                    resMock => {
                //Make sure we say that the response is valid.
                resMock.Setup(res => res.IsValid).Returns(true);
            }         // We don't care what the response looks like.
                    );

            IOptions <AutosuggestIndexOptions> config = GetMockedAutosuggestIndexOptions();
            AutosuggestController controller          = new AutosuggestController(
                client,
                config,
                NullLogger <AutosuggestController> .Instance
                );

            //NOTE: this is when actualReq will get set.
            controller.Get(
                "cgov",
                "en",
                term
                );

            SearchTemplateRequest <Suggestion> expReq = GetSearchRequest <Suggestion>(
                "cgov",                   // Search index to look in.
                "autosg_suggest_cgov_en", // Template name, preceded by the name of the directory it's stored in.
                term,                     // Search term
                10,                       // Max number of records to retrieve.
                "\"url\", \"title\", \"metatag.description\", \"metatag.dcterms.type\"",
                "all"
                );

            Assert.Equal(
                expReq,
                actualReq,
                new ElasticTools.SearchTemplateRequestComparer()
                );
        }
Esempio n. 3
0
        [InlineData(500)] // Server error
        /// <summary>
        /// Verify that controller throws the correct exception when the
        /// ES client reports an error.
        /// </summary>
        /// <param name="offset">Offset into the list of results of the item to check.</param>
        /// <param name="expectedTerm">The expected term text</param>
        public void Handle_Failed_Query(int errorCode)
        {
            IOptions <AutosuggestIndexOptions> config = GetMockedAutosuggestIndexOptions();
            AutosuggestController ctrl = new AutosuggestController(
                ElasticTools.GetErrorElasticClient(errorCode),
                config,
                NullLogger <AutosuggestController> .Instance
                );

            Assert.Throws <APIErrorException>(
                // Parameters don't matter, and for this test we don't care about saving the results
                () =>
                ctrl.Get(
                    "cgov",
                    "en",
                    "breast cancer"
                    )
                );
        }
        /// <summary>
        /// Test that the list of results exists.
        /// </summary>
        public void Check_Results_Exist()
        {
            string testFile = "AutoSuggest.CGov.En.BreastCancer.json";

            IOptions <AutosuggestIndexOptions> config = GetMockedAutosuggestIndexOptions();
            AutosuggestController ctrl = new AutosuggestController(
                ElasticTools.GetInMemoryElasticClient(testFile),
                config,
                NullLogger <AutosuggestController> .Instance
                );

            //Parameters don't matter in this case...
            Suggestions results = ctrl.Get(
                "cgov",
                "en",
                "breast cancer"
                );

            Assert.NotEmpty(results.Results);
        }
        /// <summary>
        /// Test that the suggested search strings from arbitrary offsets
        /// in the collection have the correct values
        /// </summary>
        /// <param name="offset">Offset into the list of results of the item to check.</param>
        /// <param name="expectedTerm">The expected term text</param>
        public void Check_Arbitrary_Result(int offset, string expectedTerm)
        {
            string testFile = "AutoSuggest.CGov.En.BreastCancer.json";

            IOptions <AutosuggestIndexOptions> config = GetMockedAutosuggestIndexOptions();
            AutosuggestController ctrl = new AutosuggestController(
                ElasticTools.GetInMemoryElasticClient(testFile),
                config,
                NullLogger <AutosuggestController> .Instance
                );

            //Parameters don't matter in this case...
            Suggestions results = ctrl.Get(
                "cgov",
                "en",
                "breast cancer"
                );

            Assert.Equal(expectedTerm, results.Results[offset].Term);
        }
Esempio n. 6
0
        /// <summary>
        /// Verify that controller throws the correct exception when no search text is specified.
        /// </summary>
        /// <param name="termValue">A string the text to search for.</param>
        public void Get_EmptyTerm_ReturnsError(String termValue)
        {
            string testFile = "Search.CGov.En.BreastCancer.json";

            IOptions <AutosuggestIndexOptions> config = GetMockedAutosuggestIndexOptions();
            AutosuggestController ctrl = new AutosuggestController(
                ElasticTools.GetInMemoryElasticClient(testFile),
                config,
                NullLogger <AutosuggestController> .Instance
                );

            Exception ex = Assert.Throws <APIErrorException>(
                // Parameters don't matter, and for this test we don't care about saving the results
                () =>
                ctrl.Get(
                    "some collection",
                    "en",
                    termValue
                    )
                );

            // Search without something to search for should report bad request (400)
            Assert.Equal(400, ((APIErrorException)ex).HttpStatusCode);
        }