public void CanRunIndexerAndGetIndexerStatus()
        {
            Run(() =>
            {
                SearchServiceClient searchClient = Data.GetSearchServiceClient();

                Indexer indexer = Data.CreateTestIndexer();

                IndexerDefinitionResponse createResponse = searchClient.Indexers.Create(indexer);
                Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode);

                IndexerGetStatusResponse statusResponse = searchClient.Indexers.GetStatus(indexer.Name);
                Assert.Equal(HttpStatusCode.OK, statusResponse.StatusCode);

                IndexerExecutionInfo info = statusResponse.ExecutionInfo;
                Assert.Equal(IndexerStatus.Running, info.Status);

                AzureOperationResponse runResponse = searchClient.Indexers.Run(indexer.Name);
                Assert.Equal(HttpStatusCode.Accepted, runResponse.StatusCode);

                // Set handler that injects mock_status query string, which results in service
                // returning a well-known mock response
                searchClient.AddHandlerToPipeline(new MockStatusDelegatingHandler());

                statusResponse = searchClient.Indexers.GetStatus(indexer.Name);
                Assert.Equal(HttpStatusCode.OK, statusResponse.StatusCode);

                info = statusResponse.ExecutionInfo;
                Assert.Equal(IndexerStatus.Running, info.Status);

                Assert.Equal(IndexerExecutionStatus.InProgress, info.LastResult.Status);
                Assert.Equal(3, info.ExecutionHistory.Count);

                IndexerExecutionResult newestResult = info.ExecutionHistory[0];
                IndexerExecutionResult middleResult = info.ExecutionHistory[1];
                IndexerExecutionResult oldestResult = info.ExecutionHistory[2];

                Assert.Equal(IndexerExecutionStatus.TransientFailure, newestResult.Status);
                Assert.Equal("The indexer could not connect to the data source", newestResult.ErrorMessage);
                AssertStartAndEndTimeValid(newestResult);

                Assert.Equal(IndexerExecutionStatus.Reset, middleResult.Status);
                AssertStartAndEndTimeValid(middleResult);

                Assert.Equal(IndexerExecutionStatus.Success, oldestResult.Status);
                Assert.Equal(124876, oldestResult.ItemCount);
                Assert.Equal(2, oldestResult.FailedItemCount);
                Assert.Equal("100", oldestResult.InitialTrackingState);
                Assert.Equal("200", oldestResult.FinalTrackingState);
                AssertStartAndEndTimeValid(oldestResult);

                Assert.Equal(2, oldestResult.Errors.Count);

                Assert.Equal("1", oldestResult.Errors[0].Key);
                Assert.Equal("Key field contains unsafe characters", oldestResult.Errors[0].ErrorMessage);

                Assert.Equal("121713", oldestResult.Errors[1].Key);
                Assert.Equal("Item is too large", oldestResult.Errors[1].ErrorMessage);
            });
        }
        public void CreateOrUpdateCreatesWhenIndexerDoesNotExist()
        {
            Run(() =>
            {
                SearchServiceClient searchClient = Data.GetSearchServiceClient();

                Indexer indexer = Data.CreateTestIndexer();

                IndexerDefinitionResponse response = searchClient.Indexers.CreateOrUpdate(indexer);
                Assert.Equal(HttpStatusCode.Created, response.StatusCode);
            });
        }
        public void CreateIndexerReturnsCorrectDefinition()
        {
            Run(() =>
            {
                SearchServiceClient searchClient = Data.GetSearchServiceClient();

                Indexer indexer = Data.CreateTestIndexer();

                IndexerDefinitionResponse createResponse = searchClient.Indexers.Create(indexer);
                Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode);

                AssertIndexersEqual(indexer, createResponse.Indexer);
            });
        }
        public void CanUpdateIndexer()
        {
            Run(() =>
            {
                SearchServiceClient searchClient = Data.GetSearchServiceClient();

                Indexer initial = Data.CreateTestIndexer();

                IndexerDefinitionResponse createResponse = searchClient.Indexers.Create(initial);
                Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode);

                Indexer updated     = Data.CreateTestIndexer();
                updated.Name        = initial.Name;
                updated.Description = "somethingdifferent";

                IndexerDefinitionResponse updateResponse = searchClient.Indexers.CreateOrUpdate(updated);
                Assert.Equal(HttpStatusCode.OK, updateResponse.StatusCode);

                AssertIndexersEqual(updated, updateResponse.Indexer);
            });
        }
        public void CanResetIndexerAndGetIndexerStatus()
        {
            Run(() =>
            {
                SearchServiceClient searchClient = Data.GetSearchServiceClient();

                Indexer indexer = Data.CreateTestIndexer();

                IndexerDefinitionResponse createResponse = searchClient.Indexers.Create(indexer);
                Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode);

                AzureOperationResponse resetResponse = searchClient.Indexers.Reset(indexer.Name);
                Assert.Equal(HttpStatusCode.NoContent, resetResponse.StatusCode);

                IndexerGetStatusResponse statusResponse = searchClient.Indexers.GetStatus(indexer.Name);
                Assert.Equal(HttpStatusCode.OK, statusResponse.StatusCode);

                IndexerExecutionInfo info = statusResponse.ExecutionInfo;
                Assert.Equal(IndexerStatus.Running, info.Status);
                Assert.Equal(IndexerExecutionStatus.Reset, info.LastResult.Status);
            });
        }
        public void DeleteIndexerIsIdempotent()
        {
            Run(() =>
            {
                SearchServiceClient searchClient = Data.GetSearchServiceClient();

                Indexer indexer = Data.CreateTestIndexer();

                // Try delete before the indexer even exists.
                AzureOperationResponse deleteResponse = searchClient.Indexers.Delete(indexer.Name);
                Assert.Equal(HttpStatusCode.NotFound, deleteResponse.StatusCode);

                IndexerDefinitionResponse createResponse = searchClient.Indexers.Create(indexer);
                Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode);

                // Now delete twice.
                deleteResponse = searchClient.Indexers.Delete(indexer.Name);
                Assert.Equal(HttpStatusCode.NoContent, deleteResponse.StatusCode);

                deleteResponse = searchClient.Indexers.Delete(indexer.Name);
                Assert.Equal(HttpStatusCode.NotFound, deleteResponse.StatusCode);
            });
        }
        public void CanCreateAndListIndexers()
        {
            Run(() =>
            {
                SearchServiceClient searchClient = Data.GetSearchServiceClient();

                Indexer indexer1 = Data.CreateTestIndexer();
                Indexer indexer2 = Data.CreateTestIndexer();

                IndexerDefinitionResponse createResponse = searchClient.Indexers.Create(indexer1);
                Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode);

                createResponse = searchClient.Indexers.Create(indexer2);
                Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode);

                IndexerListResponse listResponse = searchClient.Indexers.List();
                Assert.Equal(HttpStatusCode.OK, listResponse.StatusCode);
                Assert.Equal(2, listResponse.Indexers.Count);

                IEnumerable <string> indexerNames = listResponse.Indexers.Select(i => i.Name);
                Assert.Contains(indexer1.Name, indexerNames);
                Assert.Contains(indexer2.Name, indexerNames);
            });
        }