Skip to content

Commit

Permalink
Get Indexers status test (Azure#295)
Browse files Browse the repository at this point in the history
* canRunIndexerAndGetIndexerStatus
  • Loading branch information
navalev authored Nov 25, 2019
1 parent 6cb8d03 commit d6c06cb
Show file tree
Hide file tree
Showing 3 changed files with 328 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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)));
}


}

Original file line number Diff line number Diff line change
@@ -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<Void> 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)));
}


}
Original file line number Diff line number Diff line change
@@ -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" : [ ]
}

0 comments on commit d6c06cb

Please sign in to comment.