Package org.elasticsearch.test.hamcrest
Class ElasticsearchAssertions
java.lang.Object
org.elasticsearch.test.hamcrest.ElasticsearchAssertions
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
assertAcked
(ActionFuture<? extends IsAcknowledgedSupplier>... futures) static void
assertAcked
(CreateIndexRequestBuilder... requests) Assert that an index creation was fully acknowledged, meaning that both the index creation cluster state update was successful and that the requisite number of shard copies were started before returning.static void
assertAcked
(CreateIndexResponse response) static void
assertAcked
(RequestBuilder<?, ? extends IsAcknowledgedSupplier> builder) static void
assertAcked
(RequestBuilder<?, ? extends IsAcknowledgedSupplier>... builders) static void
assertAcked
(RequestBuilder<?, ? extends IsAcknowledgedSupplier> builder, org.elasticsearch.core.TimeValue timeValue) static void
assertAcked
(IsAcknowledgedSupplier response) static void
assertAllSuccessful
(SearchResponse response) static void
assertAllSuccessful
(BaseBroadcastResponse response) static void
assertBlocked
(Integer expectedBlockId, Exception exception) Checks that the given exception is aClusterBlockException
; if the given block ID is notnull
then the given exception must match that ID.static void
assertBlocked
(RequestBuilder<?, ?> builder) Executes the request and fails if the request has not been blocked.static void
assertBlocked
(RequestBuilder<?, ?> builder, Integer expectedBlockId) Executes the request and fails if the request has not been blocked by a specificClusterBlock
.static void
assertBlocked
(RequestBuilder<?, ?> builder, ClusterBlock expectedBlock) Executes the request and fails if the request has not been blocked by a specificClusterBlock
.static void
assertBlocked
(BaseBroadcastResponse replicatedBroadcastResponse) Checks that all shard requests of a replicated broadcast request failed due to a cluster blockstatic <T extends org.apache.lucene.search.Query>
TassertBooleanSubQuery
(org.apache.lucene.search.Query query, Class<T> subqueryType, int i) static void
assertCheckedResponse
(ActionFuture<SearchResponse> responseFuture, org.elasticsearch.core.CheckedConsumer<SearchResponse, IOException> consumer) static void
assertCheckedResponse
(RequestBuilder<?, SearchResponse> searchRequestBuilder, org.elasticsearch.core.CheckedConsumer<SearchResponse, IOException> consumer) static void
assertExists
(GetResponse response) static void
assertFailures
(SearchRequestBuilder searchRequestBuilder, Set<RestStatus> restStatuses, org.hamcrest.Matcher<String> reasonMatcher) static void
assertFailures
(SearchRequestBuilder searchRequestBuilder, RestStatus restStatus) static void
assertFailures
(SearchRequestBuilder searchRequestBuilder, RestStatus restStatus, org.hamcrest.Matcher<String> reasonMatcher) static void
assertFailures
(SearchResponse searchResponse) static void
assertFileExists
(Path file) Check if a file existsstatic void
assertFileNotExists
(Path file) Check if a file does not existstatic void
assertFirstHit
(SearchResponse searchResponse, org.hamcrest.Matcher<SearchHit> matcher) static void
assertFourthHit
(SearchResponse searchResponse, org.hamcrest.Matcher<SearchHit> matcher) static <E extends Throwable>
voidassertFutureThrows
(ActionFuture<?> future, Class<E> exceptionClass) Run future.actionGet() and check that it throws an exception of the right typestatic <E extends Throwable>
voidassertFutureThrows
(ActionFuture<?> future, Class<E> exceptionClass, String extraInfo) Run future.actionGet() and check that it throws an exception of the right typestatic <E extends Throwable>
voidassertFutureThrows
(ActionFuture<?> future, Class<E> exceptionClass, RestStatus status) Run future.actionGet() and check that it throws an exception of the right type, with a givenRestStatus
static <E extends Throwable>
voidassertFutureThrows
(ActionFuture<?> future, Class<E> exceptionClass, RestStatus status, String extraInfo) Run future.actionGet() and check that it throws an exception of the right type, optionally checking the exception's rest statusstatic void
assertFutureThrows
(ActionFuture<?> future, RestStatus status) static void
assertFutureThrows
(ActionFuture<?> future, RestStatus status, String extraInfo) static void
assertHighlight
(ActionFuture<SearchResponse> responseFuture, int hit, String field, int fragment, int totalFragments, org.hamcrest.Matcher<String> matcher) static void
assertHighlight
(SearchRequestBuilder searchRequestBuilder, int hit, String field, int fragment, int totalFragments, org.hamcrest.Matcher<String> matcher) static void
assertHighlight
(SearchResponse resp, int hit, String field, int fragment, int totalFragments, org.hamcrest.Matcher<String> matcher) static void
assertHighlight
(SearchResponse resp, int hit, String field, int fragment, org.hamcrest.Matcher<String> matcher) static void
assertHighlight
(SearchHit hit, String field, int fragment, int totalFragments, org.hamcrest.Matcher<String> matcher) static void
assertHighlight
(SearchHit hit, String field, int fragment, org.hamcrest.Matcher<String> matcher) static void
assertHitCount
(long expectedHitCount, SearchRequestBuilder... searchRequestBuilders) static void
assertHitCount
(ActionFuture<SearchResponse> responseFuture, long expectedHitCount) static void
assertHitCount
(SearchRequestBuilder searchRequestBuilder, long expectedHitCount) static void
assertHitCount
(SearchResponse countResponse, long expectedHitCount) static void
assertHitCountAndNoFailures
(SearchRequestBuilder searchRequestBuilder, long expectedHitCount) static void
assertIndexTemplateExists
(GetIndexTemplatesResponse templatesResponse, String name) Assert that an index template existsstatic void
assertIndexTemplateMissing
(GetIndexTemplatesResponse templatesResponse, String name) Assert that an index template is missingstatic void
assertNoFailures
(BulkResponse response) static void
assertNoFailures
(RequestBuilder<? extends ActionRequest, SearchResponse> searchRequestBuilder) static void
assertNoFailures
(SearchResponse searchResponse) static void
assertNoFailures
(BaseBroadcastResponse response) static void
assertNoFailuresAndResponse
(ActionFuture<SearchResponse> responseFuture, Consumer<SearchResponse> consumer) static void
assertNoFailuresAndResponse
(RequestBuilder<? extends ActionRequest, SearchResponse> searchRequestBuilder, Consumer<SearchResponse> consumer) static void
assertNoSearchHits
(SearchRequestBuilder searchRequestBuilder) static void
assertNoSearchHits
(SearchResponse searchResponse) static void
assertNotHighlighted
(SearchRequestBuilder searchRequestBuilder, int hit, String field) static void
assertNotHighlighted
(SearchResponse resp, int hit, String field) static void
assertNoTimeout
(ClusterHealthRequestBuilder requestBuilder) static void
assertNoTimeout
(ClusterHealthResponse response) static <T extends ActionResponse>
voidassertNoWarningHeaderOnResponse
(Client client, ActionRequestBuilder<?, T> requestBuilder, String toMatch) Check the response of a client request to ensure it does not have a warning header that contains the provided string Currently, this allows a fixed 10 seconds for response to be receivedstatic void
assertOrderedSearchHits
(SearchRequestBuilder searchRequestBuilder, String... ids) static void
assertOrderedSearchHits
(SearchResponse searchResponse, String... ids) static <E extends Throwable>
voidassertRequestBuilderThrows
(RequestBuilder<?, ?> builder, Class<E> exceptionClass, String extraInfo) Run the request from a given builder and check that it throws an exception of the right typestatic <E extends Throwable>
voidassertRequestBuilderThrows
(RequestBuilder<?, ?> builder, Class<E> exceptionClass, RestStatus status) Run the request from a given builder and check that it throws an exception of the right type, with a givenRestStatus
static void
assertRequestBuilderThrows
(RequestBuilder<?, ?> builder, RestStatus status) static void
assertRequestBuilderThrows
(RequestBuilder<?, ?> builder, RestStatus status, String extraInfo) static <R extends ActionResponse>
voidassertResponse
(ActionFuture<R> responseFuture, Consumer<R> consumer) static <Q extends ActionRequest,
R extends ActionResponse>
voidassertResponse
(RequestBuilder<Q, R> searchRequestBuilder, Consumer<R> consumer) static <Q extends ActionRequest,
R extends ActionResponse>
voidassertResponses
(Consumer<R> consumer, RequestBuilder<Q, R>... searchRequestBuilder) Same asassertResponse(RequestBuilder, Consumer)
but runs the same assertion on multiple requests that are started concurrently.static void
assertScrollResponsesAndHitCount
(Client client, org.elasticsearch.core.TimeValue keepAlive, SearchRequestBuilder searchRequestBuilder, int expectedTotalHitCount, BiConsumer<Integer, SearchResponse> responseConsumer) A helper to enable the testing of scroll requests with ref-counting.static void
assertSearchHit
(SearchResponse searchResponse, int number, org.hamcrest.Matcher<SearchHit> matcher) static void
assertSearchHits
(SearchRequestBuilder searchRequestBuilder, String... ids) static void
assertSearchHits
(SearchResponse searchResponse, String... ids) static void
assertSearchHitsWithoutFailures
(SearchRequestBuilder requestBuilder, String... ids) static void
assertSecondHit
(SearchResponse searchResponse, org.hamcrest.Matcher<SearchHit> matcher) static void
assertSortValues
(SearchRequestBuilder searchRequestBuilder, Object[]... sortValues) static void
assertSuggestion
(Suggest searchSuggest, int entry, int ord, String key, String text) static void
assertSuggestion
(Suggest searchSuggest, int entry, String key, int size, String... text) Assert suggestion returns size suggestions and the first are the provided text.static void
assertSuggestion
(Suggest searchSuggest, int entry, String key, String... text) Assert suggestion returns exactly the provided text.static void
assertSuggestionPhraseCollateMatchExists
(Suggest searchSuggest, String key, int numberOfPhraseExists) static void
assertSuggestionSize
(Suggest searchSuggest, int entry, int size, String key) static void
assertThirdHit
(SearchResponse searchResponse, org.hamcrest.Matcher<SearchHit> matcher) static void
assertToXContentEquivalent
(BytesReference expected, BytesReference actual, org.elasticsearch.xcontent.XContentType xContentType) Asserts that the providedBytesReference
s created throughToXContent.toXContent(XContentBuilder, ToXContent.Params)
hold the same content.static <T extends ActionResponse>
voidassertWarningHeaderOnResponse
(Client client, ActionRequestBuilder<?, T> requestBuilder, String toMatch) Check the response of a client request to ensure it has a warning header that contains the provided string Currently, this allows a fixed 10 seconds for response to be receivedstatic void
awaitLatch
(CountDownLatch latch, long timeout, TimeUnit unit) Wait for a latch to countdown and provide a useful error message if it does not Often latches are called asassertTrue(latch.await(1, TimeUnit.SECONDS));
In case of a failure this will just throw an assertion error without any further messagestatic String
formatShardStatus
(SearchResponse response) static String
formatShardStatus
(BaseBroadcastResponse response) static org.hamcrest.Matcher
<SearchHit> static org.hamcrest.Matcher
<SearchHit> static org.hamcrest.Matcher
<SearchHit> hasRank
(int rank) static org.hamcrest.Matcher
<SearchHit> hasScore
(float score)
-
Constructor Details
-
ElasticsearchAssertions
public ElasticsearchAssertions()
-
-
Method Details
-
assertAcked
-
assertAcked
@SafeVarargs public static void assertAcked(RequestBuilder<?, ? extends IsAcknowledgedSupplier>... builders) -
assertAcked
@SafeVarargs public static void assertAcked(ActionFuture<? extends IsAcknowledgedSupplier>... futures) -
assertAcked
public static void assertAcked(RequestBuilder<?, ? extends IsAcknowledgedSupplier> builder, org.elasticsearch.core.TimeValue timeValue) -
assertNoTimeout
-
assertNoTimeout
-
assertAcked
-
assertAcked
Assert that an index creation was fully acknowledged, meaning that both the index creation cluster state update was successful and that the requisite number of shard copies were started before returning. -
assertAcked
-
assertBlocked
Executes the request and fails if the request has not been blocked.- Parameters:
builder
- the request builder
-
assertBlocked
Checks that all shard requests of a replicated broadcast request failed due to a cluster block- Parameters:
replicatedBroadcastResponse
- the response that should only contain failed shard responses
-
assertBlocked
Executes the request and fails if the request has not been blocked by a specificClusterBlock
.- Parameters:
builder
- the request builderexpectedBlockId
- the expected block id
-
assertBlocked
Checks that the given exception is aClusterBlockException
; if the given block ID is notnull
then the given exception must match that ID. -
assertBlocked
Executes the request and fails if the request has not been blocked by a specificClusterBlock
.- Parameters:
builder
- the request builderexpectedBlock
- the expected block
-
formatShardStatus
-
formatShardStatus
-
assertNoSearchHits
-
assertNoSearchHits
-
assertSearchHits
-
assertSearchHits
-
assertSearchHitsWithoutFailures
public static void assertSearchHitsWithoutFailures(SearchRequestBuilder requestBuilder, String... ids) -
assertSortValues
public static void assertSortValues(SearchRequestBuilder searchRequestBuilder, Object[]... sortValues) -
assertOrderedSearchHits
public static void assertOrderedSearchHits(SearchRequestBuilder searchRequestBuilder, String... ids) -
assertOrderedSearchHits
-
assertHitCount
-
assertHitCount
public static void assertHitCount(long expectedHitCount, SearchRequestBuilder... searchRequestBuilders) -
assertHitCount
public static void assertHitCount(ActionFuture<SearchResponse> responseFuture, long expectedHitCount) -
assertHitCount
-
assertHitCountAndNoFailures
public static void assertHitCountAndNoFailures(SearchRequestBuilder searchRequestBuilder, long expectedHitCount) -
assertExists
-
assertFirstHit
public static void assertFirstHit(SearchResponse searchResponse, org.hamcrest.Matcher<SearchHit> matcher) -
assertSecondHit
public static void assertSecondHit(SearchResponse searchResponse, org.hamcrest.Matcher<SearchHit> matcher) -
assertThirdHit
public static void assertThirdHit(SearchResponse searchResponse, org.hamcrest.Matcher<SearchHit> matcher) -
assertFourthHit
public static void assertFourthHit(SearchResponse searchResponse, org.hamcrest.Matcher<SearchHit> matcher) -
assertSearchHit
public static void assertSearchHit(SearchResponse searchResponse, int number, org.hamcrest.Matcher<SearchHit> matcher) -
assertNoFailures
public static void assertNoFailures(RequestBuilder<? extends ActionRequest, SearchResponse> searchRequestBuilder) -
assertNoFailuresAndResponse
public static void assertNoFailuresAndResponse(RequestBuilder<? extends ActionRequest, SearchResponse> searchRequestBuilder, Consumer<SearchResponse> consumer) -
assertNoFailuresAndResponse
public static void assertNoFailuresAndResponse(ActionFuture<SearchResponse> responseFuture, Consumer<SearchResponse> consumer) throws ExecutionException, InterruptedException -
assertResponses
@SafeVarargs public static <Q extends ActionRequest,R extends ActionResponse> void assertResponses(Consumer<R> consumer, RequestBuilder<Q, R>... searchRequestBuilder) Same asassertResponse(RequestBuilder, Consumer)
but runs the same assertion on multiple requests that are started concurrently. -
assertResponse
public static <Q extends ActionRequest,R extends ActionResponse> void assertResponse(RequestBuilder<Q, R> searchRequestBuilder, Consumer<R> consumer) -
assertScrollResponsesAndHitCount
public static void assertScrollResponsesAndHitCount(Client client, org.elasticsearch.core.TimeValue keepAlive, SearchRequestBuilder searchRequestBuilder, int expectedTotalHitCount, BiConsumer<Integer, SearchResponse> responseConsumer) A helper to enable the testing of scroll requests with ref-counting.- Parameters:
keepAlive
- The TTL for the scroll context.searchRequestBuilder
- The initial search request.expectedTotalHitCount
- The number of hits that are expected to be retrieved.responseConsumer
- (respNum, response) -> {your assertions here}. respNum starts at 1, which contains the resp from the initial request.
-
assertResponse
public static <R extends ActionResponse> void assertResponse(ActionFuture<R> responseFuture, Consumer<R> consumer) throws ExecutionException, InterruptedException -
assertCheckedResponse
public static void assertCheckedResponse(RequestBuilder<?, SearchResponse> searchRequestBuilder, org.elasticsearch.core.CheckedConsumer<SearchResponse, throws IOExceptionIOException> consumer) - Throws:
IOException
-
assertCheckedResponse
public static void assertCheckedResponse(ActionFuture<SearchResponse> responseFuture, org.elasticsearch.core.CheckedConsumer<SearchResponse, IOException> consumer) throws IOException, ExecutionException, InterruptedException -
assertNoFailures
-
assertFailures
-
assertNoFailures
-
assertFailures
public static void assertFailures(SearchRequestBuilder searchRequestBuilder, RestStatus restStatus, org.hamcrest.Matcher<String> reasonMatcher) -
assertFailures
public static void assertFailures(SearchRequestBuilder searchRequestBuilder, Set<RestStatus> restStatuses, org.hamcrest.Matcher<String> reasonMatcher) -
assertFailures
-
assertNoFailures
-
assertAllSuccessful
-
assertAllSuccessful
-
assertHighlight
public static void assertHighlight(SearchResponse resp, int hit, String field, int fragment, org.hamcrest.Matcher<String> matcher) -
assertHighlight
public static void assertHighlight(SearchRequestBuilder searchRequestBuilder, int hit, String field, int fragment, int totalFragments, org.hamcrest.Matcher<String> matcher) -
assertHighlight
public static void assertHighlight(ActionFuture<SearchResponse> responseFuture, int hit, String field, int fragment, int totalFragments, org.hamcrest.Matcher<String> matcher) throws ExecutionException, InterruptedException -
assertHighlight
public static void assertHighlight(SearchResponse resp, int hit, String field, int fragment, int totalFragments, org.hamcrest.Matcher<String> matcher) -
assertHighlight
-
assertHighlight
-
assertNotHighlighted
public static void assertNotHighlighted(SearchRequestBuilder searchRequestBuilder, int hit, String field) -
assertNotHighlighted
-
assertSuggestionSize
-
assertSuggestionPhraseCollateMatchExists
-
assertSuggestion
-
assertSuggestion
Assert suggestion returns exactly the provided text. -
assertSuggestion
public static void assertSuggestion(Suggest searchSuggest, int entry, String key, int size, String... text) Assert suggestion returns size suggestions and the first are the provided text. -
assertIndexTemplateMissing
public static void assertIndexTemplateMissing(GetIndexTemplatesResponse templatesResponse, String name) Assert that an index template is missing -
assertIndexTemplateExists
public static void assertIndexTemplateExists(GetIndexTemplatesResponse templatesResponse, String name) Assert that an index template exists -
hasId
-
hasIndex
-
hasScore
-
hasRank
-
assertBooleanSubQuery
public static <T extends org.apache.lucene.search.Query> T assertBooleanSubQuery(org.apache.lucene.search.Query query, Class<T> subqueryType, int i) -
assertRequestBuilderThrows
public static <E extends Throwable> void assertRequestBuilderThrows(RequestBuilder<?, ?> builder, Class<E> exceptionClass, RestStatus status) Run the request from a given builder and check that it throws an exception of the right type, with a givenRestStatus
-
assertRequestBuilderThrows
public static <E extends Throwable> void assertRequestBuilderThrows(RequestBuilder<?, ?> builder, Class<E> exceptionClass, String extraInfo) Run the request from a given builder and check that it throws an exception of the right type- Parameters:
extraInfo
- extra information to add to the failure message
-
assertFutureThrows
public static <E extends Throwable> void assertFutureThrows(ActionFuture<?> future, Class<E> exceptionClass) Run future.actionGet() and check that it throws an exception of the right type -
assertFutureThrows
public static <E extends Throwable> void assertFutureThrows(ActionFuture<?> future, Class<E> exceptionClass, RestStatus status) Run future.actionGet() and check that it throws an exception of the right type, with a givenRestStatus
-
assertFutureThrows
public static <E extends Throwable> void assertFutureThrows(ActionFuture<?> future, Class<E> exceptionClass, String extraInfo) Run future.actionGet() and check that it throws an exception of the right type- Parameters:
extraInfo
- extra information to add to the failure message
-
assertFutureThrows
public static <E extends Throwable> void assertFutureThrows(ActionFuture<?> future, Class<E> exceptionClass, @Nullable RestStatus status, @Nullable String extraInfo) Run future.actionGet() and check that it throws an exception of the right type, optionally checking the exception's rest status- Parameters:
exceptionClass
- expected exception classstatus
-RestStatus
to check for. Can be null to disable the checkextraInfo
- extra information to add to the failure message. Can be null.
-
assertRequestBuilderThrows
-
assertRequestBuilderThrows
public static void assertRequestBuilderThrows(RequestBuilder<?, ?> builder, RestStatus status, String extraInfo) -
assertFutureThrows
-
assertFutureThrows
-
assertFileExists
Check if a file exists -
assertFileNotExists
Check if a file does not exist -
assertToXContentEquivalent
public static void assertToXContentEquivalent(BytesReference expected, BytesReference actual, org.elasticsearch.xcontent.XContentType xContentType) throws IOException Asserts that the providedBytesReference
s created throughToXContent.toXContent(XContentBuilder, ToXContent.Params)
hold the same content. The comparison is done by parsing both into a map and comparing those two, so that keys ordering doesn't matter. Also binary values (byte[]) are properly compared through arrays comparisons.- Throws:
IOException
-
awaitLatch
public static void awaitLatch(CountDownLatch latch, long timeout, TimeUnit unit) throws InterruptedException Wait for a latch to countdown and provide a useful error message if it does not Often latches are called asassertTrue(latch.await(1, TimeUnit.SECONDS));
In case of a failure this will just throw an assertion error without any further message- Parameters:
latch
- The latch to wait fortimeout
- The value of the timeoutunit
- The unit of the timeout- Throws:
InterruptedException
- An exception if the waiting is interrupted
-
assertWarningHeaderOnResponse
public static <T extends ActionResponse> void assertWarningHeaderOnResponse(Client client, ActionRequestBuilder<?, T> requestBuilder, String toMatch) throws InterruptedExceptionCheck the response of a client request to ensure it has a warning header that contains the provided string Currently, this allows a fixed 10 seconds for response to be received- Type Parameters:
T
- Type of the response- Parameters:
client
- Client making the request - Required to access the response headersrequestBuilder
- Request to be madetoMatch
- String to match in the warning headers- Throws:
InterruptedException
- If the request times out
-
assertNoWarningHeaderOnResponse
public static <T extends ActionResponse> void assertNoWarningHeaderOnResponse(Client client, ActionRequestBuilder<?, T> requestBuilder, String toMatch) throws InterruptedExceptionCheck the response of a client request to ensure it does not have a warning header that contains the provided string Currently, this allows a fixed 10 seconds for response to be received- Type Parameters:
T
- Type of the response- Parameters:
client
- Client making the request - Required to access the response headersrequestBuilder
- Request to be madetoMatch
- String to not match in the warning headers- Throws:
InterruptedException
- If the request times out
-