Ejemplo n.º 1
0
        /// <summary>
        /// Gets an ElasticClient backed by an InMemoryConnection.  This is used to mock the
        /// JSON returned by the elastic search so that we test the Nest mappings to our models.
        /// </summary>
        /// <param name="testFile"></param>
        /// <returns></returns>
        public static IElasticClient GetInMemoryElasticClient(string testFile)
        {
            //Get Response JSON
            byte[] responseBody = TestingTools.GetTestFileAsBytes(testFile);

            //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"));

            // Setup ElasticSearch stuff using the contents of the JSON file as the client response.
            InMemoryConnection conn = new InMemoryConnection(responseBody);

            var connectionSettings = new ConnectionSettings(pool, conn, sourceSerializer: JsonNetSerializer.Default);

            return(new ElasticClient(connectionSettings));
        }
        public void GetTestFileAsBytes_ReadExistingFile()
        {
            string data = @"
        {
          ""key"": ""value"",
          ""key2"": ""value2""
        }";

            byte[] expected = Encoding.UTF8.GetBytes(data);

            // Write the data to a temporary location.
            string path = Path.GetTempFileName();

            File.WriteAllText(path, data);

            // Get the data back.
            byte[] actual = TestingTools.GetTestFileAsBytes(path);

            Assert.Equal(expected, actual);
        }
 public void GetTestFileAsBytes_NonexistingFile()
 {
     Assert.Throws <FileNotFoundException>(
         () => TestingTools.GetTestFileAsBytes("NonExistingFile.json")
         );
 }