From 8abfda0b598b4be67c50b20706ff204b5221b352 Mon Sep 17 00:00:00 2001 From: Jay Modi Date: Fri, 21 Feb 2020 13:30:11 -0700 Subject: [PATCH] Rename assertThrows to prevent naming clash (#52651) This commit renames ElasticsearchAssertions#assertThrows to assertRequestBuilderThrows and assertFutureThrows to avoid a naming clash with JUnit 4.13+ and static imports of these methods. Additionally, these methods have been updated to make use of expectThrows internally to avoid duplicating the logic there. Relates #51787 Backport of #52582 --- .../admin/cluster/node/tasks/TasksIT.java | 4 +- .../admin/indices/create/CreateIndexIT.java | 4 +- .../admin/indices/exists/IndicesExistsIT.java | 6 +- .../action/termvectors/GetTermVectorsIT.java | 4 +- .../elasticsearch/cluster/NoMasterNodeIT.java | 18 ++-- .../indices/settings/UpdateSettingsIT.java | 5 +- .../template/SimpleIndexTemplateIT.java | 6 +- .../persistent/PersistentTasksExecutorIT.java | 20 ++--- .../BlobStoreRepositoryCleanupIT.java | 4 +- .../search/morelikethis/MoreLikeThisIT.java | 14 +-- .../search/scroll/SearchScrollIT.java | 13 +-- .../search/suggest/SuggestSearchIT.java | 8 +- .../DedicatedClusterSnapshotRestoreIT.java | 7 +- .../snapshots/RepositoriesIT.java | 10 +-- .../SharedClusterSnapshotRestoreIT.java | 21 ++--- .../org/elasticsearch/update/UpdateIT.java | 4 +- .../versioning/SimpleVersioningIT.java | 55 +++++++----- .../hamcrest/ElasticsearchAssertions.java | 85 ++++++++----------- .../integration/SecurityClearScrollTests.java | 4 +- 19 files changed, 147 insertions(+), 145 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/TasksIT.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/TasksIT.java index 843ae9060c876..060f45e2f4aff 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/TasksIT.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/TasksIT.java @@ -85,9 +85,9 @@ import static org.elasticsearch.common.unit.TimeValue.timeValueMillis; import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds; import static org.elasticsearch.http.HttpTransportSettings.SETTING_HTTP_MAX_HEADER_SIZE; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFutureThrows; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.empty; @@ -785,7 +785,7 @@ public void testTaskStoringFailureResult() throws Exception { request.setShouldBlock(false); // Start non-blocking test task that should fail - assertThrows( + assertFutureThrows( client().execute(TestTaskPlugin.TestTaskAction.INSTANCE, request), IllegalStateException.class ); diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java b/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java index 3b40bdb377439..94025d26dead2 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java @@ -54,7 +54,7 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBlocked; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThanOrEqualTo; @@ -341,7 +341,7 @@ public void testFailureToCreateIndexCleansUpIndicesService() { .addAlias(new Alias("alias1").writeIndex(true)) .get()); - assertThrows(client().admin().indices().prepareCreate("test-idx-2") + assertRequestBuilderThrows(client().admin().indices().prepareCreate("test-idx-2") .setSettings(settings) .addAlias(new Alias("alias1").writeIndex(true)), IllegalStateException.class); diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/exists/IndicesExistsIT.java b/server/src/test/java/org/elasticsearch/action/admin/indices/exists/IndicesExistsIT.java index 0a0c1b66d8cec..aff43fc3616e3 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/exists/IndicesExistsIT.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/exists/IndicesExistsIT.java @@ -28,7 +28,8 @@ import org.elasticsearch.test.InternalTestCluster; import java.io.IOException; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; + +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows; @ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0, transportClientRatio = 0.0, autoManageMasterNodes = false) @@ -40,7 +41,8 @@ public void testIndexExistsWithBlocksInPlace() throws IOException { .put(GatewayService.RECOVER_AFTER_NODES_SETTING.getKey(), 99).build(); String node = internalCluster().startNode(settings); - assertThrows(client(node).admin().indices().prepareExists("test").setMasterNodeTimeout(TimeValue.timeValueSeconds(0)), + assertRequestBuilderThrows( + client(node).admin().indices().prepareExists("test").setMasterNodeTimeout(TimeValue.timeValueSeconds(0)), MasterNotDiscoveredException.class); internalCluster().stopRandomNode(InternalTestCluster.nameFilter(node)); // shut down node so that test properly cleans up diff --git a/server/src/test/java/org/elasticsearch/action/termvectors/GetTermVectorsIT.java b/server/src/test/java/org/elasticsearch/action/termvectors/GetTermVectorsIT.java index 442e27c0867b9..a6f83c5111d62 100644 --- a/server/src/test/java/org/elasticsearch/action/termvectors/GetTermVectorsIT.java +++ b/server/src/test/java/org/elasticsearch/action/termvectors/GetTermVectorsIT.java @@ -55,7 +55,7 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; @@ -369,7 +369,7 @@ public void testDuelESLucene() throws Exception { for (TestConfig test : testConfigs) { TermVectorsRequestBuilder request = getRequestForConfig(test); if (test.expectedException != null) { - assertThrows(request, test.expectedException); + assertRequestBuilderThrows(request, test.expectedException); continue; } diff --git a/server/src/test/java/org/elasticsearch/cluster/NoMasterNodeIT.java b/server/src/test/java/org/elasticsearch/cluster/NoMasterNodeIT.java index ea06ec040c083..cc40c3c0bc1d9 100644 --- a/server/src/test/java/org/elasticsearch/cluster/NoMasterNodeIT.java +++ b/server/src/test/java/org/elasticsearch/cluster/NoMasterNodeIT.java @@ -53,7 +53,7 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertExists; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; @@ -96,35 +96,35 @@ public void testNoMasterActions() throws Exception { assertTrue(state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID)); }); - assertThrows(clientToMasterlessNode.prepareGet("test", "type1", "1"), + assertRequestBuilderThrows(clientToMasterlessNode.prepareGet("test", "type1", "1"), ClusterBlockException.class, RestStatus.SERVICE_UNAVAILABLE ); - assertThrows(clientToMasterlessNode.prepareGet("no_index", "type1", "1"), + assertRequestBuilderThrows(clientToMasterlessNode.prepareGet("no_index", "type1", "1"), ClusterBlockException.class, RestStatus.SERVICE_UNAVAILABLE ); - assertThrows(clientToMasterlessNode.prepareMultiGet().add("test", "type1", "1"), + assertRequestBuilderThrows(clientToMasterlessNode.prepareMultiGet().add("test", "type1", "1"), ClusterBlockException.class, RestStatus.SERVICE_UNAVAILABLE ); - assertThrows(clientToMasterlessNode.prepareMultiGet().add("no_index", "type1", "1"), + assertRequestBuilderThrows(clientToMasterlessNode.prepareMultiGet().add("no_index", "type1", "1"), ClusterBlockException.class, RestStatus.SERVICE_UNAVAILABLE ); - assertThrows(clientToMasterlessNode.admin().indices().prepareAnalyze("test", "this is a test"), + assertRequestBuilderThrows(clientToMasterlessNode.admin().indices().prepareAnalyze("test", "this is a test"), ClusterBlockException.class, RestStatus.SERVICE_UNAVAILABLE ); - assertThrows(clientToMasterlessNode.admin().indices().prepareAnalyze("no_index", "this is a test"), + assertRequestBuilderThrows(clientToMasterlessNode.admin().indices().prepareAnalyze("no_index", "this is a test"), ClusterBlockException.class, RestStatus.SERVICE_UNAVAILABLE ); - assertThrows(clientToMasterlessNode.prepareSearch("test").setSize(0), + assertRequestBuilderThrows(clientToMasterlessNode.prepareSearch("test").setSize(0), ClusterBlockException.class, RestStatus.SERVICE_UNAVAILABLE ); - assertThrows(clientToMasterlessNode.prepareSearch("no_index").setSize(0), + assertRequestBuilderThrows(clientToMasterlessNode.prepareSearch("no_index").setSize(0), ClusterBlockException.class, RestStatus.SERVICE_UNAVAILABLE ); diff --git a/server/src/test/java/org/elasticsearch/indices/settings/UpdateSettingsIT.java b/server/src/test/java/org/elasticsearch/indices/settings/UpdateSettingsIT.java index 0d5b23f1c6c7f..d2081a354d02e 100644 --- a/server/src/test/java/org/elasticsearch/indices/settings/UpdateSettingsIT.java +++ b/server/src/test/java/org/elasticsearch/indices/settings/UpdateSettingsIT.java @@ -45,7 +45,7 @@ import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_READ_ONLY; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBlocked; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; @@ -452,7 +452,8 @@ public void testEngineGCDeletesSetting() throws Exception { client().prepareIndex("test", "type", "1").setSource("f", 1).setVersionType(VersionType.EXTERNAL).setVersion(1).get(); client().prepareDelete("test", "type", "1").setVersionType(VersionType.EXTERNAL).setVersion(2).get(); // delete is still in cache this should fail - assertThrows(client().prepareIndex("test", "type", "1").setSource("f", 3).setVersionType(VersionType.EXTERNAL).setVersion(1), + assertRequestBuilderThrows( + client().prepareIndex("test", "type", "1").setSource("f", 3).setVersionType(VersionType.EXTERNAL).setVersion(1), VersionConflictEngineException.class); assertAcked(client().admin().indices().prepareUpdateSettings("test").setSettings(Settings.builder().put("index.gc_deletes", 0))); diff --git a/server/src/test/java/org/elasticsearch/indices/template/SimpleIndexTemplateIT.java b/server/src/test/java/org/elasticsearch/indices/template/SimpleIndexTemplateIT.java index e1ce312b7f674..95fd3c194d4dc 100644 --- a/server/src/test/java/org/elasticsearch/indices/template/SimpleIndexTemplateIT.java +++ b/server/src/test/java/org/elasticsearch/indices/template/SimpleIndexTemplateIT.java @@ -56,7 +56,7 @@ import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsString; @@ -109,7 +109,7 @@ public void testSimpleIndexTemplateTests() throws Exception { .get(); // test create param - assertThrows(client().admin().indices().preparePutTemplate("template_2") + assertRequestBuilderThrows(client().admin().indices().preparePutTemplate("template_2") .setPatterns(Collections.singletonList("test*")) .setSettings(indexSettings()) .setCreate(true) @@ -329,7 +329,7 @@ public void testThatInvalidGetIndexTemplatesFails() throws Exception { } private void testExpectActionRequestValidationException(String... names) { - assertThrows(client().admin().indices().prepareGetTemplates(names), + assertRequestBuilderThrows(client().admin().indices().prepareGetTemplates(names), ActionRequestValidationException.class, "get template with " + Arrays.toString(names)); } diff --git a/server/src/test/java/org/elasticsearch/persistent/PersistentTasksExecutorIT.java b/server/src/test/java/org/elasticsearch/persistent/PersistentTasksExecutorIT.java index 4acb391d9c0ee..b0c5ffe4e99c0 100644 --- a/server/src/test/java/org/elasticsearch/persistent/PersistentTasksExecutorIT.java +++ b/server/src/test/java/org/elasticsearch/persistent/PersistentTasksExecutorIT.java @@ -25,16 +25,16 @@ import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.plugins.Plugin; -import org.elasticsearch.tasks.TaskId; -import org.elasticsearch.tasks.TaskInfo; -import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.persistent.PersistentTasksCustomMetaData.PersistentTask; import org.elasticsearch.persistent.PersistentTasksService.WaitForPersistentTaskListener; import org.elasticsearch.persistent.TestPersistentTasksPlugin.State; -import org.elasticsearch.persistent.TestPersistentTasksPlugin.TestPersistentTasksExecutor; import org.elasticsearch.persistent.TestPersistentTasksPlugin.TestParams; +import org.elasticsearch.persistent.TestPersistentTasksPlugin.TestPersistentTasksExecutor; import org.elasticsearch.persistent.TestPersistentTasksPlugin.TestTasksRequestBuilder; +import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.tasks.TaskId; +import org.elasticsearch.tasks.TaskInfo; +import org.elasticsearch.test.ESIntegTestCase; import org.junit.After; import org.junit.Before; @@ -43,7 +43,7 @@ import java.util.List; import java.util.Objects; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFutureThrows; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; @@ -137,7 +137,7 @@ public void testPersistentActionCompletion() throws Exception { //try sending completion request with incorrect allocation id PlainActionFuture> failedCompletionNotificationFuture = new PlainActionFuture<>(); persistentTasksService.sendCompletionRequest(taskId, Long.MAX_VALUE, null, failedCompletionNotificationFuture); - assertThrows(failedCompletionNotificationFuture, ResourceNotFoundException.class); + assertFutureThrows(failedCompletionNotificationFuture, ResourceNotFoundException.class); // Make sure that the task is still running assertThat(client().admin().cluster().prepareListTasks().setActions(TestPersistentTasksExecutor.NAME + "[c]") .setDetailed(true).get().getTasks().size(), equalTo(1)); @@ -245,11 +245,11 @@ public void testPersistentActionStatusUpdate() throws Exception { persistentTasksService.waitForPersistentTaskCondition(taskId, task -> false, TimeValue.timeValueMillis(10), future1); - assertThrows(future1, IllegalStateException.class, "timed out after 10ms"); + assertFutureThrows(future1, IllegalStateException.class, "timed out after 10ms"); PlainActionFuture> failedUpdateFuture = new PlainActionFuture<>(); persistentTasksService.sendUpdateStateRequest(taskId, -2, new State("should fail"), failedUpdateFuture); - assertThrows(failedUpdateFuture, ResourceNotFoundException.class, "the task with id " + taskId + + assertFutureThrows(failedUpdateFuture, ResourceNotFoundException.class, "the task with id " + taskId + " and allocation id -2 doesn't exist"); // Wait for the task to disappear @@ -273,7 +273,7 @@ public void testCreatePersistentTaskWithDuplicateId() throws Exception { PlainActionFuture> future2 = new PlainActionFuture<>(); persistentTasksService.sendStartRequest(taskId, TestPersistentTasksExecutor.NAME, new TestParams("Blah"), future2); - assertThrows(future2, ResourceAlreadyExistsException.class); + assertFutureThrows(future2, ResourceAlreadyExistsException.class); assertBusy(() -> { // Wait for the task to start diff --git a/server/src/test/java/org/elasticsearch/repositories/blobstore/BlobStoreRepositoryCleanupIT.java b/server/src/test/java/org/elasticsearch/repositories/blobstore/BlobStoreRepositoryCleanupIT.java index fd195123fc0a9..2f8ab2d5c3217 100644 --- a/server/src/test/java/org/elasticsearch/repositories/blobstore/BlobStoreRepositoryCleanupIT.java +++ b/server/src/test/java/org/elasticsearch/repositories/blobstore/BlobStoreRepositoryCleanupIT.java @@ -35,7 +35,7 @@ import java.util.concurrent.TimeUnit; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFutureThrows; import static org.hamcrest.Matchers.is; @ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0) @@ -59,7 +59,7 @@ public void testRepeatCleanupsDontRemove() throws Exception { final String masterNode = startBlockedCleanup("test-repo"); logger.info("--> sending another cleanup"); - assertThrows(client().admin().cluster().prepareCleanupRepository("test-repo").execute(), IllegalStateException.class); + assertFutureThrows(client().admin().cluster().prepareCleanupRepository("test-repo").execute(), IllegalStateException.class); logger.info("--> ensure cleanup is still in progress"); final RepositoryCleanupInProgress cleanup = diff --git a/server/src/test/java/org/elasticsearch/search/morelikethis/MoreLikeThisIT.java b/server/src/test/java/org/elasticsearch/search/morelikethis/MoreLikeThisIT.java index 4492353f6f15b..7c20a16ee058f 100644 --- a/server/src/test/java/org/elasticsearch/search/morelikethis/MoreLikeThisIT.java +++ b/server/src/test/java/org/elasticsearch/search/morelikethis/MoreLikeThisIT.java @@ -56,9 +56,9 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.notNullValue; @@ -339,12 +339,12 @@ public void testNumericField() throws Exception { assertHitCount(searchResponse, 1L); // Explicit list of fields including numeric fields -> fail - assertThrows(client().prepareSearch().setQuery( + assertRequestBuilderThrows(client().prepareSearch().setQuery( new MoreLikeThisQueryBuilder(new String[] {"string_value", "int_value"}, null, new Item[] {new Item("test", "1")}).minTermFreq(1).minDocFreq(1)), SearchPhaseExecutionException.class); // mlt query with no field -> exception because _all is not enabled) - assertThrows(client().prepareSearch() + assertRequestBuilderThrows(client().prepareSearch() .setQuery(moreLikeThisQuery(new String[] {"index"}).minTermFreq(1).minDocFreq(1)), SearchPhaseExecutionException.class); @@ -354,12 +354,12 @@ public void testNumericField() throws Exception { assertHitCount(searchResponse, 2L); // mlt query with at least a numeric field -> fail by default - assertThrows(client().prepareSearch().setQuery( + assertRequestBuilderThrows(client().prepareSearch().setQuery( moreLikeThisQuery(new String[] {"string_value", "int_value"}, new String[] {"index"}, null)), SearchPhaseExecutionException.class); // mlt query with at least a numeric field -> fail by command - assertThrows(client().prepareSearch().setQuery( + assertRequestBuilderThrows(client().prepareSearch().setQuery( moreLikeThisQuery(new String[] {"string_value", "int_value"}, new String[] {"index"}, null).failOnUnsupportedField(true)), SearchPhaseExecutionException.class); @@ -371,12 +371,12 @@ public void testNumericField() throws Exception { assertHitCount(searchResponse, 2L); // mlt field query on a numeric field -> failure by default - assertThrows(client().prepareSearch().setQuery( + assertRequestBuilderThrows(client().prepareSearch().setQuery( moreLikeThisQuery(new String[] {"int_value"}, new String[] {"42"}, null).minTermFreq(1).minDocFreq(1)), SearchPhaseExecutionException.class); // mlt field query on a numeric field -> failure by command - assertThrows(client().prepareSearch().setQuery( + assertRequestBuilderThrows(client().prepareSearch().setQuery( moreLikeThisQuery(new String[] {"int_value"}, new String[] {"42"}, null).minTermFreq(1).minDocFreq(1) .failOnUnsupportedField(true)), SearchPhaseExecutionException.class); diff --git a/server/src/test/java/org/elasticsearch/search/scroll/SearchScrollIT.java b/server/src/test/java/org/elasticsearch/search/scroll/SearchScrollIT.java index ccf6bd97c1703..1080a40513ea7 100644 --- a/server/src/test/java/org/elasticsearch/search/scroll/SearchScrollIT.java +++ b/server/src/test/java/org/elasticsearch/search/scroll/SearchScrollIT.java @@ -56,9 +56,9 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoSearchHits; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; @@ -328,9 +328,9 @@ public void testSimpleScrollQueryThenFetch_clearScrollIds() throws Exception { assertThat(clearResponse.status(), equalTo(RestStatus.OK)); assertToXContentResponse(clearResponse, true, clearResponse.getNumFreed()); - assertThrows(client().prepareSearchScroll(searchResponse1.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)), + assertRequestBuilderThrows(client().prepareSearchScroll(searchResponse1.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)), RestStatus.NOT_FOUND); - assertThrows(client().prepareSearchScroll(searchResponse2.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)), + assertRequestBuilderThrows(client().prepareSearchScroll(searchResponse2.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)), RestStatus.NOT_FOUND); } @@ -436,9 +436,9 @@ public void testSimpleScrollQueryThenFetchClearAllScrollIds() throws Exception { assertThat(clearResponse.status(), equalTo(RestStatus.OK)); assertToXContentResponse(clearResponse, true, clearResponse.getNumFreed()); - assertThrows(internalCluster().transportClient().prepareSearchScroll(searchResponse1.getScrollId()) + assertRequestBuilderThrows(internalCluster().transportClient().prepareSearchScroll(searchResponse1.getScrollId()) .setScroll(TimeValue.timeValueMinutes(2)), RestStatus.NOT_FOUND); - assertThrows(internalCluster().transportClient().prepareSearchScroll(searchResponse2.getScrollId()) + assertRequestBuilderThrows(internalCluster().transportClient().prepareSearchScroll(searchResponse2.getScrollId()) .setScroll(TimeValue.timeValueMinutes(2)), RestStatus.NOT_FOUND); } @@ -486,7 +486,8 @@ public void testThatNonExistingScrollIdReturnsCorrectException() throws Exceptio ClearScrollResponse clearScrollResponse = client().prepareClearScroll().addScrollId(searchResponse.getScrollId()).get(); assertThat(clearScrollResponse.isSucceeded(), is(true)); - assertThrows(internalCluster().transportClient().prepareSearchScroll(searchResponse.getScrollId()), RestStatus.NOT_FOUND); + assertRequestBuilderThrows(internalCluster().transportClient().prepareSearchScroll(searchResponse.getScrollId()), + RestStatus.NOT_FOUND); } public void testStringSortMissingAscTerminates() throws Exception { diff --git a/server/src/test/java/org/elasticsearch/search/suggest/SuggestSearchIT.java b/server/src/test/java/org/elasticsearch/search/suggest/SuggestSearchIT.java index 379bba8ec8b24..73ff496a2a9cc 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/SuggestSearchIT.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/SuggestSearchIT.java @@ -65,10 +65,10 @@ import static org.elasticsearch.search.suggest.SuggestBuilders.termSuggestion; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSuggestion; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSuggestionPhraseCollateMatchExists; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSuggestionSize; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.equalTo; @@ -293,12 +293,12 @@ public void testUnmappedField() throws IOException, InterruptedException, Execut { SearchRequestBuilder searchBuilder = client().prepareSearch().setSize(0); searchBuilder.suggest(new SuggestBuilder().setGlobalText("tetsting sugestion").addSuggestion("did_you_mean", phraseSuggestion)); - assertThrows(searchBuilder, SearchPhaseExecutionException.class); + assertRequestBuilderThrows(searchBuilder, SearchPhaseExecutionException.class); } { SearchRequestBuilder searchBuilder = client().prepareSearch().setSize(0); searchBuilder.suggest(new SuggestBuilder().setGlobalText("tetsting sugestion").addSuggestion("did_you_mean", phraseSuggestion)); - assertThrows(searchBuilder, SearchPhaseExecutionException.class); + assertRequestBuilderThrows(searchBuilder, SearchPhaseExecutionException.class); } } @@ -748,7 +748,7 @@ public void testShardFailures() throws IOException, InterruptedException { .suggest( new SuggestBuilder().setGlobalText("tetsting sugestion").addSuggestion("did_you_mean", phraseSuggestion("fielddoesnotexist").maxErrors(5.0f))); - assertThrows(request, SearchPhaseExecutionException.class); + assertRequestBuilderThrows(request, SearchPhaseExecutionException.class); // When searching on a shard which does not hold yet any document of an existing type, we should not fail SearchResponse searchResponse = client().prepareSearch().setSize(0) diff --git a/server/src/test/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreIT.java b/server/src/test/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreIT.java index 2320e4e0d3672..1c83546c274b6 100644 --- a/server/src/test/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreIT.java +++ b/server/src/test/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreIT.java @@ -107,8 +107,9 @@ import static org.elasticsearch.index.seqno.RetentionLeaseActions.RETAIN_ALL; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFutureThrows; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -316,7 +317,7 @@ public void testRestoreCustomMetadata() throws Exception { .setWaitForCompletion(true).execute().actionGet(); logger.info("--> make sure old repository wasn't restored"); - assertThrows(client.admin().cluster().prepareGetRepositories("test-repo"), RepositoryMissingException.class); + assertRequestBuilderThrows(client.admin().cluster().prepareGetRepositories("test-repo"), RepositoryMissingException.class); assertThat(client.admin().cluster().prepareGetRepositories("test-repo-2").get().repositories().size(), equalTo(1)); logger.info("--> check that custom persistent metadata was restored"); @@ -617,7 +618,7 @@ public void testRestoreIndexWithMissingShards() throws Exception { assertAcked(client().admin().indices().prepareClose("test-idx-all")); logger.info("--> restore incomplete snapshot - should fail"); - assertThrows(client().admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap-2").setRestoreGlobalState(false) + assertFutureThrows(client().admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap-2").setRestoreGlobalState(false) .setWaitForCompletion(true).execute(), SnapshotRestoreException.class); diff --git a/server/src/test/java/org/elasticsearch/snapshots/RepositoriesIT.java b/server/src/test/java/org/elasticsearch/snapshots/RepositoriesIT.java index 455a3f18e5472..06553cd5caa44 100644 --- a/server/src/test/java/org/elasticsearch/snapshots/RepositoriesIT.java +++ b/server/src/test/java/org/elasticsearch/snapshots/RepositoriesIT.java @@ -37,7 +37,7 @@ import java.util.List; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; @@ -194,12 +194,12 @@ public void testRepositoryVerification() throws Exception { Settings readonlySettings = Settings.builder().put(settings) .put("readonly", true).build(); logger.info("--> creating repository that cannot write any files - should fail"); - assertThrows(client.admin().cluster().preparePutRepository("test-repo-1") + assertRequestBuilderThrows(client.admin().cluster().preparePutRepository("test-repo-1") .setType("mock").setSettings(settings), RepositoryVerificationException.class); logger.info("--> creating read-only repository that cannot read any files - should fail"); - assertThrows(client.admin().cluster().preparePutRepository("test-repo-2") + assertRequestBuilderThrows(client.admin().cluster().preparePutRepository("test-repo-2") .setType("mock").setSettings(readonlySettings), RepositoryVerificationException.class); @@ -208,14 +208,14 @@ public void testRepositoryVerification() throws Exception { .setType("mock").setSettings(settings).setVerify(false)); logger.info("--> verifying repository"); - assertThrows(client.admin().cluster().prepareVerifyRepository("test-repo-1"), RepositoryVerificationException.class); + assertRequestBuilderThrows(client.admin().cluster().prepareVerifyRepository("test-repo-1"), RepositoryVerificationException.class); logger.info("--> creating read-only repository that cannot read any files, but suppress verification - should be acked"); assertAcked(client.admin().cluster().preparePutRepository("test-repo-2") .setType("mock").setSettings(readonlySettings).setVerify(false)); logger.info("--> verifying repository"); - assertThrows(client.admin().cluster().prepareVerifyRepository("test-repo-2"), RepositoryVerificationException.class); + assertRequestBuilderThrows(client.admin().cluster().prepareVerifyRepository("test-repo-2"), RepositoryVerificationException.class); Path location = randomRepoPath(); diff --git a/server/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java b/server/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java index cd14a7f9f5d0e..4966dce3ba872 100644 --- a/server/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java +++ b/server/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java @@ -130,7 +130,7 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertIndexTemplateExists; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertIndexTemplateMissing; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.containsString; @@ -1371,7 +1371,7 @@ public void testDeleteSnapshotWithMissingIndexAndShardMetadata() throws Exceptio client.admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap-1").get(); logger.info("--> make sure snapshot doesn't exist"); - assertThrows(client.admin().cluster().prepareGetSnapshots("test-repo") + assertRequestBuilderThrows(client.admin().cluster().prepareGetSnapshots("test-repo") .addSnapshots("test-snap-1"), SnapshotMissingException.class); for (String index : indices) { @@ -1411,7 +1411,7 @@ public void testDeleteSnapshotWithMissingMetadata() throws Exception { client.admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap-1").get(); logger.info("--> make sure snapshot doesn't exist"); - assertThrows(client.admin().cluster().prepareGetSnapshots("test-repo") + assertRequestBuilderThrows(client.admin().cluster().prepareGetSnapshots("test-repo") .addSnapshots("test-snap-1"), SnapshotMissingException.class); } @@ -1448,7 +1448,8 @@ public void testDeleteSnapshotWithCorruptedSnapshotFile() throws Exception { client.admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap-1").get(); logger.info("--> make sure snapshot doesn't exist"); - assertThrows(client.admin().cluster().prepareGetSnapshots("test-repo").addSnapshots("test-snap-1"), SnapshotMissingException.class); + assertRequestBuilderThrows(client.admin().cluster().prepareGetSnapshots("test-repo").addSnapshots("test-snap-1"), + SnapshotMissingException.class); logger.info("--> make sure that we can create the snapshot again"); createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-1") @@ -1505,9 +1506,9 @@ public void testDeleteSnapshotWithCorruptedGlobalState() throws Exception { assertThat(snapshotStatusResponse.getSnapshots().get(0).getSnapshot().getSnapshotId().getName(), equalTo("test-snap")); assertAcked(client().admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap").get()); - assertThrows(client().admin().cluster().prepareGetSnapshots("test-repo").addSnapshots("test-snap"), + assertRequestBuilderThrows(client().admin().cluster().prepareGetSnapshots("test-repo").addSnapshots("test-snap"), SnapshotMissingException.class); - assertThrows(client().admin().cluster().prepareSnapshotStatus("test-repo").addSnapshots("test-snap"), + assertRequestBuilderThrows(client().admin().cluster().prepareSnapshotStatus("test-repo").addSnapshots("test-snap"), SnapshotMissingException.class); createSnapshotResponse = client().admin().cluster().prepareCreateSnapshot("test-repo", "test-snap") @@ -1918,11 +1919,11 @@ public void testReadonlyRepository() throws Exception { assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(1)); logger.info("--> try deleting snapshot"); - assertThrows(client.admin().cluster().prepareDeleteSnapshot("readonly-repo", "test-snap"), RepositoryException.class, + assertRequestBuilderThrows(client.admin().cluster().prepareDeleteSnapshot("readonly-repo", "test-snap"), RepositoryException.class, "cannot delete snapshot from a readonly repository"); logger.info("--> try making another snapshot"); - assertThrows(client.admin().cluster().prepareCreateSnapshot("readonly-repo", "test-snap-2") + assertRequestBuilderThrows(client.admin().cluster().prepareCreateSnapshot("readonly-repo", "test-snap-2") .setWaitForCompletion(true).setIndices("test-idx"), RepositoryException.class, "cannot create snapshot in a readonly repository"); @@ -2292,7 +2293,7 @@ public void testChangeSettingsOnRestore() throws Exception { .build(); logger.info("--> try restoring while changing the number of shards - should fail"); - assertThrows(client.admin().cluster() + assertRequestBuilderThrows(client.admin().cluster() .prepareRestoreSnapshot("test-repo", "test-snap") .setIgnoreIndexSettings("index.analysis.*") .setIndexSettings(newIncorrectIndexSettings) @@ -2303,7 +2304,7 @@ public void testChangeSettingsOnRestore() throws Exception { .put(newIndexSettings) .put(SETTING_NUMBER_OF_REPLICAS.substring(IndexMetaData.INDEX_SETTING_PREFIX.length()), randomIntBetween(-10, -1)) .build(); - assertThrows(client.admin().cluster() + assertRequestBuilderThrows(client.admin().cluster() .prepareRestoreSnapshot("test-repo", "test-snap") .setIgnoreIndexSettings("index.analysis.*") .setIndexSettings(newIncorrectReplicasIndexSettings) diff --git a/server/src/test/java/org/elasticsearch/update/UpdateIT.java b/server/src/test/java/org/elasticsearch/update/UpdateIT.java index 0bd5851e35b45..59330ab641805 100644 --- a/server/src/test/java/org/elasticsearch/update/UpdateIT.java +++ b/server/src/test/java/org/elasticsearch/update/UpdateIT.java @@ -62,7 +62,7 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFutureThrows; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; @@ -241,7 +241,7 @@ public void testNotUpsertDoc() throws Exception { createTestIndex(); ensureGreen(); - assertThrows(client().prepareUpdate(indexOrAlias(), "type1", "1") + assertFutureThrows(client().prepareUpdate(indexOrAlias(), "type1", "1") .setDoc(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject()) .setDocAsUpsert(false) .setFetchSource(true) diff --git a/server/src/test/java/org/elasticsearch/versioning/SimpleVersioningIT.java b/server/src/test/java/org/elasticsearch/versioning/SimpleVersioningIT.java index caafeaecd42fa..3a72bd130b622 100644 --- a/server/src/test/java/org/elasticsearch/versioning/SimpleVersioningIT.java +++ b/server/src/test/java/org/elasticsearch/versioning/SimpleVersioningIT.java @@ -45,8 +45,9 @@ import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFutureThrows; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.lessThanOrEqualTo; @@ -63,7 +64,7 @@ public void testExternalVersioningInitialDelete() throws Exception { assertEquals(DocWriteResponse.Result.NOT_FOUND, deleteResponse.getResult()); // this should conflict with the delete command transaction which told us that the object was deleted at version 17. - assertThrows( + assertFutureThrows( client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(13) .setVersionType(VersionType.EXTERNAL).execute(), VersionConflictEngineException.class @@ -89,7 +90,7 @@ public void testExternalGTE() throws Exception { .setVersionType(VersionType.EXTERNAL_GTE).get(); assertThat(indexResponse.getVersion(), equalTo(14L)); - assertThrows(client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(13) + assertRequestBuilderThrows(client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(13) .setVersionType(VersionType.EXTERNAL_GTE), VersionConflictEngineException.class); @@ -102,7 +103,7 @@ public void testExternalGTE() throws Exception { } // deleting with a lower version fails. - assertThrows( + assertRequestBuilderThrows( client().prepareDelete("test", "type", "1").setVersion(2).setVersionType(VersionType.EXTERNAL_GTE), VersionConflictEngineException.class); @@ -114,7 +115,7 @@ public void testExternalGTE() throws Exception { assertThat(deleteResponse.getVersion(), equalTo(v)); // Deleting with a lower version keeps on failing after a delete. - assertThrows( + assertFutureThrows( client().prepareDelete("test", "type", "1").setVersion(2).setVersionType(VersionType.EXTERNAL_GTE).execute(), VersionConflictEngineException.class); @@ -138,7 +139,7 @@ public void testExternalVersioning() throws Exception { .setVersionType(VersionType.EXTERNAL).execute().actionGet(); assertThat(indexResponse.getVersion(), equalTo(14L)); - assertThrows(client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(13) + assertFutureThrows(client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setVersion(13) .setVersionType(VersionType.EXTERNAL).execute(), VersionConflictEngineException.class); @@ -150,7 +151,7 @@ public void testExternalVersioning() throws Exception { } // deleting with a lower version fails. - assertThrows( + assertFutureThrows( client().prepareDelete("test", "type", "1").setVersion(2).setVersionType(VersionType.EXTERNAL).execute(), VersionConflictEngineException.class); @@ -161,7 +162,7 @@ public void testExternalVersioning() throws Exception { assertThat(deleteResponse.getVersion(), equalTo(17L)); // Deleting with a lower version keeps on failing after a delete. - assertThrows( + assertFutureThrows( client().prepareDelete("test", "type", "1").setVersion(2).setVersionType(VersionType.EXTERNAL).execute(), VersionConflictEngineException.class); @@ -217,7 +218,7 @@ public void testCompareAndSetInitialDelete() throws Exception { createIndex("test"); ensureGreen(); - assertThrows(client().prepareDelete("test", "type", "1").setIfSeqNo(17).setIfPrimaryTerm(10).execute(), + assertFutureThrows(client().prepareDelete("test", "type", "1").setIfSeqNo(17).setIfPrimaryTerm(10).execute(), VersionConflictEngineException.class); IndexResponse indexResponse = client().prepareIndex("test", "type", "1").setSource("field1", "value1_1") @@ -237,22 +238,25 @@ public void testCompareAndSet() { assertThat(indexResponse.getSeqNo(), equalTo(1L)); assertThat(indexResponse.getPrimaryTerm(), equalTo(1L)); - assertThrows( + assertFutureThrows( client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setIfSeqNo(10).setIfPrimaryTerm(1).execute(), VersionConflictEngineException.class); - assertThrows( + assertFutureThrows( client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setIfSeqNo(10).setIfPrimaryTerm(2).execute(), VersionConflictEngineException.class); - assertThrows( + assertFutureThrows( client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setIfSeqNo(1).setIfPrimaryTerm(2).execute(), VersionConflictEngineException.class); - assertThrows(client().prepareDelete("test", "type", "1").setIfSeqNo(10).setIfPrimaryTerm(1), VersionConflictEngineException.class); - assertThrows(client().prepareDelete("test", "type", "1").setIfSeqNo(10).setIfPrimaryTerm(2), VersionConflictEngineException.class); - assertThrows(client().prepareDelete("test", "type", "1").setIfSeqNo(1).setIfPrimaryTerm(2), VersionConflictEngineException.class); + assertRequestBuilderThrows(client().prepareDelete("test", "type", "1").setIfSeqNo(10).setIfPrimaryTerm(1), + VersionConflictEngineException.class); + assertRequestBuilderThrows(client().prepareDelete("test", "type", "1").setIfSeqNo(10).setIfPrimaryTerm(2), + VersionConflictEngineException.class); + assertRequestBuilderThrows(client().prepareDelete("test", "type", "1").setIfSeqNo(1).setIfPrimaryTerm(2), + VersionConflictEngineException.class); client().admin().indices().prepareRefresh().execute().actionGet(); for (int i = 0; i < 10; i++) { @@ -279,12 +283,16 @@ public void testCompareAndSet() { assertThat(deleteResponse.getSeqNo(), equalTo(2L)); assertThat(deleteResponse.getPrimaryTerm(), equalTo(1L)); - assertThrows(client().prepareDelete("test", "type", "1").setIfSeqNo(1).setIfPrimaryTerm(1), VersionConflictEngineException.class); - assertThrows(client().prepareDelete("test", "type", "1").setIfSeqNo(3).setIfPrimaryTerm(12), VersionConflictEngineException.class); - assertThrows(client().prepareDelete("test", "type", "1").setIfSeqNo(1).setIfPrimaryTerm(2), VersionConflictEngineException.class); + assertRequestBuilderThrows(client().prepareDelete("test", "type", "1").setIfSeqNo(1).setIfPrimaryTerm(1), + VersionConflictEngineException.class); + assertRequestBuilderThrows(client().prepareDelete("test", "type", "1").setIfSeqNo(3).setIfPrimaryTerm(12), + VersionConflictEngineException.class); + assertRequestBuilderThrows(client().prepareDelete("test", "type", "1").setIfSeqNo(1).setIfPrimaryTerm(2), + VersionConflictEngineException.class); // the doc is deleted. Even when we hit the deleted seqNo, a conditional delete should fail. - assertThrows(client().prepareDelete("test", "type", "1").setIfSeqNo(2).setIfPrimaryTerm(1), VersionConflictEngineException.class); + assertRequestBuilderThrows(client().prepareDelete("test", "type", "1").setIfSeqNo(2).setIfPrimaryTerm(1), + VersionConflictEngineException.class); } public void testSimpleVersioningWithFlush() throws Exception { @@ -299,13 +307,16 @@ public void testSimpleVersioningWithFlush() throws Exception { assertThat(indexResponse.getSeqNo(), equalTo(1L)); client().admin().indices().prepareFlush().execute().actionGet(); - assertThrows(client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setIfSeqNo(0).setIfPrimaryTerm(1), + assertRequestBuilderThrows( + client().prepareIndex("test", "type", "1").setSource("field1", "value1_1").setIfSeqNo(0).setIfPrimaryTerm(1), VersionConflictEngineException.class); - assertThrows(client().prepareIndex("test", "type", "1").setCreate(true).setSource("field1", "value1_1"), + assertRequestBuilderThrows( + client().prepareIndex("test", "type", "1").setCreate(true).setSource("field1", "value1_1"), VersionConflictEngineException.class); - assertThrows(client().prepareDelete("test", "type", "1").setIfSeqNo(0).setIfPrimaryTerm(1), VersionConflictEngineException.class); + assertRequestBuilderThrows( + client().prepareDelete("test", "type", "1").setIfSeqNo(0).setIfPrimaryTerm(1), VersionConflictEngineException.class); for (int i = 0; i < 10; i++) { assertThat(client().prepareGet("test", "type", "1").execute().actionGet().getVersion(), equalTo(2L)); diff --git a/test/framework/src/main/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java b/test/framework/src/main/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java index fdb7b8984c3b0..603524ec00585 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java +++ b/test/framework/src/main/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java @@ -77,6 +77,8 @@ import java.util.concurrent.TimeUnit; import java.util.function.Function; +import static org.apache.lucene.util.LuceneTestCase.expectThrows; +import static org.apache.lucene.util.LuceneTestCase.expectThrowsAnyOf; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -512,15 +514,16 @@ public static T assertDisjunctionSubQuery(Query query, Class void assertThrows(ActionRequestBuilder builder, Class exceptionClass) { - assertThrows(builder.execute(), exceptionClass); + public static void assertRequestBuilderThrows(ActionRequestBuilder builder, Class exceptionClass) { + assertFutureThrows(builder.execute(), exceptionClass); } /** * Run the request from a given builder and check that it throws an exception of the right type, with a given {@link RestStatus} */ - public static void assertThrows(ActionRequestBuilder builder, Class exceptionClass, RestStatus status) { - assertThrows(builder.execute(), exceptionClass, status); + public static void assertRequestBuilderThrows(ActionRequestBuilder builder, Class exceptionClass, + RestStatus status) { + assertFutureThrows(builder.execute(), exceptionClass, status); } /** @@ -528,22 +531,23 @@ public static void assertThrows(ActionRequestBuilder * * @param extraInfo extra information to add to the failure message */ - public static void assertThrows(ActionRequestBuilder builder, Class exceptionClass, String extraInfo) { - assertThrows(builder.execute(), exceptionClass, extraInfo); + public static void assertRequestBuilderThrows(ActionRequestBuilder builder, Class exceptionClass, + String extraInfo) { + assertFutureThrows(builder.execute(), exceptionClass, extraInfo); } /** * Run future.actionGet() and check that it throws an exception of the right type */ - public static void assertThrows(ActionFuture future, Class exceptionClass) { - assertThrows(future, exceptionClass, null, null); + public static void assertFutureThrows(ActionFuture future, Class exceptionClass) { + assertFutureThrows(future, exceptionClass, null, null); } /** * Run future.actionGet() and check that it throws an exception of the right type, with a given {@link RestStatus} */ - public static void assertThrows(ActionFuture future, Class exceptionClass, RestStatus status) { - assertThrows(future, exceptionClass, status, null); + public static void assertFutureThrows(ActionFuture future, Class exceptionClass, RestStatus status) { + assertFutureThrows(future, exceptionClass, status, null); } /** @@ -551,8 +555,8 @@ public static void assertThrows(ActionFuture future, Class * * @param extraInfo extra information to add to the failure message */ - public static void assertThrows(ActionFuture future, Class exceptionClass, String extraInfo) { - assertThrows(future, exceptionClass, null, extraInfo); + public static void assertFutureThrows(ActionFuture future, Class exceptionClass, String extraInfo) { + assertFutureThrows(future, exceptionClass, null, extraInfo); } /** @@ -562,9 +566,8 @@ public static void assertThrows(ActionFuture future, Class * @param status {@link org.elasticsearch.rest.RestStatus} to check for. Can be null to disable the check * @param extraInfo extra information to add to the failure message. Can be null. */ - public static void assertThrows(ActionFuture future, Class exceptionClass, - @Nullable RestStatus status, @Nullable String extraInfo) { - boolean fail = false; + public static void assertFutureThrows(ActionFuture future, Class exceptionClass, + @Nullable RestStatus status, @Nullable String extraInfo) { extraInfo = extraInfo == null || extraInfo.isEmpty() ? "" : extraInfo + ": "; extraInfo += "expected a " + exceptionClass + " exception to be thrown"; @@ -572,54 +575,36 @@ public static void assertThrows(ActionFuture future, Class extraInfo += " with status [" + status + "]"; } - try { - future.actionGet(); - fail = true; - - } catch (ElasticsearchException esException) { - assertThat(extraInfo, esException.unwrapCause(), instanceOf(exceptionClass)); - if (status != null) { - assertThat(extraInfo, ExceptionsHelper.status(esException), equalTo(status)); - } - } catch (Exception e) { - assertThat(extraInfo, e, instanceOf(exceptionClass)); - if (status != null) { - assertThat(extraInfo, ExceptionsHelper.status(e), equalTo(status)); - } + Throwable expected = expectThrowsAnyOf(Arrays.asList(exceptionClass, ElasticsearchException.class), future::actionGet); + if (expected instanceof ElasticsearchException) { + assertThat(extraInfo, ((ElasticsearchException) expected).unwrapCause(), instanceOf(exceptionClass)); + } else { + assertThat(extraInfo, expected, instanceOf(exceptionClass)); } - // has to be outside catch clause to get a proper message - if (fail) { - throw new AssertionError(extraInfo); + + if (status != null) { + assertThat(extraInfo, ExceptionsHelper.status(expected), equalTo(status)); } } - public static void assertThrows(ActionRequestBuilder builder, RestStatus status) { - assertThrows(builder.execute(), status); + public static void assertRequestBuilderThrows(ActionRequestBuilder builder, RestStatus status) { + assertFutureThrows(builder.execute(), status); } - public static void assertThrows(ActionRequestBuilder builder, RestStatus status, String extraInfo) { - assertThrows(builder.execute(), status, extraInfo); + public static void assertRequestBuilderThrows(ActionRequestBuilder builder, RestStatus status, String extraInfo) { + assertFutureThrows(builder.execute(), status, extraInfo); } - public static void assertThrows(ActionFuture future, RestStatus status) { - assertThrows(future, status, null); + public static void assertFutureThrows(ActionFuture future, RestStatus status) { + assertFutureThrows(future, status, null); } - public static void assertThrows(ActionFuture future, RestStatus status, String extraInfo) { - boolean fail = false; + public static void assertFutureThrows(ActionFuture future, RestStatus status, String extraInfo) { extraInfo = extraInfo == null || extraInfo.isEmpty() ? "" : extraInfo + ": "; extraInfo += "expected a " + status + " status exception to be thrown"; - try { - future.actionGet(); - fail = true; - } catch (Exception e) { - assertThat(extraInfo, ExceptionsHelper.status(e), equalTo(status)); - } - // has to be outside catch clause to get a proper message - if (fail) { - throw new AssertionError(extraInfo); - } + Exception e = expectThrows(Exception.class, future::actionGet); + assertThat(extraInfo, ExceptionsHelper.status(e), equalTo(status)); } /** diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/integration/SecurityClearScrollTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/integration/SecurityClearScrollTests.java index 9f86887566ac4..b1fed52b6e3e0 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/integration/SecurityClearScrollTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/integration/SecurityClearScrollTests.java @@ -25,9 +25,9 @@ import java.util.Map; import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertRequestBuilderThrows; import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.BASIC_AUTH_HEADER; import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; @@ -107,7 +107,7 @@ public void testThatClearingAllScrollIdsRequirePermissions() throws Exception { Map headers = new HashMap<>(); headers.put(SecurityField.USER_SETTING.getKey(), user); headers.put(BASIC_AUTH_HEADER, basicAuth); - assertThrows(internalCluster().transportClient().filterWithHeader(headers) + assertRequestBuilderThrows(internalCluster().transportClient().filterWithHeader(headers) .prepareClearScroll() .addScrollId("_all"), ElasticsearchSecurityException.class, "action [cluster:admin/indices/scroll/clear_all] is unauthorized for user [denied_user]");