/** * Checks that all shard requests of a replicated broadcast request failed due to a cluster block * * @param replicatedBroadcastResponse the response that should only contain failed shard responses * * */ public static void assertBlocked(BroadcastResponse replicatedBroadcastResponse) { assertThat("all shard requests should have failed", replicatedBroadcastResponse.getFailedShards(), Matchers.equalTo(replicatedBroadcastResponse.getTotalShards())); for (ShardOperationFailedException exception : replicatedBroadcastResponse.getShardFailures()) { ClusterBlockException clusterBlockException = (ClusterBlockException) ExceptionsHelper.unwrap(exception.getCause(), ClusterBlockException.class); assertNotNull("expected the cause of failure to be a ClusterBlockException but got " + exception.getCause().getMessage(), clusterBlockException); assertThat(clusterBlockException.blocks().size(), greaterThan(0)); assertThat(clusterBlockException.status(), CoreMatchers.equalTo(RestStatus.FORBIDDEN)); } }
/** * Executes the request and fails if the request has not been blocked by a specific {@link ClusterBlock}. * * @param builder the request builder * @param expectedBlock the expected block */ public static void assertBlocked(ActionRequestBuilder builder, ClusterBlock expectedBlock) { try { builder.get(); fail("Request executed with success but a ClusterBlockException was expected"); } catch (ClusterBlockException e) { assertThat(e.blocks().size(), greaterThan(0)); assertThat(e.status(), equalTo(RestStatus.FORBIDDEN)); if (expectedBlock != null) { boolean found = false; for (ClusterBlock clusterBlock : e.blocks()) { if (clusterBlock.id() == expectedBlock.id()) { found = true; break; } } assertThat("Request should have been blocked by [" + expectedBlock + "] instead of " + e.blocks(), found, equalTo(true)); } } }