From d6c06cb0bfc57ce8cddea6f091fdf61dff437a76 Mon Sep 17 00:00:00 2001 From: Nava Vaisman Levy Date: Mon, 25 Nov 2019 15:23:32 +0200 Subject: [PATCH] Get Indexers status test (#295) * canRunIndexerAndGetIndexerStatus --- .../com/azure/search/IndexerAsyncTest.java | 122 ++++++++++++++++++ .../java/com/azure/search/IndexerTest.java | 118 +++++++++++++++++ .../canRunIndexerAndGetIndexerStatus.json | 88 +++++++++++++ 3 files changed, 328 insertions(+) create mode 100644 sdk/search/azure-search/src/test/java/com/azure/search/IndexerAsyncTest.java create mode 100644 sdk/search/azure-search/src/test/java/com/azure/search/IndexerTest.java create mode 100644 sdk/search/azure-search/src/test/resources/session-records/canRunIndexerAndGetIndexerStatus.json diff --git a/sdk/search/azure-search/src/test/java/com/azure/search/IndexerAsyncTest.java b/sdk/search/azure-search/src/test/java/com/azure/search/IndexerAsyncTest.java new file mode 100644 index 0000000000000..da13925d998a3 --- /dev/null +++ b/sdk/search/azure-search/src/test/java/com/azure/search/IndexerAsyncTest.java @@ -0,0 +1,122 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.search; + +import com.azure.core.test.InterceptorManager; +import com.azure.core.test.TestMode; +import com.azure.core.test.utils.TestResourceNamer; +import com.azure.search.models.Indexer; +import com.azure.search.models.IndexerExecutionResult; +import com.azure.search.models.IndexerExecutionStatus; +import com.azure.search.models.IndexerStatus; +import com.azure.search.models.IndexingSchedule; +import org.apache.http.HttpStatus; +import org.junit.Assert; +import org.junit.Test; +import reactor.test.StepVerifier; + +import java.io.IOException; +import java.time.Duration; +import java.time.OffsetDateTime; + +/** + * This test will run only in PLAYBACK mode (even if test mode is set to recording). + * injects mock status query , which results in service + * returning a well-known mock response + */ +public class IndexerAsyncTest extends SearchServiceTestBase { + + private SearchServiceAsyncClient client; + + @Override + public void setupTest() { + final String testName = getTestName(); + + try { + interceptorManager = new InterceptorManager(testName, TestMode.PLAYBACK); + } catch (IOException e) { + Assert.fail(); + } + testResourceNamer = new TestResourceNamer(testName, TestMode.PLAYBACK, interceptorManager.getRecordedData()); + + beforeTest(); + } + + @Override + protected void beforeTest() { + client = getSearchServiceClientBuilder().buildAsyncClient(); + } + + @Override + protected void afterTest() { + } + + @Override + protected SearchServiceClientBuilder getSearchServiceClientBuilder() { + endpoint = String.format("https://%s.%s", "fake-service", "search.windows.net"); + return new SearchServiceClientBuilder() + .endpoint(endpoint) + .httpClient(interceptorManager.getPlaybackClient()); + } + + @Test + public void canRunIndexerAndGetIndexerStatus() { + + // create an indexer + Indexer indexer = + createTestIndexer("indexer") + .setDataSourceName(SQL_DATASOURCE_NAME) + .setIsDisabled(false); + client.createOrUpdateIndexer(indexer); + + StepVerifier.create(client.getIndexerStatus(indexer.getName())) + .assertNext(indexerExecutionInfo -> { + Assert.assertEquals(IndexerStatus.RUNNING, indexerExecutionInfo.getStatus()); + }) + .verifyComplete(); + + StepVerifier.create(client.runIndexerWithResponse(indexer.getName(), null, null)) + .assertNext(response -> { + Assert.assertEquals(HttpStatus.SC_ACCEPTED, response.getStatusCode()); + }) + .verifyComplete(); + + StepVerifier.create(client.getIndexerStatus(indexer.getName())) + .assertNext(indexerExecutionInfo -> { + Assert.assertEquals(IndexerStatus.RUNNING, indexerExecutionInfo.getStatus()); + Assert.assertEquals(IndexerExecutionStatus.IN_PROGRESS, indexerExecutionInfo.getLastResult().getStatus()); + Assert.assertEquals(2, indexerExecutionInfo.getExecutionHistory().size()); + + IndexerExecutionResult newestResult = indexerExecutionInfo.getExecutionHistory().get(0); + IndexerExecutionResult oldestResult = indexerExecutionInfo.getExecutionHistory().get(1); + + Assert.assertEquals(IndexerExecutionStatus.SUCCESS, newestResult.getStatus()); + Assert.assertEquals(11, newestResult.getItemCount()); + Assert.assertEquals(0, newestResult.getFailedItemCount()); + assertStartAndEndTimeValid(newestResult); + + Assert.assertEquals(IndexerExecutionStatus.TRANSIENT_FAILURE, oldestResult.getStatus()); + Assert.assertEquals("Document key cannot be missing or empty", oldestResult.getErrorMessage()); + assertStartAndEndTimeValid(newestResult); + }) + .verifyComplete(); + } + + private void assertStartAndEndTimeValid(IndexerExecutionResult result) { + Assert.assertTrue(result.getStartTime() != null); + Assert.assertNotEquals(OffsetDateTime.now(), result.getStartTime()); + Assert.assertTrue(result.getEndTime() != null); + Assert.assertNotEquals(OffsetDateTime.now(), result.getEndTime()); + } + + private Indexer createTestIndexer(String indexerName) { + return new Indexer() + .setName(indexerName) + .setTargetIndexName("indexforindexers") + .setSchedule(new IndexingSchedule().setInterval(Duration.ofDays(1))); + } + + +} + diff --git a/sdk/search/azure-search/src/test/java/com/azure/search/IndexerTest.java b/sdk/search/azure-search/src/test/java/com/azure/search/IndexerTest.java new file mode 100644 index 0000000000000..4d79fe05c8bd6 --- /dev/null +++ b/sdk/search/azure-search/src/test/java/com/azure/search/IndexerTest.java @@ -0,0 +1,118 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.search; + +import com.azure.core.http.rest.Response; +import com.azure.core.test.InterceptorManager; +import com.azure.core.test.TestMode; +import com.azure.core.test.utils.TestResourceNamer; +import com.azure.search.models.Indexer; +import com.azure.search.models.IndexerExecutionInfo; +import com.azure.search.models.IndexerExecutionResult; +import com.azure.search.models.IndexerExecutionStatus; +import com.azure.search.models.IndexerStatus; +import com.azure.search.models.IndexingSchedule; +import org.apache.http.HttpStatus; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.time.Duration; +import java.time.OffsetDateTime; + +/** + * This test will run only in PLAYBACK mode (even if test mode is set to recording). + * injects mock status query , which results in service + * returning a well-known mock response + */ +public class IndexerTest extends SearchServiceTestBase { + + private SearchServiceClient client; + + @Override + public void setupTest() { + final String testName = getTestName(); + + try { + interceptorManager = new InterceptorManager(testName, TestMode.PLAYBACK); + } catch (IOException e) { + Assert.fail(); + } + testResourceNamer = new TestResourceNamer(testName, TestMode.PLAYBACK, interceptorManager.getRecordedData()); + + beforeTest(); + } + + @Override + protected void beforeTest() { + client = getSearchServiceClientBuilder().buildClient(); + } + + @Override + protected void afterTest() { + } + + @Override + protected SearchServiceClientBuilder getSearchServiceClientBuilder() { + endpoint = String.format("https://%s.%s", "fake-service", "search.windows.net"); + return new SearchServiceClientBuilder() + .endpoint(endpoint) + .httpClient(interceptorManager.getPlaybackClient()); + } + + @Test + /** + * This test uses a mock response, will run only in PLAYBACK mode + * using canRunIndexerAndGetIndexerStatus.json + */ + public void canRunIndexerAndGetIndexerStatus() { + + // create an indexer + Indexer indexer = + createTestIndexer("indexer") + .setDataSourceName(SQL_DATASOURCE_NAME) + .setIsDisabled(false); + client.createOrUpdateIndexer(indexer); + + IndexerExecutionInfo indexerExecutionInfo = client.getIndexerStatus(indexer.getName()); + Assert.assertEquals(IndexerStatus.RUNNING, indexerExecutionInfo.getStatus()); + + Response response = client.runIndexerWithResponse(indexer.getName(), null, null); + Assert.assertEquals(HttpStatus.SC_ACCEPTED, response.getStatusCode()); + + indexerExecutionInfo = client.getIndexerStatus(indexer.getName()); + Assert.assertEquals(IndexerStatus.RUNNING, indexerExecutionInfo.getStatus()); + Assert.assertEquals(IndexerExecutionStatus.IN_PROGRESS, indexerExecutionInfo.getLastResult().getStatus()); + Assert.assertEquals(2, indexerExecutionInfo.getExecutionHistory().size()); + + IndexerExecutionResult newestResult = indexerExecutionInfo.getExecutionHistory().get(0); + IndexerExecutionResult oldestResult = indexerExecutionInfo.getExecutionHistory().get(1); + + Assert.assertEquals(IndexerExecutionStatus.SUCCESS, newestResult.getStatus()); + Assert.assertEquals(11, newestResult.getItemCount()); + Assert.assertEquals(0, newestResult.getFailedItemCount()); + Assert.assertEquals(0, newestResult.getFailedItemCount()); + assertStartAndEndTimeValid(newestResult); + + Assert.assertEquals(IndexerExecutionStatus.TRANSIENT_FAILURE, oldestResult.getStatus()); + Assert.assertEquals("Document key cannot be missing or empty", oldestResult.getErrorMessage()); + assertStartAndEndTimeValid(newestResult); + } + + private void assertStartAndEndTimeValid(IndexerExecutionResult result) { + Assert.assertTrue(result.getStartTime() != null); + Assert.assertNotEquals(OffsetDateTime.now(), result.getStartTime()); + Assert.assertTrue(result.getEndTime() != null); + Assert.assertNotEquals(OffsetDateTime.now(), result.getEndTime()); + } + + private Indexer createTestIndexer(String indexerName) { + return new Indexer() + .setName(indexerName) + .setTargetIndexName("indexforindexers") + .setSchedule(new IndexingSchedule().setInterval(Duration.ofDays(1))); + } + + +} diff --git a/sdk/search/azure-search/src/test/resources/session-records/canRunIndexerAndGetIndexerStatus.json b/sdk/search/azure-search/src/test/resources/session-records/canRunIndexerAndGetIndexerStatus.json new file mode 100644 index 0000000000000..51ef473e2ba45 --- /dev/null +++ b/sdk/search/azure-search/src/test/resources/session-records/canRunIndexerAndGetIndexerStatus.json @@ -0,0 +1,88 @@ +{ + "networkCallRecords" : [ { + "Method" : "PUT", + "Uri" : "https://fake-service.search.windows.net/indexers('indexer')?api-version=2019-05-06", + "Headers" : { + "Content-Type" : "application/json; charset=utf-8" + }, + "Response" : { + "Pragma" : "no-cache", + "retry-after" : "0", + "request-id" : "640b002d-f321-4819-9922-c9c3947636b6", + "StatusCode" : "201", + "Date" : "Mon, 18 Nov 2019 12:11:56 GMT", + "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", + "Cache-Control" : "no-cache", + "ETag" : "W/\"0x8D76C20813CA3FE\"", + "elapsed-time" : "1516", + "OData-Version" : "4.0", + "Expires" : "-1", + "Content-Length" : "406", + "Body" : "{\"@odata.context\":\"https://fake-service.search.windows.net/$metadata#indexers/$entity\",\"@odata.etag\":\"\\\"0x8D76C20813CA3FE\\\"\",\"name\":\"indexer\",\"description\":null,\"dataSourceName\":\"azs-java-test-sql\",\"skillsetName\":null,\"targetIndexName\":\"indexforindexers\",\"disabled\":false,\"schedule\":{\"interval\":\"P1D\",\"startTime\":\"0001-01-01T00:00:00Z\"},\"parameters\":null,\"fieldMappings\":[],\"outputFieldMappings\":[]}", + "Preference-Applied" : "odata.include-annotations=\"*\"", + "Content-Type" : "application/json; odata.metadata=minimal", + "Location" : "https://fake-service.search.windows.net/indexers('indexer')?api-version=2019-05-06" + }, + "Exception" : null + }, { + "Method" : "GET", + "Uri" : "https://fake-service.search.windows.net/indexers('indexer')/search.status?api-version=2019-05-06", + "Headers" : { }, + "Response" : { + "Pragma" : "no-cache", + "retry-after" : "0", + "request-id" : "112449f0-adcb-49cf-91c1-a34dcb670672", + "StatusCode" : "200", + "Date" : "Mon, 18 Nov 2019 12:11:57 GMT", + "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", + "Cache-Control" : "no-cache", + "elapsed-time" : "31", + "OData-Version" : "4.0", + "Expires" : "-1", + "Content-Length" : "312", + "Body" : "{\"@odata.context\":\"https://fake-service.search.windows.net/$metadata#Microsoft.Azure.Search.V2019_05_06.IndexerExecutionInfo\",\"name\":\"indexer\",\"status\":\"running\",\"lastResult\":null,\"executionHistory\":[],\"limits\":{\"maxRunTime\":\"PT0S\",\"maxDocumentExtractionSize\":0,\"maxDocumentContentCharactersToExtract\":0}}", + "Preference-Applied" : "odata.include-annotations=\"*\"", + "Content-Type" : "application/json; odata.metadata=minimal" + }, + "Exception" : null + }, { + "Method" : "POST", + "Uri" : "https://fake-service.search.windows.net/indexers('indexer')/search.run?api-version=2019-05-06", + "Headers" : { }, + "Response" : { + "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", + "Cache-Control" : "no-cache", + "elapsed-time" : "102", + "Expires" : "-1", + "Pragma" : "no-cache", + "retry-after" : "0", + "Content-Length" : "0", + "request-id" : "13afb89c-d49c-48d0-8e71-c1954899c380", + "StatusCode" : "202", + "Date" : "Mon, 18 Nov 2019 12:11:57 GMT" + }, + "Exception" : null + }, { + "Method" : "GET", + "Uri" : "https://fake-service.search.windows.net/indexers('indexer')/search.status?api-version=2019-05-06", + "Headers" : { }, + "Response" : { + "Pragma" : "no-cache", + "retry-after" : "0", + "request-id" : "112449f0-adcb-49cf-91c1-a34dcb670672", + "StatusCode" : "200", + "Date" : "Mon, 18 Nov 2019 12:11:57 GMT", + "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", + "Cache-Control" : "no-cache", + "elapsed-time" : "31", + "OData-Version" : "4.0", + "Expires" : "-1", + "Content-Length" : "312", + "Body" : "{\"@odata.context\":\"https://fake-service.search.windows.net/$metadata#Microsoft.Azure.Search.V2019_05_06.IndexerExecutionInfo\",\"name\":\"indexer\",\"status\":\"running\",\"lastResult\":{\"status\" : \"inProgress\"},\"executionHistory\":[{\"status\" : \"success\",\"errorMessage\" : null,\"startTime\" : \"2014-11-26T03:37:18.853Z\",\"endTime\" : \"2014-11-26T03:37:19.012Z\",\"errors\" : [],\"warnings\" : [],\"itemsProcessed\" : 11,\"itemsFailed\" : 0,\"initialTrackingState\" : null,\"finalTrackingState\" : null}, {\"status\" : \"transientFailure\",\"errorMessage\" : \"Document key cannot be missing or empty\",\"startTime\" : \"2014-11-26T03:28:10.125Z\",\"endTime\" : \"2014-11-26T03:28:12.007Z\",\"errors\" : [{\"errorMessage\" : \"Document key cannot be missing or empty.\",\"statusCode\" : 400}],\"itemsProcessed\" : 1,\"itemsFailed\" : 1,\"initialTrackingState\" : null,\"finalTrackingState\" : null}],\"limits\":{\"maxRunTime\":\"PT0S\",\"maxDocumentExtractionSize\":0,\"maxDocumentContentCharactersToExtract\":0}}", + "Preference-Applied" : "odata.include-annotations=\"*\"", + "Content-Type" : "application/json; odata.metadata=minimal" + }, + "Exception" : null + } ], + "variables" : [ ] +}